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

Last change on this file since 1591 was 1, checked in by myroslav, 18 years ago

Building directory structure

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