Skip to content

Commit dbe2c32

Browse files
committed
test clients
1 parent 2e9dd68 commit dbe2c32

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

tests/conftest.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,23 @@ def users_data(faker, num_users):
9090
users.append(types.User.parse_obj(data))
9191

9292
return json_data, users
93+
94+
95+
@pytest.fixture
96+
def clients_data(num_clients):
97+
json_data = []
98+
clients = []
99+
100+
for _ in range(num_clients):
101+
data = {
102+
"object": "client",
103+
"id": f"client_{uuid.uuid4().hex}",
104+
"last_active_session_id": f"sess_{uuid.uuid4().hex}",
105+
"sign_in_attempt_id": None,
106+
"sign_up_attempt_id": None,
107+
"ended": False,
108+
}
109+
json_data.append(data)
110+
clients.append(types.Client.parse_obj(data))
111+
112+
return json_data, clients

tests/test_client.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,29 @@ async def test_users_service_handles_errors(self, client, httpserver, server_400
7272
await client.users.update("123", types.UpdateUserRequest())
7373

7474
self._check_error(e.value, server_400_error)
75+
76+
async def test_clients_service_handles_errors(self, client, httpserver, server_400_error):
77+
httpserver.expect_request("/clients/", "GET").respond_with_json(
78+
{"errors": [server_400_error.dict()]}, status=400
79+
)
80+
httpserver.expect_request("/clients/123/", "GET").respond_with_json(
81+
{"errors": [server_400_error.dict()]}, status=400
82+
)
83+
httpserver.expect_request("/clients/verify/", "POST").respond_with_json(
84+
{"errors": [server_400_error.dict()]}, status=400
85+
)
86+
87+
with pytest.raises(errors.ClerkAPIException) as e:
88+
await client.clients.list()
89+
90+
self._check_error(e.value, server_400_error)
91+
92+
with pytest.raises(errors.ClerkAPIException) as e:
93+
await client.clients.get("123")
94+
95+
self._check_error(e.value, server_400_error)
96+
97+
with pytest.raises(errors.ClerkAPIException) as e:
98+
await client.clients.verify("token")
99+
100+
self._check_error(e.value, server_400_error)

tests/test_clients.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
4+
@pytest.mark.asyncio
5+
class TestList:
6+
@pytest.mark.parametrize("num_clients", [0, 1, 2])
7+
async def test_valid_data_is_parsed_properly(self, client, httpserver, clients_data):
8+
httpserver.expect_request("/clients/", "GET").respond_with_json(clients_data[0])
9+
clients = await client.clients.list()
10+
assert clients == clients_data[1]
11+
12+
13+
@pytest.mark.asyncio
14+
class TestGet:
15+
@pytest.mark.parametrize("num_clients", [1])
16+
async def test_valid_data_is_parsed_properly(self, client, httpserver, clients_data):
17+
expected_client = clients_data[1][0]
18+
clients_json = clients_data[0][0]
19+
20+
httpserver.expect_request(f"/clients/{expected_client.id}/", "GET").respond_with_json(
21+
clients_json
22+
)
23+
client = await client.clients.get(expected_client.id)
24+
assert client == expected_client
25+
26+
27+
@pytest.mark.asyncio
28+
class TestVerify:
29+
@pytest.mark.parametrize("num_clients", [1])
30+
async def test_valid_data_is_parsed_properly(self, client, httpserver, clients_data):
31+
expected_client = clients_data[1][0]
32+
clients_json = clients_data[0][0]
33+
34+
httpserver.expect_request(
35+
f"/clients/verify/", "POST", json={"token": "123"}
36+
).respond_with_json(clients_json)
37+
client = await client.clients.verify("123")
38+
assert client == expected_client

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ commands = isort -c --diff clerk/ tests/
1818
commands = black --check clerk/ tests/
1919

2020
[testenv]
21+
env =
22+
PYTHONHASHSEED = 1
2123
deps = .
2224
commands = pytest {posargs}
2325
poetry_add_dev_dependencies = True

0 commit comments

Comments
 (0)