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