source: products/quintagroup.plonecomments/trunk/quintagroup/plonecomments/tests/testQPloneCommentsCommenting.py @ 3116

Last change on this file since 3116 was 3116, checked in by kroman0, 11 years ago

Pyflakes and pep8 fixes #3

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