blob: d8a305e8b56d6bd9d3933a209e801ee222097580 [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
Copybara854996b2021-09-07 19:36:02 +000023 def testProcessResponse_InvalidJSON(self):
24 r = self.FakeResponse('}{')
25 with self.assertRaises(ValueError):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020026 client_config_svc._process_response(r)
Copybara854996b2021-09-07 19:36:02 +000027
28 def testProcessResponse_NoContent(self):
29 r = self.FakeResponse('{"wrong-key": "some-value"}')
30 with self.assertRaises(KeyError):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020031 client_config_svc._process_response(r)
Copybara854996b2021-09-07 19:36:02 +000032
33 def testProcessResponse_NotB64(self):
34 # 'asd' is not a valid base64-encoded string.
35 r = self.FakeResponse('{"content": "asd"}')
36 with self.assertRaises(TypeError):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020037 client_config_svc._process_response(r)
Copybara854996b2021-09-07 19:36:02 +000038
39 def testProcessResponse_NotProto(self):
40 # 'asdf' is a valid base64-encoded string.
41 r = self.FakeResponse('{"content": "asdf"}')
42 with self.assertRaises(Exception):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020043 client_config_svc._process_response(r)
Copybara854996b2021-09-07 19:36:02 +000044
45 def testProcessResponse_Success(self):
46 with open(client_config_svc.CONFIG_FILE_PATH) as f:
47 r = self.FakeResponse('{"content": "%s"}' % base64.b64encode(f.read()))
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020048 c = client_config_svc._process_response(r)
Copybara854996b2021-09-07 19:36:02 +000049 assert '123456789.apps.googleusercontent.com' in c
50
51
52class ClientConfigServiceTest(unittest.TestCase):
53
54 def setUp(self):
55 self.client_config_svc = client_config_svc.GetClientConfigSvc()
56 self.client_email = '123456789@developer.gserviceaccount.com'
57 self.client_id = '123456789.apps.googleusercontent.com'
58 self.allowed_origins = {'chicken.test', 'cow.test', 'goat.test'}
59
60 def testGetDisplayNames(self):
61 display_names_map = self.client_config_svc.GetDisplayNames()
62 self.assertIn(self.client_email, display_names_map)
63 self.assertEqual('johndoe@example.com',
64 display_names_map[self.client_email])
65
66 def testGetQPMDict(self):
67 qpm_map = self.client_config_svc.GetQPM()
68 self.assertIn(self.client_email, qpm_map)
69 self.assertEqual(1, qpm_map[self.client_email])
70 self.assertNotIn('bugdroid1@chromium.org', qpm_map)
71
72 def testGetClientIDEmails(self):
73 auth_client_ids, auth_emails = self.client_config_svc.GetClientIDEmails()
74 self.assertIn(self.client_id, auth_client_ids)
75 self.assertIn(self.client_email, auth_emails)
76
77 def testGetAllowedOriginsSet(self):
78 origins = self.client_config_svc.GetAllowedOriginsSet()
79 self.assertEqual(self.allowed_origins, origins)
80
81 def testForceLoad(self):
82 EXPIRES_IN = client_config_svc.ClientConfigService.EXPIRES_IN
83 NOW = 1493007338
84 # First time it will always read the config
85 self.client_config_svc.load_time = NOW
86 self.client_config_svc.GetConfigs(use_cache=True)
87 self.assertNotEqual(NOW, self.client_config_svc.load_time)
88
89 # use_cache is false and it will read the config
90 self.client_config_svc.load_time = NOW
91 self.client_config_svc.GetConfigs(
92 use_cache=False, cur_time=NOW + 1)
93 self.assertNotEqual(NOW, self.client_config_svc.load_time)
94
95 # Cache expires after some time and it will read the config
96 self.client_config_svc.load_time = NOW
97 self.client_config_svc.GetConfigs(
98 use_cache=True, cur_time=NOW + EXPIRES_IN + 1)
99 self.assertNotEqual(NOW, self.client_config_svc.load_time)
100
101 # otherwise it should just use the cache
102 self.client_config_svc.load_time = NOW
103 self.client_config_svc.GetConfigs(
104 use_cache=True, cur_time=NOW + EXPIRES_IN - 1)
105 self.assertEqual(NOW, self.client_config_svc.load_time)
106
107
108class ClientConfigServiceFunctionsTest(unittest.TestCase):
109
110 def setUp(self):
111 self.client_email = '123456789@developer.gserviceaccount.com'
112 self.allowed_origins = {'chicken.test', 'cow.test', 'goat.test'}
113
114 def testGetServiceAccountMap(self):
115 service_account_map = client_config_svc.GetServiceAccountMap()
116 self.assertIn(self.client_email, service_account_map)
117 self.assertEqual(
118 'johndoe@example.com',
119 service_account_map[self.client_email])
120 self.assertNotIn('bugdroid1@chromium.org', service_account_map)
121
122 def testGetQPMDict(self):
123 qpm_map = client_config_svc.GetQPMDict()
124 self.assertIn(self.client_email, qpm_map)
125 self.assertEqual(1, qpm_map[self.client_email])
126 self.assertNotIn('bugdroid1@chromium.org', qpm_map)
127
128 def testGetAllowedOriginsSet(self):
129 allowed_origins = client_config_svc.GetAllowedOriginsSet()
130 self.assertEqual(self.allowed_origins, allowed_origins)