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