1 | # |
---|
2 | # Test configuration form working |
---|
3 | # |
---|
4 | import re |
---|
5 | |
---|
6 | from Products.CMFCore.utils import getToolByName |
---|
7 | from Products.CMFPlone.tests.utils import MockMailHost |
---|
8 | from quintagroup.plonecomments.tests.helperNotify import setProperties, \ |
---|
9 | testMailExistance |
---|
10 | from quintagroup.plonecomments.utils import getMsg |
---|
11 | from quintagroup.plonecomments.tests.base import FunctionalTestCase |
---|
12 | from quintagroup.plonecomments.tests.config import PROPERTY_SHEET |
---|
13 | |
---|
14 | REXP_TO = re.compile("To:\s*(.*?)$", re.M) |
---|
15 | REXP_SUBJ = re.compile("Subject:\s*(.*?)$", re.M) |
---|
16 | |
---|
17 | |
---|
18 | class TestNotification(FunctionalTestCase): |
---|
19 | |
---|
20 | def setApprovePublished(self, swithA=1, swithP=1): |
---|
21 | self.prefs._updateProperty('enable_approve_notification', swithA) |
---|
22 | self.prefs._updateProperty('enable_published_notification', swithP) |
---|
23 | |
---|
24 | def beforeTearDown(self): |
---|
25 | self.portal.MailHost = self.portal._original_MailHost |
---|
26 | |
---|
27 | def afterSetUp(self): |
---|
28 | self.portal._original_MailHost = self.portal.MailHost |
---|
29 | self.portal.MailHost = MockMailHost('MailHost') |
---|
30 | |
---|
31 | self.loginAsPortalOwner() |
---|
32 | |
---|
33 | # VERY IMPORTANT to guarantee product skin's content visibility |
---|
34 | self._refreshSkinData() |
---|
35 | |
---|
36 | '''Preparation for functional testing''' |
---|
37 | self.discussion = getToolByName(self.portal, 'portal_discussion', None) |
---|
38 | # Allow discussion for Document |
---|
39 | portal_types = getToolByName(self.portal, 'portal_types', None) |
---|
40 | doc_fti = portal_types.getTypeInfo('Document') |
---|
41 | doc_fti._updateProperty('allow_discussion', 1) |
---|
42 | |
---|
43 | # Make sure Documents are visible by default |
---|
44 | # XXX only do this for plone 3 |
---|
45 | self.portal.portal_workflow.setChainForPortalTypes(('Document',), |
---|
46 | 'plone_workflow') |
---|
47 | |
---|
48 | portal_properties = getToolByName(self.portal, 'portal_properties', |
---|
49 | None) |
---|
50 | self.prefs = portal_properties[PROPERTY_SHEET] |
---|
51 | |
---|
52 | # Add Manager user - 'dm' and add him to Discussion Manager group |
---|
53 | self.portal.portal_membership.addMember('dm', 'secret', ['Manager'], |
---|
54 | []) |
---|
55 | portal_groups = getToolByName(self.portal, 'portal_groups') |
---|
56 | dm_group = portal_groups.getGroupById('DiscussionManager') |
---|
57 | dm_group.addMember('dm') |
---|
58 | self.logout() |
---|
59 | self.login('dm') |
---|
60 | # For prepare mail sending - enter an e-mail adress |
---|
61 | self.portal.email_from_address = 'mail@plone.test' |
---|
62 | self.prefs._updateProperty('email_discussion_manager', |
---|
63 | 'discussion.manager@test.com') |
---|
64 | member = self.portal.portal_membership.getAuthenticatedMember() |
---|
65 | member.setMemberProperties({'email': 'creator@test.com'}) |
---|
66 | |
---|
67 | # Add testing document to portal |
---|
68 | self.portal.invokeFactory('Document', id='my_doc', title='Doc') |
---|
69 | self.my_doc = self.portal['my_doc'] |
---|
70 | self.my_doc.edit(text_format='plain', text='hello world') |
---|
71 | # Create talkback for document and Prepare REQUEST |
---|
72 | self.discussion.getDiscussionFor(self.my_doc) |
---|
73 | self.request = self.app.REQUEST |
---|
74 | creator = self.portal.portal_membership.getAuthenticatedMember() |
---|
75 | self.request.form['Creator'] = creator.getUserName() |
---|
76 | self.request.form['subject'] = "Reply 1" |
---|
77 | self.request.form['body_text'] = "text of reply" |
---|
78 | |
---|
79 | #prepareMailSendTest() |
---|
80 | |
---|
81 | def test_bug_parent_reply(self): |
---|
82 | setProperties(self.prefs, 'enable_reply_user_notification') |
---|
83 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
84 | 'text of reply for my_doc') |
---|
85 | discussion_for = self.discussion.getDiscussionFor(self.my_doc) |
---|
86 | parent_reply = discussion_for.getReplies()[0] |
---|
87 | parent_reply.discussion_reply('reply', 'text') |
---|
88 | |
---|
89 | def test_bug_mistakable_names(self): |
---|
90 | setProperties(self.prefs, 'enable_reply_user_notification') |
---|
91 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
92 | 'text of reply for my_doc') |
---|
93 | discussion_for = self.discussion.getDiscussionFor(self.my_doc) |
---|
94 | parent_reply = discussion_for.getReplies()[0] |
---|
95 | |
---|
96 | args = {'mto': 'user_email@gmail.com', |
---|
97 | 'mfrom': 'admin_email@gmail.com', |
---|
98 | 'obj': parent_reply, |
---|
99 | 'organization_name': 'org_name', |
---|
100 | 'name': parent_reply.getOwnerTuple()[1]} |
---|
101 | |
---|
102 | msg = getMsg(self.portal, 'reply_notify_template', args) |
---|
103 | patt = re.compile('\\n([^,]*?),\\n\\n') |
---|
104 | m = patt.search(msg) |
---|
105 | if m: |
---|
106 | name = m.group(1) |
---|
107 | self.assertEqual(parent_reply.getOwnerTuple()[1], name) |
---|
108 | else: |
---|
109 | raise "No name" |
---|
110 | |
---|
111 | def test_notificafion_disabled(self): |
---|
112 | self.portal.MailHost.reset() |
---|
113 | setProperties(self.prefs) |
---|
114 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
115 | 'text of reply for my_doc') |
---|
116 | self.failIf(testMailExistance(self), |
---|
117 | 'Mail was sended when all notification was disabled.') |
---|
118 | |
---|
119 | def test_published_comment_notification(self): |
---|
120 | self.portal.MailHost.reset() |
---|
121 | setProperties(self.prefs, 'enable_published_notification') |
---|
122 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
123 | 'text of reply for my_doc') |
---|
124 | self.failUnless(testMailExistance(self), |
---|
125 | 'Mail was not sended when enable_published_notification.') |
---|
126 | |
---|
127 | def test_approve_comment_notification(self): |
---|
128 | self.portal.MailHost.reset() |
---|
129 | setProperties(self.prefs, 'enable_approve_notification') |
---|
130 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
131 | 'text of reply for my_doc') |
---|
132 | self.failUnless(testMailExistance(self), |
---|
133 | 'Mail was not sended when enable_approve_notification.') |
---|
134 | |
---|
135 | def test_reply_comment_user_notification(self): |
---|
136 | self.portal.MailHost.reset() |
---|
137 | setProperties(self.prefs, 'enable_reply_user_notification') |
---|
138 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
139 | 'text of reply for my_doc') |
---|
140 | self.failIf(testMailExistance(self), 'Mail was sended for simple reply' |
---|
141 | ' when enable_reply_user_notification.') |
---|
142 | |
---|
143 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
144 | reply.discussion_reply('A Reply for comment', |
---|
145 | 'text of reply for comment') |
---|
146 | self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
147 | self.failUnless(testMailExistance(self), |
---|
148 | 'Mail was not sended when enable_reply_user_notification.') |
---|
149 | |
---|
150 | def test_rejected_comment_notification(self): |
---|
151 | self.portal.MailHost.reset() |
---|
152 | setProperties(self.prefs, 'enable_rejected_user_notification', |
---|
153 | 'enable_moderation') |
---|
154 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
155 | 'text of reply for my_doc') |
---|
156 | self.failIf(testMailExistance(self), 'Mail was sended when ' |
---|
157 | 'enable_rejected_user_notification was enabled.') |
---|
158 | |
---|
159 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
160 | self.portal.REQUEST.set('ids', [reply.getId()]) |
---|
161 | self.portal.prefs_recent_comments_delete() |
---|
162 | self.failUnless(testMailExistance(self), |
---|
163 | 'Mail was not sended when enable_rejected_user_notification.') |
---|
164 | |
---|
165 | def test_approve_comment_user__notification(self): |
---|
166 | self.portal.MailHost.reset() |
---|
167 | setProperties(self.prefs, 'enable_approve_user_notification') |
---|
168 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
169 | 'text of reply for my_doc') |
---|
170 | self.failUnless(testMailExistance(self), |
---|
171 | 'Mail was not sended when enable_approve_user_notification.') |
---|
172 | |
---|
173 | def test_bug_notification_on_single_reply_publish(self): |
---|
174 | """ Bug: no notification sent on publishing single comment. |
---|
175 | Must be 3 mails: for replier about replying on his commen; |
---|
176 | for replier about publishig his comment; |
---|
177 | for document creator about adding new comment. |
---|
178 | """ |
---|
179 | properties = ['enable_approve_user_notification', |
---|
180 | 'enable_reply_user_notification', |
---|
181 | 'enable_published_notification'] |
---|
182 | setProperties(self.prefs, *properties) |
---|
183 | #setProperties(self.prefs, 'enable_published_notification', ) |
---|
184 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
185 | 'text of reply for my_doc') |
---|
186 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
187 | reply.discussion_reply('A Reply for reply for my_doc', |
---|
188 | 'text of reply on reply for my_doc') |
---|
189 | reply2 = self.discussion.getDiscussionFor(reply).getReplies()[0] |
---|
190 | |
---|
191 | self.portal.MailHost.reset() |
---|
192 | reply2.discussion_publish_comment() |
---|
193 | mails = [str(m) for m in self.portal.MailHost.messages] |
---|
194 | subjects = lambda m: REXP_SUBJ.search(m).group(1) |
---|
195 | is_subject = lambda m: REXP_SUBJ.search(m) |
---|
196 | self.failUnless([1 |
---|
197 | for m in mails |
---|
198 | if is_subject(m) and 'replied' in subjects(m)], |
---|
199 | 'No notification for reply.' % properties) |
---|
200 | self.failUnless([1 |
---|
201 | for m in mails |
---|
202 | if is_subject(m) and 'added' in subjects(m)], |
---|
203 | 'No notification for adding comment.' % properties) |
---|
204 | self.failUnless([1 |
---|
205 | for m in mails |
---|
206 | if is_subject(m) and 'published' in subjects(m)], |
---|
207 | 'No notification for publishing comment.' % properties) |
---|
208 | |
---|
209 | def test_bug_notification_on_single_reply_delete(self): |
---|
210 | """ Bug: no notification sent on deleting single comment. |
---|
211 | Mail about rejecing comment should be sent to comentator. |
---|
212 | """ |
---|
213 | properties = ['enable_rejected_user_notification', ] |
---|
214 | setProperties(self.prefs, *properties) |
---|
215 | #setProperties(self.prefs, 'enable_published_notification', ) |
---|
216 | self.my_doc.discussion_reply('A Reply for my_doc', |
---|
217 | 'text of reply for my_doc') |
---|
218 | reply = self.discussion.getDiscussionFor(self.my_doc).getReplies()[0] |
---|
219 | |
---|
220 | self.portal.MailHost.reset() |
---|
221 | reply.deleteDiscussion() |
---|
222 | mails = [str(m) for m in self.portal.MailHost.messages] |
---|
223 | subject = 'Your comment on Doc was not approved' |
---|
224 | self.failUnless([1 |
---|
225 | for m in mails |
---|
226 | if REXP_SUBJ.search(m) and \ |
---|
227 | REXP_SUBJ.search(m).group(1) == subject], |
---|
228 | 'No notification for rejecting comment.' % properties) |
---|
229 | |
---|
230 | |
---|
231 | def test_suite(): |
---|
232 | from unittest import TestSuite, makeSuite |
---|
233 | suite = TestSuite() |
---|
234 | suite.addTest(makeSuite(TestNotification)) |
---|
235 | return suite |
---|