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 | """Tests for the user profile page.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import mock |
| 11 | import unittest |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 12 | import ezt |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 13 | import six |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 14 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 15 | from google.appengine.ext import testbed |
| 16 | import pytest |
| 17 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 18 | from framework import framework_views |
| 19 | from framework import permissions |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 20 | from mrproto import project_pb2 |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 21 | from services import service_manager |
| 22 | from sitewide import userprofile |
| 23 | from testing import fake |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 24 | |
| 25 | REGULAR_USER_ID = 111 |
| 26 | ADMIN_USER_ID = 222 |
| 27 | OTHER_USER_ID = 333 |
| 28 | STATES = { |
| 29 | 'live': project_pb2.ProjectState.LIVE, |
| 30 | 'archived': project_pb2.ProjectState.ARCHIVED, |
| 31 | } |
| 32 | |
| 33 | |
| 34 | def MakeReqInfo( |
| 35 | user_pb, user_id, viewed_user_pb, viewed_user_id, viewed_user_name, |
| 36 | perms=permissions.USER_PERMISSIONSET): |
| 37 | mr = fake.MonorailRequest(None, perms=perms) |
| 38 | mr.auth.user_pb = user_pb |
| 39 | mr.auth.user_id = user_id |
| 40 | mr.auth.effective_ids = {user_id} |
| 41 | mr.viewed_user_auth.email = viewed_user_name |
| 42 | mr.viewed_user_auth.user_pb = viewed_user_pb |
| 43 | mr.viewed_user_auth.user_id = viewed_user_id |
| 44 | mr.viewed_user_auth.effective_ids = {viewed_user_id} |
| 45 | mr.viewed_user_auth.user_view = framework_views.UserView(viewed_user_pb) |
| 46 | mr.viewed_user_name = viewed_user_name |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 47 | mr.request_path = '/' |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 48 | return mr |
| 49 | |
| 50 | |
| 51 | class UserProfileTest(unittest.TestCase): |
| 52 | |
| 53 | def setUp(self): |
| 54 | self.patcher_1 = mock.patch( |
| 55 | 'framework.framework_helpers.UserSettings.GatherUnifiedSettingsPageData') |
| 56 | self.mock_guspd = self.patcher_1.start() |
| 57 | self.mock_guspd.return_value = {'unified': None} |
| 58 | |
| 59 | services = service_manager.Services( |
| 60 | project=fake.ProjectService(), |
| 61 | user=fake.UserService(), |
| 62 | usergroup=fake.UserGroupService(), |
| 63 | project_star=fake.ProjectStarService(), |
| 64 | user_star=fake.UserStarService()) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 65 | self.servlet = userprofile.UserProfile(services=services) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 66 | |
| 67 | for user_id in ( |
| 68 | REGULAR_USER_ID, ADMIN_USER_ID, OTHER_USER_ID): |
| 69 | services.user.TestAddUser('%s@gmail.com' % user_id, user_id) |
| 70 | |
| 71 | for user in ['regular', 'other']: |
| 72 | for relation in ['owner', 'member']: |
| 73 | for state_name, state in STATES.items(): |
| 74 | services.project.TestAddProject( |
| 75 | '%s-%s-%s' % (user, relation, state_name), state=state) |
| 76 | |
| 77 | # Add projects |
| 78 | for state_name, state in STATES.items(): |
| 79 | services.project.TestAddProject( |
| 80 | 'regular-owner-%s' % state_name, state=state, |
| 81 | owner_ids=[REGULAR_USER_ID]) |
| 82 | services.project.TestAddProject( |
| 83 | 'regular-member-%s' % state_name, state=state, |
| 84 | committer_ids=[REGULAR_USER_ID]) |
| 85 | services.project.TestAddProject( |
| 86 | 'other-owner-%s' % state_name, state=state, |
| 87 | owner_ids=[OTHER_USER_ID]) |
| 88 | services.project.TestAddProject( |
| 89 | 'other-member-%s' % state_name, state=state, |
| 90 | committer_ids=[OTHER_USER_ID]) |
| 91 | |
| 92 | self.regular_user = services.user.GetUser('fake cnxn', REGULAR_USER_ID) |
| 93 | self.admin_user = services.user.GetUser('fake cnxn', ADMIN_USER_ID) |
| 94 | self.admin_user.is_site_admin = True |
| 95 | self.other_user = services.user.GetUser('fake cnxn', OTHER_USER_ID) |
| 96 | |
| 97 | self.testbed = testbed.Testbed() |
| 98 | self.testbed.activate() |
| 99 | self.testbed.init_memcache_stub() |
| 100 | self.testbed.init_datastore_v3_stub() |
| 101 | |
| 102 | def tearDown(self): |
| 103 | self.testbed.deactivate() |
| 104 | mock.patch.stopall() |
| 105 | |
| 106 | def assertProjectsAnyOrder(self, value_to_test, *expected_project_names): |
| 107 | actual_project_names = [project_view.project_name |
| 108 | for project_view in value_to_test] |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 109 | six.assertCountEqual(self, expected_project_names, actual_project_names) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 110 | |
| 111 | def testGatherPageData_RegularUserViewingOtherUserProjects(self): |
| 112 | """A user can see the other users' live projects, but not archived ones.""" |
| 113 | mr = MakeReqInfo( |
| 114 | self.regular_user, REGULAR_USER_ID, self.other_user, |
| 115 | OTHER_USER_ID, 'other@xyz.com') |
| 116 | |
| 117 | page_data = self.servlet.GatherPageData(mr) |
| 118 | |
| 119 | self.assertProjectsAnyOrder(page_data['owner_of_projects'], |
| 120 | 'other-owner-live') |
| 121 | self.assertProjectsAnyOrder(page_data['committer_of_projects'], |
| 122 | 'other-member-live') |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 123 | self.assertProjectsAnyOrder( |
| 124 | page_data['owner_of_archived_projects'], 'other-owner-archived') |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 125 | self.assertEqual('ot...@xyz.com', page_data['viewed_user_display_name']) |
| 126 | self.assertEqual(ezt.boolean(False), page_data['can_delete_user']) |
| 127 | self.mock_guspd.assert_called_once_with( |
| 128 | 111, mr.viewed_user_auth.user_view, mr.viewed_user_auth.user_pb, |
| 129 | None) |
| 130 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame^] | 131 | @pytest.mark.skip(reason='Test is flaky (https://crbug.com/monorail/12052)') |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 132 | def testGatherPageData_RegularUserViewingOwnProjects(self): |
| 133 | """A user can see all their own projects: live or archived.""" |
| 134 | mr = MakeReqInfo( |
| 135 | self.regular_user, REGULAR_USER_ID, self.regular_user, |
| 136 | REGULAR_USER_ID, 'self@xyz.com') |
| 137 | |
| 138 | page_data = self.servlet.GatherPageData(mr) |
| 139 | |
| 140 | self.assertEqual('self@xyz.com', page_data['viewed_user_display_name']) |
| 141 | self.assertEqual(ezt.boolean(False), page_data['can_delete_user']) |
| 142 | self.assertProjectsAnyOrder(page_data['owner_of_projects'], |
| 143 | 'regular-owner-live') |
| 144 | self.assertProjectsAnyOrder(page_data['committer_of_projects'], |
| 145 | 'regular-member-live') |
| 146 | self.assertProjectsAnyOrder( |
| 147 | page_data['owner_of_archived_projects'], |
| 148 | 'regular-owner-archived') |
| 149 | self.mock_guspd.assert_called_once_with( |
| 150 | 111, mr.viewed_user_auth.user_view, mr.viewed_user_auth.user_pb, |
| 151 | None) |
| 152 | |
| 153 | def testGatherPageData_RegularUserViewingStarredUsers(self): |
| 154 | """A user can see display names of other users that they starred.""" |
| 155 | mr = MakeReqInfo( |
| 156 | self.regular_user, REGULAR_USER_ID, self.regular_user, |
| 157 | REGULAR_USER_ID, 'self@xyz.com') |
| 158 | self.servlet.services.user_star.SetStar( |
| 159 | 'cnxn', OTHER_USER_ID, REGULAR_USER_ID, True) |
| 160 | |
| 161 | page_data = self.servlet.GatherPageData(mr) |
| 162 | |
| 163 | starred_users = page_data['starred_users'] |
| 164 | self.assertEqual(1, len(starred_users)) |
| 165 | self.assertEqual('333@gmail.com', starred_users[0].email) |
| 166 | self.assertEqual('["3...@gmail.com"]', page_data['starred_users_json']) |
| 167 | self.mock_guspd.assert_called_once_with( |
| 168 | 111, mr.viewed_user_auth.user_view, mr.viewed_user_auth.user_pb, |
| 169 | None) |
| 170 | |
| 171 | def testGatherPageData_AdminViewingOtherUserAddress(self): |
| 172 | """Site admins always see full email addresses of other users.""" |
| 173 | mr = MakeReqInfo( |
| 174 | self.admin_user, ADMIN_USER_ID, self.other_user, |
| 175 | OTHER_USER_ID, 'other@xyz.com', |
| 176 | perms=permissions.ADMIN_PERMISSIONSET) |
| 177 | |
| 178 | page_data = self.servlet.GatherPageData(mr) |
| 179 | |
| 180 | self.assertEqual('other@xyz.com', page_data['viewed_user_display_name']) |
| 181 | self.assertEqual(ezt.boolean(True), page_data['can_delete_user']) |
| 182 | self.mock_guspd.assert_called_once_with( |
| 183 | 222, mr.viewed_user_auth.user_view, mr.viewed_user_auth.user_pb, |
| 184 | mock.ANY) |
| 185 | |
| 186 | def testGatherPageData_RegularUserViewingOtherUserAddressUnobscured(self): |
| 187 | """Email should be revealed to others depending on obscure_email.""" |
| 188 | mr = MakeReqInfo( |
| 189 | self.regular_user, REGULAR_USER_ID, self.other_user, |
| 190 | OTHER_USER_ID, 'other@xyz.com') |
| 191 | mr.viewed_user_auth.user_view.obscure_email = False |
| 192 | |
| 193 | page_data = self.servlet.GatherPageData(mr) |
| 194 | |
| 195 | self.assertEqual('other@xyz.com', page_data['viewed_user_display_name']) |
| 196 | self.mock_guspd.assert_called_once_with( |
| 197 | 111, mr.viewed_user_auth.user_view, mr.viewed_user_auth.user_pb, |
| 198 | None) |
| 199 | |
| 200 | def testGatherPageData_RegularUserViewingOtherUserAddressObscured(self): |
| 201 | """Email should be revealed to others depending on obscure_email.""" |
| 202 | mr = MakeReqInfo( |
| 203 | self.regular_user, REGULAR_USER_ID, self.other_user, |
| 204 | OTHER_USER_ID, 'other@xyz.com') |
| 205 | mr.viewed_user_auth.user_view.obscure_email = True |
| 206 | |
| 207 | page_data = self.servlet.GatherPageData(mr) |
| 208 | |
| 209 | self.assertEqual('ot...@xyz.com', page_data['viewed_user_display_name']) |
| 210 | self.assertEqual(ezt.boolean(False), page_data['can_delete_user']) |
| 211 | self.mock_guspd.assert_called_once_with( |
| 212 | 111, mr.viewed_user_auth.user_view, mr.viewed_user_auth.user_pb, |
| 213 | None) |
| 214 | |
| 215 | def testGatherPageData_NoLinkedAccounts(self): |
| 216 | """An account with no linked accounts should not show anything linked.""" |
| 217 | mr = MakeReqInfo( |
| 218 | self.regular_user, REGULAR_USER_ID, self.other_user, |
| 219 | OTHER_USER_ID, 'other@xyz.com') |
| 220 | |
| 221 | page_data = self.servlet.GatherPageData(mr) |
| 222 | |
| 223 | self.assertIsNone(page_data['linked_parent']) |
| 224 | self.assertEqual([], page_data['linked_children']) |
| 225 | |
| 226 | def testGatherPageData_ParentAccounts(self): |
| 227 | """An account with a parent linked account should show it.""" |
| 228 | self.other_user.linked_parent_id = REGULAR_USER_ID |
| 229 | mr = MakeReqInfo( |
| 230 | self.regular_user, REGULAR_USER_ID, self.other_user, |
| 231 | OTHER_USER_ID, 'other@xyz.com') |
| 232 | |
| 233 | page_data = self.servlet.GatherPageData(mr) |
| 234 | |
| 235 | self.assertEqual('111@gmail.com', page_data['linked_parent'].email) |
| 236 | self.assertEqual([], page_data['linked_children']) |
| 237 | |
| 238 | def testGatherPageData_ChildAccounts(self): |
| 239 | """An account with a child linked account should show them.""" |
| 240 | self.other_user.linked_child_ids = [REGULAR_USER_ID, ADMIN_USER_ID] |
| 241 | mr = MakeReqInfo( |
| 242 | self.regular_user, REGULAR_USER_ID, self.other_user, |
| 243 | OTHER_USER_ID, 'other@xyz.com') |
| 244 | |
| 245 | page_data = self.servlet.GatherPageData(mr) |
| 246 | |
| 247 | self.assertEqual(None, page_data['linked_parent']) |
| 248 | self.assertEqual( |
| 249 | ['111@gmail.com', '222@gmail.com'], |
| 250 | [uv.email for uv in page_data['linked_children']]) |