Skip to content

Commit f20fc33

Browse files
committed
Simplify mocking with default values mocker
1 parent 3d2a8de commit f20fc33

File tree

2 files changed

+67
-46
lines changed

2 files changed

+67
-46
lines changed

tests/conftest.py

+48
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Test fixtures."""
2+
23
import datetime
34

45
import pytest
@@ -78,3 +79,50 @@ def frozen_time(mocker):
7879
now.return_value = datetime.datetime(
7980
2023, 8, 13, 12, 30, 15, tzinfo=datetime.timezone.utc
8081
)
82+
83+
84+
@pytest.fixture
85+
def discourse_request(discourse_host, discourse_client, requests_mock):
86+
"""Fixture for mocking Discourse API requests.
87+
88+
The only request arguments are the method and the path.
89+
90+
Example:
91+
92+
>>> def test_something(discourse_request):
93+
>>> request = discourse_request(
94+
>>> "put", # the method, case-insensitive
95+
>>> "/the-path.json?q=4", # the absolute path with query, NO host
96+
>>> headers={'content-type': 'text/plain'}, # override default headers
97+
>>> content=b"ERROR", # override bytestring response
98+
>>> )
99+
100+
If `content` is provided, that will be used as the response body.
101+
If `json` is provided, then the body will return the given JSON-
102+
compatable Python structure (e.g. dictionary).
103+
If neither is given then the return `json` will be an empty
104+
dictionary (`{}`).
105+
106+
Returns a function for inserting sensible default values.
107+
"""
108+
109+
def inner(method, path, headers=None, json=None, content=None):
110+
full_path = f"{discourse_host}{path}"
111+
if not headers:
112+
headers = {
113+
"Content-Type": "application/json; charset=utf-8",
114+
"Api-Key": discourse_client.api_key,
115+
"Api-Username": discourse_client.api_username,
116+
}
117+
118+
kwargs = {}
119+
if content:
120+
kwargs["content"] = content
121+
elif json:
122+
kwargs["json"] = json
123+
else:
124+
kwargs["json"] = {}
125+
126+
return requests_mock.request(method, full_path, headers=headers, **kwargs)
127+
128+
return inner

tests/test_client.py

+19-46
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ def test_empty_content_http_ok(discourse_host, discourse_client, requests_mock):
2020

2121

2222
class TestUserManagement:
23-
def test_get_user(self, discourse_host, discourse_client, requests_mock):
24-
request = requests_mock.get(
25-
f"{discourse_host}/users/someuser.json",
26-
headers={"Content-Type": "application/json; charset=utf-8"},
27-
json={"user": "someuser"},
23+
def test_get_user(self, discourse_host, discourse_client, discourse_request):
24+
request = discourse_request(
25+
"get", "/users/someuser.json", json={"user": "someuser"}
2826
)
2927
discourse_client.user("someuser")
28+
3029
assert request.called_once
3130

3231
def test_create_user(self, discourse_host, discourse_client, requests_mock):
@@ -47,52 +46,34 @@ def test_create_user(self, discourse_host, discourse_client, requests_mock):
4746
assert session_request.called_once
4847
assert user_request.called_once
4948

50-
def test_update_email(self, discourse_host, discourse_client, requests_mock):
51-
request = requests_mock.put(
52-
f"{discourse_host}/users/someuser/preferences/email",
53-
headers={"Content-Type": "application/json; charset=utf-8"},
54-
json={},
55-
)
49+
def test_update_email(self, discourse_host, discourse_client, discourse_request):
50+
request = discourse_request("put", "/users/someuser/preferences/email")
5651
discourse_client.update_email("someuser", "[email protected]")
5752

5853
assert request.called_once
5954

60-
def test_update_user(self, discourse_client, requests_mock):
61-
request = requests_mock.put(
62-
f"{discourse_client.host}/users/someuser",
63-
headers={"Content-Type": "application/json; charset=utf-8"},
64-
json={},
65-
)
55+
def test_update_user(self, discourse_client, discourse_request):
56+
request = discourse_request("put", "/users/someuser")
6657
discourse_client.update_user("someuser", a="a", b="b")
6758

6859
assert request.called_once
6960

70-
def test_update_username(self, discourse_client, requests_mock):
71-
request = requests_mock.put(
72-
f"{discourse_client.host}/users/someuser/preferences/username",
73-
headers={"Content-Type": "application/json; charset=utf-8"},
74-
json={},
75-
)
61+
def test_update_username(self, discourse_client, discourse_request):
62+
request = discourse_request("put", "/users/someuser/preferences/username")
7663
discourse_client.update_username("someuser", "newname")
7764

7865
assert request.called_once
7966

80-
def test_by_external_id(self, discourse_client, requests_mock):
81-
request = requests_mock.get(
82-
f"{discourse_client.host}/users/by-external/123",
83-
headers={"Content-Type": "application/json; charset=utf-8"},
84-
json={"user": "123"},
67+
def test_by_external_id(self, discourse_client, discourse_request):
68+
request = discourse_request(
69+
"get", "/users/by-external/123", json={"user": "123"}
8570
)
8671
discourse_client.by_external_id(123)
8772

8873
assert request.called_once
8974

90-
def test_suspend_user(self, discourse_client, requests_mock, frozen_time):
91-
request = requests_mock.put(
92-
f"{discourse_client.host}/admin/users/123/suspend",
93-
headers={"Content-Type": "application/json; charset=utf-8"},
94-
json={},
95-
)
75+
def test_suspend_user(self, discourse_client, discourse_request, frozen_time):
76+
request = discourse_request("put", "/admin/users/123/suspend")
9677
discourse_client.suspend(123, 1, "Testing")
9778

9879
assert request.called_once
@@ -103,22 +84,14 @@ def test_suspend_user(self, discourse_client, requests_mock, frozen_time):
10384
assert request_payload["reason"] == ["Testing"]
10485
assert request_payload["suspend_until"] == ["2023-08-14T12:30:15+00:00"]
10586

106-
def test_unsuspend_user(self, discourse_client, requests_mock):
107-
request = requests_mock.put(
108-
f"{discourse_client.host}/admin/users/123/unsuspend",
109-
headers={"Content-Type": "application/json; charset=utf-8"},
110-
json={},
111-
)
87+
def test_unsuspend_user(self, discourse_client, discourse_request):
88+
request = discourse_request("put", "/admin/users/123/unsuspend")
11289
discourse_client.unsuspend(123)
11390

11491
assert request.called_once
11592

116-
def test_user_bagdes(self, discourse_client, requests_mock):
117-
request = requests_mock.get(
118-
f"{discourse_client.host}/user-badges/myusername.json",
119-
headers={"Content-Type": "application/json; charset=utf-8"},
120-
json={},
121-
)
93+
def test_user_bagdes(self, discourse_client, discourse_request):
94+
request = discourse_request("get", "/user-badges/myusername.json")
12295
discourse_client.user_badges("myusername")
12396

12497
assert request.called_once

0 commit comments

Comments
 (0)