1 | # |
---|
2 | # Test adding comments possibility on switching on/off moderation |
---|
3 | # |
---|
4 | |
---|
5 | from Products.CMFCore.utils import getToolByName |
---|
6 | from zExceptions import Unauthorized |
---|
7 | from quintagroup.plonecomments.tests.base import FunctionalTestCase |
---|
8 | from quintagroup.plonecomments.tests.config import USERS, PROPERTY_SHEET, \ |
---|
9 | DM_USERS_IDS, COMMON_USERS_IDS |
---|
10 | |
---|
11 | |
---|
12 | class TestCommBase(FunctionalTestCase): |
---|
13 | |
---|
14 | def afterSetUp(self): |
---|
15 | self.loginAsPortalOwner() |
---|
16 | self.request = self.app.REQUEST |
---|
17 | |
---|
18 | # VERY IMPORTANT to guarantee product skin's content visibility |
---|
19 | self._refreshSkinData() |
---|
20 | |
---|
21 | # Add all users |
---|
22 | self.membership = getToolByName(self.portal, 'portal_membership', None) |
---|
23 | for user_id in USERS.keys(): |
---|
24 | self.membership.addMember(user_id, USERS[user_id]['passw'], |
---|
25 | USERS[user_id]['roles'], []) |
---|
26 | |
---|
27 | # Add users to Discussion Manager group |
---|
28 | portal_groups = getToolByName(self.portal, 'portal_groups') |
---|
29 | dm_group = portal_groups.getGroupById('DiscussionManager') |
---|
30 | [dm_group.addMember(u) for u in DM_USERS_IDS] |
---|
31 | |
---|
32 | # Allow discussion for Document |
---|
33 | portal_types = getToolByName(self.portal, 'portal_types', None) |
---|
34 | doc_fti = portal_types.getTypeInfo('Document') |
---|
35 | doc_fti._updateProperty('allow_discussion', 1) |
---|
36 | |
---|
37 | # Make sure Documents are visible by default |
---|
38 | # XXX only do this for plone 3 |
---|
39 | self.portal.portal_workflow.setChainForPortalTypes(('Document',), |
---|
40 | 'plone_workflow') |
---|
41 | |
---|
42 | # Add testing documents to portal. Add one document for avery user. |
---|
43 | # For testing behaviors, where made some changes to document state |
---|
44 | # it's more usefull. |
---|
45 | self.discussion = getToolByName(self.portal, 'portal_discussion', None) |
---|
46 | self.all_users_id = DM_USERS_IDS + COMMON_USERS_IDS |
---|
47 | for user_id in self.all_users_id: |
---|
48 | doc_id = 'doc_%s' % user_id |
---|
49 | self.portal.invokeFactory('Document', id=doc_id) |
---|
50 | doc_obj = getattr(self.portal, doc_id) |
---|
51 | doc_obj.edit(text_format='plain', |
---|
52 | text='hello world from %s' % doc_id) |
---|
53 | # Create talkback for document and Add comment to doc_obj |
---|
54 | self.discussion.getDiscussionFor(doc_obj) |
---|
55 | doc_obj.discussion_reply('A Reply for %s' % doc_id, |
---|
56 | 'text of reply for %s' % doc_id) |
---|
57 | |
---|
58 | |
---|
59 | class TestMixinAnonymOn: |
---|
60 | |
---|
61 | def getDiscussionReplies(self, doc_obj): |
---|
62 | return self.discussion.getDiscussionFor(doc_obj).getReplies() |
---|
63 | |
---|
64 | def afterSetUp(self): |
---|
65 | pass |
---|
66 | |
---|
67 | def testAddCommentToDocAnonymUsers(self): |
---|
68 | |
---|
69 | # ADDING COMMENTS MUST ALLOWED for anonymous users |
---|
70 | self.login('dm_admin') |
---|
71 | doc_obj = getattr(self.portal, "doc_anonym") |
---|
72 | replies_before = len(self.getDiscussionReplies(doc_obj)) |
---|
73 | |
---|
74 | # Create talkback for document and Add comment |
---|
75 | self.logout() |
---|
76 | doc_obj.discussion_reply("Anonym reply", "text of 'anonym' reply") |
---|
77 | self.login('dm_admin') |
---|
78 | replies_after = len(self.getDiscussionReplies(doc_obj)) |
---|
79 | self.failUnless(replies_after - replies_before, |
---|
80 | "Anonymous user CAN'T really add comment in terned ON *Anonymous " |
---|
81 | "commenting mode*.") |
---|
82 | |
---|
83 | def testAddCommentToDocNotAnonymUsers(self): |
---|
84 | |
---|
85 | # All users CAN ADD COMMENTS |
---|
86 | not_anonym_users = [u for u in self.all_users_id if not u == 'anonym'] |
---|
87 | failed_users = [] |
---|
88 | for u in not_anonym_users: |
---|
89 | self.login('dm_admin') |
---|
90 | doc_id = "doc_%s" % u |
---|
91 | doc_obj = getattr(self.portal, doc_id) |
---|
92 | replies_before = self.getDiscussionReplies(doc_obj) |
---|
93 | self.login(u) |
---|
94 | |
---|
95 | # Create talkback for document and Add comment |
---|
96 | doc_obj.discussion_reply("%s's reply" % u, |
---|
97 | "text of '%s' reply" % u) |
---|
98 | |
---|
99 | # Check is comment added |
---|
100 | self.login('dm_admin') |
---|
101 | replies_after = self.getDiscussionReplies(doc_obj) |
---|
102 | disparity = len(replies_after) - len(replies_before) |
---|
103 | if not disparity: |
---|
104 | failed_users.append(u) |
---|
105 | self.failIf(failed_users, |
---|
106 | "%s - user(s) can not really add comment." % failed_users) |
---|
107 | |
---|
108 | |
---|
109 | class TestMixinAnonymOff: |
---|
110 | |
---|
111 | def getDiscussionReplies(self, doc_obj): |
---|
112 | return self.discussion.getDiscussionFor(doc_obj).getReplies() |
---|
113 | |
---|
114 | def afterSetUp(self): |
---|
115 | all_users_id = DM_USERS_IDS + COMMON_USERS_IDS |
---|
116 | self.not_like_anonym = ['admin', 'member', 'dm_admin', 'dm_member'] |
---|
117 | self.like_anonym = [u for u in all_users_id |
---|
118 | if u not in self.not_like_anonym] |
---|
119 | |
---|
120 | def testAddCommentToDocLikeAnonymUsers(self): |
---|
121 | # ADDING COMMENTS MUST REFUSED for anonymous users |
---|
122 | failed_users = [] |
---|
123 | for u in self.like_anonym: |
---|
124 | self.login('dm_admin') |
---|
125 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
126 | replies_before = self.getDiscussionReplies(doc_obj) |
---|
127 | |
---|
128 | # Create talkback for document and Add comment |
---|
129 | self.logout() |
---|
130 | if not u == 'anonym': |
---|
131 | self.login(u) |
---|
132 | self.assertRaises(Unauthorized, doc_obj.discussion_reply, |
---|
133 | "%s's reply" % u, "text of '%s' reply" % u) |
---|
134 | self.login('dm_admin') |
---|
135 | replies_after = self.getDiscussionReplies(doc_obj) |
---|
136 | disparity = len(replies_after) - len(replies_before) |
---|
137 | if disparity: |
---|
138 | failed_users.append(u) |
---|
139 | self.failIf(failed_users, |
---|
140 | "%s user(s) CAN really add comment in terned OFF " |
---|
141 | "*Anonymous commenting mode*." % failed_users) |
---|
142 | |
---|
143 | def testAddCommentToDocNotLikeAnonymUsers(self): |
---|
144 | # All users CAN ADD COMMENTS |
---|
145 | failed_users = [] |
---|
146 | for u in self.not_like_anonym: |
---|
147 | self.login('dm_admin') |
---|
148 | doc_id = "doc_%s" % u |
---|
149 | doc_obj = getattr(self.portal, doc_id) |
---|
150 | replies_before = self.getDiscussionReplies(doc_obj) |
---|
151 | self.login(u) |
---|
152 | |
---|
153 | # Create talkback for document and Add comment |
---|
154 | doc_obj.discussion_reply("%s's reply" % u, |
---|
155 | "text of '%s' reply" % u) |
---|
156 | |
---|
157 | # Check is comment added |
---|
158 | self.login('dm_admin') |
---|
159 | replies_after = self.getDiscussionReplies(doc_obj) |
---|
160 | disparity = len(replies_after) - len(replies_before) |
---|
161 | if not disparity: |
---|
162 | failed_users.append(u) |
---|
163 | self.failIf(failed_users, |
---|
164 | "%s - user(s) can not really add commentin terned OFF " |
---|
165 | "*Anonymous commenting mode*." % failed_users) |
---|
166 | |
---|
167 | |
---|
168 | class TestMixinModerationOn: |
---|
169 | |
---|
170 | def getDiscussionReplies(self, doc_obj): |
---|
171 | return self.discussion.getDiscussionFor(doc_obj).getReplies() |
---|
172 | |
---|
173 | def afterSetUp(self): |
---|
174 | # Get Moderation state |
---|
175 | pp = getToolByName(self.portal, 'portal_properties') |
---|
176 | config_ps = getattr(pp, PROPERTY_SHEET, None) |
---|
177 | EnableAnonymComm = getattr(config_ps, "enable_anonymous_commenting") |
---|
178 | |
---|
179 | # Group users depending on Anonymous commenting enabling/disabling |
---|
180 | if EnableAnonymComm: |
---|
181 | self.allowable_dm_users = DM_USERS_IDS |
---|
182 | self.allowable_common_users = COMMON_USERS_IDS |
---|
183 | self.illegal_dm_users = [] |
---|
184 | self.illegal_common_users = [] |
---|
185 | else: |
---|
186 | self.allowable_dm_users = ['dm_admin', 'dm_member'] |
---|
187 | self.allowable_common_users = ['admin', 'member'] |
---|
188 | self.illegal_dm_users = [u for u in DM_USERS_IDS |
---|
189 | if not u in self.allowable_dm_users] |
---|
190 | self.illegal_common_users = [u for u in COMMON_USERS_IDS |
---|
191 | if not u in self.allowable_common_users] |
---|
192 | |
---|
193 | def testAddCommentToNotPublishedReplyDMUsers(self): |
---|
194 | # DiscussionManager's group's members with Manager or Member roles |
---|
195 | # CAN ADD COMMENTS to reply IN ANY STATE (published/not published) |
---|
196 | failed_users = [] |
---|
197 | for u in self.allowable_dm_users: |
---|
198 | self.login(u) |
---|
199 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
200 | # Get reply to this document |
---|
201 | reply = self.getDiscussionReplies(doc_obj)[0] |
---|
202 | # Create talkback for reply and Add comment |
---|
203 | self.discussion.getDiscussionFor(reply) |
---|
204 | reply.discussion_reply("%s's reply" % u, "text of '%s' reply" % u) |
---|
205 | replies_to_reply = self.getDiscussionReplies(reply) |
---|
206 | if not replies_to_reply: |
---|
207 | failed_users.append(u) |
---|
208 | self.failIf(failed_users, |
---|
209 | "%s - member(s) of DiscussionManager group CAN'T really " |
---|
210 | "ADD comment" % failed_users) |
---|
211 | |
---|
212 | # This is actual only in terned OFF *Anonymous commenting mode* |
---|
213 | failed_users = [] |
---|
214 | for u in self.illegal_dm_users: |
---|
215 | self.login(u) |
---|
216 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
217 | # Get reply to this document |
---|
218 | reply = self.getDiscussionReplies(doc_obj)[0] |
---|
219 | # Create talkback for reply and Add comment |
---|
220 | self.discussion.getDiscussionFor(reply) |
---|
221 | self.assertRaises(Unauthorized, reply.discussion_reply, |
---|
222 | "%s's reply" % u, "text of '%s' reply" % u) |
---|
223 | replies_to_reply = self.getDiscussionReplies(reply) |
---|
224 | if replies_to_reply: |
---|
225 | failed_users.append(u) |
---|
226 | self.failIf(failed_users, |
---|
227 | "%s user(s) CAN really add comment in terned OFF " |
---|
228 | "*Anonymous commenting mode*." % failed_users) |
---|
229 | |
---|
230 | """ |
---|
231 | def testAddCommentToNotPublishedReplyNotDMUsers(self): |
---|
232 | # Users without DiscussionManager role CAN'T ACCESS an so ADD COMMENTS |
---|
233 | # TO NOT PUBLISHED reply. |
---|
234 | manager = 'dm_admin' |
---|
235 | for u in self.allowable_common_users: |
---|
236 | self.login(manager) |
---|
237 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
238 | reply = self.getDiscussionReplies(doc_obj)[0] |
---|
239 | reply_to_reply = self.getDiscussionReplies(reply) |
---|
240 | reply_to_reply_before = len(reply_to_reply) |
---|
241 | self.logout() |
---|
242 | if not u=='anonym': |
---|
243 | self.login(u) |
---|
244 | # On adding reply to not published reply MUST generate |
---|
245 | # Unauthorized exception |
---|
246 | self.assertRaises(Unauthorized, reply.discussion_reply, |
---|
247 | "Reply %s" % u, "text of %s reply" % u) |
---|
248 | """ |
---|
249 | |
---|
250 | def testAddCommentToPublishedReplyALLUsers(self): |
---|
251 | # All users CAN ADD COMMENTS to published reply |
---|
252 | manager = 'dm_admin' |
---|
253 | allowable_users = self.allowable_dm_users + self.allowable_common_users |
---|
254 | illegal_users = self.illegal_dm_users + self.illegal_common_users |
---|
255 | all_users = allowable_users + illegal_users |
---|
256 | |
---|
257 | # 1. Publish comments |
---|
258 | self.login(manager) |
---|
259 | for u in all_users: |
---|
260 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
261 | reply = self.getDiscussionReplies(doc_obj)[0] |
---|
262 | reply.discussion_publish_comment() |
---|
263 | |
---|
264 | # 2.Check adding reply to reply for allowable users |
---|
265 | failed_users = [] |
---|
266 | for u in allowable_users: |
---|
267 | self.logout() |
---|
268 | if not u == 'anonym': |
---|
269 | self.login(u) |
---|
270 | |
---|
271 | # Create talkback for document and Add comment |
---|
272 | self.discussion.getDiscussionFor(reply) |
---|
273 | reply.discussion_reply("Reply %s" % u, "text of %s reply" % u) |
---|
274 | |
---|
275 | # Check is comment added |
---|
276 | self.login(manager) |
---|
277 | reply_to_reply = self.getDiscussionReplies(reply) |
---|
278 | if not reply_to_reply: |
---|
279 | failed_users.append(u) |
---|
280 | self.failIf(failed_users, |
---|
281 | "%s - user(s) can not really add comment " |
---|
282 | "to PUBLISHED reply." % failed_users) |
---|
283 | |
---|
284 | # 3.Check adding reply to reply for illegal users |
---|
285 | for u in illegal_users: |
---|
286 | self.logout() |
---|
287 | if not u == 'anonym': |
---|
288 | self.login(u) |
---|
289 | |
---|
290 | # On adding reply to not published reply MUST generate |
---|
291 | # Unauthorized exception |
---|
292 | self.discussion.getDiscussionFor(reply) |
---|
293 | self.assertRaises(Unauthorized, reply.discussion_reply, |
---|
294 | "Reply %s" % u, "text of %s reply" % u) |
---|
295 | |
---|
296 | |
---|
297 | class TestMixinModerationOff: |
---|
298 | |
---|
299 | def afterSetUp(self): |
---|
300 | # Get Moderation state |
---|
301 | pp = getToolByName(self.portal, 'portal_properties') |
---|
302 | config_ps = getattr(pp, PROPERTY_SHEET, None) |
---|
303 | EnableAnonymComm = getattr(config_ps, "enable_anonymous_commenting") |
---|
304 | |
---|
305 | # Group users depending on Anonymous commenting enabling/disabling |
---|
306 | if EnableAnonymComm: |
---|
307 | self.allowable_users = DM_USERS_IDS + COMMON_USERS_IDS |
---|
308 | self.illegal_users = [] |
---|
309 | else: |
---|
310 | self.allowable_users = ['dm_admin', 'dm_member', 'admin', 'member'] |
---|
311 | self.illegal_users = [u for u in self.all_users_id |
---|
312 | if not u in self.allowable_users] |
---|
313 | |
---|
314 | # Add testing document to portal in Moderation OFF mode. |
---|
315 | self.discussion = getToolByName(self.portal, 'portal_discussion', None) |
---|
316 | self.doc_moder_off_id = 'doc_moderation_off' |
---|
317 | self.portal.invokeFactory('Document', id=self.doc_moder_off_id) |
---|
318 | doc_obj = getattr(self.portal, self.doc_moder_off_id) |
---|
319 | doc_obj.edit(text_format='plain', |
---|
320 | text='hello world from in moderation off mode') |
---|
321 | |
---|
322 | # Create talkback for document and Add comment to 'doc_moderatio_off' |
---|
323 | self.discussion.getDiscussionFor(doc_obj) |
---|
324 | doc_obj.discussion_reply("A Reply to '%s'" % self.doc_moder_off_id, |
---|
325 | "text of reply to '%s'" % self.doc_moder_off_id) |
---|
326 | |
---|
327 | def testAddCommentToReplyAllowableUsers(self): |
---|
328 | # Users CAN ADD COMMENTS |
---|
329 | failed_users = [] |
---|
330 | for u in self.allowable_users: |
---|
331 | self.logout() |
---|
332 | if not u == 'anonym': |
---|
333 | self.login(u) |
---|
334 | doc_obj = getattr(self.portal, self.doc_moder_off_id) |
---|
335 | |
---|
336 | # Get reply to this document |
---|
337 | reply_to_doc = self.getDiscussionReplies(doc_obj)[0] |
---|
338 | |
---|
339 | # Create talkback for reply and Add comment |
---|
340 | replies_before = self.getDiscussionReplies(reply_to_doc) |
---|
341 | if not replies_before: |
---|
342 | self.discussion.getDiscussionFor(reply_to_doc) |
---|
343 | reply_to_doc.discussion_reply("%s's reply" % u, |
---|
344 | "text of '%s' reply" % u) |
---|
345 | replies_after = self.getDiscussionReplies(reply_to_doc) |
---|
346 | disparity = len(replies_after) - len(replies_before) |
---|
347 | if not disparity: |
---|
348 | failed_users.append(u) |
---|
349 | self.failIf(failed_users, |
---|
350 | "%s - member(s) CAN'T really ADD comment in terned " |
---|
351 | "off comments Moderation mode." % failed_users) |
---|
352 | |
---|
353 | def testAddCommentToReplyIllegalUsers(self): |
---|
354 | # This users CAN'T ADD COMMENTS |
---|
355 | # This is actual only in terned OFF *Anonymous commenting mode* |
---|
356 | for u in self.illegal_users: |
---|
357 | self.logout() |
---|
358 | if not u == 'anonym': |
---|
359 | self.login(u) |
---|
360 | doc_obj = getattr(self.portal, self.doc_moder_off_id) |
---|
361 | |
---|
362 | # Get reply to this document |
---|
363 | reply_to_doc = self.getDiscussionReplies(doc_obj)[0] |
---|
364 | |
---|
365 | # Create talkback for reply and Add comment |
---|
366 | self.discussion.getDiscussionFor(reply_to_doc) |
---|
367 | self.assertRaises(Unauthorized, reply_to_doc.discussion_reply, |
---|
368 | "%s's reply" % u, "text of '%s' reply" % u) |
---|
369 | |
---|
370 | |
---|
371 | class TestModerationAnonymComm(TestCommBase, TestMixinAnonymOn, |
---|
372 | TestMixinModerationOn): |
---|
373 | |
---|
374 | def afterSetUp(self): |
---|
375 | TestCommBase.afterSetUp(self) |
---|
376 | |
---|
377 | # Preparation for functional testing |
---|
378 | # Tern On Moderation and tern on Anonymous commenting |
---|
379 | self.request.form['enable_anonymous_commenting'] = 'True' |
---|
380 | self.request.form['enable_moderation'] = 'True' |
---|
381 | self.portal.prefs_comments_setup() |
---|
382 | |
---|
383 | # Initialize base classes |
---|
384 | TestMixinAnonymOn.afterSetUp(self) |
---|
385 | TestMixinModerationOn.afterSetUp(self) |
---|
386 | |
---|
387 | |
---|
388 | class TestModerationOFFAnonymComm(TestCommBase, TestMixinAnonymOff, |
---|
389 | TestMixinModerationOn): |
---|
390 | |
---|
391 | def afterSetUp(self): |
---|
392 | TestCommBase.afterSetUp(self) |
---|
393 | |
---|
394 | # Preparation for functional testing |
---|
395 | # Tern On Moderation and tern off Anonymous commenting |
---|
396 | self.request.form['enable_moderation'] = 'True' |
---|
397 | self.portal.prefs_comments_setup() |
---|
398 | |
---|
399 | # Initialize base classes |
---|
400 | TestMixinAnonymOff.afterSetUp(self) |
---|
401 | TestMixinModerationOn.afterSetUp(self) |
---|
402 | |
---|
403 | |
---|
404 | class TestAnonymCommOFFModeration(TestCommBase, TestMixinAnonymOn, |
---|
405 | TestMixinModerationOff): |
---|
406 | |
---|
407 | def afterSetUp(self): |
---|
408 | TestCommBase.afterSetUp(self) |
---|
409 | |
---|
410 | # Preparation for functional testing |
---|
411 | # Tern On Anonymous commenting and tern off Moderation |
---|
412 | self.request.form['enable_anonymous_commenting'] = 'True' |
---|
413 | self.portal.prefs_comments_setup() |
---|
414 | |
---|
415 | # Initialize base classes |
---|
416 | TestMixinAnonymOn.afterSetUp(self) |
---|
417 | TestMixinModerationOff.afterSetUp(self) |
---|
418 | |
---|
419 | |
---|
420 | class TestOFFModerationOFFAnonymComm(TestCommBase, TestMixinAnonymOff, |
---|
421 | TestMixinModerationOff): |
---|
422 | |
---|
423 | def afterSetUp(self): |
---|
424 | TestCommBase.afterSetUp(self) |
---|
425 | |
---|
426 | # Preparation for functional testing |
---|
427 | # Tern Off Moderation and tern off Anonymous commenting |
---|
428 | self.portal.prefs_comments_setup() |
---|
429 | |
---|
430 | # Initialize base classes |
---|
431 | TestMixinAnonymOff.afterSetUp(self) |
---|
432 | TestMixinModerationOff.afterSetUp(self) |
---|
433 | |
---|
434 | |
---|
435 | def test_suite(): |
---|
436 | from unittest import TestSuite, makeSuite |
---|
437 | suite = TestSuite() |
---|
438 | suite.addTest(makeSuite(TestModerationAnonymComm)) |
---|
439 | suite.addTest(makeSuite(TestModerationOFFAnonymComm)) |
---|
440 | suite.addTest(makeSuite(TestAnonymCommOFFModeration)) |
---|
441 | suite.addTest(makeSuite(TestOFFModerationOFFAnonymComm)) |
---|
442 | |
---|
443 | return suite |
---|