-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathtest_client.py
More file actions
465 lines (379 loc) · 13.7 KB
/
test_client.py
File metadata and controls
465 lines (379 loc) · 13.7 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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
from __future__ import annotations
from unittest import mock
import pytest
from hcloud import Client
from hcloud.actions import BoundAction
from hcloud.certificates import (
BoundCertificate,
Certificate,
CertificatesClient,
ManagedCertificateStatus,
)
class TestBoundCertificate:
@pytest.fixture()
def bound_certificate(self, client: Client):
return BoundCertificate(client.certificates, data=dict(id=14))
@pytest.mark.parametrize("params", [{"page": 1, "per_page": 10}, {}])
def test_get_actions_list(
self,
request_mock: mock.MagicMock,
client: Client,
bound_certificate,
response_get_actions,
params,
):
request_mock.return_value = response_get_actions
result = bound_certificate.get_actions_list(**params)
request_mock.assert_called_with(
method="GET",
url="/certificates/14/actions",
params=params,
)
actions = result.actions
assert result.meta is not None
assert len(actions) == 1
assert isinstance(actions[0], BoundAction)
assert actions[0]._client == client.actions
assert actions[0].id == 13
assert actions[0].command == "change_protection"
def test_get_actions(
self,
request_mock: mock.MagicMock,
client: Client,
bound_certificate,
response_get_actions,
):
request_mock.return_value = response_get_actions
actions = bound_certificate.get_actions()
params = {"page": 1, "per_page": 50}
request_mock.assert_called_with(
method="GET",
url="/certificates/14/actions",
params=params,
)
assert len(actions) == 1
assert isinstance(actions[0], BoundAction)
assert actions[0]._client == client.actions
assert actions[0].id == 13
assert actions[0].command == "change_protection"
def test_bound_certificate_init(self, certificate_response):
bound_certificate = BoundCertificate(
client=mock.MagicMock(), data=certificate_response["certificate"]
)
assert bound_certificate.id == 2323
assert bound_certificate.name == "My Certificate"
assert bound_certificate.type == "managed"
assert (
bound_certificate.fingerprint
== "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f"
)
assert bound_certificate.certificate == "-----BEGIN CERTIFICATE-----\n..."
assert len(bound_certificate.domain_names) == 3
assert bound_certificate.domain_names[0] == "example.com"
assert bound_certificate.domain_names[1] == "webmail.example.com"
assert bound_certificate.domain_names[2] == "www.example.com"
assert isinstance(bound_certificate.status, ManagedCertificateStatus)
assert bound_certificate.status.issuance == "failed"
assert bound_certificate.status.renewal == "scheduled"
assert bound_certificate.status.error.code == "error_code"
assert bound_certificate.status.error.message == "error message"
def test_update(
self,
request_mock: mock.MagicMock,
bound_certificate,
response_update_certificate,
):
request_mock.return_value = response_update_certificate
certificate = bound_certificate.update(name="New name")
request_mock.assert_called_with(
method="PUT",
url="/certificates/14",
json={"name": "New name"},
)
assert certificate.id == 2323
assert certificate.name == "New name"
def test_delete(
self,
request_mock: mock.MagicMock,
bound_certificate,
generic_action,
):
request_mock.return_value = generic_action
delete_success = bound_certificate.delete()
request_mock.assert_called_with(
method="DELETE",
url="/certificates/14",
)
assert delete_success is True
def test_retry_issuance(
self,
request_mock: mock.MagicMock,
bound_certificate,
response_retry_issuance_action,
):
request_mock.return_value = response_retry_issuance_action
action = bound_certificate.retry_issuance()
request_mock.assert_called_with(
method="POST",
url="/certificates/14/actions/retry",
)
assert action.id == 14
assert action.command == "issue_certificate"
class TestCertificatesClient:
@pytest.fixture()
def certificates_client(self, client: Client):
return CertificatesClient(client)
def test_get_by_id(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
certificate_response,
):
request_mock.return_value = certificate_response
certificate = certificates_client.get_by_id(1)
request_mock.assert_called_with(
method="GET",
url="/certificates/1",
)
assert certificate._client is certificates_client
assert certificate.id == 2323
assert certificate.name == "My Certificate"
@pytest.mark.parametrize(
"params",
[
{
"name": "My Certificate",
"label_selector": "k==v",
"page": 1,
"per_page": 10,
},
{"name": ""},
{},
],
)
def test_get_list(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
two_certificates_response,
params,
):
request_mock.return_value = two_certificates_response
result = certificates_client.get_list(**params)
request_mock.assert_called_with(
method="GET",
url="/certificates",
params=params,
)
certificates = result.certificates
assert len(certificates) == 2
certificates1 = certificates[0]
certificates2 = certificates[1]
assert certificates1._client is certificates_client
assert certificates1.id == 2323
assert certificates1.name == "My Certificate"
assert certificates2._client is certificates_client
assert certificates2.id == 2324
assert certificates2.name == "My website cert"
@pytest.mark.parametrize(
"params", [{"name": "My Certificate", "label_selector": "label1"}, {}]
)
def test_get_all(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
two_certificates_response,
params,
):
request_mock.return_value = two_certificates_response
certificates = certificates_client.get_all(**params)
params.update({"page": 1, "per_page": 50})
request_mock.assert_called_with(
method="GET",
url="/certificates",
params=params,
)
assert len(certificates) == 2
certificates1 = certificates[0]
certificates2 = certificates[1]
assert certificates1._client is certificates_client
assert certificates1.id == 2323
assert certificates1.name == "My Certificate"
assert certificates2._client is certificates_client
assert certificates2.id == 2324
assert certificates2.name == "My website cert"
def test_get_by_name(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
one_certificates_response,
):
request_mock.return_value = one_certificates_response
certificates = certificates_client.get_by_name("My Certificate")
params = {"name": "My Certificate"}
request_mock.assert_called_with(
method="GET",
url="/certificates",
params=params,
)
assert certificates._client is certificates_client
assert certificates.id == 2323
assert certificates.name == "My Certificate"
def test_create(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
certificate_response,
):
request_mock.return_value = certificate_response
certificate = certificates_client.create(
name="My Certificate",
certificate="-----BEGIN CERTIFICATE-----\n...",
private_key="-----BEGIN PRIVATE KEY-----\n...",
)
request_mock.assert_called_with(
method="POST",
url="/certificates",
json={
"name": "My Certificate",
"certificate": "-----BEGIN CERTIFICATE-----\n...",
"private_key": "-----BEGIN PRIVATE KEY-----\n...",
"type": "uploaded",
},
)
assert certificate.id == 2323
assert certificate.name == "My Certificate"
def test_create_managed(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
create_managed_certificate_response,
):
request_mock.return_value = create_managed_certificate_response
create_managed_certificate_rsp = certificates_client.create_managed(
name="My Certificate", domain_names=["example.com", "*.example.org"]
)
request_mock.assert_called_with(
method="POST",
url="/certificates",
json={
"name": "My Certificate",
"domain_names": ["example.com", "*.example.org"],
"type": "managed",
},
)
assert create_managed_certificate_rsp.certificate.id == 2323
assert create_managed_certificate_rsp.certificate.name == "My Certificate"
assert create_managed_certificate_rsp.action.id == 14
assert create_managed_certificate_rsp.action.command == "issue_certificate"
@pytest.mark.parametrize(
"certificate",
[Certificate(id=1), BoundCertificate(mock.MagicMock(), dict(id=1))],
)
def test_update(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
certificate,
response_update_certificate,
):
request_mock.return_value = response_update_certificate
certificate = certificates_client.update(certificate, name="New name")
request_mock.assert_called_with(
method="PUT",
url="/certificates/1",
json={"name": "New name"},
)
assert certificate.id == 2323
assert certificate.name == "New name"
@pytest.mark.parametrize(
"certificate",
[Certificate(id=1), BoundCertificate(mock.MagicMock(), dict(id=1))],
)
def test_delete(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
certificate,
generic_action,
):
request_mock.return_value = generic_action
delete_success = certificates_client.delete(certificate)
request_mock.assert_called_with(
method="DELETE",
url="/certificates/1",
)
assert delete_success is True
@pytest.mark.parametrize(
"certificate",
[Certificate(id=1), BoundCertificate(mock.MagicMock(), dict(id=1))],
)
def test_retry_issuance(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
certificate,
response_retry_issuance_action,
):
request_mock.return_value = response_retry_issuance_action
action = certificates_client.retry_issuance(certificate)
request_mock.assert_called_with(
method="POST",
url="/certificates/1/actions/retry",
)
assert action.id == 14
assert action.command == "issue_certificate"
def test_actions_get_by_id(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
response_get_actions,
):
request_mock.return_value = {"action": response_get_actions["actions"][0]}
action = certificates_client.actions.get_by_id(13)
request_mock.assert_called_with(
method="GET",
url="/certificates/actions/13",
)
assert isinstance(action, BoundAction)
assert action._client == certificates_client._parent.actions
assert action.id == 13
assert action.command == "change_protection"
def test_actions_get_list(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
response_get_actions,
):
request_mock.return_value = response_get_actions
result = certificates_client.actions.get_list()
request_mock.assert_called_with(
method="GET",
url="/certificates/actions",
params={},
)
actions = result.actions
assert result.meta is not None
assert len(actions) == 1
assert isinstance(actions[0], BoundAction)
assert actions[0]._client == certificates_client._parent.actions
assert actions[0].id == 13
assert actions[0].command == "change_protection"
def test_actions_get_all(
self,
request_mock: mock.MagicMock,
certificates_client: CertificatesClient,
response_get_actions,
):
request_mock.return_value = response_get_actions
actions = certificates_client.actions.get_all()
request_mock.assert_called_with(
method="GET",
url="/certificates/actions",
params={"page": 1, "per_page": 50},
)
assert len(actions) == 1
assert isinstance(actions[0], BoundAction)
assert actions[0]._client == certificates_client._parent.actions
assert actions[0].id == 13
assert actions[0].command == "change_protection"