blob: 04ef491f2a9456e82d2a84efa770d0eea1d20dd0 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# 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.
Copybara854996b2021-09-07 19:36:02 +00004
5"""Tests for MonorailContext."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import unittest
11
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020012try:
13 from mox3 import mox
14except ImportError:
15 import mox
Copybara854996b2021-09-07 19:36:02 +000016
17from framework import authdata
18from framework import monorailcontext
19from framework import permissions
20from framework import profiler
21from framework import template_helpers
22from framework import sql
23from services import service_manager
24from testing import fake
25
26
27class 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)