blob: fbcd2f9e08e8e933158072939e5e71373cc93aad [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001# Copyright 2016 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 client config service."""
6from __future__ import print_function
7from __future__ import division
8from __future__ import absolute_import
9
10import base64
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010011import binascii
12import six
Copybara854996b2021-09-07 19:36:02 +000013import unittest
14
15from services import client_config_svc
16
17
18class LoadApiClientConfigsTest(unittest.TestCase):
19
20 class FakeResponse(object):
21 def __init__(self, content):
22 self.content = content
23
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010024 def testProcessResponse_InvalidContent(self):
25 r = self.FakeResponse('')
26 with self.assertRaises(AttributeError):
27 client_config_svc._process_response(r)
28
Copybara854996b2021-09-07 19:36:02 +000029 def testProcessResponse_InvalidJSON(self):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010030 r = self.FakeResponse(b')]}\'}{')
Copybara854996b2021-09-07 19:36:02 +000031 with self.assertRaises(ValueError):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020032 client_config_svc._process_response(r)
Copybara854996b2021-09-07 19:36:02 +000033
34 def testProcessResponse_NoContent(self):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010035 r = self.FakeResponse(b')]}\'{"wrong-key": "some-value"}')
Copybara854996b2021-09-07 19:36:02 +000036 with self.assertRaises(KeyError):
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_NotB64(self):
40 # 'asd' is not a valid base64-encoded string.
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010041 r = self.FakeResponse(b')]}\'{"rawContent": "asd"}')
42 with self.assertRaises(binascii.Error):
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_NotProto(self):
46 # 'asdf' is a valid base64-encoded string.
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010047 r = self.FakeResponse(b')]}\'{"rawContent": "asdf"}')
48 with self.assertRaises(UnicodeDecodeError):
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020049 client_config_svc._process_response(r)
Copybara854996b2021-09-07 19:36:02 +000050
51 def testProcessResponse_Success(self):
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010052 with open(client_config_svc.CONFIG_FILE_PATH, 'rb') as f:
53 r = self.FakeResponse(
54 b')]}\'{"rawContent": "%s"}' % base64.b64encode(f.read()))
Adrià Vilanova Martínez9f9ade52022-10-10 23:20:11 +020055 c = client_config_svc._process_response(r)
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +010056 assert b'123456789.apps.googleusercontent.com' in c
Copybara854996b2021-09-07 19:36:02 +000057
58
59class ClientConfigServiceTest(unittest.TestCase):
60
61 def setUp(self):
62 self.client_config_svc = client_config_svc.GetClientConfigSvc()
63 self.client_email = '123456789@developer.gserviceaccount.com'
64 self.client_id = '123456789.apps.googleusercontent.com'
65 self.allowed_origins = {'chicken.test', 'cow.test', 'goat.test'}
66
67 def testGetDisplayNames(self):
68 display_names_map = self.client_config_svc.GetDisplayNames()
69 self.assertIn(self.client_email, display_names_map)
70 self.assertEqual('johndoe@example.com',
71 display_names_map[self.client_email])
72
73 def testGetQPMDict(self):
74 qpm_map = self.client_config_svc.GetQPM()
75 self.assertIn(self.client_email, qpm_map)
76 self.assertEqual(1, qpm_map[self.client_email])
77 self.assertNotIn('bugdroid1@chromium.org', qpm_map)
78
79 def testGetClientIDEmails(self):
80 auth_client_ids, auth_emails = self.client_config_svc.GetClientIDEmails()
81 self.assertIn(self.client_id, auth_client_ids)
82 self.assertIn(self.client_email, auth_emails)
83
84 def testGetAllowedOriginsSet(self):
85 origins = self.client_config_svc.GetAllowedOriginsSet()
86 self.assertEqual(self.allowed_origins, origins)
87
88 def testForceLoad(self):
89 EXPIRES_IN = client_config_svc.ClientConfigService.EXPIRES_IN
90 NOW = 1493007338
91 # First time it will always read the config
92 self.client_config_svc.load_time = NOW
93 self.client_config_svc.GetConfigs(use_cache=True)
94 self.assertNotEqual(NOW, self.client_config_svc.load_time)
95
96 # use_cache is false and it will read the config
97 self.client_config_svc.load_time = NOW
98 self.client_config_svc.GetConfigs(
99 use_cache=False, cur_time=NOW + 1)
100 self.assertNotEqual(NOW, self.client_config_svc.load_time)
101
102 # Cache expires after some time and it will read the config
103 self.client_config_svc.load_time = NOW
104 self.client_config_svc.GetConfigs(
105 use_cache=True, cur_time=NOW + EXPIRES_IN + 1)
106 self.assertNotEqual(NOW, self.client_config_svc.load_time)
107
108 # otherwise it should just use the cache
109 self.client_config_svc.load_time = NOW
110 self.client_config_svc.GetConfigs(
111 use_cache=True, cur_time=NOW + EXPIRES_IN - 1)
112 self.assertEqual(NOW, self.client_config_svc.load_time)
113
114
115class ClientConfigServiceFunctionsTest(unittest.TestCase):
116
117 def setUp(self):
118 self.client_email = '123456789@developer.gserviceaccount.com'
119 self.allowed_origins = {'chicken.test', 'cow.test', 'goat.test'}
120
121 def testGetServiceAccountMap(self):
122 service_account_map = client_config_svc.GetServiceAccountMap()
123 self.assertIn(self.client_email, service_account_map)
124 self.assertEqual(
125 'johndoe@example.com',
126 service_account_map[self.client_email])
127 self.assertNotIn('bugdroid1@chromium.org', service_account_map)
128
129 def testGetQPMDict(self):
130 qpm_map = client_config_svc.GetQPMDict()
131 self.assertIn(self.client_email, qpm_map)
132 self.assertEqual(1, qpm_map[self.client_email])
133 self.assertNotIn('bugdroid1@chromium.org', qpm_map)
134
135 def testGetAllowedOriginsSet(self):
136 allowed_origins = client_config_svc.GetAllowedOriginsSet()
137 self.assertEqual(self.allowed_origins, allowed_origins)