Files
back-end/tests/test_users.py
2023-04-17 07:51:53 -06:00

115 lines
3.1 KiB
Python

import pytest
from tests.base import avatar_index, client, register_user, username
from tests.config.database import clear_db
patched_username = "New name"
patched_password = "New password"
patched_avatar_index = 2
@pytest.mark.asyncio
async def test_get_current_user():
"""Test the GET /users endpoint to get info about the current user"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.get("/users", headers=headers)
assert response.status_code == 200
response = response.json()
assert response["username"] == username
assert response["avatar_index"] == avatar_index
@pytest.mark.asyncio
async def test_get_current_user_without_auth():
"""Getting the current user without a token should fail"""
clear_db()
response = client.get("/users", headers={"Content-Type": "application/json"})
assert response.status_code == 403
@pytest.mark.asyncio
async def test_patch_user():
"""Test the patching of a user's username, password and avatar"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.patch(
"/users",
json={
"username": patched_username,
"password": patched_password,
"avatar_index": patched_avatar_index,
},
headers=headers,
)
assert response.status_code == 200
response = client.post(
"/login",
headers={"Content-Type": "application/json"},
json={"username": patched_username, "password": patched_password},
)
assert response.status_code == 200
token = response.json()["access_token"]
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.get("/users", headers=headers)
assert response.status_code == 200
# Correctness of password and username is already asserted by the login
assert response.json()["avatar_index"] == patched_avatar_index
@pytest.mark.asyncio
async def test_patch_user_with_empty_fields():
"""Patching a user with empty fields should fail"""
clear_db()
token = await register_user()
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
response = client.patch(
"/users",
json={
"username": patched_username,
"password": patched_password,
"avatar_index": "",
},
headers=headers,
)
assert response.status_code == 422
response = client.patch(
"/users",
json={
"username": patched_username,
"password": "",
"avatar_index": patched_avatar_index,
},
headers=headers,
)
assert response.status_code == 400
response = client.patch(
"/users",
json={
"username": "",
"password": patched_password,
"avatar_index": patched_avatar_index,
},
headers=headers,
)
assert response.status_code == 400