Skip to content

Commit 686fce8

Browse files
committed
test: use BoundModelTestCase (1)
1 parent 9c69789 commit 686fce8

File tree

6 files changed

+136
-419
lines changed

6 files changed

+136
-419
lines changed

hcloud/certificates/client.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ def get_actions_list(
5353
Specifies how many results are returned by page
5454
:return: (List[:class:`BoundAction <hcloud.actions.client.BoundAction>`], :class:`Meta <hcloud.core.domain.Meta>`)
5555
"""
56-
return self._client.get_actions_list(self, status, sort, page, per_page)
56+
return self._client.get_actions_list(
57+
self,
58+
status=status,
59+
sort=sort,
60+
page=page,
61+
per_page=per_page,
62+
)
5763

5864
def get_actions(
5965
self,
@@ -68,7 +74,11 @@ def get_actions(
6874
Specify how the results are sorted. Choices: `id` `id:asc` `id:desc` `command` `command:asc` `command:desc` `status` `status:asc` `status:desc` `progress` `progress:asc` `progress:desc` `started` `started:asc` `started:desc` `finished` `finished:asc` `finished:desc`
6975
:return: List[:class:`BoundAction <hcloud.actions.client.BoundAction>`]
7076
"""
71-
return self._client.get_actions(self, status, sort)
77+
return self._client.get_actions(
78+
self,
79+
status=status,
80+
sort=sort,
81+
)
7282

7383
def update(
7484
self,
@@ -83,7 +93,11 @@ def update(
8393
User-defined labels (key-value pairs)
8494
:return: :class:`BoundCertificate <hcloud.certificates.client.BoundCertificate>`
8595
"""
86-
return self._client.update(self, name, labels)
96+
return self._client.update(
97+
self,
98+
name=name,
99+
labels=labels,
100+
)
87101

88102
def delete(self) -> bool:
89103
"""Deletes a certificate.

hcloud/firewalls/client.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def update(
140140
New Name to set
141141
:return: :class:`BoundFirewall <hcloud.firewalls.client.BoundFirewall>`
142142
"""
143-
return self._client.update(self, labels, name)
143+
return self._client.update(self, name=name, labels=labels)
144144

145145
def delete(self) -> bool:
146146
"""Deletes a Firewall.
@@ -155,7 +155,7 @@ def set_rules(self, rules: list[FirewallRule]) -> list[BoundAction]:
155155
:return: List[:class:`BoundAction <hcloud.actions.client.BoundAction>`]
156156
"""
157157

158-
return self._client.set_rules(self, rules)
158+
return self._client.set_rules(self, rules=rules)
159159

160160
def apply_to_resources(
161161
self,
@@ -165,7 +165,7 @@ def apply_to_resources(
165165
:param resources: List[:class:`FirewallResource <hcloud.firewalls.domain.FirewallResource>`]
166166
:return: List[:class:`BoundAction <hcloud.actions.client.BoundAction>`]
167167
"""
168-
return self._client.apply_to_resources(self, resources)
168+
return self._client.apply_to_resources(self, resources=resources)
169169

170170
def remove_from_resources(
171171
self,
@@ -175,7 +175,7 @@ def remove_from_resources(
175175
:param resources: List[:class:`FirewallResource <hcloud.firewalls.domain.FirewallResource>`]
176176
:return: List[:class:`BoundAction <hcloud.actions.client.BoundAction>`]
177177
"""
178-
return self._client.remove_from_resources(self, resources)
178+
return self._client.remove_from_resources(self, resources=resources)
179179

180180

181181
class FirewallsPageResult(NamedTuple):

tests/unit/certificates/conftest.py

Lines changed: 28 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,36 @@
44

55

66
@pytest.fixture()
7-
def certificate_response():
7+
def certificate1():
88
return {
9-
"certificate": {
10-
"id": 2323,
11-
"name": "My Certificate",
12-
"type": "managed",
13-
"labels": {},
14-
"certificate": "-----BEGIN CERTIFICATE-----\n...",
15-
"created": "2019-01-08T12:10:00+00:00",
16-
"not_valid_before": "2019-01-08T10:00:00+00:00",
17-
"not_valid_after": "2019-07-08T09:59:59+00:00",
18-
"domain_names": ["example.com", "webmail.example.com", "www.example.com"],
19-
"fingerprint": "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f",
20-
"status": {
21-
"issuance": "failed",
22-
"renewal": "scheduled",
23-
"error": {"code": "error_code", "message": "error message"},
24-
},
25-
"used_by": [{"id": 42, "type": "server"}],
26-
}
9+
"id": 2323,
10+
"name": "My Certificate",
11+
"type": "managed",
12+
"labels": {},
13+
"certificate": "-----BEGIN CERTIFICATE-----\n...",
14+
"created": "2019-01-08T12:10:00+00:00",
15+
"not_valid_before": "2019-01-08T10:00:00+00:00",
16+
"not_valid_after": "2019-07-08T09:59:59+00:00",
17+
"domain_names": ["example.com", "webmail.example.com", "www.example.com"],
18+
"fingerprint": "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f",
19+
"status": {
20+
"issuance": "failed",
21+
"renewal": "scheduled",
22+
"error": {"code": "error_code", "message": "error message"},
23+
},
24+
"used_by": [{"id": 42, "type": "server"}],
2725
}
2826

2927

3028
@pytest.fixture()
31-
def create_managed_certificate_response():
29+
def certificate_response(certificate1):
30+
return {"certificate": certificate1}
31+
32+
33+
@pytest.fixture()
34+
def create_managed_certificate_response(certificate1):
3235
return {
33-
"certificate": {
34-
"id": 2323,
35-
"name": "My Certificate",
36-
"type": "managed",
37-
"labels": {},
38-
"certificate": "-----BEGIN CERTIFICATE-----\n...",
39-
"created": "2019-01-08T12:10:00+00:00",
40-
"not_valid_before": "2019-01-08T10:00:00+00:00",
41-
"not_valid_after": "2019-07-08T09:59:59+00:00",
42-
"domain_names": ["example.com", "webmail.example.com", "www.example.com"],
43-
"fingerprint": "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f",
44-
"status": {"issuance": "pending", "renewal": "scheduled", "error": None},
45-
"used_by": [{"id": 42, "type": "load_balancer"}],
46-
},
36+
"certificate": certificate1,
4737
"action": {
4838
"id": 14,
4939
"command": "issue_certificate",
@@ -58,27 +48,10 @@ def create_managed_certificate_response():
5848

5949

6050
@pytest.fixture()
61-
def two_certificates_response():
51+
def two_certificates_response(certificate1):
6252
return {
6353
"certificates": [
64-
{
65-
"id": 2323,
66-
"name": "My Certificate",
67-
"labels": {},
68-
"type": "uploaded",
69-
"certificate": "-----BEGIN CERTIFICATE-----\n...",
70-
"created": "2019-01-08T12:10:00+00:00",
71-
"not_valid_before": "2019-01-08T10:00:00+00:00",
72-
"not_valid_after": "2019-07-08T09:59:59+00:00",
73-
"domain_names": [
74-
"example.com",
75-
"webmail.example.com",
76-
"www.example.com",
77-
],
78-
"fingerprint": "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f",
79-
"status": None,
80-
"used_by": [{"id": 42, "type": "load_balancer"}],
81-
},
54+
certificate1,
8255
{
8356
"id": 2324,
8457
"name": "My website cert",
@@ -102,29 +75,8 @@ def two_certificates_response():
10275

10376

10477
@pytest.fixture()
105-
def one_certificates_response():
106-
return {
107-
"certificates": [
108-
{
109-
"id": 2323,
110-
"name": "My Certificate",
111-
"labels": {},
112-
"type": "uploaded",
113-
"certificate": "-----BEGIN CERTIFICATE-----\n...",
114-
"created": "2019-01-08T12:10:00+00:00",
115-
"not_valid_before": "2019-01-08T10:00:00+00:00",
116-
"not_valid_after": "2019-07-08T09:59:59+00:00",
117-
"domain_names": [
118-
"example.com",
119-
"webmail.example.com",
120-
"www.example.com",
121-
],
122-
"fingerprint": "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f",
123-
"status": None,
124-
"used_by": [{"id": 42, "type": "load_balancer"}],
125-
}
126-
]
127-
}
78+
def one_certificates_response(certificate1):
79+
return {"certificates": [certificate1]}
12880

12981

13082
@pytest.fixture()

tests/unit/certificates/test_client.py

Lines changed: 30 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -12,88 +12,40 @@
1212
ManagedCertificateStatus,
1313
)
1414

15+
from ..conftest import BoundModelTestCase
1516

16-
class TestBoundCertificate:
17-
@pytest.fixture()
18-
def bound_certificate(self, client: Client):
19-
return BoundCertificate(client.certificates, data=dict(id=14))
20-
21-
def test_bound_certificate_init(self, certificate_response):
22-
bound_certificate = BoundCertificate(
23-
client=mock.MagicMock(), data=certificate_response["certificate"]
24-
)
2517

26-
assert bound_certificate.id == 2323
27-
assert bound_certificate.name == "My Certificate"
28-
assert bound_certificate.type == "managed"
29-
assert (
30-
bound_certificate.fingerprint
31-
== "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f"
32-
)
33-
assert bound_certificate.certificate == "-----BEGIN CERTIFICATE-----\n..."
34-
assert len(bound_certificate.domain_names) == 3
35-
assert bound_certificate.domain_names[0] == "example.com"
36-
assert bound_certificate.domain_names[1] == "webmail.example.com"
37-
assert bound_certificate.domain_names[2] == "www.example.com"
38-
assert isinstance(bound_certificate.status, ManagedCertificateStatus)
39-
assert bound_certificate.status.issuance == "failed"
40-
assert bound_certificate.status.renewal == "scheduled"
41-
assert bound_certificate.status.error.code == "error_code"
42-
assert bound_certificate.status.error.message == "error message"
18+
class TestBoundCertificate(BoundModelTestCase):
19+
methods = [
20+
BoundCertificate.update,
21+
BoundCertificate.delete,
22+
BoundCertificate.retry_issuance,
23+
]
4324

44-
def test_update(
45-
self,
46-
request_mock: mock.MagicMock,
47-
bound_certificate,
48-
response_update_certificate,
49-
):
50-
request_mock.return_value = response_update_certificate
51-
52-
certificate = bound_certificate.update(name="New name")
53-
54-
request_mock.assert_called_with(
55-
method="PUT",
56-
url="/certificates/14",
57-
json={"name": "New name"},
58-
)
59-
60-
assert certificate.id == 2323
61-
assert certificate.name == "New name"
62-
63-
def test_delete(
64-
self,
65-
request_mock: mock.MagicMock,
66-
bound_certificate,
67-
action_response,
68-
):
69-
request_mock.return_value = action_response
70-
71-
delete_success = bound_certificate.delete()
72-
73-
request_mock.assert_called_with(
74-
method="DELETE",
75-
url="/certificates/14",
76-
)
77-
78-
assert delete_success is True
79-
80-
def test_retry_issuance(
81-
self,
82-
request_mock: mock.MagicMock,
83-
bound_certificate,
84-
response_retry_issuance_action,
85-
):
86-
request_mock.return_value = response_retry_issuance_action
87-
88-
action = bound_certificate.retry_issuance()
89-
90-
request_mock.assert_called_with(
91-
method="POST",
92-
url="/certificates/14/actions/retry",
93-
)
25+
@pytest.fixture()
26+
def resource_client(self, client: Client):
27+
return client.certificates
9428

95-
assert action.id == 14
96-
assert action.command == "issue_certificate"
29+
@pytest.fixture()
30+
def bound_model(self, resource_client, certificate1):
31+
return BoundCertificate(resource_client, data=certificate1)
32+
33+
def test_init(self, bound_model: BoundCertificate):
34+
o = bound_model
35+
assert o.id == 2323
36+
assert o.name == "My Certificate"
37+
assert o.type == "managed"
38+
assert o.fingerprint == "03:c7:55:9b:2a:d1:04:17:09:f6:d0:7f:18:34:63:d4:3e:5f"
39+
assert o.certificate == "-----BEGIN CERTIFICATE-----\n..."
40+
assert len(o.domain_names) == 3
41+
assert o.domain_names[0] == "example.com"
42+
assert o.domain_names[1] == "webmail.example.com"
43+
assert o.domain_names[2] == "www.example.com"
44+
assert isinstance(o.status, ManagedCertificateStatus)
45+
assert o.status.issuance == "failed"
46+
assert o.status.renewal == "scheduled"
47+
assert o.status.error.code == "error_code"
48+
assert o.status.error.message == "error message"
9749

9850

9951
class TestCertificatesClient:

0 commit comments

Comments
 (0)