Skip to content

Commit 8b97f0e

Browse files
committed
test: use request_mock for all bound model tests
1 parent 706b133 commit 8b97f0e

File tree

14 files changed

+776
-349
lines changed

14 files changed

+776
-349
lines changed

tests/unit/certificates/test_client.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from hcloud import Client
78
from hcloud.actions import BoundAction
89
from hcloud.certificates import (
910
BoundCertificate,
@@ -15,16 +16,21 @@
1516

1617
class TestBoundCertificate:
1718
@pytest.fixture()
18-
def bound_certificate(self, hetzner_client):
19-
return BoundCertificate(client=hetzner_client.certificates, data=dict(id=14))
19+
def bound_certificate(self, client: Client):
20+
return BoundCertificate(client.certificates, data=dict(id=14))
2021

2122
@pytest.mark.parametrize("params", [{"page": 1, "per_page": 10}, {}])
2223
def test_get_actions_list(
23-
self, hetzner_client, bound_certificate, response_get_actions, params
24+
self,
25+
request_mock: mock.MagicMock,
26+
client: Client,
27+
bound_certificate,
28+
response_get_actions,
29+
params,
2430
):
25-
hetzner_client.request.return_value = response_get_actions
31+
request_mock.return_value = response_get_actions
2632
result = bound_certificate.get_actions_list(**params)
27-
hetzner_client.request.assert_called_with(
33+
request_mock.assert_called_with(
2834
url="/certificates/14/actions", method="GET", params=params
2935
)
3036

@@ -33,23 +39,29 @@ def test_get_actions_list(
3339

3440
assert len(actions) == 1
3541
assert isinstance(actions[0], BoundAction)
36-
assert actions[0]._client == hetzner_client.actions
42+
assert actions[0]._client == client.actions
3743
assert actions[0].id == 13
3844
assert actions[0].command == "change_protection"
3945

40-
def test_get_actions(self, hetzner_client, bound_certificate, response_get_actions):
41-
hetzner_client.request.return_value = response_get_actions
46+
def test_get_actions(
47+
self,
48+
request_mock: mock.MagicMock,
49+
client: Client,
50+
bound_certificate,
51+
response_get_actions,
52+
):
53+
request_mock.return_value = response_get_actions
4254
actions = bound_certificate.get_actions()
4355

4456
params = {"page": 1, "per_page": 50}
4557

46-
hetzner_client.request.assert_called_with(
58+
request_mock.assert_called_with(
4759
url="/certificates/14/actions", method="GET", params=params
4860
)
4961

5062
assert len(actions) == 1
5163
assert isinstance(actions[0], BoundAction)
52-
assert actions[0]._client == hetzner_client.actions
64+
assert actions[0]._client == client.actions
5365
assert actions[0].id == 13
5466
assert actions[0].command == "change_protection"
5567

@@ -77,32 +89,41 @@ def test_bound_certificate_init(self, certificate_response):
7789
assert bound_certificate.status.error.message == "error message"
7890

7991
def test_update(
80-
self, hetzner_client, bound_certificate, response_update_certificate
92+
self,
93+
request_mock: mock.MagicMock,
94+
bound_certificate,
95+
response_update_certificate,
8196
):
82-
hetzner_client.request.return_value = response_update_certificate
97+
request_mock.return_value = response_update_certificate
8398
certificate = bound_certificate.update(name="New name")
84-
hetzner_client.request.assert_called_with(
99+
request_mock.assert_called_with(
85100
url="/certificates/14", method="PUT", json={"name": "New name"}
86101
)
87102

88103
assert certificate.id == 2323
89104
assert certificate.name == "New name"
90105

91-
def test_delete(self, hetzner_client, bound_certificate, generic_action):
92-
hetzner_client.request.return_value = generic_action
106+
def test_delete(
107+
self,
108+
request_mock: mock.MagicMock,
109+
bound_certificate,
110+
generic_action,
111+
):
112+
request_mock.return_value = generic_action
93113
delete_success = bound_certificate.delete()
94-
hetzner_client.request.assert_called_with(
95-
url="/certificates/14", method="DELETE"
96-
)
114+
request_mock.assert_called_with(url="/certificates/14", method="DELETE")
97115

98116
assert delete_success is True
99117

100118
def test_retry_issuance(
101-
self, hetzner_client, bound_certificate, response_retry_issuance_action
119+
self,
120+
request_mock: mock.MagicMock,
121+
bound_certificate,
122+
response_retry_issuance_action,
102123
):
103-
hetzner_client.request.return_value = response_retry_issuance_action
124+
request_mock.return_value = response_retry_issuance_action
104125
action = bound_certificate.retry_issuance()
105-
hetzner_client.request.assert_called_with(
126+
request_mock.assert_called_with(
106127
url="/certificates/14/actions/retry", method="POST"
107128
)
108129

tests/unit/conftest.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# pylint: disable=redefined-outer-name
2+
13
from __future__ import annotations
24

35
from unittest import mock
@@ -7,6 +9,18 @@
79
from hcloud import Client
810

911

12+
@pytest.fixture()
13+
def request_mock() -> mock.MagicMock:
14+
return mock.MagicMock()
15+
16+
17+
@pytest.fixture()
18+
def client(request_mock) -> Client:
19+
c = Client(token="TOKEN")
20+
c.request = request_mock
21+
return c
22+
23+
1024
@pytest.fixture(autouse=True, scope="function")
1125
def mocked_requests():
1226
patcher = mock.patch("hcloud._client.requests")

tests/unit/firewalls/test_client.py

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import pytest
66

7+
from hcloud import Client
78
from hcloud.actions import BoundAction
89
from hcloud.firewalls import (
910
BoundFirewall,
@@ -18,8 +19,8 @@
1819

1920
class TestBoundFirewall:
2021
@pytest.fixture()
21-
def bound_firewall(self, hetzner_client):
22-
return BoundFirewall(client=hetzner_client.firewalls, data=dict(id=1))
22+
def bound_firewall(self, client: Client):
23+
return BoundFirewall(client.firewalls, data=dict(id=1))
2324

2425
def test_bound_firewall_init(self, firewall_response):
2526
bound_firewall = BoundFirewall(
@@ -75,11 +76,16 @@ def test_bound_firewall_init(self, firewall_response):
7576
"params", [{}, {"sort": ["created"], "page": 1, "per_page": 2}]
7677
)
7778
def test_get_actions_list(
78-
self, hetzner_client, bound_firewall, response_get_actions, params
79+
self,
80+
request_mock: mock.MagicMock,
81+
client: Client,
82+
bound_firewall,
83+
response_get_actions,
84+
params,
7985
):
80-
hetzner_client.request.return_value = response_get_actions
86+
request_mock.return_value = response_get_actions
8187
result = bound_firewall.get_actions_list(**params)
82-
hetzner_client.request.assert_called_with(
88+
request_mock.assert_called_with(
8389
url="/firewalls/1/actions", method="GET", params=params
8490
)
8591

@@ -88,35 +94,45 @@ def test_get_actions_list(
8894

8995
assert len(actions) == 1
9096
assert isinstance(actions[0], BoundAction)
91-
assert actions[0]._client == hetzner_client.actions
97+
assert actions[0]._client == client.actions
9298
assert actions[0].id == 13
9399
assert actions[0].command == "set_firewall_rules"
94100

95101
@pytest.mark.parametrize("params", [{}, {"sort": ["created"]}])
96102
def test_get_actions(
97-
self, hetzner_client, bound_firewall, response_get_actions, params
103+
self,
104+
request_mock: mock.MagicMock,
105+
client: Client,
106+
bound_firewall,
107+
response_get_actions,
108+
params,
98109
):
99-
hetzner_client.request.return_value = response_get_actions
110+
request_mock.return_value = response_get_actions
100111
actions = bound_firewall.get_actions(**params)
101112

102113
params.update({"page": 1, "per_page": 50})
103114

104-
hetzner_client.request.assert_called_with(
115+
request_mock.assert_called_with(
105116
url="/firewalls/1/actions", method="GET", params=params
106117
)
107118

108119
assert len(actions) == 1
109120
assert isinstance(actions[0], BoundAction)
110-
assert actions[0]._client == hetzner_client.actions
121+
assert actions[0]._client == client.actions
111122
assert actions[0].id == 13
112123
assert actions[0].command == "set_firewall_rules"
113124

114-
def test_update(self, hetzner_client, bound_firewall, response_update_firewall):
115-
hetzner_client.request.return_value = response_update_firewall
125+
def test_update(
126+
self,
127+
request_mock: mock.MagicMock,
128+
bound_firewall,
129+
response_update_firewall,
130+
):
131+
request_mock.return_value = response_update_firewall
116132
firewall = bound_firewall.update(
117133
name="New Corporate Intranet Protection", labels={}
118134
)
119-
hetzner_client.request.assert_called_with(
135+
request_mock.assert_called_with(
120136
url="/firewalls/1",
121137
method="PUT",
122138
json={"name": "New Corporate Intranet Protection", "labels": {}},
@@ -125,14 +141,23 @@ def test_update(self, hetzner_client, bound_firewall, response_update_firewall):
125141
assert firewall.id == 38
126142
assert firewall.name == "New Corporate Intranet Protection"
127143

128-
def test_delete(self, hetzner_client, bound_firewall):
144+
def test_delete(
145+
self,
146+
request_mock: mock.MagicMock,
147+
bound_firewall,
148+
):
129149
delete_success = bound_firewall.delete()
130-
hetzner_client.request.assert_called_with(url="/firewalls/1", method="DELETE")
150+
request_mock.assert_called_with(url="/firewalls/1", method="DELETE")
131151

132152
assert delete_success is True
133153

134-
def test_set_rules(self, hetzner_client, bound_firewall, response_set_rules):
135-
hetzner_client.request.return_value = response_set_rules
154+
def test_set_rules(
155+
self,
156+
request_mock: mock.MagicMock,
157+
bound_firewall,
158+
response_set_rules,
159+
):
160+
request_mock.return_value = response_set_rules
136161
actions = bound_firewall.set_rules(
137162
[
138163
FirewallRule(
@@ -143,7 +168,7 @@ def test_set_rules(self, hetzner_client, bound_firewall, response_set_rules):
143168
)
144169
]
145170
)
146-
hetzner_client.request.assert_called_with(
171+
request_mock.assert_called_with(
147172
url="/firewalls/1/actions/set_rules",
148173
method="POST",
149174
json={
@@ -162,13 +187,16 @@ def test_set_rules(self, hetzner_client, bound_firewall, response_set_rules):
162187
assert actions[0].progress == 100
163188

164189
def test_apply_to_resources(
165-
self, hetzner_client, bound_firewall, response_set_rules
190+
self,
191+
request_mock: mock.MagicMock,
192+
bound_firewall,
193+
response_set_rules,
166194
):
167-
hetzner_client.request.return_value = response_set_rules
195+
request_mock.return_value = response_set_rules
168196
actions = bound_firewall.apply_to_resources(
169197
[FirewallResource(type=FirewallResource.TYPE_SERVER, server=Server(id=5))]
170198
)
171-
hetzner_client.request.assert_called_with(
199+
request_mock.assert_called_with(
172200
url="/firewalls/1/actions/apply_to_resources",
173201
method="POST",
174202
json={"apply_to": [{"type": "server", "server": {"id": 5}}]},
@@ -178,13 +206,16 @@ def test_apply_to_resources(
178206
assert actions[0].progress == 100
179207

180208
def test_remove_from_resources(
181-
self, hetzner_client, bound_firewall, response_set_rules
209+
self,
210+
request_mock: mock.MagicMock,
211+
bound_firewall,
212+
response_set_rules,
182213
):
183-
hetzner_client.request.return_value = response_set_rules
214+
request_mock.return_value = response_set_rules
184215
actions = bound_firewall.remove_from_resources(
185216
[FirewallResource(type=FirewallResource.TYPE_SERVER, server=Server(id=5))]
186217
)
187-
hetzner_client.request.assert_called_with(
218+
request_mock.assert_called_with(
188219
url="/firewalls/1/actions/remove_from_resources",
189220
method="POST",
190221
json={"remove_from": [{"type": "server", "server": {"id": 5}}]},

0 commit comments

Comments
 (0)