source: products/qPloneComments/tags/3.2.2/tests/testQPloneCommentsCommenting.py @ 1591

Last change on this file since 1591 was 87, checked in by crchemist, 18 years ago

killed 0.3.0 tag

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