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

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

Building directory structure

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