blob: 5e9b87a47d802d510e153e4afa6ea8ab3448a787 [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 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 client config service."""
7from __future__ import print_function
8from __future__ import division
9from __future__ import absolute_import
10
11import base64
12import unittest
13
14from services import client_config_svc
15
16
17class LoadApiClientConfigsTest(unittest.TestCase):
18
19 class FakeResponse(object):
20 def __init__(self, content):
21 self.content = content
22
23 def setUp(self):
24 self.handler = client_config_svc.LoadApiClientConfigs()
25
26 def testProcessResponse_InvalidJSON(self):
27 r = self.FakeResponse('}{')
28 with self.assertRaises(ValueError):
29 self.handler._process_response(r)
30
31 def testProcessResponse_NoContent(self):
32 r = self.FakeResponse('{"wrong-key": "some-value"}')
33 with self.assertRaises(KeyError):
34 self.handler._process_response(r)
35
36 def testProcessResponse_NotB64(self):
37 # 'asd' is not a valid base64-encoded string.
38 r = self.FakeResponse('{"content": "asd"}')
39 with self.assertRaises(TypeError):
40 self.handler._process_response(r)
41
42 def testProcessResponse_NotProto(self):
43 # 'asdf' is a valid base64-encoded string.
44 r = self.FakeResponse('{"content": "asdf"}')
45 with self.assertRaises(Exception):
46 self.handler._process_response(r)
47
48 def testProcessResponse_Success(self):
49 with open(client_config_svc.CONFIG_FILE_PATH) as f:
50 r = self.FakeResponse('{"content": "%s"}' % base64.b64encode(f.read()))
51 c = self.handler._process_response(r)
52 assert '123456789.apps.googleusercontent.com' in c
53
54
55class ClientConfigServiceTest(unittest.TestCase):
56
57 def setUp(self):
58 self.client_config_svc = client_config_svc.GetClientConfigSvc()
59 self.client_email = '123456789@developer.gserviceaccount.com'
60 self.client_id = '123456789.apps.googleusercontent.com'
61 self.allowed_origins = {'chicken.test', 'cow.test', 'goat.test'}
62
63 def testGetDisplayNames(self):
64 display_names_map = self.client_config_svc.GetDisplayNames()
65 self.assertIn(self.client_email, display_names_map)
66 self.assertEqual('johndoe@example.com',
67 display_names_map[self.client_email])
68
69 def testGetQPMDict(self):
70 qpm_map = self.client_config_svc.GetQPM()
71 self.assertIn(self.client_email, qpm_map)
72 self.assertEqual(1, qpm_map[self.client_email])
73 self.assertNotIn('bugdroid1@chromium.org', qpm_map)
74
75 def testGetClientIDEmails(self):
76 auth_client_ids, auth_emails = self.client_config_svc.GetClientIDEmails()
77 self.assertIn(self.client_id, auth_client_ids)
78 self.assertIn(self.client_email, auth_emails)
79
80 def testGetAllowedOriginsSet(self):
81 origins = self.client_config_svc.GetAllowedOriginsSet()
82 self.assertEqual(self.allowed_origins, origins)
83
84 def testForceLoad(self):
85 EXPIRES_IN = client_config_svc.ClientConfigService.EXPIRES_IN
86 NOW = 1493007338
87 # First time it will always read the config
88 self.client_config_svc.load_time = NOW
89 self.client_config_svc.GetConfigs(use_cache=True)
90 self.assertNotEqual(NOW, self.client_config_svc.load_time)
91
92 # use_cache is false and it will read the config
93 self.client_config_svc.load_time = NOW
94 self.client_config_svc.GetConfigs(
95 use_cache=False, cur_time=NOW + 1)
96 self.assertNotEqual(NOW, self.client_config_svc.load_time)
97
98 # Cache expires after some time and it will read the config
99 self.client_config_svc.load_time = NOW
100 self.client_config_svc.GetConfigs(
101 use_cache=True, cur_time=NOW + EXPIRES_IN + 1)
102 self.assertNotEqual(NOW, self.client_config_svc.load_time)
103
104 # otherwise it should just use the cache
105 self.client_config_svc.load_time = NOW
106 self.client_config_svc.GetConfigs(
107 use_cache=True, cur_time=NOW + EXPIRES_IN - 1)
108 self.assertEqual(NOW, self.client_config_svc.load_time)
109
110
111class ClientConfigServiceFunctionsTest(unittest.TestCase):
112
113 def setUp(self):
114 self.client_email = '123456789@developer.gserviceaccount.com'
115 self.allowed_origins = {'chicken.test', 'cow.test', 'goat.test'}
116
117 def testGetServiceAccountMap(self):
118 service_account_map = client_config_svc.GetServiceAccountMap()
119 self.assertIn(self.client_email, service_account_map)
120 self.assertEqual(
121 'johndoe@example.com',
122 service_account_map[self.client_email])
123 self.assertNotIn('bugdroid1@chromium.org', service_account_map)
124
125 def testGetQPMDict(self):
126 qpm_map = client_config_svc.GetQPMDict()
127 self.assertIn(self.client_email, qpm_map)
128 self.assertEqual(1, qpm_map[self.client_email])
129 self.assertNotIn('bugdroid1@chromium.org', qpm_map)
130
131 def testGetAllowedOriginsSet(self):
132 allowed_origins = client_config_svc.GetAllowedOriginsSet()
133 self.assertEqual(self.allowed_origins, allowed_origins)