blob: a15fc75561dcdb9c6a330d0b23c459ccc4b8ce3b [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 the sitewide servicer."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import time
12import unittest
13
14import mock
Copybara854996b2021-09-07 19:36:02 +000015
16import settings
17from api import sitewide_servicer
18from api.api_proto import common_pb2
19from api.api_proto import sitewide_pb2
20from framework import monorailcontext
21from framework import xsrf
22from services import service_manager
23from testing import fake
24
25
26class SitewideServicerTest(unittest.TestCase):
27
28 def setUp(self):
29 self.cnxn = fake.MonorailConnection()
30 self.services = service_manager.Services(
31 usergroup=fake.UserGroupService(),
32 user=fake.UserService())
33 self.user_1 = self.services.user.TestAddUser('owner@example.com', 111)
34 self.sitewide_svcr = sitewide_servicer.SitewideServicer(
35 self.services, make_rate_limiter=False)
36
37 def CallWrapped(self, wrapped_handler, *args, **kwargs):
38 return wrapped_handler.wrapped(self.sitewide_svcr, *args, **kwargs)
39
40 @mock.patch('services.secrets_svc.GetXSRFKey')
41 @mock.patch('time.time')
42 def testRefreshToken(self, mockTime, mockGetXSRFKey):
43 """We can refresh an expired token."""
44 mockGetXSRFKey.side_effect = lambda: 'fakeXSRFKey'
45 # The token is at the brink of being too old
46 mockTime.side_effect = lambda: 1 + xsrf.REFRESH_TOKEN_TIMEOUT_SEC
47
48 token_path = 'token_path'
49 token = xsrf.GenerateToken(111, token_path, 1)
50
51 request = sitewide_pb2.RefreshTokenRequest(
52 token=token, token_path=token_path)
53 mc = monorailcontext.MonorailContext(
54 self.services, cnxn=self.cnxn, requester='owner@example.com')
55 response = self.CallWrapped(self.sitewide_svcr.RefreshToken, mc, request)
56
57 self.assertEqual(
58 sitewide_pb2.RefreshTokenResponse(
59 token='QSaKMyXhY752g7n8a34HyTo4NjQwMDE=',
60 token_expires_sec=870901),
61 response)
62
63 @mock.patch('services.secrets_svc.GetXSRFKey')
64 @mock.patch('time.time')
65 def testRefreshToken_InvalidToken(self, mockTime, mockGetXSRFKey):
66 """We reject attempts to refresh an invalid token."""
67 mockGetXSRFKey.side_effect = ['fakeXSRFKey']
68 mockTime.side_effect = [123]
69
70 token_path = 'token_path'
71 token = 'invalidToken'
72
73 request = sitewide_pb2.RefreshTokenRequest(
74 token=token, token_path=token_path)
75 mc = monorailcontext.MonorailContext(
76 self.services, cnxn=self.cnxn, requester='owner@example.com')
77
78 with self.assertRaises(xsrf.TokenIncorrect):
79 self.CallWrapped(self.sitewide_svcr.RefreshToken, mc, request)
80
81 @mock.patch('services.secrets_svc.GetXSRFKey')
82 @mock.patch('time.time')
83 def testRefreshToken_TokenTooOld(self, mockTime, mockGetXSRFKey):
84 """We reject attempts to refresh a token that's too old."""
85 mockGetXSRFKey.side_effect = lambda: 'fakeXSRFKey'
86 mockTime.side_effect = lambda: 2 + xsrf.REFRESH_TOKEN_TIMEOUT_SEC
87
88 token_path = 'token_path'
89 token = xsrf.GenerateToken(111, token_path, 1)
90
91 request = sitewide_pb2.RefreshTokenRequest(
92 token=token, token_path=token_path)
93 mc = monorailcontext.MonorailContext(
94 self.services, cnxn=self.cnxn, requester='owner@example.com')
95
96 with self.assertRaises(xsrf.TokenIncorrect):
97 self.CallWrapped(self.sitewide_svcr.RefreshToken, mc, request)
98
99 def testGetServerStatus_Normal(self):
100 request = sitewide_pb2.GetServerStatusRequest()
101 mc = monorailcontext.MonorailContext(
102 self.services, cnxn=self.cnxn, requester='owner@example.com')
103 response = self.CallWrapped(self.sitewide_svcr.GetServerStatus, mc, request)
104
105 self.assertEqual(
106 sitewide_pb2.GetServerStatusResponse(),
107 response)
108
109 @mock.patch('settings.banner_message', 'Message')
110 def testGetServerStatus_BannerMessage(self):
111 request = sitewide_pb2.GetServerStatusRequest()
112 mc = monorailcontext.MonorailContext(
113 self.services, cnxn=self.cnxn, requester='owner@example.com')
114 response = self.CallWrapped(self.sitewide_svcr.GetServerStatus, mc, request)
115
116 self.assertEqual(
117 sitewide_pb2.GetServerStatusResponse(banner_message='Message'),
118 response)
119
120 @mock.patch('settings.banner_time', (2019, 6, 13, 18, 30))
121 def testGetServerStatus_BannerTime(self):
122 request = sitewide_pb2.GetServerStatusRequest()
123 mc = monorailcontext.MonorailContext(
124 self.services, cnxn=self.cnxn, requester='owner@example.com')
125 response = self.CallWrapped(self.sitewide_svcr.GetServerStatus, mc, request)
126
127 self.assertEqual(
128 sitewide_pb2.GetServerStatusResponse(banner_time=1560450600),
129 response)
130
131 @mock.patch('settings.read_only', True)
132 def testGetServerStatus_ReadOnly(self):
133 request = sitewide_pb2.GetServerStatusRequest()
134 mc = monorailcontext.MonorailContext(
135 self.services, cnxn=self.cnxn, requester='owner@example.com')
136 response = self.CallWrapped(self.sitewide_svcr.GetServerStatus, mc, request)
137
138 self.assertEqual(
139 sitewide_pb2.GetServerStatusResponse(read_only=True),
140 response)