-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathtest_auth.py
More file actions
67 lines (58 loc) · 2.14 KB
/
test_auth.py
File metadata and controls
67 lines (58 loc) · 2.14 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
import jwt
import pytest
from dirty_equals import IsPositiveFloat, IsStr, IsUUID
from httpx import AsyncClient
from inline_snapshot import snapshot
from starlette import status
pytestmark = pytest.mark.anyio
# TODO: parametrize test with diff urls
async def test_add_user(client: AsyncClient):
payload = {
"email": "joe@grillazz.com",
"first_name": "Joe",
"last_name": "Garcia",
"password": "s1lly",
}
response = await client.post("/user/", json=payload)
assert response.status_code == status.HTTP_201_CREATED
assert response.json() == snapshot(
{
"id": IsUUID(4),
"email": "joe@grillazz.com",
"first_name": "Joe",
"last_name": "Garcia",
"access_token": IsStr(),
}
)
claimset = jwt.decode(
response.json()["access_token"], options={"verify_signature": False}
)
assert claimset["expiry"] == IsPositiveFloat()
assert claimset["platform"] == "python-httpx/0.28.1"
# TODO: parametrize test with diff urls including 404 and 401
async def test_get_token(client: AsyncClient):
# Create the user first
user_payload = {
"email": "joe@grillazz.com",
"first_name": "Joe",
"last_name": "Garcia",
"password": "s1lly",
}
create_user_response = await client.post("/user/", json=user_payload)
assert create_user_response.status_code == status.HTTP_201_CREATED
# Now request the token
token_payload = {"email": "joe@grillazz.com", "password": "s1lly"}
response = await client.post(
"/user/token",
data=token_payload,
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
assert response.status_code == status.HTTP_201_CREATED
claimset = jwt.decode(
response.json()["access_token"], options={"verify_signature": False}
)
assert claimset["email"] == token_payload["email"]
assert claimset["expiry"] == IsPositiveFloat()
assert claimset["platform"] == "python-httpx/0.28.1"
# TODO: baerer token test
# TODO: > get token > test endpoint auth with token > expire token on redis > test endpoint auth with token