Files
back-end/tests/test_authentication.py
2023-04-10 14:07:30 -06:00

140 lines
3.9 KiB
Python

import sys
import pytest
from fastapi.testclient import TestClient
sys.path.append("..")
from src.main import app, get_db
from tests.config.database import clear_db, override_get_db
app.dependency_overrides[get_db] = override_get_db
client = TestClient(app)
username1 = "user1"
username2 = "user2"
password = "password"
avatar = "lion"
@pytest.mark.asyncio
async def test_register():
"""LEAVE THIS TEST AT THE TOP OF THE FILE!"""
"""Test the register endpoint"""
clear_db()
response = client.post(
"/register",
headers={"Content-Type": "application/json"},
json={"username": username1, "password": password, "avatar": avatar},
)
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"""
response = client.post(
"/register",
headers={"Content-Type": "application/json"},
json={"username": username1, "password": password, "avatar": avatar},
)
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"""
response = client.post(
"/register",
headers={"Content-Type": "application/json"},
json={"password": password, "avatar": avatar},
)
assert response.status_code == 422
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"""
response = client.post(
"/register",
headers={"Content-Type": "application/json"},
json={"username": username2, "avatar": avatar},
)
assert response.status_code == 422
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"""
response = client.post(
"/register",
headers={"Content-Type": "application/json"},
json={"username": username2, "password": password},
)
# Not ideal that this is 400 instead of 422, but had no other choice than to give this field a default value
assert response.status_code == 400
assert "access_token" not in response.json()
@pytest.mark.asyncio
async def test_login():
"""Test the login endpoint"""
response = client.post(
"/login",
headers={"Content-Type": "application/json"},
json={"username": username1, "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():
wrong_password = password + "extra characters"
response = client.post(
"/login",
headers={"Content-Type": "application/json"},
json={"username": username1, "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"""
response = client.post(
"/login",
headers={"Content-Type": "application/json"},
json={"username": username1},
)
assert response.status_code == 422
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"""
response = client.post(
"/login",
headers={"Content-Type": "application/json"},
json={"username": username1},
)
assert response.status_code == 422
assert "access_token" not in response.json()