Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 1 | # 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. |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 4 | |
| 5 | """Tests for the client config service.""" |
| 6 | from __future__ import print_function |
| 7 | from __future__ import division |
| 8 | from __future__ import absolute_import |
| 9 | |
| 10 | import base64 |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 11 | import binascii |
| 12 | import six |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 13 | import unittest |
| 14 | |
| 15 | from services import client_config_svc |
| 16 | |
| 17 | |
| 18 | class LoadApiClientConfigsTest(unittest.TestCase): |
| 19 | |
| 20 | class FakeResponse(object): |
| 21 | def __init__(self, content): |
| 22 | self.content = content |
| 23 | |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 24 | def testProcessResponse_InvalidContent(self): |
| 25 | r = self.FakeResponse('') |
| 26 | with self.assertRaises(AttributeError): |
| 27 | client_config_svc._process_response(r) |
| 28 | |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 29 | def testProcessResponse_InvalidJSON(self): |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 30 | r = self.FakeResponse(b')]}\'}{') |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 31 | with self.assertRaises(ValueError): |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 32 | client_config_svc._process_response(r) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 33 | |
| 34 | def testProcessResponse_NoContent(self): |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 35 | r = self.FakeResponse(b')]}\'{"wrong-key": "some-value"}') |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 36 | with self.assertRaises(KeyError): |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 37 | client_config_svc._process_response(r) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 38 | |
| 39 | def testProcessResponse_NotB64(self): |
| 40 | # 'asd' is not a valid base64-encoded string. |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 41 | r = self.FakeResponse(b')]}\'{"rawContent": "asd"}') |
| 42 | with self.assertRaises(binascii.Error): |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 43 | client_config_svc._process_response(r) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 44 | |
| 45 | def testProcessResponse_NotProto(self): |
| 46 | # 'asdf' is a valid base64-encoded string. |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 47 | r = self.FakeResponse(b')]}\'{"rawContent": "asdf"}') |
| 48 | with self.assertRaises(UnicodeDecodeError): |
Adrià Vilanova Martínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 49 | client_config_svc._process_response(r) |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 50 | |
| 51 | def testProcessResponse_Success(self): |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 52 | 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ínez | 9f9ade5 | 2022-10-10 23:20:11 +0200 | [diff] [blame] | 55 | c = client_config_svc._process_response(r) |
Adrià Vilanova Martínez | f19ea43 | 2024-01-23 20:20:52 +0100 | [diff] [blame] | 56 | assert b'123456789.apps.googleusercontent.com' in c |
Copybara | 854996b | 2021-09-07 19:36:02 +0000 | [diff] [blame] | 57 | |
| 58 | |
| 59 | class 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 | |
| 115 | class 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) |