Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/module_utils/test__scaleway.py

raw
1 # Copyright (c) Ansible project
2 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
3 # SPDX-License-Identifier: GPL-3.0-or-later
4
5 from __future__ import annotations
6
7 import unittest
8
9 from ansible_collections.community.general.plugins.module_utils._scaleway import SecretVariables, argon2
10
11
12 class SecretVariablesTestCase(unittest.TestCase):
13 def test_dict_to_list(self):
14 source = dict(attribute1="value1", attribute2="value2")
15 expect = [dict(key="attribute1", value="value1"), dict(key="attribute2", value="value2")]
16
17 result = SecretVariables.dict_to_list(source)
18 result = sorted(result, key=lambda el: el["key"])
19 self.assertEqual(result, expect)
20
21 def test_list_to_dict(self):
22 source = [
23 dict(
24 key="secret1",
25 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$NuZk+6UATHNFV78nFRXFvA$3kivcXfzNHI1c/4ZBpP8BeBSGhhI82NfOh4Dd48JJgc",
26 ),
27 dict(
28 key="secret2",
29 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$etGO/Z8ImYDeKr6uFsyPAQ$FbL5+hG/duDEpa8UCYqXpEUQ5EacKg6i2iAs+Dq4dAI",
30 ),
31 ]
32 expect = dict(
33 secret1="$argon2id$v=19$m=65536,t=1,p=2$NuZk+6UATHNFV78nFRXFvA$3kivcXfzNHI1c/4ZBpP8BeBSGhhI82NfOh4Dd48JJgc",
34 secret2="$argon2id$v=19$m=65536,t=1,p=2$etGO/Z8ImYDeKr6uFsyPAQ$FbL5+hG/duDEpa8UCYqXpEUQ5EacKg6i2iAs+Dq4dAI",
35 )
36
37 self.assertEqual(SecretVariables.list_to_dict(source, hashed=True), expect)
38
39 def test_list_to_dict_2(self):
40 source = [dict(key="secret1", value="value1"), dict(key="secret2", value="value2")]
41 expect = dict(secret1="value1", secret2="value2")
42
43 self.assertEqual(SecretVariables.list_to_dict(source, hashed=False), expect)
44
45 @unittest.skipIf(argon2 is None, "Missing required 'argon2' library")
46 def test_decode_full(self):
47 source_secret = [
48 dict(
49 key="secret1",
50 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$NuZk+6UATHNFV78nFRXFvA$3kivcXfzNHI1c/4ZBpP8BeBSGhhI82NfOh4Dd48JJgc",
51 ),
52 dict(
53 key="secret2",
54 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$etGO/Z8ImYDeKr6uFsyPAQ$FbL5+hG/duDEpa8UCYqXpEUQ5EacKg6i2iAs+Dq4dAI",
55 ),
56 ]
57 source_value = [
58 dict(key="secret1", value="value1"),
59 dict(key="secret2", value="value2"),
60 ]
61
62 expect = [
63 dict(key="secret1", value="value1"),
64 dict(key="secret2", value="value2"),
65 ]
66
67 result = SecretVariables.decode(source_secret, source_value)
68 result = sorted(result, key=lambda el: el["key"])
69 self.assertEqual(result, expect)
70
71 @unittest.skipIf(argon2 is None, "Missing required 'argon2' library")
72 def test_decode_dict_divergent_values(self):
73 source_secret = [
74 dict(
75 key="secret1",
76 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$NuZk+6UATHNFV78nFRXFvA$3kivcXfzNHI1c/4ZBpP8BeBSGhhI82NfOh4Dd48JJgc",
77 ),
78 dict(
79 key="secret2",
80 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$etGO/Z8ImYDeKr6uFsyPAQ$FbL5+hG/duDEpa8UCYqXpEUQ5EacKg6i2iAs+Dq4dAI",
81 ),
82 ]
83 source_value = [
84 dict(key="secret1", value="value1"),
85 dict(key="secret2", value="diverged_value2"),
86 ]
87
88 expect = [
89 dict(key="secret1", value="value1"),
90 dict(
91 key="secret2",
92 value="$argon2id$v=19$m=65536,t=1,p=2$etGO/Z8ImYDeKr6uFsyPAQ$FbL5+hG/duDEpa8UCYqXpEUQ5EacKg6i2iAs+Dq4dAI",
93 ),
94 ]
95
96 result = SecretVariables.decode(source_secret, source_value)
97 result = sorted(result, key=lambda el: el["key"])
98 self.assertEqual(result, expect)
99
100 @unittest.skipIf(argon2 is None, "Missing required 'argon2' library")
101 def test_decode_dict_missing_values_left(self):
102 source_secret = [
103 dict(
104 key="secret1",
105 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$NuZk+6UATHNFV78nFRXFvA$3kivcXfzNHI1c/4ZBpP8BeBSGhhI82NfOh4Dd48JJgc",
106 ),
107 ]
108 source_value = [
109 dict(key="secret1", value="value1"),
110 dict(key="secret2", value="value2"),
111 ]
112
113 expect = [
114 dict(key="secret1", value="value1"),
115 ]
116
117 result = SecretVariables.decode(source_secret, source_value)
118 result = sorted(result, key=lambda el: el["key"])
119 self.assertEqual(result, expect)
120
121 @unittest.skipIf(argon2 is None, "Missing required 'argon2' library")
122 def test_decode_dict_missing_values_right(self):
123 source_secret = [
124 dict(
125 key="secret1",
126 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$NuZk+6UATHNFV78nFRXFvA$3kivcXfzNHI1c/4ZBpP8BeBSGhhI82NfOh4Dd48JJgc",
127 ),
128 dict(
129 key="secret2",
130 hashed_value="$argon2id$v=19$m=65536,t=1,p=2$etGO/Z8ImYDeKr6uFsyPAQ$FbL5+hG/duDEpa8UCYqXpEUQ5EacKg6i2iAs+Dq4dAI",
131 ),
132 ]
133 source_value = [
134 dict(key="secret1", value="value1"),
135 ]
136
137 expect = [
138 dict(key="secret1", value="value1"),
139 dict(
140 key="secret2",
141 value="$argon2id$v=19$m=65536,t=1,p=2$etGO/Z8ImYDeKr6uFsyPAQ$FbL5+hG/duDEpa8UCYqXpEUQ5EacKg6i2iAs+Dq4dAI",
142 ),
143 ]
144
145 result = SecretVariables.decode(source_secret, source_value)
146 result = sorted(result, key=lambda el: el["key"])
147 self.assertEqual(result, expect)