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