mirror of
https://github.com/grillazz/fastapi-sqlalchemy-asyncpg.git
synced 2026-01-17 11:40:39 +03:00
refactor: improve test_get_token by creating user before token request
This commit is contained in:
@@ -37,17 +37,28 @@ async def test_add_user(client: AsyncClient):
|
|||||||
|
|
||||||
# TODO: parametrize test with diff urls including 404 and 401
|
# TODO: parametrize test with diff urls including 404 and 401
|
||||||
async def test_get_token(client: AsyncClient):
|
async def test_get_token(client: AsyncClient):
|
||||||
payload = {"email": "joe@grillazz.com", "password": "s1lly"}
|
# First, create the user required for this test
|
||||||
|
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 for the newly created user
|
||||||
|
token_payload = {"email": "joe@grillazz.com", "password": "s1lly"}
|
||||||
response = await client.post(
|
response = await client.post(
|
||||||
"/user/token",
|
"/user/token",
|
||||||
data=payload,
|
data=token_payload,
|
||||||
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
||||||
)
|
)
|
||||||
assert response.status_code == status.HTTP_201_CREATED
|
assert response.status_code == status.HTTP_201_CREATED
|
||||||
claimset = jwt.decode(
|
claimset = jwt.decode(
|
||||||
response.json()["access_token"], options={"verify_signature": False}
|
response.json()["access_token"], options={"verify_signature": False}
|
||||||
)
|
)
|
||||||
assert claimset["email"] == payload["email"]
|
assert claimset["email"] == token_payload["email"]
|
||||||
assert claimset["expiry"] == IsPositiveFloat()
|
assert claimset["expiry"] == IsPositiveFloat()
|
||||||
assert claimset["platform"] == "python-httpx/0.28.1"
|
assert claimset["platform"] == "python-httpx/0.28.1"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user