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