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