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