Get started with user tests
This commit is contained in:
@@ -2,6 +2,7 @@ from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from src.database import Base
|
||||
from src.models import CourseProgress, HighScore, User
|
||||
|
||||
SQLALCHEMY_DATABASE_URL = "postgresql://admin:WeSign123!@localhost/wesigntest"
|
||||
|
||||
@@ -12,6 +13,15 @@ TestSessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
|
||||
def clear_db():
|
||||
db = TestSessionLocal()
|
||||
|
||||
db.query(HighScore).delete()
|
||||
db.query(CourseProgress).delete()
|
||||
db.query(User).delete()
|
||||
db.commit()
|
||||
|
||||
|
||||
def override_get_db():
|
||||
try:
|
||||
db = TestSessionLocal()
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import sys
|
||||
|
||||
import pytest
|
||||
from sqlalchemy import create_engine
|
||||
|
||||
sys.path.append("..")
|
||||
|
||||
from src.database import Base as ProductionBase
|
||||
from tests.config.database import (SQLALCHEMY_DATABASE_URL, TestBase,
|
||||
TestSessionLocal)
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_session():
|
||||
engine = create_engine(SQLALCHEMY_DATABASE_URL)
|
||||
print(SQLALCHEMY_DATABASE_URL)
|
||||
ProductionBase.metadata.create_all(bind=engine)
|
||||
TestBase.metadata.create_all(bind=engine)
|
||||
session = TestSessionLocal(bind=engine)
|
||||
try:
|
||||
yield session
|
||||
finally:
|
||||
session.rollback()
|
||||
TestBase.metadata.drop_all(bind=engine)
|
||||
ProductionBase.metadata.drop_all(bind=engine)
|
||||
session.close()
|
||||
@@ -6,20 +6,144 @@ from fastapi.testclient import TestClient
|
||||
sys.path.append("..")
|
||||
|
||||
from src.main import app, get_db
|
||||
from tests.config.database import override_get_db
|
||||
from tests.config.database import clear_db, override_get_db
|
||||
|
||||
app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
|
||||
patched_username = "New name"
|
||||
patched_password = "New password"
|
||||
patched_avatar = "New avatar"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_add_user():
|
||||
async def test_get_current_user():
|
||||
"""Test the GET /users endpoint to get info about the current user"""
|
||||
clear_db()
|
||||
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": "user27", "password": "mettn", "avatar": "lion"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
print(response)
|
||||
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
|
||||
response = response.json()
|
||||
assert response["username"] == username
|
||||
assert response["avatar"] == avatar
|
||||
|
||||
|
||||
@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()
|
||||
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
token = response.json()["access_token"]
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
response = client.patch(
|
||||
"/users",
|
||||
json={
|
||||
"username": patched_username,
|
||||
"password": patched_password,
|
||||
"avatar": patched_avatar,
|
||||
},
|
||||
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"] == patched_avatar
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_patch_user_with_empty_fields():
|
||||
"""Patching a user with empty fields should fail"""
|
||||
clear_db()
|
||||
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
assert response.status_code == 200
|
||||
|
||||
token = response.json()["access_token"]
|
||||
|
||||
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
||||
|
||||
response = client.patch(
|
||||
"/users",
|
||||
json={
|
||||
"username": patched_username,
|
||||
"password": patched_password,
|
||||
"avatar": "",
|
||||
},
|
||||
headers=headers,
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
response = client.patch(
|
||||
"/users",
|
||||
json={
|
||||
"username": patched_username,
|
||||
"password": "",
|
||||
"avatar": patched_avatar,
|
||||
},
|
||||
headers=headers,
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
response = client.patch(
|
||||
"/users",
|
||||
json={
|
||||
"username": "",
|
||||
"password": patched_password,
|
||||
"avatar": patched_avatar,
|
||||
},
|
||||
headers=headers,
|
||||
)
|
||||
assert response.status_code == 400
|
||||
|
||||
Reference in New Issue
Block a user