source: products/qPloneComments/tags/3.1/tests/testQPloneCommentsModeration.py @ 1591

Last change on this file since 1591 was 115, checked in by chervol, 18 years ago

fixed qtopic imports

  • Property svn:eol-style set to native
File size: 9.7 KB
Line 
1#
2# Test moderation behavior
3#
4
5from Products.PloneTestCase import PloneTestCase
6from Products.CMFCore.utils import getToolByName
7import re
8
9from common import *
10
11PloneTestCase.installProduct(PRODUCT)
12PloneTestCase.setupPloneSite()
13
14
15class TestModeration(PloneTestCase.FunctionalTestCase):
16
17    def afterSetUp(self):
18        self.loginAsPortalOwner()
19        self.portal.portal_quickinstaller.installProduct(PRODUCT)
20
21        # Add all users
22        addMembers(self.portal, USERS)
23
24        # Add users to Discussion Manager group
25        add2Group(self.portal, 'DiscussionManager', DM_USERS_IDS)
26
27        # Allow discussion for Document
28        portal_types = getToolByName(self.portal, 'portal_types')
29        doc_fti = portal_types.getTypeInfo('Document')
30        doc_fti._updateProperty('allow_discussion', 1)
31
32        # Make sure Documents are visible by default
33        self.portal.portal_workflow.setChainForPortalTypes(('Document',), 'plone_workflow')
34
35        # Add testing documents to portal. Add one document for avery user.
36        # For testing behaviors, where made some changes to document state it's more usefull.
37        self.discussion = getToolByName(self.portal, 'portal_discussion', None)
38        all_users_id = DM_USERS_IDS + COMMON_USERS_IDS
39        for user_id in all_users_id:
40            doc_id = 'doc_%s' % user_id
41            self.portal.invokeFactory('Document', id=doc_id)
42            doc_obj = getattr(self.portal, doc_id)
43            doc_obj.edit(text_format='plain', text='hello world from %s' % doc_id)
44            # Create talkback for document and Add comment to doc_obj
45            self.discussion.getDiscussionFor(doc_obj)
46            doc_obj.discussion_reply('A Reply for %s' % doc_id,'text of reply for %s' % doc_id)
47
48    ## TEST VIEWING
49
50    def testViewRepliesNotPublishedDMUsers(self):
51        # All members of DiscussionManager group MUST VIEW comments
52        doc = getattr(self.portal, 'doc_%s' % DM_USERS_IDS[0])
53        for u in DM_USERS_IDS:
54            self.login(u)
55            replies = self.discussion.getDiscussionFor(doc).getReplies()
56            self.assert_(replies, "Viewing discussion item forbiden for %s - member of DiscussionManager group" % u)
57
58    def testViewRepliesNotPublishedNotDMUsers(self):
59        # All common users SHOULD NOT VIEW NOT PUBLISHED comments
60        doc = getattr(self.portal, 'doc_%s' % DM_USERS_IDS[0])
61        roles = [r['name'] for r in self.portal.rolesOfPermission('Moderate Discussion') if r['selected'] == 'SELECTED']
62        authorized_users = [user for user in COMMON_USERS_IDS if user !='anonym']
63        users_without_md_perm = [u for u in authorized_users if filter(lambda x: x not in roles, USERS[u]['roles'])]
64        for u in users_without_md_perm:
65            self.logout()
66            if not u=='anonym':
67                self.login(u)
68            replies = self.discussion.getDiscussionFor(doc).getReplies()
69            self.assert_(not replies, "Viewing of NOT published discussion item allow %s - user without 'Moderate Discussion' permission" % u)
70
71    def testViewRepliesPublishedAllUsers(self):
72        # All users MUST VIEW PUBLISHED comments
73        # Get any document and publish it's comment
74        doc = getattr(self.portal, 'doc_%s' % 'dm_admin')
75        self.login('dm_admin')
76        di = self.discussion.getDiscussionFor(doc).getReplies()[0]
77        di.discussion_publish_comment()
78
79        all_users_id = USERS.keys() + ['anonym']
80        for u in all_users_id:
81            self.logout()
82            if not u=='anonym':
83                self.login(u)
84            replies = self.discussion.getDiscussionFor(doc).getReplies()
85            self.assert_(replies, "Viewing PUBLISHED discussion item forbiden for %s user" % u)
86
87    ## TEST PUBLISHING
88
89    def testViewPublishButtonNonDMUsers(self):
90        # Publish button MUST BE ABSENT in document view form
91        # Pattern for publish button presence checking
92        pattern = re.compile('.*<input.+?value="Publish"', re.S|re.M)
93        roles = [r['name'] for r in self.portal.rolesOfPermission('Moderate Discussion') if r['selected'] == 'SELECTED']
94        authorized_users = [user for user in COMMON_USERS_IDS if user !='anonym']
95        users_without_md_perm = [u for u in authorized_users if filter(lambda x: x not in roles, USERS[u]['roles'])]
96        for u in users_without_md_perm:
97            self.logout()
98            auth = "%s:" % u
99            if not u=='anonym':
100                self.login(u)
101                auth = '%s:%s' % (u,USERS[u]['passw'])
102            doc_id = "doc_%s" % u
103            html = str(self.publish(self.portal.id+'/%s' % doc_id, auth))
104            m = pattern.match(html)
105            self.assert_(not m, "Publish button present for %s - user without Moderate Discussion permission" % u)
106
107    def testViewPublishButtonDMUsers(self):
108        # Publish button MUST PRESENT in document view form
109        # Pattern for publish button presence checking
110        pattern = re.compile('.*<input.+?value="Publish"',re.S|re.M)
111        # pattern = re.compile('.*<input\\s*class="standalone"\\s*type="submit"\\s*value="Publish"\\s*/>', re.S|re.M)
112        import pdb; pdb.set_trace()
113        for u in DM_USERS_IDS:
114            self.login(u)
115            auth = '%s:%s' % (u,USERS[u]['passw'])
116            doc_id = "doc_%s" % u
117            html = str(self.publish(self.portal.id+'/%s' % doc_id, auth))
118            m = pattern.match(html)
119            self.assert_(m, "Publish button NOT PRESENT for %s - member of DiscussionManager group" % u)
120
121
122    def testPublishing(self):
123        # Check whether perform real publishing
124        # Pattern for publish button presence checking
125        pattern = re.compile('.*<input.+?value="Publish"',re.S|re.M)
126        for u in DM_USERS_IDS:
127            doc_id = "doc_%s" % u
128            doc_obj = getattr(self.portal, doc_id)
129            getReplies = self.discussion.getDiscussionFor(doc_obj).getReplies
130            # Check whether anonymous get no reply
131            self.logout()
132            self.assert_(not getReplies(), "View not published reply ALLOW for Anonymous")
133            # Login with actual (tested) user with DiscussionManager role and publish discussion
134            self.login(u)
135            self.assert_(getReplies(), "%s - member of DiscussionManager group NOT VIEW not published reply" % u)
136            getReplies()[0].discussion_publish_comment()
137            # Check if Publish button still present in document view page
138            auth = "%s:" % u
139            if not u=='anonym':
140                auth = '%s:%s' % (u,USERS[u]['passw'])
141            html = str(self.publish(self.portal.id+'/%s' % doc_id, auth))
142            m = pattern.match(html)
143            self.assert_(not m, "Publish button present for %s - DiscussionManager role user after publishing" % u)
144            # Check whether Anonym view published reply
145            self.logout()
146            self.assert_(getReplies(), "%s - member of DiscussionManager group NOT PUBLISH reply" % u)
147
148    ## TEST DELETING
149
150    def testViewDeleteButtonNonDMUsers(self):
151        # Check Delete reply button presense ONLY for PUBLISHED reply.
152        # Because of NOT PUBLUISHED replies is not visible at all for common users.
153        # Delete reply button in document view form MUST BE ABSENT for all EXCEPT manager.
154        # Publish replies
155        self.logout()
156        self.login('dm_admin')
157        for u in COMMON_USERS_IDS:
158            doc_id = "doc_%s" % u
159            doc_obj = getattr(self.portal, doc_id)
160            reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0]
161            reply.discussion_publish_comment()
162        # Prepare pattern for delete reply button presence checking
163        pattern = re.compile('.*<input\\s*class="destructive"\\s*type="submit"\\s*value="Remove"\\s*/>', re.S|re.M)
164        for u in COMMON_USERS_IDS:
165            self.logout()
166            auth = "%s:" % u
167            if not u=='anonym':
168                self.login(u)
169                auth = '%s:%s' % (u,USERS[u]['passw'])
170            doc_id = "doc_%s" % u
171            html = str(self.publish(self.portal.id+'/%s' % doc_id, auth))
172            m = pattern.match(html)
173            if not u=='anonym' and 'Manager' in USERS[u]['roles']:
174                self.assert_(m, "%s - user with Manager role NOT VIEW Delete reply button for published reply on document view form" % u)
175            else:
176                self.assert_(not m, "%s - user without Manager role CAN VIEW Delete reply button for published reply on document view form" % u)
177
178    def testDeleting(self):
179        # Manager with DiscussionManager role CAN delete ANY REPLY.
180        # Manager without DiscussionManager role [common manager] CAN delete ONLY PUBLISHED REPLY.
181        # Get Managers
182        managers = [u for u in USERS.keys() if 'Manager' in USERS[u]['roles']]
183        dm_man = [u for u in managers if u.startswith('dm_')][0]
184        common_man = [u for u in managers if not u.startswith('dm_')][0]
185        # Publish document for common manager
186        self.login(dm_man)
187        doc_obj = getattr(self.portal, "doc_%s" % common_man)
188        reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0]
189        reply.discussion_publish_comment()
190        # Check for really deleting
191        for u in managers:
192            self.login(u)
193            auth = '%s:%s' % (u,USERS[u]['passw'])
194            doc_id = "doc_%s" % u
195            doc_obj = getattr(self.portal, doc_id)
196            getReplies = self.discussion.getDiscussionFor(doc_obj).getReplies
197            self.assert_(getReplies(), "%s - user with Manager role not view discussion reply" % u)
198            getReplies()[0].deleteDiscussion()
199            self.assert_(not getReplies(), "%s - user with Manager role not really delete discussion" % u)
200
201
202def test_suite():
203    from unittest import TestSuite, makeSuite
204    suite = TestSuite()
205    suite.addTest(makeSuite(TestModeration))
206    return suite
Note: See TracBrowser for help on using the repository browser.