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