[832] | 1 | # |
---|
| 2 | # Test configuration form working |
---|
| 3 | # |
---|
| 4 | |
---|
| 5 | from Products.CMFCore.permissions import ManagePortal, ReplyToItem |
---|
| 6 | |
---|
| 7 | from quintagroup.plonecomments.utils import getMsg |
---|
| 8 | |
---|
| 9 | import re |
---|
| 10 | from helperNotify import * |
---|
[1633] | 11 | from email import message_from_string |
---|
[832] | 12 | from email.Header import Header |
---|
[880] | 13 | from base import getToolByName, FunctionalTestCase |
---|
| 14 | from common import * |
---|
| 15 | from config import * |
---|
[832] | 16 | |
---|
[1633] | 17 | from Products.CMFPlone.tests.utils import MockMailHost |
---|
| 18 | |
---|
[832] | 19 | USERS = {# Common Members |
---|
| 20 | 'admin':{'passw': 'secret_admin', 'roles': ['Manager']}, |
---|
| 21 | 'owner':{'passw': 'secret_creator', 'roles': ['Member']}, |
---|
| 22 | 'replier1':{'passw': 'secret_member', 'roles': ['Member']}, |
---|
| 23 | 'replier2':{'passw': 'secret_member', 'roles': ['Member']}, |
---|
| 24 | # Members for discussion manager group |
---|
| 25 | 'dm_admin':{'passw': 'secret_dm_admin', 'roles': ['Manager']}, |
---|
| 26 | } |
---|
| 27 | DM_USERS_IDS = [u for u in USERS.keys() if u.startswith('dm_')] |
---|
| 28 | |
---|
| 29 | REXP_TO = re.compile("To:\s*(.*?)$",re.M) |
---|
| 30 | REXP_SUBJ = re.compile("Subject:\s*(.*?)$",re.M) |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | class TestNotificationRecipients(FunctionalTestCase): |
---|
| 34 | """ Test is notifications sends to right recipients. """ |
---|
| 35 | |
---|
| 36 | def prepareRequest4Reply(self, member_id): |
---|
| 37 | self.login(member_id) |
---|
| 38 | self.request = self.app.REQUEST |
---|
| 39 | self.request.form['Creator'] = self.membership.getAuthenticatedMember().getUserName() |
---|
| 40 | self.request.form['subject'] = "Reply of '%s'" % self.request.form['Creator'] |
---|
| 41 | self.request.form['body_text'] = "text of reply" |
---|
| 42 | |
---|
[1633] | 43 | def beforeTearDown(self): |
---|
| 44 | self.portal.MailHost = self.portal._original_MailHost |
---|
| 45 | |
---|
[832] | 46 | def afterSetUp(self): |
---|
[1633] | 47 | self.portal._original_MailHost = self.portal.MailHost |
---|
| 48 | self.portal.MailHost = mailhost = MockMailHost('MailHost') |
---|
| 49 | |
---|
[832] | 50 | self.loginAsPortalOwner() |
---|
| 51 | |
---|
| 52 | # VERY IMPORTANT to guarantee product skin's content visibility |
---|
| 53 | self._refreshSkinData() |
---|
| 54 | '''Preparation for functional testing''' |
---|
| 55 | self.membership = getToolByName(self.portal, 'portal_membership', None) |
---|
| 56 | self.discussion = getToolByName(self.portal, 'portal_discussion', None) |
---|
[880] | 57 | |
---|
[832] | 58 | # Allow discussion for Document |
---|
| 59 | portal_types = getToolByName(self.portal, 'portal_types', None) |
---|
| 60 | doc_fti = portal_types.getTypeInfo('Document') |
---|
| 61 | doc_fti._updateProperty('allow_discussion', 1) |
---|
| 62 | |
---|
| 63 | # Make sure Documents are visible by default |
---|
| 64 | # XXX only do this for plone 3 |
---|
| 65 | self.portal.portal_workflow.setChainForPortalTypes(('Document',), 'plone_workflow') |
---|
| 66 | portal_properties = getToolByName(self.portal, 'portal_properties', None) |
---|
| 67 | self.prefs = portal_properties[PROPERTY_SHEET] |
---|
| 68 | |
---|
| 69 | # Add users and add members to DiscussionManager group |
---|
| 70 | addMembers(self.portal, USERS) |
---|
| 71 | add2Group(self.portal, 'DiscussionManager', DM_USERS_IDS) |
---|
| 72 | self.createMemberarea('owner') |
---|
| 73 | |
---|
| 74 | # Prepare mail sending - enter an e-mail adress, and allow all possible notifications |
---|
| 75 | self.portal.email_from_address = 'mail@plone.test' |
---|
| 76 | setProperties(self.prefs, 'enable_moderation', 'enable_approve_notification', |
---|
| 77 | 'enable_approve_user_notification','enable_reply_user_notification', |
---|
| 78 | 'enable_published_notification', 'enable_rejected_user_notification') |
---|
| 79 | self.prefs._updateProperty('email_discussion_manager', 'discussion.manager@test.com') |
---|
| 80 | self.prefs._updateProperty('email_subject_prefix', 'PREFIX') |
---|
| 81 | |
---|
| 82 | # Add testing document to portal |
---|
| 83 | self.login('owner') |
---|
| 84 | self.portal.Members['owner'].invokeFactory('Document', id='my_doc', title="Test document") |
---|
| 85 | self.my_doc = self.portal.Members['owner']['my_doc'] |
---|
| 86 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
| 87 | |
---|
| 88 | # Create talkback for document and Prepare REQUEST |
---|
| 89 | self.discussion.getDiscussionFor(self.my_doc) |
---|
[1633] | 90 | #prepareMailSendTest() |
---|
[832] | 91 | |
---|
| 92 | def checkToANDSubj(self, mails, to, subj): |
---|
[1633] | 93 | messages = [str(m) for m in mails if REXP_TO.search(str(m)) and REXP_TO.search(str(m)).group(1)==to] |
---|
[880] | 94 | self.failUnless(len(messages) > 0, "No message sent to '%s' recipient" % to) |
---|
[1719] | 95 | self.failUnless([1 for m in messages if REXP_SUBJ.search(m) and REXP_SUBJ.search(m).group(1)==subj],\ |
---|
[880] | 96 | "There is no message for '%s' recipient with '%s' subject" % (to,subj)) |
---|
[832] | 97 | |
---|
| 98 | def test_Reply(self): |
---|
[1633] | 99 | self.portal.MailHost.reset() |
---|
| 100 | |
---|
[832] | 101 | self.prepareRequest4Reply('replier1') |
---|
| 102 | self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc') |
---|
| 103 | |
---|
[1633] | 104 | mails = self.portal.MailHost.messages |
---|
[832] | 105 | self.assertEqual(len(mails), 1) |
---|
| 106 | self.checkToANDSubj(mails, to="discussion.manager@test.com", |
---|
| 107 | subj="[PREFIX] New comment awaits moderation") |
---|
| 108 | |
---|
| 109 | def test_PublishReply(self): |
---|
| 110 | self.prepareRequest4Reply('replier1') |
---|
| 111 | self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc') |
---|
| 112 | self.login('dm_admin') |
---|
| 113 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
[1633] | 114 | self.portal.MailHost.reset() |
---|
| 115 | |
---|
[832] | 116 | |
---|
| 117 | reply.discussion_publish_comment() |
---|
[1633] | 118 | mails = self.portal.MailHost.messages |
---|
[832] | 119 | self.assertEqual(len(mails), 2) |
---|
| 120 | self.checkToANDSubj(mails, to="owner@test.com", subj="[PREFIX] New comment added") |
---|
[1059] | 121 | self.checkToANDSubj(mails, to="replier1@test.com", subj='Your comment on Test document is now published') |
---|
[832] | 122 | |
---|
| 123 | def test_Publish2ndReply(self): |
---|
| 124 | self.prepareRequest4Reply('replier1') |
---|
| 125 | self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc') |
---|
| 126 | self.login('dm_admin') |
---|
| 127 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
| 128 | reply.discussion_publish_comment() |
---|
| 129 | self.prepareRequest4Reply('replier2') |
---|
| 130 | reply.discussion_reply('A Reply for reply for my_doc' ,'text of reply on reply for my_doc') |
---|
| 131 | self.login('dm_admin') |
---|
| 132 | reply2 = self.discussion.getDiscussionFor(reply).getReplies()[0] |
---|
[1633] | 133 | self.portal.MailHost.reset() |
---|
| 134 | |
---|
[832] | 135 | |
---|
| 136 | reply2.discussion_publish_comment() |
---|
[1633] | 137 | mails = self.portal.MailHost.messages |
---|
[832] | 138 | self.assertEqual(len(mails), 3) |
---|
| 139 | self.checkToANDSubj(mails, to="owner@test.com", subj="[PREFIX] New comment added") |
---|
[1059] | 140 | self.checkToANDSubj(mails, to="replier1@test.com", subj='Someone replied to your comment on Test document') |
---|
| 141 | self.checkToANDSubj(mails, to="replier2@test.com", subj='Your comment on Test document is now published') |
---|
[832] | 142 | |
---|
| 143 | def test_DeleteReply(self): |
---|
| 144 | self.prepareRequest4Reply('replier1') |
---|
| 145 | self.my_doc.discussion_reply('A Reply for my_doc' ,'text of reply for my_doc') |
---|
| 146 | self.login('dm_admin') |
---|
| 147 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
[1633] | 148 | self.portal.MailHost.reset() |
---|
| 149 | |
---|
[832] | 150 | |
---|
| 151 | reply.deleteDiscussion() |
---|
[1633] | 152 | mails = self.portal.MailHost.messages |
---|
[832] | 153 | self.assertEqual(len(mails), 1) |
---|
[1059] | 154 | self.checkToANDSubj(mails, to="replier1@test.com", subj='Your comment on Test document was not approved') |
---|
[832] | 155 | |
---|
| 156 | |
---|
| 157 | def test_suite(): |
---|
| 158 | from unittest import TestSuite, makeSuite |
---|
| 159 | suite = TestSuite() |
---|
| 160 | suite.addTest(makeSuite(TestNotificationRecipients)) |
---|
| 161 | return suite |
---|