Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 1 | # Copyright 2016 The Chromium Authors |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Unittest for People List servlet.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
| 12 | from framework import authdata |
| 13 | from framework import permissions |
| 14 | from project import peoplelist |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 15 | from mrproto import user_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | from services import service_manager |
| 17 | from testing import fake |
| 18 | from testing import testing_helpers |
| 19 | |
| 20 | |
| 21 | class PeopleListTest(unittest.TestCase): |
| 22 | """Tests for the PeopleList servlet.""" |
| 23 | |
| 24 | def setUp(self): |
| 25 | services = service_manager.Services( |
| 26 | project=fake.ProjectService(), |
| 27 | user=fake.UserService(), |
| 28 | usergroup=fake.UserGroupService()) |
| 29 | services.user.TestAddUser('jrobbins@gmail.com', 111) |
| 30 | services.user.TestAddUser('jrobbins@jrobbins.org', 222) |
| 31 | services.user.TestAddUser('jrobbins@chromium.org', 333) |
| 32 | services.user.TestAddUser('imso31337@gmail.com', 999) |
| 33 | self.project = services.project.TestAddProject('proj') |
| 34 | self.project.owner_ids.extend([111]) |
| 35 | self.project.committer_ids.extend([222]) |
| 36 | self.project.contributor_ids.extend([333]) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 37 | self.servlet = peoplelist.PeopleList(services=services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 38 | |
| 39 | def VerifyAccess(self, exception_expected): |
| 40 | mr = testing_helpers.MakeMonorailRequest( |
| 41 | path='/p/proj/people/list', |
| 42 | project=self.project, |
| 43 | perms=permissions.OWNER_ACTIVE_PERMISSIONSET) |
| 44 | self.servlet.AssertBasePermission(mr) |
| 45 | # Owner never raises PermissionException. |
| 46 | |
| 47 | mr = testing_helpers.MakeMonorailRequest( |
| 48 | path='/p/proj/people/list', |
| 49 | project=self.project, |
| 50 | perms=permissions.COMMITTER_ACTIVE_PERMISSIONSET) |
| 51 | self.servlet.AssertBasePermission(mr) |
| 52 | # Committer never raises PermissionException. |
| 53 | |
| 54 | mr = testing_helpers.MakeMonorailRequest( |
| 55 | path='/p/proj/people/list', |
| 56 | project=self.project, |
| 57 | perms=permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET) |
| 58 | if exception_expected: |
| 59 | self.assertRaises(permissions.PermissionException, |
| 60 | self.servlet.AssertBasePermission, mr) |
| 61 | else: |
| 62 | self.servlet.AssertBasePermission(mr) |
| 63 | # No PermissionException raised |
| 64 | |
| 65 | # Sign-out users |
| 66 | mr = testing_helpers.MakeMonorailRequest( |
| 67 | path='/p/proj/people/detail?u=555', |
| 68 | project=self.project, |
| 69 | perms=permissions.READ_ONLY_PERMISSIONSET) |
| 70 | if exception_expected: |
| 71 | self.assertRaises(permissions.PermissionException, |
| 72 | self.servlet.AssertBasePermission, mr) |
| 73 | else: |
| 74 | self.servlet.AssertBasePermission(mr) |
| 75 | |
| 76 | # Non-membr users |
| 77 | mr = testing_helpers.MakeMonorailRequest( |
| 78 | path='/p/proj/people/detail?u=555', |
| 79 | project=self.project, |
| 80 | perms=permissions.USER_PERMISSIONSET) |
| 81 | if exception_expected: |
| 82 | self.assertRaises(permissions.PermissionException, |
| 83 | self.servlet.AssertBasePermission, mr) |
| 84 | else: |
| 85 | self.servlet.AssertBasePermission(mr) |
| 86 | |
| 87 | def testAssertBasePermission_Normal(self): |
| 88 | self.VerifyAccess(False) |
| 89 | |
| 90 | def testAssertBasePermission_HideMembers(self): |
| 91 | self.project.only_owners_see_contributors = True |
| 92 | self.VerifyAccess(True) |
| 93 | |
| 94 | def testGatherPageData(self): |
| 95 | mr = testing_helpers.MakeMonorailRequest( |
| 96 | path='/p/proj/people/list', |
| 97 | project=self.project, |
| 98 | perms=permissions.OWNER_ACTIVE_PERMISSIONSET) |
| 99 | mr.auth = authdata.AuthData() |
| 100 | page_data = self.servlet.GatherPageData(mr) |
| 101 | |
| 102 | self.assertEqual(1, page_data['total_num_owners']) |
| 103 | # TODO(jrobbins): fill in tests for all other aspects. |
| 104 | |
| 105 | def testProcessFormData_Permission(self): |
| 106 | """Only owners could add/remove members.""" |
| 107 | mr = testing_helpers.MakeMonorailRequest( |
| 108 | path='/p/proj/people/list', |
| 109 | project=self.project, |
| 110 | perms=permissions.CONTRIBUTOR_ACTIVE_PERMISSIONSET) |
| 111 | self.assertRaises(permissions.PermissionException, |
| 112 | self.servlet.ProcessFormData, mr, {}) |
| 113 | |
| 114 | mr = testing_helpers.MakeMonorailRequest( |
| 115 | path='/p/proj/people/list', |
| 116 | project=self.project, |
| 117 | perms=permissions.OWNER_ACTIVE_PERMISSIONSET) |
| 118 | self.servlet.ProcessFormData(mr, {}) |
| 119 | |
| 120 | def testGatherHelpData_Anon(self): |
| 121 | mr = testing_helpers.MakeMonorailRequest( |
| 122 | path='/p/proj/people/list', |
| 123 | project=self.project) |
| 124 | help_data = self.servlet.GatherHelpData(mr, {}) |
| 125 | self.assertEqual( |
| 126 | {'account_cue': None, 'cue': None}, |
| 127 | help_data) |
| 128 | |
| 129 | def testGatherHelpData_Nonmember(self): |
| 130 | mr = testing_helpers.MakeMonorailRequest( |
| 131 | path='/p/proj/people/list', |
| 132 | project=self.project) |
| 133 | mr.auth.user_id = 999 |
| 134 | mr.auth.effective_ids = {999} |
| 135 | help_data = self.servlet.GatherHelpData(mr, {}) |
| 136 | self.assertEqual( |
| 137 | {'account_cue': None, 'cue': 'how_to_join_project'}, |
| 138 | help_data) |
| 139 | |
| 140 | self.servlet.services.user.SetUserPrefs( |
| 141 | 'cnxn', 999, |
| 142 | [user_pb2.UserPrefValue(name='how_to_join_project', value='true')]) |
| 143 | help_data = self.servlet.GatherHelpData(mr, {}) |
| 144 | self.assertEqual( |
| 145 | {'account_cue': None, 'cue': None}, |
| 146 | help_data) |
| 147 | |
| 148 | def testGatherHelpData_Member(self): |
| 149 | mr = testing_helpers.MakeMonorailRequest( |
| 150 | path='/p/proj/people/list', |
| 151 | project=self.project) |
| 152 | mr.auth.user_id = 111 |
| 153 | mr.auth.effective_ids = {111} |
| 154 | help_data = self.servlet.GatherHelpData(mr, {}) |
| 155 | self.assertEqual( |
| 156 | {'account_cue': None, 'cue': None}, |
| 157 | help_data) |