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