blob: 3ee79252970ae71807597c444280f4fe527318c9 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Unittest for Hotlist People servlet."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020011try:
12 from mox3 import mox
13except ImportError:
14 import mox
Copybara854996b2021-09-07 19:36:02 +000015import unittest
16import logging
17
18import ezt
19
20from testing import fake
21from features import hotlistpeople
22from framework import permissions
23from services import service_manager
24from testing import testing_helpers
25
26class HotlistPeopleListTest(unittest.TestCase):
27
28 def setUp(self):
29 self.services = service_manager.Services(
30 features=fake.FeaturesService(),
31 project=fake.ProjectService(),
32 user=fake.UserService(),
33 usergroup=fake.UserGroupService())
34 self.owner_user = self.services.user.TestAddUser('buzbuz@gmail.com', 111)
35 self.editor_user = self.services.user.TestAddUser('monica@gmail.com', 222)
36 self.non_member_user = self.services.user.TestAddUser(
37 'who-dis@gmail.com', 333)
38 self.private_hotlist = self.services.features.TestAddHotlist(
39 'PrivateHotlist', 'owner only', [111], [222], is_private=True)
40 self.public_hotlist = self.services.features.TestAddHotlist(
41 'PublicHotlist', 'everyone', [111], [222], is_private=False)
42 self.servlet = hotlistpeople.HotlistPeopleList(
43 'req', 'res', services=self.services)
44 self.mox = mox.Mox()
45
46 def tearDown(self):
47 self.mox.UnsetStubs()
48 self.mox.ResetAll()
49
50 def testAssertBasePermission(self):
51 # owner can view people in private hotlist
52 mr = testing_helpers.MakeMonorailRequest(
53 hotlist=self.private_hotlist, perms=permissions.EMPTY_PERMISSIONSET)
54 mr.auth.effective_ids = {111, 444}
55 self.servlet.AssertBasePermission(mr)
56
57 # editor can view people in private hotlist
58 mr.auth.effective_ids = {222, 333}
59 self.servlet.AssertBasePermission(mr)
60
61 # non-members cannot view people in private hotlist
62 mr.auth.effective_ids = {444, 333}
63 self.assertRaises(permissions.PermissionException,
64 self.servlet.AssertBasePermission, mr)
65
66 # owner can view people in public hotlist
67 mr = testing_helpers.MakeMonorailRequest(hotlist=self.public_hotlist)
68 mr.auth.effective_ids = {111, 444}
69 self.servlet.AssertBasePermission(mr)
70
71 # editor can view people in public hotlist
72 mr.auth.effective_ids = {222, 333}
73 self.servlet.AssertBasePermission(mr)
74
75 # non-members cannot view people in public hotlist
76 mr.auth.effective_ids = {444, 333}
77 self.servlet.AssertBasePermission(mr)
78
79 def testGatherPageData(self):
80 mr = testing_helpers.MakeMonorailRequest(
81 hotlist=self.public_hotlist, perms=permissions.EMPTY_PERMISSIONSET)
82 mr.auth.user_id = 111
83 mr.auth.effective_ids = {111}
84 mr.cnxn = 'fake cnxn'
85 page_data = self.servlet.GatherPageData(mr)
86 self.assertEqual(ezt.boolean(True), page_data['offer_membership_editing'])
87 self.assertEqual(ezt.boolean(False), page_data['offer_remove_self'])
88 self.assertEqual(page_data['total_num_owners'], 1)
89 self.assertEqual(page_data['newly_added_views'], [])
90 self.assertEqual(len(page_data['pagination'].visible_results), 2)
91
92 # non-owners cannot edit people list
93 mr.auth.user_id = 222
94 mr.auth.effective_ids = {222}
95 page_data = self.servlet.GatherPageData(mr)
96 self.assertEqual(ezt.boolean(False), page_data['offer_membership_editing'])
97 self.assertEqual(ezt.boolean(True), page_data['offer_remove_self'])
98
99 mr.auth.user_id = 333
100 mr.auth.effective_ids = {333}
101 page_data = self.servlet.GatherPageData(mr)
102 self.assertEqual(ezt.boolean(False), page_data['offer_membership_editing'])
103 self.assertEqual(ezt.boolean(False), page_data['offer_remove_self'])
104
105 def testProcessFormData_Permission(self):
106 """Only owner can change member of hotlist."""
107 mr = testing_helpers.MakeMonorailRequest(
108 path='/u/buzbuz@gmail.com/hotlists/PrivateHotlist/people',
109 hotlist=self.private_hotlist, perms=permissions.EMPTY_PERMISSIONSET)
110 mr.auth.effective_ids = {111, 444}
111 self.servlet.ProcessFormData(mr, {})
112
113 mr.auth.effective_ids = {222, 444}
114 self.assertRaises(permissions.PermissionException,
115 self.servlet.ProcessFormData, mr, {})
116
117 def testProcessRemoveMembers(self):
118 hotlist = self.servlet.services.features.TestAddHotlist(
119 'HotlistName', 'removing 222, monica', [111], [222])
120 mr = testing_helpers.MakeMonorailRequest(
121 path='/u/buzbuz@gmail.com/hotlists/HotlistName/people',
122 hotlist=hotlist)
123 mr.hotlist_id = hotlist.hotlist_id
124 post_data = fake.PostData(
125 remove = ['monica@gmail.com'])
126 url = self.servlet.ProcessRemoveMembers(
127 mr, post_data, '/u/111/hotlists/HotlistName')
128 self.assertTrue('/u/111/hotlists/HotlistName/people' in url)
129 self.assertEqual(hotlist.editor_ids, [])
130
131 def testProcessRemoveSelf(self):
132 hotlist = self.servlet.services.features.TestAddHotlist(
133 'HotlistName', 'self removing 222, monica', [111], [222])
134 mr = testing_helpers.MakeMonorailRequest(
135 path='/u/buzbuz@gmail.com/hotlists/HotlistName/people',
136 hotlist=hotlist)
137 mr.hotlist_id = hotlist.hotlist_id
138 mr.cnxn = 'fake cnxn'
139 # The owner cannot be removed using ProcessRemoveSelf(); this is enforced
140 # by permission in ProcessFormData, not in the function itself;
141 # nor may a random user...
142 mr.auth.user_id = 333
143 mr.auth.effective_ids = {333}
144 url = self.servlet.ProcessRemoveSelf(mr, '/u/111/hotlists/HotlistName')
145 self.assertTrue('/u/111/hotlists/HotlistName/people' in url)
146 self.assertEqual(hotlist.owner_ids, [111])
147 self.assertEqual(hotlist.editor_ids, [222])
148 # ...but an editor can.
149 mr.auth.user_id = 222
150 mr.auth.effective_ids = {222}
151 url = self.servlet.ProcessRemoveSelf(mr, '/u/111/hotlists/HotlistName')
152 self.assertTrue('/u/111/hotlists/HotlistName/people' in url)
153 self.assertEqual(hotlist.owner_ids, [111])
154 self.assertEqual(hotlist.editor_ids, [])
155
156 def testProcessAddMembers(self):
157 hotlist = self.servlet.services.features.TestAddHotlist(
158 'HotlistName', 'adding 333, who-dis', [111], [222])
159 mr = testing_helpers.MakeMonorailRequest(
160 path='/u/buzbuz@gmail.com/hotlists/HotlistName/people',
161 hotlist=hotlist)
162 mr.hotlist_id = hotlist.hotlist_id
163 post_data = fake.PostData(
164 addmembers = ['who-dis@gmail.com'],
165 role = ['editor'])
166 url = self.servlet.ProcessAddMembers(
167 mr, post_data, '/u/111/hotlists/HotlistName')
168 self.assertTrue('/u/111/hotlists/HotlistName/people' in url)
169 self.assertEqual(hotlist.editor_ids, [222, 333])
170
171 def testProcessAddMembers_OwnerToEditor(self):
172 hotlist = self.servlet.services.features.TestAddHotlist(
173 'HotlistName', 'adding owner 111, buzbuz as editor', [111], [222])
174 mr = testing_helpers.MakeMonorailRequest(
175 path='/u/buzbuz@gmail.com/hotlists/HotlistName/people',
176 hotlist=hotlist)
177 mr.hotlist_id = hotlist.hotlist_id
178 addmembers_input = 'buzbuz@gmail.com'
179 post_data = fake.PostData(
180 addmembers = [addmembers_input],
181 role = ['editor'])
182 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
183 self.servlet.PleaseCorrect(
184 mr, initial_add_members=addmembers_input, initially_expand_form=True)
185 self.mox.ReplayAll()
186 url = self.servlet.ProcessAddMembers(
187 mr, post_data, '/u/111/hotlists/HotlistName')
188 self.mox.VerifyAll()
189 self.assertEqual(
190 'Cannot have a hotlist without an owner; please leave at least one.',
191 mr.errors.addmembers)
192 self.assertIsNone(url)
193 # Verify that no changes have actually occurred.
194 self.assertEqual(hotlist.owner_ids, [111])
195 self.assertEqual(hotlist.editor_ids, [222])
196
197 def testProcessChangeOwnership(self):
198 hotlist = self.servlet.services.features.TestAddHotlist(
199 'HotlistName', 'new owner 333, who-dis', [111], [222])
200 mr = testing_helpers.MakeMonorailRequest(
201 path='/u/buzbuz@gmail.com/hotlists/HotlistName/people',
202 hotlist=hotlist)
203 mr.hotlist_id = hotlist.hotlist_id
204 post_data = fake.PostData(
205 changeowners = ['who-dis@gmail.com'],
206 becomeeditor = ['on'])
207 url = self.servlet.ProcessChangeOwnership(mr, post_data)
208 self.assertTrue('/u/333/hotlists/HotlistName/people' in url)
209 self.assertEqual(hotlist.owner_ids, [333])
210 self.assertEqual(hotlist.editor_ids, [222, 111])
211
212 def testProcessChangeOwnership_UnownedHotlist(self):
213 hotlist = self.services.features.TestAddHotlist(
214 'unowned', 'new owner 333, who-dis', [], [222])
215 mr = testing_helpers.MakeMonorailRequest(
216 path='/whatever',
217 hotlist=hotlist)
218 mr.hotlist_id = hotlist.hotlist_id
219 post_data = fake.PostData(
220 changeowners = ['who-dis@gmail.com'],
221 becomeeditor = ['on'])
222 self.servlet.ProcessChangeOwnership(mr, post_data)
223 self.assertEqual([333], mr.hotlist.owner_ids)
224
225 def testProcessChangeOwnership_BadEmail(self):
226 hotlist = self.servlet.services.features.TestAddHotlist(
227 'HotlistName', 'new owner 333, who-dis', [111], [222])
228 mr = testing_helpers.MakeMonorailRequest(
229 path='/u/buzbuz@gmail.com/hotlists/HotlistName/people',
230 hotlist=hotlist)
231 mr.hotlist_id = hotlist.hotlist_id
232 changeowners_input = 'who-dis@gmail.com, extra-email@gmail.com'
233 post_data = fake.PostData(
234 changeowners = [changeowners_input],
235 becomeeditor = ['on'])
236 self.mox.StubOutWithMock(self.servlet, 'PleaseCorrect')
237 self.servlet.PleaseCorrect(
238 mr, initial_new_owner_username=changeowners_input, open_dialog='yes')
239 self.mox.ReplayAll()
240 url = self.servlet.ProcessChangeOwnership(mr, post_data)
241 self.mox.VerifyAll()
242 self.assertEqual(
243 'Please add one valid user email.', mr.errors.transfer_ownership)
244 self.assertIsNone(url)
245
246 def testProcessChangeOwnership_DuplicateName(self):
247 # other_hotlist = self.servlet.services.features.TestAddHotlist(
248 # 'HotlistName', 'hotlist with same name', [333], [])
249 # hotlist = self.servlet.services.features.TestAddHotlist(
250 # 'HotlistName', 'new owner 333, who-dis', [111], [222])
251
252 # in the test_hotlists dict of features_service in testing/fake
253 # 'other_hotlist' is overwritten by 'hotlist'
254 # TODO(jojwang): edit the fake features_service to allow hotlists
255 # with the same name but different owners
256 pass