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