Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame^] | 1 | # Copyright 2020 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. |
| 4 | """Tests for the users servicer.""" |
| 5 | from __future__ import print_function |
| 6 | from __future__ import division |
| 7 | from __future__ import absolute_import |
| 8 | |
| 9 | import unittest |
| 10 | import mock |
| 11 | |
| 12 | from google.protobuf import empty_pb2 |
| 13 | |
| 14 | from api import resource_name_converters as rnc |
| 15 | from api.v3 import users_servicer |
| 16 | from api.v3 import converters |
| 17 | from api.v3.api_proto import users_pb2 |
| 18 | from api.v3.api_proto import user_objects_pb2 |
| 19 | from framework import exceptions |
| 20 | from framework import monorailcontext |
| 21 | from framework import permissions |
| 22 | from testing import fake |
| 23 | from testing import testing_helpers |
| 24 | from services import features_svc |
| 25 | from services import user_svc |
| 26 | from services import service_manager |
| 27 | |
| 28 | |
| 29 | class UsersServicerTest(unittest.TestCase): |
| 30 | |
| 31 | def setUp(self): |
| 32 | self.cnxn = fake.MonorailConnection() |
| 33 | self.services = service_manager.Services( |
| 34 | user=fake.UserService(), |
| 35 | usergroup=fake.UserGroupService(), |
| 36 | project=fake.ProjectService(), |
| 37 | project_star=fake.ProjectStarService()) |
| 38 | self.users_svcr = users_servicer.UsersServicer( |
| 39 | self.services, make_rate_limiter=False) |
| 40 | |
| 41 | self.user_1 = self.services.user.TestAddUser('user_111@example.com', 111) |
| 42 | self.user_2 = self.services.user.TestAddUser('user_222@example.com', 222) |
| 43 | self.user_3 = self.services.user.TestAddUser('user_333@example.com', 333) |
| 44 | |
| 45 | self.project_1 = self.services.project.TestAddProject( |
| 46 | 'proj', project_id=789) |
| 47 | |
| 48 | self.converter = None |
| 49 | |
| 50 | def CallWrapped(self, wrapped_handler, mc, *args, **kwargs): |
| 51 | self.converter = converters.Converter(mc, self.services) |
| 52 | self.users_svcr.converter = self.converter |
| 53 | return wrapped_handler.wrapped(self.users_svcr, mc, *args, **kwargs) |
| 54 | |
| 55 | def testGetUser(self): |
| 56 | request = users_pb2.GetUserRequest(name='users/222') |
| 57 | mc = monorailcontext.MonorailContext( |
| 58 | self.services, cnxn=self.cnxn, requester=self.user_1.email) |
| 59 | mc.LookupLoggedInUserPerms(None) |
| 60 | response = self.CallWrapped(self.users_svcr.GetUser, mc, request) |
| 61 | expected_response = user_objects_pb2.User( |
| 62 | name='users/222', |
| 63 | display_name=testing_helpers.ObscuredEmail(self.user_2.email), |
| 64 | email=testing_helpers.ObscuredEmail(self.user_2.email), |
| 65 | availability_message='User never visited') |
| 66 | self.assertEqual(response, expected_response) |
| 67 | |
| 68 | def testBatchGetUsers(self): |
| 69 | request = users_pb2.BatchGetUsersRequest( |
| 70 | names=['users/222', 'users/333']) |
| 71 | mc = monorailcontext.MonorailContext( |
| 72 | self.services, cnxn=self.cnxn, requester=self.user_1.email) |
| 73 | mc.LookupLoggedInUserPerms(None) |
| 74 | response = self.CallWrapped(self.users_svcr.BatchGetUsers, mc, request) |
| 75 | expected_users = [ |
| 76 | user_objects_pb2.User( |
| 77 | name='users/222', |
| 78 | display_name=testing_helpers.ObscuredEmail(self.user_2.email), |
| 79 | email=testing_helpers.ObscuredEmail(self.user_2.email), |
| 80 | availability_message='User never visited'), |
| 81 | user_objects_pb2.User( |
| 82 | name='users/333', |
| 83 | display_name=testing_helpers.ObscuredEmail(self.user_3.email), |
| 84 | email=testing_helpers.ObscuredEmail(self.user_3.email), |
| 85 | availability_message='User never visited') |
| 86 | ] |
| 87 | self.assertEqual( |
| 88 | response, users_pb2.BatchGetUsersResponse(users=expected_users)) |
| 89 | |
| 90 | @mock.patch('api.v3.api_constants.MAX_BATCH_USERS', 2) |
| 91 | def testBatchGetUsers_TooMany(self): |
| 92 | mc = monorailcontext.MonorailContext( |
| 93 | self.services, cnxn=self.cnxn, requester=self.user_1.email) |
| 94 | request = users_pb2.BatchGetUsersRequest( |
| 95 | names=['users/222', 'users/333', 'users/444']) |
| 96 | with self.assertRaises(exceptions.InputException): |
| 97 | self.CallWrapped(self.users_svcr.BatchGetUsers, mc, request) |
| 98 | |
| 99 | def testStarProject(self): |
| 100 | request = users_pb2.StarProjectRequest(project='projects/proj') |
| 101 | mc = monorailcontext.MonorailContext( |
| 102 | self.services, cnxn=self.cnxn, requester=self.user_1.email) |
| 103 | mc.LookupLoggedInUserPerms(None) |
| 104 | response = self.CallWrapped(self.users_svcr.StarProject, mc, request) |
| 105 | expected_name = 'users/111/projectStars/proj' |
| 106 | |
| 107 | self.assertEqual(response, user_objects_pb2.ProjectStar(name=expected_name)) |
| 108 | |
| 109 | def testUnStarProject(self): |
| 110 | request = users_pb2.UnStarProjectRequest(project='projects/proj') |
| 111 | mc = monorailcontext.MonorailContext( |
| 112 | self.services, cnxn=self.cnxn, requester=self.user_1.email) |
| 113 | mc.LookupLoggedInUserPerms(None) |
| 114 | response = self.CallWrapped(self.users_svcr.UnStarProject, mc, request) |
| 115 | |
| 116 | self.assertEqual(response, empty_pb2.Empty()) |
| 117 | |
| 118 | is_starred = self.services.project_star.IsItemStarredBy(self.cnxn, 789, 111) |
| 119 | self.assertFalse(is_starred) |
| 120 | |
| 121 | def testListProjectStars(self): |
| 122 | request = users_pb2.ListProjectStarsRequest(parent='users/111') |
| 123 | mc = monorailcontext.MonorailContext( |
| 124 | self.services, cnxn=self.cnxn, requester=self.user_1.email) |
| 125 | mc.LookupLoggedInUserPerms(None) |
| 126 | |
| 127 | self.services.project_star.SetStar( |
| 128 | self.cnxn, self.project_1.project_id, self.user_1.user_id, True) |
| 129 | |
| 130 | response = self.CallWrapped(self.users_svcr.ListProjectStars, mc, request) |
| 131 | |
| 132 | expected_response = users_pb2.ListProjectStarsResponse( |
| 133 | project_stars=[ |
| 134 | user_objects_pb2.ProjectStar(name='users/111/projectStars/proj') |
| 135 | ]) |
| 136 | self.assertEqual(response, expected_response) |