source: products/quintagroup.plonecomments/trunk/quintagroup/plonecomments/tests/testQPloneCommentsModeration.py @ 3112

Last change on this file since 3112 was 3112, checked in by kroman0, 13 years ago

Merged fixes for pyflakes and pylint

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