The great endpoint refactor
This commit is contained in:
@@ -12,22 +12,32 @@ app.dependency_overrides[get_db] = override_get_db
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
username1 = "user1"
|
||||
username2 = "user2"
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
return response.json()["access_token"]
|
||||
|
||||
|
||||
@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},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -37,10 +47,13 @@ async def test_register():
|
||||
@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": username1, "password": password, "avatar": avatar},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
@@ -50,6 +63,8 @@ async def test_register_duplicate_name_should_fail():
|
||||
@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"},
|
||||
@@ -63,10 +78,12 @@ async def test_register_without_username_should_fail():
|
||||
@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": username2, "avatar": avatar},
|
||||
json={"username": username, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
@@ -76,10 +93,12 @@ async def test_register_without_password_should_fail():
|
||||
@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": username2, "password": password},
|
||||
json={"username": username, "password": password},
|
||||
)
|
||||
|
||||
# Not ideal that this is 400 instead of 422, but had no other choice than to give this field a default value
|
||||
@@ -90,10 +109,13 @@ async def test_register_without_avatar_should_fail():
|
||||
@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": username1, "password": password},
|
||||
json={"username": username, "password": password},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -102,11 +124,14 @@ async def test_login():
|
||||
|
||||
@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": username1, "password": wrong_password},
|
||||
json={"username": username, "password": wrong_password},
|
||||
)
|
||||
|
||||
assert response.status_code == 401
|
||||
@@ -116,10 +141,13 @@ async def test_login_wrong_password_should_fail():
|
||||
@pytest.mark.asyncio
|
||||
async def test_login_without_username_should_fail():
|
||||
"""Test whether logging in without passing a username fails"""
|
||||
clear_db()
|
||||
await register_user()
|
||||
|
||||
response = client.post(
|
||||
"/login",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username1},
|
||||
json={"password": password},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
@@ -129,10 +157,13 @@ async def test_login_without_username_should_fail():
|
||||
@pytest.mark.asyncio
|
||||
async def test_login_without_password_should_fail():
|
||||
"""Test whether logging in without passing a password fails"""
|
||||
clear_db()
|
||||
await register_user()
|
||||
|
||||
response = client.post(
|
||||
"/login",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username1},
|
||||
json={"username": username},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
|
||||
@@ -52,6 +52,7 @@ async def test_register_creates_progress_of_zero():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_all_returns_all():
|
||||
"""Test whether the 'All'-course fetches all course progress values"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
@@ -68,6 +69,7 @@ async def test_get_all_returns_all():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_get_nonexisting_course_should_fail():
|
||||
"""Test whether fetching the progress of a nonexisting course fails"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
@@ -81,6 +83,7 @@ async def test_get_nonexisting_course_should_fail():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_patch_course_progress():
|
||||
"""Test whether patching the progress value of a course works properly"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
@@ -91,9 +94,9 @@ async def test_patch_course_progress():
|
||||
progress_value = random.uniform(0, 1)
|
||||
|
||||
response = client.patch(
|
||||
f"/courseprogress",
|
||||
f"/courseprogress/{course}",
|
||||
headers=headers,
|
||||
json={"progress_value": progress_value, "course": course},
|
||||
json={"progress_value": progress_value},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -102,6 +105,7 @@ async def test_patch_course_progress():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_patch_all_should_patch_all_courses():
|
||||
"""Test whether patching the 'All'-course updates all progress values"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
@@ -110,9 +114,9 @@ async def test_patch_all_should_patch_all_courses():
|
||||
progress_value = random.uniform(0, 1)
|
||||
|
||||
response = client.patch(
|
||||
f"/courseprogress",
|
||||
"/courseprogress/All",
|
||||
headers=headers,
|
||||
json={"progress_value": progress_value, "course": "All"},
|
||||
json={"progress_value": progress_value},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
@@ -129,6 +133,7 @@ async def test_patch_all_should_patch_all_courses():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_patch_nonexisting_course_should_fail():
|
||||
"""Test whether patching a nonexisting course fails"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
@@ -139,9 +144,9 @@ async def test_patch_nonexisting_course_should_fail():
|
||||
progress_value = random.uniform(0, 1)
|
||||
|
||||
response = client.patch(
|
||||
f"/courseprogress",
|
||||
f"/courseprogress/{fake_course}",
|
||||
headers=headers,
|
||||
json={"progress_value": progress_value, "course": fake_course},
|
||||
json={"progress_value": progress_value},
|
||||
)
|
||||
|
||||
assert response.status_code == 422
|
||||
@@ -149,6 +154,7 @@ async def test_patch_nonexisting_course_should_fail():
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_patch_course_with_invalid_value_should_fail():
|
||||
"""Test whether patching a course progress value with an invalid value fails"""
|
||||
clear_db()
|
||||
token = await register_user()
|
||||
|
||||
@@ -158,17 +164,17 @@ async def test_patch_course_with_invalid_value_should_fail():
|
||||
too_low_progress_value = random.uniform(0, 1) - 2
|
||||
|
||||
response = client.patch(
|
||||
f"/courseprogress",
|
||||
"/courseprogress/All",
|
||||
headers=headers,
|
||||
json={"progress_value": too_high_progress_value, "course": "All"},
|
||||
json={"progress_value": too_high_progress_value},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
response = client.patch(
|
||||
f"/courseprogress",
|
||||
"/courseprogress/All",
|
||||
headers=headers,
|
||||
json={"progress_value": too_low_progress_value, "course": "All"},
|
||||
json={"progress_value": too_low_progress_value},
|
||||
)
|
||||
|
||||
assert response.status_code == 400
|
||||
|
||||
@@ -15,3 +15,21 @@ client = TestClient(app)
|
||||
username = "user1"
|
||||
password = "password"
|
||||
avatar = "lion"
|
||||
|
||||
|
||||
async def register_user():
|
||||
response = client.post(
|
||||
"/register",
|
||||
headers={"Content-Type": "application/json"},
|
||||
json={"username": username, "password": password, "avatar": avatar},
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
|
||||
return response.json()["access_token"]
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_post_highscore():
|
||||
"""Test whether posting a new high score succeeds"""
|
||||
clear_db()
|
||||
|
||||
Reference in New Issue
Block a user