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