Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 1 | # Copyright 2017 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 | """Unit tests for the authdata module.""" |
| 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 | |
| 14 | from google.appengine.api import users |
| 15 | |
| 16 | from framework import authdata |
| 17 | from services import service_manager |
| 18 | from testing import fake |
| 19 | |
| 20 | |
| 21 | class AuthDataTest(unittest.TestCase): |
| 22 | |
| 23 | def setUp(self): |
| 24 | self.cnxn = fake.MonorailConnection() |
| 25 | self.services = service_manager.Services( |
| 26 | user=fake.UserService(), |
| 27 | usergroup=fake.UserGroupService()) |
| 28 | self.user_1 = self.services.user.TestAddUser('test@example.com', 111) |
| 29 | |
| 30 | def testFromRequest(self): |
| 31 | |
| 32 | class FakeUser(object): |
| 33 | email = lambda _: self.user_1.email |
| 34 | |
| 35 | with mock.patch.object(users, 'get_current_user', |
| 36 | autospec=True) as mock_get_current_user: |
| 37 | mock_get_current_user.return_value = FakeUser() |
| 38 | auth = authdata.AuthData.FromRequest(self.cnxn, self.services) |
| 39 | self.assertEqual(auth.user_id, 111) |
| 40 | |
| 41 | def testFromEmail(self): |
| 42 | auth = authdata.AuthData.FromEmail( |
| 43 | self.cnxn, self.user_1.email, self.services) |
| 44 | self.assertEqual(auth.user_id, 111) |
| 45 | self.assertEqual(auth.user_pb.email, self.user_1.email) |
| 46 | |
| 47 | def testFromuserId(self): |
| 48 | auth = authdata.AuthData.FromUserID(self.cnxn, 111, self.services) |
| 49 | self.assertEqual(auth.user_id, 111) |
| 50 | self.assertEqual(auth.user_pb.email, self.user_1.email) |
| 51 | |
| 52 | def testFromUser(self): |
| 53 | auth = authdata.AuthData.FromUser(self.cnxn, self.user_1, self.services) |
| 54 | self.assertEqual(auth.user_id, 111) |
| 55 | self.assertEqual(auth.user_pb.email, self.user_1.email) |