2011-04-22 10:54:35 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2010-02-11 22:05:28 +01:00
|
|
|
from Acquisition import aq_base
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.app.discussion.interfaces import IConversation
|
2015-05-03 08:41:56 +02:00
|
|
|
from plone.app.discussion.testing import PLONE_APP_DISCUSSION_INTEGRATION_TESTING # noqa
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.app.testing import setRoles
|
2016-02-05 01:39:53 +01:00
|
|
|
from plone.app.testing import TEST_USER_ID
|
2015-05-03 08:16:39 +02:00
|
|
|
from plone.registry.interfaces import IRegistry
|
2016-02-05 01:39:53 +01:00
|
|
|
from Products.CMFPlone.interfaces import IMailSchema
|
|
|
|
from Products.CMFPlone.tests.utils import MockMailHost
|
|
|
|
from Products.MailHost.interfaces import IMailHost
|
2010-02-11 22:05:28 +01:00
|
|
|
from zope.component import createObject
|
|
|
|
from zope.component import getSiteManager
|
2014-12-13 16:20:08 +01:00
|
|
|
from zope.component import getUtility
|
2015-05-03 08:16:39 +02:00
|
|
|
from zope.component import queryUtility
|
2010-02-11 22:05:28 +01:00
|
|
|
|
2017-05-08 09:24:38 +02:00
|
|
|
import unittest
|
2010-02-11 22:05:28 +01:00
|
|
|
|
|
|
|
|
2011-04-16 11:31:03 +02:00
|
|
|
class TestUserNotificationUnit(unittest.TestCase):
|
2010-03-11 20:23:53 +01:00
|
|
|
|
2011-04-16 11:31:03 +02:00
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-16 11:31:03 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.portal = self.layer['portal']
|
|
|
|
setRoles(self.portal, TEST_USER_ID, ['Manager'])
|
2010-10-30 17:02:05 +02:00
|
|
|
# Set up a mock mailhost
|
|
|
|
self.portal._original_MailHost = self.portal.MailHost
|
|
|
|
self.portal.MailHost = mailhost = MockMailHost('MailHost')
|
|
|
|
sm = getSiteManager(context=self.portal)
|
|
|
|
sm.unregisterUtility(provided=IMailHost)
|
|
|
|
sm.registerUtility(mailhost, provided=IMailHost)
|
|
|
|
# We need to fake a valid mail setup
|
2014-12-13 16:20:08 +01:00
|
|
|
registry = getUtility(IRegistry)
|
|
|
|
mail_settings = registry.forInterface(IMailSchema, prefix='plone')
|
2016-02-05 01:39:53 +01:00
|
|
|
mail_settings.email_from_address = 'portal@plone.test'
|
2010-10-30 17:02:05 +02:00
|
|
|
self.mailhost = self.portal.MailHost
|
|
|
|
# Enable user notification setting
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
registry['plone.app.discussion.interfaces.IDiscussionSettings' +
|
|
|
|
'.user_notification_enabled'] = True
|
2011-03-08 21:15:37 +01:00
|
|
|
# Archetypes content types store data as utf-8 encoded strings
|
|
|
|
# The missing u in front of a string is therefor not missing
|
2016-02-05 01:39:53 +01:00
|
|
|
self.portal.doc1.title = 'Kölle Alaaf' # What is 'Fasching'?
|
2010-10-30 17:02:05 +02:00
|
|
|
self.conversation = IConversation(self.portal.doc1)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
def beforeTearDown(self):
|
|
|
|
self.portal.MailHost = self.portal._original_MailHost
|
|
|
|
sm = getSiteManager(context=self.portal)
|
|
|
|
sm.unregisterUtility(provided=IMailHost)
|
2010-12-16 00:52:56 +01:00
|
|
|
sm.registerUtility(aq_base(self.portal._original_MailHost),
|
2010-10-30 17:02:05 +02:00
|
|
|
provided=IMailHost)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
def test_notify_user(self):
|
|
|
|
# Add a comment with user notification enabled. Add another comment
|
|
|
|
# and make sure an email is send to the user of the first comment.
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
2016-02-05 01:39:53 +01:00
|
|
|
comment.author_email = 'john@plone.test'
|
2010-10-30 17:02:05 +02:00
|
|
|
self.conversation.addComment(comment)
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-22 10:54:35 +02:00
|
|
|
comment_id = self.conversation.addComment(comment)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 1)
|
|
|
|
self.assertTrue(self.mailhost.messages[0])
|
2020-09-23 15:26:30 +02:00
|
|
|
msg = self.mailhost.messages[0]
|
|
|
|
msg = msg.decode("utf-8")
|
2021-01-22 16:29:35 +01:00
|
|
|
self.assertIn('To: john@plone.test', msg)
|
|
|
|
self.assertIn('From: portal@plone.test', msg)
|
2010-10-30 17:02:05 +02:00
|
|
|
# We expect the headers to be properly header encoded (7-bit):
|
2021-01-22 16:29:35 +01:00
|
|
|
self.assertIn(
|
|
|
|
'Subject: =?utf-8?q?A_comment_has_been_posted=2E?=',
|
|
|
|
msg)
|
2011-04-22 10:54:35 +02:00
|
|
|
# The output should be encoded in a reasonable manner
|
2021-01-22 16:29:35 +01:00
|
|
|
# (in this case quoted-printable).
|
|
|
|
# Depending on which Python version and which Products.MailHost version,
|
|
|
|
# you may get lines separated by '\n' or '\r\n' in here.
|
|
|
|
msg = msg.replace('\r\n', '\n')
|
|
|
|
self.assertIn(
|
|
|
|
'A comment on "K=C3=B6lle Alaaf" has been posted here:',
|
|
|
|
msg)
|
|
|
|
self.assertIn(
|
|
|
|
'http://nohost/plone/d=\noc1/view#{0}'.format(comment_id),
|
|
|
|
msg)
|
|
|
|
self.assertIn('Comment text', msg)
|
|
|
|
self.assertNotIn('Approve comment', msg)
|
|
|
|
self.assertNotIn('Delete comment', msg)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
def test_do_not_notify_user_when_notification_is_disabled(self):
|
|
|
|
registry = queryUtility(IRegistry)
|
2013-04-17 19:04:45 +02:00
|
|
|
registry[
|
|
|
|
'plone.app.discussion.interfaces.IDiscussionSettings.' +
|
|
|
|
'user_notification_enabled'
|
|
|
|
] = False
|
2010-10-30 17:02:05 +02:00
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
2016-02-05 01:39:53 +01:00
|
|
|
comment.author_email = 'john@plone.test'
|
2010-10-30 17:02:05 +02:00
|
|
|
self.conversation.addComment(comment)
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
self.conversation.addComment(comment)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 0)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
def test_do_not_notify_user_when_email_address_is_given(self):
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
|
|
|
self.conversation.addComment(comment)
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
self.conversation.addComment(comment)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 0)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
def test_do_not_notify_user_when_no_sender_is_available(self):
|
|
|
|
# Set sender mail address to none and make sure no email is send to
|
|
|
|
# the moderator.
|
2014-12-13 16:20:08 +01:00
|
|
|
registry = getUtility(IRegistry)
|
|
|
|
mail_settings = registry.forInterface(IMailSchema, prefix='plone')
|
|
|
|
mail_settings.email_from_address = None
|
2010-10-30 17:02:05 +02:00
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
2016-02-05 01:39:53 +01:00
|
|
|
comment.author_email = 'john@plone.test'
|
2010-10-30 17:02:05 +02:00
|
|
|
self.conversation.addComment(comment)
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-10-30 17:02:05 +02:00
|
|
|
self.conversation.addComment(comment)
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 0)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-12-16 00:41:57 +01:00
|
|
|
def test_notify_only_once(self):
|
|
|
|
# When a user has added two comments in a conversation and has
|
|
|
|
# both times requested email notification, do not send him two
|
|
|
|
# emails when another comment has been added.
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
2016-02-05 01:39:53 +01:00
|
|
|
comment.author_email = 'john@plone.test'
|
2010-12-16 00:41:57 +01:00
|
|
|
self.conversation.addComment(comment)
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
|
|
|
comment.user_notification = True
|
2016-02-05 01:39:53 +01:00
|
|
|
comment.author_email = 'john@plone.test'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-12-16 00:41:57 +01:00
|
|
|
self.conversation.addComment(comment)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-12-16 00:41:57 +01:00
|
|
|
# Note that we might want to get rid of this message, as the
|
|
|
|
# new comment is added by the same user.
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 1)
|
2010-12-16 00:41:57 +01:00
|
|
|
self.mailhost.reset()
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 0)
|
2010-12-16 00:41:57 +01:00
|
|
|
|
2010-02-13 22:31:17 +01:00
|
|
|
|
2011-04-16 11:31:03 +02:00
|
|
|
class TestModeratorNotificationUnit(unittest.TestCase):
|
|
|
|
|
|
|
|
layer = PLONE_APP_DISCUSSION_INTEGRATION_TESTING
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-16 11:31:03 +02:00
|
|
|
def setUp(self):
|
|
|
|
self.portal = self.layer['portal']
|
|
|
|
setRoles(self.portal, TEST_USER_ID, ['Manager'])
|
2010-02-11 22:05:28 +01:00
|
|
|
# Set up a mock mailhost
|
|
|
|
self.portal._original_MailHost = self.portal.MailHost
|
|
|
|
self.portal.MailHost = mailhost = MockMailHost('MailHost')
|
|
|
|
sm = getSiteManager(context=self.portal)
|
|
|
|
sm.unregisterUtility(provided=IMailHost)
|
|
|
|
sm.registerUtility(mailhost, provided=IMailHost)
|
|
|
|
# We need to fake a valid mail setup
|
2014-12-13 16:20:08 +01:00
|
|
|
registry = getUtility(IRegistry)
|
|
|
|
mail_settings = registry.forInterface(IMailSchema, prefix='plone')
|
2016-02-05 01:39:53 +01:00
|
|
|
mail_settings.email_from_address = 'portal@plone.test'
|
2010-02-11 22:05:28 +01:00
|
|
|
self.mailhost = self.portal.MailHost
|
|
|
|
# Enable comment moderation
|
|
|
|
self.portal.portal_types['Document'].allow_discussion = True
|
|
|
|
self.portal.portal_workflow.setChainForPortalTypes(
|
|
|
|
('Discussion Item',),
|
2018-06-18 17:04:41 +02:00
|
|
|
('comment_review_workflow',),
|
2013-04-17 19:04:45 +02:00
|
|
|
)
|
2010-02-11 22:05:28 +01:00
|
|
|
# Enable moderator notification setting
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2013-04-17 19:04:45 +02:00
|
|
|
registry[
|
|
|
|
'plone.app.discussion.interfaces.IDiscussionSettings.' +
|
|
|
|
'moderator_notification_enabled'
|
|
|
|
] = True
|
2011-03-08 21:15:21 +01:00
|
|
|
# Archetypes content types store data as utf-8 encoded strings
|
|
|
|
# The missing u in front of a string is therefor not missing
|
2016-02-05 01:39:53 +01:00
|
|
|
self.portal.doc1.title = 'Kölle Alaaf' # What is 'Fasching'?
|
2010-02-11 22:05:28 +01:00
|
|
|
self.conversation = IConversation(self.portal.doc1)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
def beforeTearDown(self):
|
|
|
|
self.portal.MailHost = self.portal._original_MailHost
|
|
|
|
sm = getSiteManager(context=self.portal)
|
|
|
|
sm.unregisterUtility(provided=IMailHost)
|
2010-12-16 00:52:56 +01:00
|
|
|
sm.registerUtility(aq_base(self.portal._original_MailHost),
|
2010-08-31 19:28:07 +02:00
|
|
|
provided=IMailHost)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
def test_notify_moderator(self):
|
2019-12-02 09:09:49 +01:00
|
|
|
"""Add a comment and make sure an email is send to the moderator."""
|
2010-02-11 22:05:28 +01:00
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
2019-12-02 09:09:49 +01:00
|
|
|
comment.author_email = 'john@plone.test'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-22 10:54:35 +02:00
|
|
|
comment_id = self.conversation.addComment(comment)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 1)
|
|
|
|
self.assertTrue(self.mailhost.messages[0])
|
2010-02-11 22:05:28 +01:00
|
|
|
msg = self.mailhost.messages[0]
|
2020-09-23 15:26:30 +02:00
|
|
|
msg = msg.decode("utf-8")
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertTrue('To: portal@plone.test' in msg)
|
|
|
|
self.assertTrue('From: portal@plone.test' in msg)
|
2011-04-22 10:54:35 +02:00
|
|
|
# We expect the headers to be properly header encoded (7-bit):
|
|
|
|
self.assertTrue(
|
2021-01-22 16:29:35 +01:00
|
|
|
'Subject: =?utf-8?q?A_comment_has_been_posted=2E?='
|
2011-04-22 10:54:35 +02:00
|
|
|
in msg)
|
|
|
|
# The output should be encoded in a reasonable manner
|
|
|
|
# (in this case quoted-printable):
|
|
|
|
self.assertTrue(
|
2019-12-02 09:09:49 +01:00
|
|
|
'A comment on "K=C3=B6lle Alaaf" has been posted'
|
|
|
|
in msg
|
2016-02-05 01:39:53 +01:00
|
|
|
)
|
|
|
|
self.assertIn(
|
2019-12-02 09:09:49 +01:00
|
|
|
'http://nohost/plone/doc1/view#{0}'.format(comment_id),
|
|
|
|
msg
|
2016-02-05 01:39:53 +01:00
|
|
|
)
|
|
|
|
self.assertIn(
|
2019-12-02 09:09:49 +01:00
|
|
|
comment.author_email,
|
|
|
|
msg
|
2016-02-05 01:39:53 +01:00
|
|
|
)
|
|
|
|
self.assertIn(
|
2019-12-02 09:09:49 +01:00
|
|
|
comment.text,
|
|
|
|
msg
|
2016-02-05 01:39:53 +01:00
|
|
|
)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-07 23:31:56 +02:00
|
|
|
def test_notify_moderator_specific_address(self):
|
|
|
|
# A moderator email address can be specified in the control panel.
|
|
|
|
registry = queryUtility(IRegistry)
|
|
|
|
registry['plone.app.discussion.interfaces.IDiscussionSettings' +
|
|
|
|
'.moderator_email'] = 'test@example.com'
|
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-07 23:31:56 +02:00
|
|
|
self.conversation.addComment(comment)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 1)
|
2011-04-07 23:31:56 +02:00
|
|
|
msg = self.mailhost.messages[0]
|
2020-09-23 15:26:30 +02:00
|
|
|
msg = msg.decode("utf-8")
|
2020-09-23 15:35:32 +02:00
|
|
|
self.assertTrue('To: test@example.com' in msg)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
def test_do_not_notify_moderator_when_no_sender_is_available(self):
|
|
|
|
# Set sender mail address to nonw and make sure no email is send to the
|
|
|
|
# moderator.
|
2014-12-13 16:20:08 +01:00
|
|
|
registry = getUtility(IRegistry)
|
|
|
|
mail_settings = registry.forInterface(IMailSchema, prefix='plone')
|
|
|
|
mail_settings.email_from_address = None
|
2010-02-11 22:05:28 +01:00
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
self.conversation.addComment(comment)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 0)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
def test_do_not_notify_moderator_when_notification_is_disabled(self):
|
2010-12-16 00:52:56 +01:00
|
|
|
# Disable moderator notification setting and make sure no email is send
|
2010-02-11 22:05:28 +01:00
|
|
|
# to the moderator.
|
2010-07-12 15:47:53 +02:00
|
|
|
registry = queryUtility(IRegistry)
|
2010-08-31 19:28:07 +02:00
|
|
|
registry['plone.app.discussion.interfaces.IDiscussionSettings.' +
|
|
|
|
'moderator_notification_enabled'] = False
|
2010-02-11 22:05:28 +01:00
|
|
|
comment = createObject('plone.Comment')
|
|
|
|
comment.text = 'Comment text'
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2010-02-11 22:05:28 +01:00
|
|
|
self.conversation.addComment(comment)
|
2012-01-09 16:43:54 +01:00
|
|
|
|
2011-04-15 18:23:38 +02:00
|
|
|
self.assertEqual(len(self.mailhost.messages), 0)
|