-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommand_credentials_test.py
More file actions
324 lines (243 loc) · 10.4 KB
/
command_credentials_test.py
File metadata and controls
324 lines (243 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from unittest.mock import patch
import yaml
import pytest
import os
from click.testing import CliRunner
from pydantic.v1.error_wrappers import ValidationError
import proxygen_cli.cli.command_credentials as cmd_credentials
from proxygen_cli.lib.credentials import Credentials
CLIENT_ID = os.environ["PROXYGEN_CLIENT_ID"]
CLIENT_SECRET = os.environ["PROXYGEN_CLIENT_SECRET"]
def get_test_credentials(**kwargs):
base_credentials = {
"base_url": "https://mock-keycloak-url.nhs.uk",
"client_id": "mock-api-client",
"client_secret": "1a2f4g5",
"password": "mock-password",
"username": "mock-user",
}
updated_credentials = base_credentials | kwargs
text_format_credentials = [
f"{key}: {value}" for key, value in updated_credentials.items()
]
return "\n".join(text_format_credentials)
#get mock values for credentials.yaml file
def get_test_proxygen_client_credentials(**kwargs):
client_credentials = {
"base_url": "https://mock-keycloak-url.nhs.uk",
"client_id": CLIENT_ID,
"client_secret": CLIENT_SECRET,
"password": "mock-password",
"username": "mock-user",
}
updated_credentials = client_credentials | kwargs
text_format_credentials = [
f"{key}: {value}" for key, value in updated_credentials.items()
]
return "\n".join(text_format_credentials)
#set username and password must be successful
def test_set_proxygen_user_credential(patch_config, credentials_file):
mock_credentials = get_test_proxygen_client_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
runner.invoke(cmd_credentials.set, ["username", "new-username"] )
expected_credentials = "\n".join(
[
"base_url: https://mock-keycloak-url.nhs.uk",
"client_id: "+CLIENT_ID,
"client_secret: "+CLIENT_SECRET,
"password: mock-password",
"username: new-username",
]
)
# Check the file has been written to
assert credentials_file() == expected_credentials
#overwrite username and password must be successful, with client id and secret values unmodified
def test_update_proxygen_user_credentials(patch_config, credentials_file):
mock_credentials = get_test_proxygen_client_credentials()
# Convert mock_credentials to a dictionary
mock_credentials_dict = yaml.safe_load(mock_credentials)
# Set an initial value for the username and password field
initial_username = "old-username"
mock_credentials_dict["username"] = initial_username
initial_password = "old-password"
mock_credentials_dict["password"] = initial_password
with patch_config(credentials=mock_credentials):
runner = CliRunner()
runner.invoke(cmd_credentials.set, ["username", "new-username123"])
runner.invoke(cmd_credentials.set, ["password", "new-password123"])
expected_credentials = "\n".join(
[
"base_url: https://mock-keycloak-url.nhs.uk",
"client_id: "+CLIENT_ID,
"client_secret: "+CLIENT_SECRET,
"password: new-password123",
"username: new-username123",
]
)
# Check the file has been written to
assert credentials_file() == expected_credentials
#overwrite client id and secret must be successful
def test_update_proxygen_client_credentials(patch_config, credentials_file):
mock_credentials = get_test_proxygen_client_credentials()
# Convert mock_credentials to a dictionary
mock_credentials_dict = yaml.safe_load(mock_credentials)
print("HELLO")
print(mock_credentials_dict)
# Set an initial value for the username and password field
initial_clientid = "old-clientid"
mock_credentials_dict["username"] = initial_clientid
initial_clientsecret = "old-clientsecret"
mock_credentials_dict["password"] = initial_clientsecret
with patch_config(credentials=mock_credentials):
runner = CliRunner()
runner.invoke(cmd_credentials.set, ["client_id", "new-clientid123"])
runner.invoke(cmd_credentials.set, ["client_secret", "new-clientsecret123"])
expected_credentials = "\n".join(
[
"base_url: https://mock-keycloak-url.nhs.uk",
"client_id: new-clientid123",
"client_secret: new-clientsecret123",
"password: mock-password",
"username: mock-user",
]
)
# Check the file has been written to
assert credentials_file() == expected_credentials
#verify error for missing proxygen username and password
def test_missing_proxygen_user_credentials(update_config):
mock_credentials = get_test_proxygen_client_credentials(username="",password="")
update_config(credentials=mock_credentials)
with pytest.raises(ValidationError):
Credentials()
def test_missing_credentials(update_config):
mock_credentials = get_test_credentials(username="")
update_config(credentials=mock_credentials)
with pytest.raises(ValidationError):
Credentials()
def test_list_credentials(patch_config):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
result = runner.invoke(cmd_credentials.list)
assert result.output.strip() == mock_credentials
def test_get_credential(patch_config):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
result = runner.invoke(cmd_credentials.get, ["client_id"])
assert result.output.strip() == "mock-api-client"
def test_get_invalid_setting(patch_config):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
result = runner.invoke(cmd_credentials.get, ["invalid"])
assert "'invalid' is not one of 'base_url'" in result.output.strip()
def test_private_key_id_missing(patch_config):
"""
Ensure that if the private_key_path is set, then we validate key_id.
We mock out the file path validation using an extra patch as we're already
mocking part of Credentials with the patch_config mark.
"""
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials), \
patch(("proxygen_cli.lib.credentials.Credentials."
"_validate_private_key_path")) as mock_creds:
mock_creds.return_value = True
runner = CliRunner()
result = runner.invoke(
cmd_credentials.set,
["private_key_path", "not_a_valid_file"]
)
assert "Private key specified with no associated Key ID" in \
result.output.strip()
def test_set_credential(patch_config, credentials_file):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
runner.invoke(cmd_credentials.set, ["username", "new-username"])
expected_credentials = "\n".join(
[
"base_url: https://mock-keycloak-url.nhs.uk",
"client_id: mock-api-client",
"client_secret: 1a2f4g5",
"password: mock-password",
"username: new-username",
]
)
# Check the file has been written to
assert credentials_file() == expected_credentials
def test_update_credentials(patch_config, credentials_file):
mock_credentials = get_test_credentials()
# Convert mock_credentials to a dictionary
mock_credentials_dict = yaml.safe_load(mock_credentials)
# Set an initial value for the username field
initial_username = "old-username"
mock_credentials_dict["username"] = initial_username
with patch_config(credentials=mock_credentials):
runner = CliRunner()
runner.invoke(cmd_credentials.set, ["username", "new-username123"])
expected_credentials = "\n".join(
[
"base_url: https://mock-keycloak-url.nhs.uk",
"client_id: mock-api-client",
"client_secret: 1a2f4g5",
"password: mock-password",
"username: new-username123", # Updated value
]
)
# Check the file has been written to
assert credentials_file() == expected_credentials
def test_add_key_id(patch_config, credentials_file):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
runner.invoke(cmd_credentials.set, ["key_id", "test_kid"])
expected_credentials = "\n".join(
[
"base_url: https://mock-keycloak-url.nhs.uk",
"client_id: mock-api-client",
"client_secret: 1a2f4g5",
"key_id: test_kid",
"password: mock-password",
"username: mock-user",
]
)
# Check the file has been written to
assert credentials_file() == expected_credentials
def test_set_invalid_setting(patch_config):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
result = runner.invoke(cmd_credentials.set, ["invalid", "new-username"])
assert "Error: Invalid value: extra fields not permitted" in result.output.strip()
def test_set_invalid_private_key_path(patch_config):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
result = runner.invoke(
cmd_credentials.set, ["private_key_path", "invalid_private_key_path"]
)
assert ".proxygen/invalid_private_key_path does not exist" in result.output.strip()
def test_remove_setting(patch_config, credentials_file):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
runner.invoke(cmd_credentials.rm, ["private_key_path"])
expected_credentials = "\n".join(
[
"base_url: https://mock-keycloak-url.nhs.uk",
"client_id: mock-api-client",
"client_secret: 1a2f4g5",
"password: mock-password",
"username: mock-user",
]
)
# Check the file has been written to
assert credentials_file() == expected_credentials
def test_remove_invalid_setting(patch_config):
mock_credentials = get_test_credentials()
with patch_config(credentials=mock_credentials):
runner = CliRunner()
result = runner.invoke(cmd_credentials.rm, ["invalid"])
assert "'invalid' is not one of 'base_url'" in result.output.strip()