1# Copyright 2024 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. 4 5from __future__ import annotations 6 7import json 8 9import hjson 10from immutabledict import immutabledict 11 12from crossbench.cli.config.secrets import Secret, SecretsConfig, SecretType 13from tests.crossbench.cli.config.base import BaseConfigTestCase 14 15 16class SecretsConfigTestCase(BaseConfigTestCase): 17 18 def test_parse_empty(self): 19 secrets = SecretsConfig.parse({}) 20 self.assertEqual(secrets.secrets, immutabledict()) 21 22 def test_parse_google(self): 23 secrets = SecretsConfig.parse( 24 {"google": { 25 "password": "pw", 26 "account": "user@test.com" 27 }}) 28 self.assertEqual(secrets.secrets[SecretType.GOOGLE], 29 Secret(SecretType.GOOGLE, "user@test.com", "pw")) 30 secrets = SecretsConfig.parse( 31 {"google": { 32 "user": "user@test.com", 33 "password": "" 34 }}) 35 self.assertEqual(secrets.secrets[SecretType.GOOGLE], 36 Secret(SecretType.GOOGLE, "user@test.com", "")) 37 38 def test_equal_empty(self): 39 secrets_1 = SecretsConfig.parse({}) 40 secrets_2 = SecretsConfig.parse({}) 41 self.assertEqual(secrets_1, secrets_1) 42 self.assertEqual(secrets_1, secrets_2) 43 self.assertEqual(secrets_2, secrets_1) 44 45 def test_equal_single_item(self): 46 secrets_empty = SecretsConfig.parse({}) 47 secrets_1 = SecretsConfig.parse( 48 {"google": { 49 "password": "pw", 50 "account": "user@test.com" 51 }}) 52 secrets_2 = SecretsConfig.parse( 53 {"google": { 54 "password": "pw", 55 "account": "user@test.com" 56 }}) 57 self.assertEqual(secrets_1, secrets_1) 58 self.assertEqual(secrets_1, secrets_2) 59 self.assertEqual(secrets_2, secrets_1) 60 self.assertNotEqual(secrets_1, secrets_empty) 61 self.assertNotEqual(secrets_empty, secrets_1) 62 self.assertNotEqual(secrets_2, secrets_empty) 63 self.assertNotEqual(secrets_empty, secrets_2) 64 65 def test_not_equal_single_item(self): 66 secrets_1 = SecretsConfig.parse( 67 {"google": { 68 "password": "pw", 69 "account": "user@test.com" 70 }}) 71 secrets_2 = SecretsConfig.parse( 72 {"google": { 73 "password": "PASSWORD", 74 "account": "user@test.com" 75 }}) 76 self.assertNotEqual(secrets_1, secrets_2) 77 78 def test_parse_inline_hjson(self): 79 config_data = {"google": {"password": "pw", "account": "user@test.com"}} 80 secrets_inline_hjson = SecretsConfig.parse(hjson.dumps(config_data)) 81 secrets_inline_json = SecretsConfig.parse(json.dumps(config_data)) 82 secrets_dict = SecretsConfig.parse(config_data) 83 self.assertEqual(secrets_inline_hjson, secrets_dict) 84 self.assertEqual(secrets_inline_json, secrets_dict) 85