145 lines
3.9 KiB
Python
145 lines
3.9 KiB
Python
import pytest
|
|
|
|
from tests.base import avatar_index, client, password, register_user, username
|
|
from tests.config.database import clear_db
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register():
|
|
"""Test the register endpoint"""
|
|
clear_db()
|
|
|
|
response = client.post(
|
|
"/register",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"username": username, "password": password, "avatar_index": avatar_index},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert len(response.json()["access_token"]) > 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_duplicate_name_should_fail():
|
|
"""Test whether registering a user with an existing username fails"""
|
|
clear_db()
|
|
await register_user()
|
|
|
|
response = client.post(
|
|
"/register",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"username": username, "password": password, "avatar_index": avatar_index},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
assert "access_token" not in response.json()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_without_username_should_fail():
|
|
"""Test whether registering a user without passing a username fails"""
|
|
clear_db()
|
|
|
|
response = client.post(
|
|
"/register",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"password": password, "avatar_index": avatar_index},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
assert "access_token" not in response.json()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_without_password_should_fail():
|
|
"""Test whether registering a user without passing a password fails"""
|
|
clear_db()
|
|
|
|
response = client.post(
|
|
"/register",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"username": username, "avatar_index": avatar_index},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
assert "access_token" not in response.json()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_register_without_avatar_should_fail():
|
|
"""Test whether registering a user without passing an avatar fails"""
|
|
clear_db()
|
|
|
|
response = client.post(
|
|
"/register",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"username": username, "password": password},
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
assert "access_token" not in response.json()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login():
|
|
"""Test the login endpoint"""
|
|
clear_db()
|
|
await register_user()
|
|
|
|
response = client.post(
|
|
"/login",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"username": username, "password": password},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert len(response.json()["access_token"]) > 0
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_wrong_password_should_fail():
|
|
clear_db()
|
|
await register_user()
|
|
|
|
wrong_password = password + "extra characters"
|
|
response = client.post(
|
|
"/login",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"username": username, "password": wrong_password},
|
|
)
|
|
|
|
assert response.status_code == 401
|
|
assert "access_token" not in response.json()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_without_username_should_fail():
|
|
"""Test whether logging in without passing a username fails, since the default is an empty string"""
|
|
clear_db()
|
|
await register_user()
|
|
|
|
response = client.post(
|
|
"/login",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"password": password},
|
|
)
|
|
|
|
assert response.status_code == 401
|
|
assert "access_token" not in response.json()
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_login_without_password_should_fail():
|
|
"""Test whether logging in without passing a password fails, since the default is an empty string"""
|
|
clear_db()
|
|
await register_user()
|
|
|
|
response = client.post(
|
|
"/login",
|
|
headers={"Content-Type": "application/json"},
|
|
json={"username": username},
|
|
)
|
|
|
|
assert response.status_code == 401
|
|
assert "access_token" not in response.json()
|