Debian Codegraph @

ansible_collections/community/general/tests/unit/plugins/lookup/onepassword_common.py

raw
1 # Copyright (c) 2022 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 json
8 import os
9
10 from ansible_collections.community.general.plugins.lookup.onepassword import (
11 OnePassCLIv2,
12 )
13
14
15 def load_file(file):
16 with open(os.path.join(os.path.dirname(__file__), "onepassword_fixtures", file)) as f:
17 return json.loads(f.read())
18
19
20 # Intentionally excludes metadata leaf nodes that would exist in real output if not relevant.
21 MOCK_ENTRIES = {
22 OnePassCLIv2: [
23 {
24 "vault_name": "Test Vault",
25 "queries": [
26 "ywvdbojsguzgrgnokmcxtydgdv",
27 "Authy Backup",
28 ],
29 "expected": ["OctoberPoppyNuttyDraperySabbath", "OctoberPoppyNuttyDraperySabbath"],
30 "output": load_file("v2_out_01.json"),
31 },
32 {
33 # Request a custom field where ID and label are different
34 "vault_name": "Test Vault",
35 "queries": ["Dummy Login"],
36 "kwargs": {
37 "field": "password1",
38 },
39 "expected": ["data in custom field"],
40 "output": load_file("v2_out_02.json"),
41 },
42 {
43 # Request data from a custom section
44 "vault_name": "Test Vault",
45 "queries": ["Duplicate Sections"],
46 "kwargs": {
47 "field": "s2 text",
48 "section": "Section 2",
49 },
50 "expected": ["first value"],
51 "output": load_file("v2_out_03.json"),
52 },
53 {
54 # Request data from an omitted value (label lookup, no section)
55 "vault_name": "Test Vault",
56 "queries": ["Omitted values"],
57 "kwargs": {
58 "field": "label-without-value",
59 },
60 "expected": [""],
61 "output": load_file("v2_out_04.json"),
62 },
63 {
64 # Request data from an omitted value (id lookup, no section)
65 "vault_name": "Test Vault",
66 "queries": ["Omitted values"],
67 "kwargs": {
68 "field": "67890q7mspf4x6zrlw3qejn7m",
69 },
70 "expected": [""],
71 "output": load_file("v2_out_04.json"),
72 },
73 {
74 # Request data from an omitted value (label lookup, with section)
75 "vault_name": "Test Vault",
76 "queries": ["Omitted values"],
77 "kwargs": {"field": "section-label-without-value", "section": "Section-Without-Values"},
78 "expected": [""],
79 "output": load_file("v2_out_04.json"),
80 },
81 {
82 # Request data from an omitted value (id lookup, with section)
83 "vault_name": "Test Vault",
84 "queries": ["Omitted values"],
85 "kwargs": {
86 "field": "123345q7mspf4x6zrlw3qejn7m",
87 "section": "section-without-values",
88 },
89 "expected": [""],
90 "output": load_file("v2_out_04.json"),
91 },
92 {
93 # Query item without section by lowercase id (case matching)
94 "vault_name": "Test Vault",
95 "queries": ["LabelCasing"],
96 "kwargs": {
97 "field": "lowercaseid",
98 },
99 "expected": ["lowercaseid"],
100 "output": load_file("v2_out_05.json"),
101 },
102 {
103 # Query item without section by lowercase id (case not matching)
104 "vault_name": "Test Vault",
105 "queries": ["LabelCasing"],
106 "kwargs": {
107 "field": "LOWERCASEID",
108 },
109 "expected": ["lowercaseid"],
110 "output": load_file("v2_out_05.json"),
111 },
112 {
113 # Query item without section by lowercase label (case matching)
114 "vault_name": "Test Vault",
115 "queries": ["LabelCasing"],
116 "kwargs": {
117 "field": "lowercaselabel",
118 },
119 "expected": ["lowercaselabel"],
120 "output": load_file("v2_out_05.json"),
121 },
122 {
123 # Query item without section by lowercase label (case not matching)
124 "vault_name": "Test Vault",
125 "queries": ["LabelCasing"],
126 "kwargs": {
127 "field": "LOWERCASELABEL",
128 },
129 "expected": ["lowercaselabel"],
130 "output": load_file("v2_out_05.json"),
131 },
132 {
133 # Query item without section by mixed case id (case matching)
134 "vault_name": "Test Vault",
135 "queries": ["LabelCasing"],
136 "kwargs": {
137 "field": "MiXeDcAsEiD",
138 },
139 "expected": ["mixedcaseid"],
140 "output": load_file("v2_out_05.json"),
141 },
142 {
143 # Query item without section by mixed case id (case not matching)
144 "vault_name": "Test Vault",
145 "queries": ["LabelCasing"],
146 "kwargs": {
147 "field": "mixedcaseid",
148 },
149 "expected": ["mixedcaseid"],
150 "output": load_file("v2_out_05.json"),
151 },
152 {
153 # Query item without section by mixed case label (case matching)
154 "vault_name": "Test Vault",
155 "queries": ["LabelCasing"],
156 "kwargs": {
157 "field": "MiXeDcAsElAbEl",
158 },
159 "expected": ["mixedcaselabel"],
160 "output": load_file("v2_out_05.json"),
161 },
162 {
163 # Query item without section by mixed case label (case not matching)
164 "vault_name": "Test Vault",
165 "queries": ["LabelCasing"],
166 "kwargs": {
167 "field": "mixedcaselabel",
168 },
169 "expected": ["mixedcaselabel"],
170 "output": load_file("v2_out_05.json"),
171 },
172 {
173 # Query item with section by lowercase id (case matching)
174 "vault_name": "Test Vault",
175 "queries": ["LabelCasing"],
176 "kwargs": {
177 "field": "sectionlowercaseid",
178 "section": "section-with-values",
179 },
180 "expected": ["sectionlowercaseid"],
181 "output": load_file("v2_out_05.json"),
182 },
183 {
184 # Query item with section by lowercase id (case not matching)
185 "vault_name": "Test Vault",
186 "queries": ["LabelCasing"],
187 "kwargs": {
188 "field": "SECTIONLOWERCASEID",
189 "section": "section-with-values",
190 },
191 "expected": ["sectionlowercaseid"],
192 "output": load_file("v2_out_05.json"),
193 },
194 {
195 # Query item with section by lowercase label (case matching)
196 "vault_name": "Test Vault",
197 "queries": ["LabelCasing"],
198 "kwargs": {
199 "field": "sectionlowercaselabel",
200 "section": "section-with-values",
201 },
202 "expected": ["sectionlowercaselabel"],
203 "output": load_file("v2_out_05.json"),
204 },
205 {
206 # Query item with section by lowercase label (case not matching)
207 "vault_name": "Test Vault",
208 "queries": ["LabelCasing"],
209 "kwargs": {
210 "field": "SECTIONLOWERCASELABEL",
211 "section": "section-with-values",
212 },
213 "expected": ["sectionlowercaselabel"],
214 "output": load_file("v2_out_05.json"),
215 },
216 {
217 # Query item with section by lowercase id (case matching)
218 "vault_name": "Test Vault",
219 "queries": ["LabelCasing"],
220 "kwargs": {
221 "field": "SeCtIoNmIxEdCaSeId",
222 "section": "section-with-values",
223 },
224 "expected": ["sectionmixedcaseid"],
225 "output": load_file("v2_out_05.json"),
226 },
227 {
228 # Query item with section by lowercase id (case not matching)
229 "vault_name": "Test Vault",
230 "queries": ["LabelCasing"],
231 "kwargs": {
232 "field": "sectionmixedcaseid",
233 "section": "section-with-values",
234 },
235 "expected": ["sectionmixedcaseid"],
236 "output": load_file("v2_out_05.json"),
237 },
238 {
239 # Query item with section by lowercase label (case matching)
240 "vault_name": "Test Vault",
241 "queries": ["LabelCasing"],
242 "kwargs": {
243 "field": "SeCtIoNmIxEdCaSeLaBeL",
244 "section": "section-with-values",
245 },
246 "expected": ["sectionmixedcaselabel"],
247 "output": load_file("v2_out_05.json"),
248 },
249 {
250 # Query item with section by lowercase label (case not matching)
251 "vault_name": "Test Vault",
252 "queries": ["LabelCasing"],
253 "kwargs": {
254 "field": "sectionmixedcaselabel",
255 "section": "section-with-values",
256 },
257 "expected": ["sectionmixedcaselabel"],
258 "output": load_file("v2_out_05.json"),
259 },
260 ],
261 }
262
263 SSH_KEY_MOCK_ENTRIES = [
264 # loads private key in PKCS#8 format by default
265 {
266 "vault_name": "Personal",
267 "queries": ["ssh key"],
268 "expected": ["-----BEGIN PRIVATE KEY-----\n..........=\n-----END PRIVATE KEY-----\n"],
269 "output": load_file("ssh_key_output.json"),
270 },
271 # loads private key in PKCS#8 format becasue ssh_format=false
272 {
273 "vault_name": "Personal",
274 "queries": ["ssh key"],
275 "kwargs": {
276 "ssh_format": False,
277 },
278 "expected": ["-----BEGIN PRIVATE KEY-----\n..........=\n-----END PRIVATE KEY-----\n"],
279 "output": load_file("ssh_key_output.json"),
280 },
281 # loads private key in ssh format
282 {
283 "vault_name": "Personal",
284 "queries": ["ssh key"],
285 "kwargs": {
286 "ssh_format": True,
287 },
288 "expected": ["-----BEGIN OPENSSH PRIVATE KEY-----\r\n.....\r\n-----END OPENSSH PRIVATE KEY-----\r\n"],
289 "output": load_file("ssh_key_output.json"),
290 },
291 ]