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', |
---|
46 | None) |
---|
47 | self.all_users_id = DM_USERS_IDS + COMMON_USERS_IDS |
---|
48 | for user_id in self.all_users_id: |
---|
49 | doc_id = 'doc_%s' % user_id |
---|
50 | self.portal.invokeFactory('Document', id=doc_id) |
---|
51 | doc_obj = getattr(self.portal, doc_id) |
---|
52 | doc_obj.edit(text_format='plain', |
---|
53 | text='hello world from %s' % doc_id) |
---|
54 | # Create talkback for document and Add comment to doc_obj |
---|
55 | self.discussion.getDiscussionFor(doc_obj) |
---|
56 | doc_obj.discussion_reply('A Reply for %s' % doc_id, |
---|
57 | 'text of reply for %s' % doc_id) |
---|
58 | |
---|
59 | |
---|
60 | class TestMixinAnonymOn: |
---|
61 | |
---|
62 | def afterSetUp(self): |
---|
63 | pass |
---|
64 | |
---|
65 | def testAddCommentToDocAnonymUsers(self): |
---|
66 | |
---|
67 | # ADDING COMMENTS MUST ALLOWED for anonymous users |
---|
68 | self.login('dm_admin') |
---|
69 | doc_obj = getattr(self.portal, "doc_anonym") |
---|
70 | discussion_for = self.discussion.getDiscussionFor(doc_obj) |
---|
71 | replies_before = len(discussion_for.getReplies()) |
---|
72 | |
---|
73 | # Create talkback for document and Add comment |
---|
74 | self.logout() |
---|
75 | doc_obj.discussion_reply("Anonym reply", "text of 'anonym' reply") |
---|
76 | self.login('dm_admin') |
---|
77 | replies_after = len(discussion_for.getReplies()) |
---|
78 | self.failUnless(replies_after - replies_before, |
---|
79 | "Anonymous user CAN'T really add comment in terned ON " |
---|
80 | "*Anonymous commenting mode*.") |
---|
81 | |
---|
82 | def testAddCommentToDocNotAnonymUsers(self): |
---|
83 | |
---|
84 | # All users CAN ADD COMMENTS |
---|
85 | not_anonym_users = [u for u in self.all_users_id if not u == 'anonym'] |
---|
86 | failed_users = [] |
---|
87 | for u in not_anonym_users: |
---|
88 | self.login('dm_admin') |
---|
89 | doc_id = "doc_%s" % u |
---|
90 | doc_obj = getattr(self.portal, doc_id) |
---|
91 | discussion_for = self.discussion.getDiscussionFor(doc_obj) |
---|
92 | replies_before = discussion_for.getReplies() |
---|
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 = discussion_for.getReplies() |
---|
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 afterSetUp(self): |
---|
112 | all_users_id = DM_USERS_IDS + COMMON_USERS_IDS |
---|
113 | self.not_like_anonym = ['admin', 'member', 'dm_admin', 'dm_member'] |
---|
114 | self.like_anonym = [u |
---|
115 | for u in all_users_id |
---|
116 | if u not in self.not_like_anonym] |
---|
117 | |
---|
118 | def testAddCommentToDocLikeAnonymUsers(self): |
---|
119 | |
---|
120 | # ADDING COMMENTS MUST REFUSED for anonymous users |
---|
121 | failed_users = [] |
---|
122 | for u in self.like_anonym: |
---|
123 | self.login('dm_admin') |
---|
124 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
125 | discussion_for = self.discussion.getDiscussionFor(doc_obj) |
---|
126 | replies_before = discussion_for.getReplies() |
---|
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 = discussion_for.getReplies() |
---|
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 | |
---|
145 | # All users CAN ADD COMMENTS |
---|
146 | failed_users = [] |
---|
147 | for u in self.not_like_anonym: |
---|
148 | self.login('dm_admin') |
---|
149 | doc_id = "doc_%s" % u |
---|
150 | doc_obj = getattr(self.portal, doc_id) |
---|
151 | discussion_for = self.discussion.getDiscussionFor(doc_obj) |
---|
152 | replies_before = discussion_for.getReplies() |
---|
153 | self.login(u) |
---|
154 | |
---|
155 | # Create talkback for document and Add comment |
---|
156 | doc_obj.discussion_reply("%s's reply" % u, |
---|
157 | "text of '%s' reply" % u) |
---|
158 | |
---|
159 | # Check is comment added |
---|
160 | self.login('dm_admin') |
---|
161 | replies_after = discussion_for.getReplies() |
---|
162 | disparity = len(replies_after) - len(replies_before) |
---|
163 | if not disparity: |
---|
164 | failed_users.append(u) |
---|
165 | self.failIf(failed_users, |
---|
166 | "%s - user(s) can not really add commentin terned OFF " |
---|
167 | "*Anonymous commenting mode*." % failed_users) |
---|
168 | |
---|
169 | |
---|
170 | class TestMixinModerationOn: |
---|
171 | |
---|
172 | def afterSetUp(self): |
---|
173 | |
---|
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 |
---|
189 | for u in DM_USERS_IDS |
---|
190 | if not u in self.allowable_dm_users] |
---|
191 | self.illegal_common_users = [u |
---|
192 | for u in COMMON_USERS_IDS |
---|
193 | if not u in self.allowable_common_users] |
---|
194 | |
---|
195 | def testAddCommentToNotPublishedReplyDMUsers(self): |
---|
196 | |
---|
197 | # DiscussionManager's group's members with Manager or Member roles |
---|
198 | # CAN ADD COMMENTS to reply IN ANY STATE (published/not published) |
---|
199 | failed_users = [] |
---|
200 | for u in self.allowable_dm_users: |
---|
201 | self.login(u) |
---|
202 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
203 | # Get reply to this document |
---|
204 | reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0] |
---|
205 | # Create talkback for reply and Add comment |
---|
206 | self.discussion.getDiscussionFor(reply) |
---|
207 | reply.discussion_reply("%s's reply" % u, "text of '%s' reply" % u) |
---|
208 | discussion_for = self.discussion.getDiscussionFor(reply) |
---|
209 | replies_to_reply = discussion_for.getReplies() |
---|
210 | if not replies_to_reply: |
---|
211 | failed_users.append(u) |
---|
212 | self.failIf(failed_users, "%s - member(s) of DiscussionManager group " |
---|
213 | "CAN'T really ADD comment" % failed_users) |
---|
214 | |
---|
215 | # This is actual only in terned OFF *Anonymous commenting mode* |
---|
216 | failed_users = [] |
---|
217 | for u in self.illegal_dm_users: |
---|
218 | self.login(u) |
---|
219 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
220 | # Get reply to this document |
---|
221 | reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0] |
---|
222 | # Create talkback for reply and Add comment |
---|
223 | self.discussion.getDiscussionFor(reply) |
---|
224 | self.assertRaises(Unauthorized, reply.discussion_reply, |
---|
225 | "%s's reply" % u, "text of '%s' reply" % u) |
---|
226 | discussion_for = self.discussion.getDiscussionFor(reply) |
---|
227 | replies_to_reply = discussion_for.getReplies() |
---|
228 | if replies_to_reply: |
---|
229 | failed_users.append(u) |
---|
230 | self.failIf(failed_users, |
---|
231 | "%s user(s) CAN really add comment in terned OFF " |
---|
232 | "*Anonymous commenting mode*." % failed_users) |
---|
233 | |
---|
234 | """ |
---|
235 | def testAddCommentToNotPublishedReplyNotDMUsers(self): |
---|
236 | # Users without DiscussionManager role CAN'T ACCESS an so ADD COMMENTS |
---|
237 | # TO NOT PUBLISHED reply. |
---|
238 | manager = 'dm_admin' |
---|
239 | for u in self.allowable_common_users: |
---|
240 | self.login(manager) |
---|
241 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
242 | reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0] |
---|
243 | discussion_for = self.discussion.getDiscussionFor(reply) |
---|
244 | reply_to_reply = discussion_for.getReplies() |
---|
245 | reply_to_reply_before = len(reply_to_reply) |
---|
246 | self.logout() |
---|
247 | if not u=='anonym': |
---|
248 | self.login(u) |
---|
249 | # On adding reply to not published reply MUST generte |
---|
250 | # Unauthorized exception |
---|
251 | self.assertRaises(Unauthorized, reply.discussion_reply, |
---|
252 | "Reply %s" % u, "text of %s reply" % u) |
---|
253 | """ |
---|
254 | |
---|
255 | def testAddCommentToPublishedReplyALLUsers(self): |
---|
256 | |
---|
257 | # All users CAN ADD COMMENTS to published reply |
---|
258 | manager = 'dm_admin' |
---|
259 | allowable_users = self.allowable_dm_users + self.allowable_common_users |
---|
260 | illegal_users = self.illegal_dm_users + self.illegal_common_users |
---|
261 | all_users = allowable_users + illegal_users |
---|
262 | |
---|
263 | # 1. Publish comments |
---|
264 | self.login(manager) |
---|
265 | for u in all_users: |
---|
266 | doc_obj = getattr(self.portal, "doc_%s" % u) |
---|
267 | reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0] |
---|
268 | reply.discussion_publish_comment() |
---|
269 | |
---|
270 | # 2.Check adding reply to reply for allowable users |
---|
271 | failed_users = [] |
---|
272 | for u in allowable_users: |
---|
273 | self.logout() |
---|
274 | if not u == 'anonym': |
---|
275 | self.login(u) |
---|
276 | |
---|
277 | # Create talkback for document and Add comment |
---|
278 | self.discussion.getDiscussionFor(reply) |
---|
279 | reply.discussion_reply("Reply %s" % u, "text of %s reply" % u) |
---|
280 | |
---|
281 | # Check is comment added |
---|
282 | self.login(manager) |
---|
283 | discussion_for = self.discussion.getDiscussionFor(reply) |
---|
284 | reply_to_reply = discussion_for.getReplies() |
---|
285 | if not reply_to_reply: |
---|
286 | failed_users.append(u) |
---|
287 | self.failIf(failed_users, "%s - user(s) can not really add comment to" |
---|
288 | " PUBLISHED reply." % failed_users) |
---|
289 | |
---|
290 | # 3.Check adding reply to reply for illegal users |
---|
291 | for u in illegal_users: |
---|
292 | self.logout() |
---|
293 | if not u == 'anonym': |
---|
294 | self.login(u) |
---|
295 | |
---|
296 | # On adding reply to not published reply MUST generte |
---|
297 | # Unauthorized exception |
---|
298 | self.discussion.getDiscussionFor(reply) |
---|
299 | self.assertRaises(Unauthorized, reply.discussion_reply, |
---|
300 | "Reply %s" % u, "text of %s reply" % u) |
---|
301 | |
---|
302 | |
---|
303 | class TestMixinModerationOff: |
---|
304 | |
---|
305 | def afterSetUp(self): |
---|
306 | |
---|
307 | # Get Moderation state |
---|
308 | pp = getToolByName(self.portal, 'portal_properties') |
---|
309 | config_ps = getattr(pp, PROPERTY_SHEET, None) |
---|
310 | EnableAnonymComm = getattr(config_ps, "enable_anonymous_commenting") |
---|
311 | |
---|
312 | # Group users depending on Anonymous commenting enabling/disabling |
---|
313 | if EnableAnonymComm: |
---|
314 | self.allowable_users = DM_USERS_IDS + COMMON_USERS_IDS |
---|
315 | self.illegal_users = [] |
---|
316 | else: |
---|
317 | self.allowable_users = ['dm_admin', 'dm_member', 'admin', 'member'] |
---|
318 | self.illegal_users = [u |
---|
319 | for u in self.all_users_id |
---|
320 | if not u in self.allowable_users] |
---|
321 | |
---|
322 | # Add testing document to portal in Moderation OFF mode. |
---|
323 | self.discussion = getToolByName(self.portal, 'portal_discussion', None) |
---|
324 | self.doc_moder_off_id = 'doc_moderation_off' |
---|
325 | self.portal.invokeFactory('Document', id=self.doc_moder_off_id) |
---|
326 | doc_obj = getattr(self.portal, self.doc_moder_off_id) |
---|
327 | doc_obj.edit(text_format='plain', |
---|
328 | text='hello world from in moderation off mode') |
---|
329 | |
---|
330 | # Create talkback for document and Add comment to 'doc_moderatio_off' |
---|
331 | self.discussion.getDiscussionFor(doc_obj) |
---|
332 | doc_obj.discussion_reply("A Reply to '%s'" % self.doc_moder_off_id, |
---|
333 | "text of reply to '%s'" % self.doc_moder_off_id) |
---|
334 | |
---|
335 | def testAddCommentToReplyAllowableUsers(self): |
---|
336 | |
---|
337 | # Users CAN ADD COMMENTS |
---|
338 | failed_users = [] |
---|
339 | for u in self.allowable_users: |
---|
340 | self.logout() |
---|
341 | if not u == 'anonym': |
---|
342 | self.login(u) |
---|
343 | doc_obj = getattr(self.portal, self.doc_moder_off_id) |
---|
344 | |
---|
345 | # Get reply to this document |
---|
346 | discussion_for = self.discussion.getDiscussionFor(doc_obj) |
---|
347 | reply_to_doc = discussion_for.getReplies()[0] |
---|
348 | |
---|
349 | # Create talkback for reply and Add comment |
---|
350 | discussion_for = self.discussion.getDiscussionFor(reply_to_doc) |
---|
351 | replies_before = discussion_for.getReplies() |
---|
352 | if not replies_before: |
---|
353 | self.discussion.getDiscussionFor(reply_to_doc) |
---|
354 | reply_to_doc.discussion_reply("%s's reply" % u, |
---|
355 | "text of '%s' reply" % u) |
---|
356 | replies_after = discussion_for.getReplies() |
---|
357 | disparity = len(replies_after) - len(replies_before) |
---|
358 | if not disparity: |
---|
359 | failed_users.append(u) |
---|
360 | self.failIf(failed_users, |
---|
361 | "%s - member(s) CAN'T really ADD comment in terned off" |
---|
362 | " comments Moderation mode." % failed_users) |
---|
363 | |
---|
364 | def testAddCommentToReplyIllegalUsers(self): |
---|
365 | |
---|
366 | # This users CAN'T ADD COMMENTS |
---|
367 | # This is actual only in terned OFF *Anonymous commenting mode* |
---|
368 | for u in self.illegal_users: |
---|
369 | self.logout() |
---|
370 | if not u == 'anonym': |
---|
371 | self.login(u) |
---|
372 | doc_obj = getattr(self.portal, self.doc_moder_off_id) |
---|
373 | |
---|
374 | # Get reply to this document |
---|
375 | discussion_for = self.discussion.getDiscussionFor(doc_obj) |
---|
376 | reply_to_doc = discussion_for.getReplies()[0] |
---|
377 | |
---|
378 | # Create talkback for reply and Add comment |
---|
379 | self.discussion.getDiscussionFor(reply_to_doc) |
---|
380 | self.assertRaises(Unauthorized, reply_to_doc.discussion_reply, |
---|
381 | "%s's reply" % u, "text of '%s' reply" % u) |
---|
382 | |
---|
383 | |
---|
384 | class TestModerationAnonymComm(TestCommBase, TestMixinAnonymOn, |
---|
385 | TestMixinModerationOn): |
---|
386 | |
---|
387 | def afterSetUp(self): |
---|
388 | TestCommBase.afterSetUp(self) |
---|
389 | |
---|
390 | # Preparation for functional testing |
---|
391 | # Tern On Moderation and tern on Anonymous commenting |
---|
392 | self.request.form['enable_anonymous_commenting'] = 'True' |
---|
393 | self.request.form['enable_moderation'] = 'True' |
---|
394 | self.portal.prefs_comments_setup() |
---|
395 | |
---|
396 | # Initialize base classes |
---|
397 | TestMixinAnonymOn.afterSetUp(self) |
---|
398 | TestMixinModerationOn.afterSetUp(self) |
---|
399 | |
---|
400 | |
---|
401 | class TestModerationOFFAnonymComm(TestCommBase, TestMixinAnonymOff, |
---|
402 | TestMixinModerationOn): |
---|
403 | |
---|
404 | def afterSetUp(self): |
---|
405 | TestCommBase.afterSetUp(self) |
---|
406 | |
---|
407 | # Preparation for functional testing |
---|
408 | # Tern On Moderation and tern off Anonymous commenting |
---|
409 | self.request.form['enable_moderation'] = 'True' |
---|
410 | self.portal.prefs_comments_setup() |
---|
411 | |
---|
412 | # Initialize base classes |
---|
413 | TestMixinAnonymOff.afterSetUp(self) |
---|
414 | TestMixinModerationOn.afterSetUp(self) |
---|
415 | |
---|
416 | |
---|
417 | class TestAnonymCommOFFModeration(TestCommBase, TestMixinAnonymOn, |
---|
418 | TestMixinModerationOff): |
---|
419 | |
---|
420 | def afterSetUp(self): |
---|
421 | TestCommBase.afterSetUp(self) |
---|
422 | |
---|
423 | # Preparation for functional testing |
---|
424 | # Tern On Anonymous commenting and tern off Moderation |
---|
425 | self.request.form['enable_anonymous_commenting'] = 'True' |
---|
426 | self.portal.prefs_comments_setup() |
---|
427 | |
---|
428 | # Initialize base classes |
---|
429 | TestMixinAnonymOn.afterSetUp(self) |
---|
430 | TestMixinModerationOff.afterSetUp(self) |
---|
431 | |
---|
432 | |
---|
433 | class TestOFFModerationOFFAnonymComm(TestCommBase, TestMixinAnonymOff, |
---|
434 | TestMixinModerationOff): |
---|
435 | |
---|
436 | def afterSetUp(self): |
---|
437 | TestCommBase.afterSetUp(self) |
---|
438 | |
---|
439 | # Preparation for functional testing |
---|
440 | # Tern Off Moderation and tern off Anonymous commenting |
---|
441 | self.portal.prefs_comments_setup() |
---|
442 | |
---|
443 | # Initialize base classes |
---|
444 | TestMixinAnonymOff.afterSetUp(self) |
---|
445 | TestMixinModerationOff.afterSetUp(self) |
---|
446 | |
---|
447 | |
---|
448 | def test_suite(): |
---|
449 | from unittest import TestSuite, makeSuite |
---|
450 | suite = TestSuite() |
---|
451 | suite.addTest(makeSuite(TestModerationAnonymComm)) |
---|
452 | suite.addTest(makeSuite(TestModerationOFFAnonymComm)) |
---|
453 | suite.addTest(makeSuite(TestAnonymCommOFFModeration)) |
---|
454 | suite.addTest(makeSuite(TestOFFModerationOFFAnonymComm)) |
---|
455 | |
---|
456 | return suite |
---|