source: products/quintagroup.plonecomments/branches/jquery/quintagroup/plonecomments/tests/testQPloneCommentsModeration.py @ 3110

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

Pylint fixes #2

File size: 10.4 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',),
31                                                           'plone_workflow')
32
33        # Add testing documents to portal. Add one document for avery user.
34        # For testing behaviors, where made some changes to document state
35        # it's more usefull.
36        self.discussion = getToolByName(self.portal, 'portal_discussion', None)
37        all_users_id = DM_USERS_IDS + COMMON_USERS_IDS
38        for user_id in all_users_id:
39            doc_id = 'doc_%s' % user_id
40            self.portal.invokeFactory('Document', id=doc_id)
41            doc_obj = getattr(self.portal, doc_id)
42            doc_obj.edit(text_format='plain',
43                         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,
47                                     'text of reply for %s' % doc_id)
48
49    ## TEST VIEWING
50
51    def testViewRepliesNotPublishedDMUsers(self):
52        # All members of DiscussionManager group MUST VIEW comments
53        doc = getattr(self.portal, 'doc_%s' % DM_USERS_IDS[0])
54        for u in DM_USERS_IDS:
55            self.login(u)
56            replies = self.discussion.getDiscussionFor(doc).getReplies()
57            self.failUnless(replies,
58                "Viewing discussion item forbiden for %s - "
59                "member of DiscussionManager group" % u)
60
61    def testViewRepliesNotPublishedNotDMUsers(self):
62        # All common users SHOULD NOT VIEW NOT PUBLISHED comments
63        doc = getattr(self.portal, 'doc_%s' % DM_USERS_IDS[0])
64        roles = [r['name']
65                 for r in self.portal.rolesOfPermission('Moderate Discussion')
66                 if r['selected'] == 'SELECTED']
67        authorized_users = [user for user in COMMON_USERS_IDS
68                                 if user != 'anonym']
69        users_without_md_perm = [u for u in authorized_users
70            if filter(lambda x: x not in roles, USERS[u]['roles'])]
71        for u in users_without_md_perm:
72            self.logout()
73            if not u == 'anonym':
74                self.login(u)
75            replies = self.discussion.getDiscussionFor(doc).getReplies()
76            self.failIf(replies,
77                "Viewing of NOT published discussion item allow %s - "
78                "user without 'Moderate Discussion' permission" % u)
79
80    def testViewRepliesPublishedAllUsers(self):
81        # All users MUST VIEW PUBLISHED comments
82        # Get any document and publish it's comment
83        doc = getattr(self.portal, 'doc_%s' % 'dm_admin')
84        self.login('dm_admin')
85        di = self.discussion.getDiscussionFor(doc).getReplies()[0]
86        di.discussion_publish_comment()
87
88        all_users_id = USERS.keys() + ['anonym']
89        for u in all_users_id:
90            self.logout()
91            if not u == 'anonym':
92                self.login(u)
93            replies = self.discussion.getDiscussionFor(doc).getReplies()
94            self.failUnless(replies,
95                "Viewing PUBLISHED discussion item forbiden for %s user" % u)
96
97    ## TEST PUBLISHING
98
99    def testViewPublishButtonNonDMUsers(self):
100        # Publish button MUST BE ABSENT in document view form
101        # Pattern for publish button presence checking
102        pattern = re.compile('.*<input.+?value="Publish"', re.S | re.M)
103        roles = [r['name']
104                 for r in self.portal.rolesOfPermission('Moderate Discussion')
105                 if r['selected'] == 'SELECTED']
106        authorized_users = [user for user in COMMON_USERS_IDS
107                                 if user != 'anonym']
108        users_without_md_perm = [u for u in authorized_users
109            if filter(lambda x: x not in roles, USERS[u]['roles'])]
110        for u in users_without_md_perm:
111            self.logout()
112            auth = "%s:" % u
113            if not u == 'anonym':
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.failIf(m,
120                "Publish button present for %s - user "
121                "without Moderate Discussion permission" % u)
122
123    """
124    def testViewPublishButtonDMUsers(self):
125        # Publish button MUST PRESENT in document view form
126        # Pattern for publish button presence checking
127        pattern = re.compile('.*<input.+?value="Publish"',re.S|re.M)
128        # pattern = re.compile('.*<input\\s*class="standalone"\\s*type='
129        #                      '"submit"\\s*value="Publish"\\s*/>', re.S|re.M)
130        for u in DM_USERS_IDS:
131            self.login(u)
132            auth = '%s:%s' % (u,USERS[u]['passw'])
133            doc_id = "doc_%s" % u
134            html = str(self.publish(self.portal.id+'/%s' % doc_id, auth))
135            m = pattern.match(html)
136            self.failUnless(m,
137                "Publish button NOT PRESENT for %s - "
138                "member of DiscussionManager group" % u)
139    """
140
141    def testPublishing(self):
142        # Check whether perform real publishing
143        # Pattern for publish button presence checking
144        pattern = re.compile('.*<input.+?value="Publish"', re.S | re.M)
145        for u in DM_USERS_IDS:
146            doc_id = "doc_%s" % u
147            doc_obj = getattr(self.portal, doc_id)
148            getReplies = self.discussion.getDiscussionFor(doc_obj).getReplies
149            # Check whether anonymous get no reply
150            self.logout()
151            self.failIf(getReplies(),
152                        "View not published reply ALLOW for Anonymous")
153            # Login with actual (tested) user with DiscussionManager role and
154            # publish discussion
155            self.login(u)
156            self.failUnless(getReplies(),
157                "%s - member of DiscussionManager group "
158                "NOT VIEW not published reply" % u)
159            getReplies()[0].discussion_publish_comment()
160            # Check if Publish button still present in document view page
161            auth = "%s:" % u
162            if not u == 'anonym':
163                auth = '%s:%s' % (u, USERS[u]['passw'])
164            html = str(self.publish(self.portal.id + '/%s' % doc_id, auth))
165            m = pattern.match(html)
166            self.failIf(m,
167                "Publish button present for %s - DiscussionManager"
168                " role user after publishing" % u)
169            # Check whether Anonym view published reply
170            self.logout()
171            self.failUnless(getReplies(),
172                "%s - member of DiscussionManager group NOT PUBLISH reply" % u)
173
174    ## TEST DELETING
175
176    """
177    def testViewDeleteButtonNonDMUsers(self):
178        # Check Delete reply button presense ONLY for PUBLISHED reply.
179        # Because of NOT PUBLUISHED replies is not visible at all for common
180        # users.
181        # Delete reply button in document view form MUST BE ABSENT for all
182        # EXCEPT manager.
183        # Publish replies
184        self.logout()
185        self.login('dm_admin')
186        for u in COMMON_USERS_IDS:
187            doc_id = "doc_%s" % u
188            doc_obj = getattr(self.portal, doc_id)
189            reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0]
190            reply.discussion_publish_comment()
191        # Prepare pattern for delete reply button presence checking
192        pattern = re.compile('.*<input\\s*class="destructive"\\s*type="submit"'
193                             '\\s*value="Remove"\\s*/>', re.S|re.M)
194        for u in COMMON_USERS_IDS:
195            self.logout()
196            auth = "%s:" % u
197            if not u=='anonym':
198                self.login(u)
199                auth = '%s:%s' % (u,USERS[u]['passw'])
200            doc_id = "doc_%s" % u
201            html = str(self.publish(self.portal.id+'/%s' % doc_id, auth))
202            m = pattern.match(html)
203            if not u=='anonym' and 'Manager' in USERS[u]['roles']:
204                self.failUnless(m,
205                    "%s - user with Manager role NOT VIEW Delete reply button"
206                    " for published reply on document view form" % u)
207            else:
208                self.failIf(m,
209                    "%s - user without Manager role CAN VIEW Delete reply "
210                    "button for published reply on document view form" % u)
211    """
212
213    def testDeleting(self):
214        # Manager with DiscussionManager role CAN delete ANY REPLY.
215        # Manager without DiscussionManager role [common manager] CAN delete
216        # ONLY PUBLISHED REPLY.
217        # Get Managers
218        managers = [u for u in USERS.keys() if 'Manager' in USERS[u]['roles']]
219        dm_man = [u for u in managers if u.startswith('dm_')][0]
220        common_man = [u for u in managers if not u.startswith('dm_')][0]
221        # Publish document for common manager
222        self.login(dm_man)
223        doc_obj = getattr(self.portal, "doc_%s" % common_man)
224        reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0]
225        reply.discussion_publish_comment()
226        # Check for really deleting
227        for u in managers:
228            self.login(u)
229            doc_id = "doc_%s" % u
230            doc_obj = getattr(self.portal, doc_id)
231            getReplies = self.discussion.getDiscussionFor(doc_obj).getReplies
232            self.failUnless(getReplies(),
233                "%s - user with Manager role not view discussion reply" % u)
234            getReplies()[0].deleteDiscussion()
235            self.failIf(getReplies(),
236                "%s - user with Manager role not really delete discussion" % u)
237
238
239def test_suite():
240    from unittest import TestSuite, makeSuite
241    suite = TestSuite()
242    suite.addTest(makeSuite(TestModeration))
243    return suite
Note: See TracBrowser for help on using the repository browser.