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