1 | # |
---|
2 | # Test moderation behavior |
---|
3 | # |
---|
4 | |
---|
5 | import os, sys, string |
---|
6 | if __name__ == '__main__': |
---|
7 | execfile(os.path.join(sys.path[0], 'framework.py')) |
---|
8 | |
---|
9 | from Products.PloneTestCase import PloneTestCase |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | import re |
---|
12 | |
---|
13 | from common import * |
---|
14 | |
---|
15 | PloneTestCase.installProduct(PRODUCT) |
---|
16 | PloneTestCase.setupPloneSite() |
---|
17 | |
---|
18 | class TestModeration(PloneTestCase.FunctionalTestCase): |
---|
19 | |
---|
20 | def afterSetUp(self): |
---|
21 | self.loginAsPortalOwner() |
---|
22 | |
---|
23 | self.qi = getToolByName(self.portal, 'portal_quickinstaller', None) |
---|
24 | self.qi.installProduct(PRODUCT) |
---|
25 | # VERY IMPORTANT to guarantee product skin's content visibility |
---|
26 | self._refreshSkinData() |
---|
27 | |
---|
28 | # Get Plone version |
---|
29 | self.version = getToolByName(self.portal, 'portal_migration').getFileSystemVersion() |
---|
30 | |
---|
31 | # Add all users |
---|
32 | addMembers(self.portal, USERS) |
---|
33 | |
---|
34 | # For correct testing notification - add 'portal_owner' with 'email' |
---|
35 | #self.membership.addMember('portal_owner', 'secret' , ['Manager'], []) |
---|
36 | #member = self.membership.getMemberById('portal_owner') |
---|
37 | #member.setMemberProperties({'email':'creator@test.com'}) |
---|
38 | #self.login('portal_owner') |
---|
39 | |
---|
40 | # Add users to Discussion Manager group |
---|
41 | add2Group(self.portal, 'DiscussionManager', DM_USERS_IDS) |
---|
42 | |
---|
43 | #portal_groups = getToolByName(self.portal, 'portal_groups') |
---|
44 | ##portal_groups.addGroup('DiscussionManager', roles=['DiscussionManager']) |
---|
45 | #dm_group = portal_groups.getGroupById('DiscussionManager') |
---|
46 | #dm_users = [dm_group.addMember(u) for u in DM_USERS_IDS] |
---|
47 | |
---|
48 | # Allow discussion for Document |
---|
49 | portal_types = getToolByName(self.portal, 'portal_types', None) |
---|
50 | doc_fti = portal_types.getTypeInfo('Document') |
---|
51 | doc_fti._updateProperty('allow_discussion', 1) |
---|
52 | |
---|
53 | # Make sure Documents are visible by default |
---|
54 | # XXX only do this for plone 3 |
---|
55 | self.portal.portal_workflow.setChainForPortalTypes(('Document',), 'plone_workflow') |
---|
56 | |
---|
57 | # Add testing documents to portal. Add one document for avery user. |
---|
58 | # For testing behaviors, where made some changes to document state it's more usefull. |
---|
59 | self.discussion = getToolByName(self.portal, 'portal_discussion', None) |
---|
60 | all_users_id = DM_USERS_IDS + COMMON_USERS_IDS |
---|
61 | for user_id in all_users_id: |
---|
62 | doc_id = 'doc_%s' % user_id |
---|
63 | self.portal.invokeFactory('Document', id=doc_id) |
---|
64 | doc_obj = getattr(self.portal, doc_id) |
---|
65 | doc_obj.edit(text_format='plain', text='hello world from %s' % doc_id) |
---|
66 | # Create talkback for document and Add comment to doc_obj |
---|
67 | self.discussion.getDiscussionFor(doc_obj) |
---|
68 | doc_obj.discussion_reply('A Reply for %s' % doc_id,'text of reply for %s' % doc_id) |
---|
69 | |
---|
70 | ## TEST VIEWING |
---|
71 | |
---|
72 | def testViewRepliesNotPublishedDMUsers(self): |
---|
73 | # All members of DiscussionManager group MUST VIEW comments |
---|
74 | doc = getattr(self.portal, 'doc_%s' % DM_USERS_IDS[0]) |
---|
75 | for u in DM_USERS_IDS: |
---|
76 | self.logout() |
---|
77 | self.login(u) |
---|
78 | replies = self.discussion.getDiscussionFor(doc).getReplies() |
---|
79 | self.assert_(replies, "Viewing discussion item forbiden for %s - member of DiscussionManager group" % u) |
---|
80 | |
---|
81 | |
---|
82 | def testViewRepliesNotPublishedNotDMUsers(self): |
---|
83 | # All common users SHOULD NOT VIEW NOT PUBLISHED comments |
---|
84 | doc = getattr(self.portal, 'doc_%s' % DM_USERS_IDS[0]) |
---|
85 | roles = [r['name'] for r in self.portal.rolesOfPermission('Moderate Discussion') if r['selected'] == 'SELECTED'] |
---|
86 | authorized_users = [user for user in COMMON_USERS_IDS if user !='anonym'] |
---|
87 | users_without_md_perm = [u for u in authorized_users if filter(lambda x: x not in roles, USERS[u]['roles'])] |
---|
88 | for u in users_without_md_perm: |
---|
89 | self.logout() |
---|
90 | if not u=='anonym': |
---|
91 | self.login(u) |
---|
92 | replies = self.discussion.getDiscussionFor(doc).getReplies() |
---|
93 | self.assert_(not replies, "Viewing of NOT published discussion item allow %s - user without 'Moderate Discussion' permission" % u) |
---|
94 | |
---|
95 | def testViewRepliesPublishedAllUsers(self): |
---|
96 | # All users MUST VIEW PUBLISHED comments |
---|
97 | # Get any document and publish it's comment |
---|
98 | doc = getattr(self.portal, 'doc_%s' % 'dm_admin') |
---|
99 | self.login('dm_admin') |
---|
100 | di = self.discussion.getDiscussionFor(doc).getReplies()[0] |
---|
101 | di.discussion_publish_comment() |
---|
102 | |
---|
103 | all_users_id = USERS.keys() + ['anonym'] |
---|
104 | for u in all_users_id: |
---|
105 | self.logout() |
---|
106 | if not u=='anonym': |
---|
107 | self.login(u) |
---|
108 | replies = self.discussion.getDiscussionFor(doc).getReplies() |
---|
109 | self.assert_(replies, "Viewing PUBLISHED discussion item forbiden for %s user" % u) |
---|
110 | |
---|
111 | ## TEST PUBLISHING |
---|
112 | |
---|
113 | def testViewPublishButtonNonDMUsers(self): |
---|
114 | # Publish button MUST BE ABSENT in document view form |
---|
115 | # Pattern for publish button presence checking |
---|
116 | if self.version.startswith("2.1"): |
---|
117 | pattern = re.compile('.*<input\\s*class="standalone"\\s*type="submit"\\s*value="Publish"\\s*/>',\ |
---|
118 | re.S|re.M) |
---|
119 | elif self.version.startswith("2.5") or self.version.startswith('3.0'): |
---|
120 | pattern = re.compile('.*<input.+?value="Publish"',\ |
---|
121 | re.S|re.M) |
---|
122 | else: |
---|
123 | pattern = re.compile('.*<input\\s*class="standalone"\\s*type="submit"\\s*value="Publish This Discussion"\\s*/>',\ |
---|
124 | re.S|re.M) |
---|
125 | |
---|
126 | roles = [r['name'] for r in self.portal.rolesOfPermission('Moderate Discussion') if r['selected'] == 'SELECTED'] |
---|
127 | authorized_users = [user for user in COMMON_USERS_IDS if user !='anonym'] |
---|
128 | users_without_md_perm = [u for u in authorized_users if filter(lambda x: x not in roles, USERS[u]['roles'])] |
---|
129 | for u in users_without_md_perm: |
---|
130 | self.logout() |
---|
131 | auth = "%s:" % u |
---|
132 | if not u=='anonym': |
---|
133 | self.login(u) |
---|
134 | auth = '%s:%s' % (u,USERS[u]['passw']) |
---|
135 | doc_id = "doc_%s" % u |
---|
136 | html = str(self.publish(self.portal.id+'/%s' % doc_id, auth)) |
---|
137 | m = pattern.match(html) |
---|
138 | self.assert_(not m, "Publish button present for %s - user without Moderate Discussion permission" % u) |
---|
139 | |
---|
140 | def testViewPublishButtonDMUsers(self): |
---|
141 | # Publish button MUST PRESENT in document view form |
---|
142 | # Pattern for publish button presence checking |
---|
143 | if self.version.startswith("2.1"): |
---|
144 | pattern = re.compile('.*<input\\s*class="standalone"\\s*type="submit"\\s*value="Publish"\\s*/>',\ |
---|
145 | re.S|re.M) |
---|
146 | elif self.version.startswith("2.5") or self.version.startswith('3.0'): |
---|
147 | pattern = re.compile('.*<input.+?value="Publish"',\ |
---|
148 | re.S|re.M) |
---|
149 | else: |
---|
150 | pattern = re.compile('.*<input\\s*class="standalone"\\s*type="submit"\\s*value="Publish"\\s*/>',\ |
---|
151 | re.S|re.M) |
---|
152 | for u in DM_USERS_IDS: |
---|
153 | self.logout() |
---|
154 | self.login(u) |
---|
155 | auth = '%s:%s' % (u,USERS[u]['passw']) |
---|
156 | doc_id = "doc_%s" % u |
---|
157 | html = str(self.publish(self.portal.id+'/%s' % doc_id, auth)) |
---|
158 | m = pattern.match(html) |
---|
159 | self.assert_(m, "Publish button NOT PRESENT for %s - member of DiscussionManager group" % u) |
---|
160 | |
---|
161 | |
---|
162 | def testPublishing(self): |
---|
163 | # Check whether perform real publishing |
---|
164 | # Pattern for publish button presence checking |
---|
165 | if self.version.startswith("2.1"): |
---|
166 | pattern = re.compile('.*<input\\s*class="standalone"\\s*type="submit"\\s*value="Publish"\\s*/>',\ |
---|
167 | re.S|re.M) |
---|
168 | elif self.version.startswith("2.5") or self.version.startswith('3.0'): |
---|
169 | pattern = re.compile('.*<input.+?value="Publish"',\ |
---|
170 | re.S|re.M) |
---|
171 | else: |
---|
172 | pattern = re.compile('.*<input\\s*class="standalone"\\s*type="submit"\\s*value="Publish This Discussion"\\s*/>',\ |
---|
173 | re.S|re.M) |
---|
174 | for u in DM_USERS_IDS: |
---|
175 | doc_id = "doc_%s" % u |
---|
176 | doc_obj = getattr(self.portal, doc_id) |
---|
177 | getReplies = self.discussion.getDiscussionFor(doc_obj).getReplies |
---|
178 | # Check whether anonymous get no reply |
---|
179 | self.logout() |
---|
180 | self.assert_(not getReplies(), "View not published reply ALLOW for Anonymous") |
---|
181 | # Login with actual (tested) user with DiscussionManager role and publish discussion |
---|
182 | self.login(u) |
---|
183 | self.assert_(getReplies(), "%s - member of DiscussionManager group NOT VIEW not published reply" % u) |
---|
184 | getReplies()[0].discussion_publish_comment() |
---|
185 | # Check if Publish button still present in document view page |
---|
186 | auth = "%s:" % u |
---|
187 | if not u=='anonym': |
---|
188 | auth = '%s:%s' % (u,USERS[u]['passw']) |
---|
189 | html = str(self.publish(self.portal.id+'/%s' % doc_id, auth)) |
---|
190 | m = pattern.match(html) |
---|
191 | self.assert_(not m, "Publish button present for %s - DiscussionManager role user after publishing" % u) |
---|
192 | # Check whether Anonym view published reply |
---|
193 | self.logout() |
---|
194 | self.assert_(getReplies(), "%s - member of DiscussionManager group NOT PUBLISH reply" % u) |
---|
195 | |
---|
196 | |
---|
197 | ## TEST DELETING |
---|
198 | |
---|
199 | def testViewDeleteButtonNonDMUsers(self): |
---|
200 | # Check Delete reply button presense ONLY for PUBLISHED reply. |
---|
201 | # Because of NOT PUBLUISHED replies is not visible at all for common users. |
---|
202 | # Delete reply button in document view form MUST BE ABSENT for all EXCEPT manager. |
---|
203 | # Publish replies |
---|
204 | self.logout() |
---|
205 | self.login('dm_admin') |
---|
206 | for u in COMMON_USERS_IDS: |
---|
207 | doc_id = "doc_%s" % u |
---|
208 | doc_obj = getattr(self.portal, doc_id) |
---|
209 | reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0] |
---|
210 | reply.discussion_publish_comment() |
---|
211 | # Prepare pattern for delete reply button presence checking |
---|
212 | if self.version.startswith("2.1"): |
---|
213 | pattern = re.compile('.*<input\\s*class="destructive"\\s*type="submit"\\s*value="Remove"\\s*/>',\ |
---|
214 | re.S|re.M) |
---|
215 | elif self.version.startswith("2.5") or self.version.startswith('3.0'): |
---|
216 | pattern = re.compile('.*<input\\s*class="destructive"\\s*type="submit"\\s*value="Remove"\\s*/>',\ |
---|
217 | re.S|re.M) |
---|
218 | else: |
---|
219 | pattern = re.compile('.*<input\\s*class="destructive"\\s*type="submit"\\s*value="Remove"\\s*/>',\ |
---|
220 | re.S|re.M) |
---|
221 | for u in COMMON_USERS_IDS: |
---|
222 | self.logout() |
---|
223 | auth = "%s:" % u |
---|
224 | if not u=='anonym': |
---|
225 | self.login(u) |
---|
226 | auth = '%s:%s' % (u,USERS[u]['passw']) |
---|
227 | doc_id = "doc_%s" % u |
---|
228 | html = str(self.publish(self.portal.id+'/%s' % doc_id, auth)) |
---|
229 | m = pattern.match(html) |
---|
230 | if not u=='anonym' and 'Manager' in USERS[u]['roles']: |
---|
231 | self.assert_(m, "%s - user with Manager role NOT VIEW Delete reply button for published reply on document view form" % u) |
---|
232 | else: |
---|
233 | self.assert_(not m, "%s - user without Manager role CAN VIEW Delete reply button for published reply on document view form" % u) |
---|
234 | |
---|
235 | def testDeleting(self): |
---|
236 | # Manager with DiscussionManager role CAN delete ANY REPLY. |
---|
237 | # Manager without DiscussionManager role [common manager] CAN delete ONLY PUBLISHED REPLY. |
---|
238 | # Get Managers |
---|
239 | managers = [u for u in USERS.keys() if 'Manager' in USERS[u]['roles']] |
---|
240 | dm_man = [u for u in managers if u.startswith('dm_')][0] |
---|
241 | common_man = [u for u in managers if not u.startswith('dm_')][0] |
---|
242 | # Publish document for common manager |
---|
243 | self.logout() |
---|
244 | self.login(dm_man) |
---|
245 | doc_obj = getattr(self.portal, "doc_%s" % common_man) |
---|
246 | reply = self.discussion.getDiscussionFor(doc_obj).getReplies()[0] |
---|
247 | reply.discussion_publish_comment() |
---|
248 | # Check for really deleting |
---|
249 | for u in managers: |
---|
250 | self.logout() |
---|
251 | self.login(u) |
---|
252 | auth = '%s:%s' % (u,USERS[u]['passw']) |
---|
253 | doc_id = "doc_%s" % u |
---|
254 | doc_obj = getattr(self.portal, doc_id) |
---|
255 | getReplies = self.discussion.getDiscussionFor(doc_obj).getReplies |
---|
256 | self.assert_(getReplies(), "%s - user with Manager role not view discussion reply" % u) |
---|
257 | getReplies()[0].deleteDiscussion() |
---|
258 | self.assert_(not getReplies(), "%s - user with Manager role not really delete discussion" % u) |
---|
259 | |
---|
260 | |
---|
261 | TESTS = [TestModeration] |
---|
262 | |
---|
263 | def test_suite(): |
---|
264 | from unittest import TestSuite, makeSuite |
---|
265 | suite = TestSuite() |
---|
266 | suite.addTest(makeSuite(TestModeration)) |
---|
267 | return suite |
---|
268 | |
---|
269 | if __name__ == '__main__': |
---|
270 | framework() |
---|
271 | |
---|