[832] | 1 | # |
---|
| 2 | # Test moderation behavior |
---|
| 3 | # |
---|
| 4 | |
---|
| 5 | import re |
---|
[3112] | 6 | from Products.CMFCore.utils import getToolByName |
---|
| 7 | from quintagroup.plonecomments.tests.common import addMembers, add2Group |
---|
| 8 | from quintagroup.plonecomments.tests.base import FunctionalTestCase |
---|
| 9 | from quintagroup.plonecomments.tests.config import USERS, DM_USERS_IDS, \ |
---|
| 10 | COMMON_USERS_IDS |
---|
[832] | 11 | |
---|
| 12 | |
---|
| 13 | class 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() |
---|
[880] | 53 | self.failUnless(replies, |
---|
| 54 | "Viewing discussion item forbiden for %s - " |
---|
| 55 | "member of DiscussionManager group" % u) |
---|
[832] | 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() |
---|
[880] | 68 | self.failIf(replies, |
---|
| 69 | "Viewing of NOT published discussion item allow %s - " |
---|
| 70 | "user without 'Moderate Discussion' permission" % u) |
---|
[832] | 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() |
---|
[880] | 86 | self.failUnless(replies, |
---|
| 87 | "Viewing PUBLISHED discussion item forbiden for %s user" % u) |
---|
[832] | 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) |
---|
[880] | 107 | self.failIf(m, |
---|
| 108 | "Publish button present for %s - user without Moderate Discussion permission" % u) |
---|
[832] | 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) |
---|
[880] | 122 | self.failUnless(m, |
---|
| 123 | "Publish button NOT PRESENT for %s - member of DiscussionManager group" % u) |
---|
[832] | 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() |
---|
[880] | 136 | self.failIf(getReplies(), "View not published reply ALLOW for Anonymous") |
---|
[832] | 137 | # Login with actual (tested) user with DiscussionManager role and publish discussion |
---|
| 138 | self.login(u) |
---|
[880] | 139 | self.failUnless(getReplies(), |
---|
| 140 | "%s - member of DiscussionManager group NOT VIEW not published reply" % u) |
---|
[832] | 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) |
---|
[880] | 148 | self.failIf(m, |
---|
| 149 | "Publish button present for %s - DiscussionManager role user after publishing" % u) |
---|
[832] | 150 | # Check whether Anonym view published reply |
---|
| 151 | self.logout() |
---|
[880] | 152 | self.failUnless(getReplies(), |
---|
| 153 | "%s - member of DiscussionManager group NOT PUBLISH reply" % u) |
---|
[832] | 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']: |
---|
[880] | 182 | self.failUnless(m, |
---|
| 183 | "%s - user with Manager role NOT VIEW Delete reply button for " |
---|
| 184 | "published reply on document view form" % u) |
---|
[832] | 185 | else: |
---|
[880] | 186 | self.failIf(m, |
---|
| 187 | "%s - user without Manager role CAN VIEW Delete reply button for " |
---|
| 188 | "published reply on document view form" % u) |
---|
[832] | 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 |
---|
[880] | 210 | self.failUnless(getReplies(), "%s - user with Manager role not view discussion reply" % u) |
---|
[832] | 211 | getReplies()[0].deleteDiscussion() |
---|
[880] | 212 | self.failIf(getReplies(), "%s - user with Manager role not really delete discussion" % u) |
---|
[832] | 213 | |
---|
| 214 | |
---|
| 215 | def test_suite(): |
---|
| 216 | from unittest import TestSuite, makeSuite |
---|
| 217 | suite = TestSuite() |
---|
| 218 | suite.addTest(makeSuite(TestModeration)) |
---|
| 219 | return suite |
---|