source: products/qPloneComments/tags/1.7b/tests/testQPloneCommentsCommenting.py @ 458

Last change on this file since 458 was 1, checked in by myroslav, 19 years ago

Building directory structure

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