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