Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # Copyright 2018 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 MonorailContext.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import unittest |
| 11 | |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 12 | try: |
| 13 | from mox3 import mox |
| 14 | except ImportError: |
| 15 | import mox |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 16 | |
| 17 | from framework import authdata |
| 18 | from framework import monorailcontext |
| 19 | from framework import permissions |
| 20 | from framework import profiler |
| 21 | from framework import template_helpers |
| 22 | from framework import sql |
| 23 | from services import service_manager |
| 24 | from testing import fake |
| 25 | |
| 26 | |
| 27 | class MonorailContextTest(unittest.TestCase): |
| 28 | |
| 29 | def setUp(self): |
| 30 | self.mox = mox.Mox() |
| 31 | self.cnxn = fake.MonorailConnection() |
| 32 | self.services = service_manager.Services( |
| 33 | user=fake.UserService(), |
| 34 | usergroup=fake.UserGroupService(), |
| 35 | project=fake.ProjectService()) |
| 36 | self.project = self.services.project.TestAddProject( |
| 37 | 'proj', project_id=789, owner_ids=[111]) |
| 38 | self.user = self.services.user.TestAddUser('owner@example.com', 111) |
| 39 | |
| 40 | def tearDown(self): |
| 41 | self.mox.UnsetStubs() |
| 42 | self.mox.ResetAll() |
| 43 | |
| 44 | def testConstructor_PassingAuthAndPerms(self): |
| 45 | """We can easily make an mc for testing.""" |
| 46 | auth = authdata.AuthData(user_id=111, email='owner@example.com') |
| 47 | mc = monorailcontext.MonorailContext( |
| 48 | None, cnxn=self.cnxn, auth=auth, perms=permissions.USER_PERMISSIONSET) |
| 49 | self.assertEqual(self.cnxn, mc.cnxn) |
| 50 | self.assertEqual(auth, mc.auth) |
| 51 | self.assertEqual(permissions.USER_PERMISSIONSET, mc.perms) |
| 52 | self.assertTrue(isinstance(mc.profiler, profiler.Profiler)) |
| 53 | self.assertEqual([], mc.warnings) |
| 54 | self.assertTrue(isinstance(mc.errors, template_helpers.EZTError)) |
| 55 | |
| 56 | mc.CleanUp() |
| 57 | self.assertIsNone(mc.cnxn) |
| 58 | |
| 59 | def testConstructor_AsUsedInApp(self): |
| 60 | """We can make an mc like it is done in the app or a test.""" |
| 61 | self.mox.StubOutClassWithMocks(sql, 'MonorailConnection') |
| 62 | mock_cnxn = sql.MonorailConnection() |
| 63 | mock_cnxn.Close() |
| 64 | requester = 'new-user@example.com' |
| 65 | self.mox.ReplayAll() |
| 66 | |
| 67 | mc = monorailcontext.MonorailContext(self.services, requester=requester) |
| 68 | mc.LookupLoggedInUserPerms(self.project) |
| 69 | self.assertEqual(mock_cnxn, mc.cnxn) |
| 70 | self.assertEqual(requester, mc.auth.email) |
| 71 | self.assertEqual(permissions.USER_PERMISSIONSET, mc.perms) |
| 72 | self.assertTrue(isinstance(mc.profiler, profiler.Profiler)) |
| 73 | self.assertEqual([], mc.warnings) |
| 74 | self.assertTrue(isinstance(mc.errors, template_helpers.EZTError)) |
| 75 | |
| 76 | mc.CleanUp() |
| 77 | self.assertIsNone(mc.cnxn) |
| 78 | |
| 79 | # Double Cleanup or Cleanup with no cnxn is not a crash. |
| 80 | mc.CleanUp() |
| 81 | self.assertIsNone(mc.cnxn) |
| 82 | |
| 83 | def testRepr(self): |
| 84 | """We get nice debugging strings.""" |
| 85 | auth = authdata.AuthData(user_id=111, email='owner@example.com') |
| 86 | mc = monorailcontext.MonorailContext( |
| 87 | None, cnxn=self.cnxn, auth=auth, perms=permissions.USER_PERMISSIONSET) |
| 88 | repr_str = '%r' % mc |
| 89 | self.assertTrue(repr_str.startswith('MonorailContext(')) |
| 90 | self.assertIn('owner@example.com', repr_str) |
| 91 | self.assertIn('view', repr_str) |