301 lines
8.5 KiB
Python
301 lines
8.5 KiB
Python
import random
|
|
|
|
import pytest
|
|
|
|
from src.enums import CourseEnum
|
|
from tests.base import client, get_headers, register_user
|
|
from tests.config.database import clear_db
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_learnables_should_succeed():
|
|
"""Test whether creating a new learnable succeeds"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
headers = get_headers(token)
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
nr_learnables = random.randint(1, 5)
|
|
for i in range(nr_learnables):
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": i,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course} {i}",
|
|
},
|
|
headers=headers,
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
response = client.get(f"/learnables/{course}", headers=headers)
|
|
|
|
assert response.status_code == 200
|
|
|
|
response = response.json()
|
|
|
|
assert len(response) == nr_learnables
|
|
@pytest.mark.asyncio
|
|
async def test_patch_learnables_should_succeed():
|
|
"""Test whether patching learnables succeeds"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
headers = get_headers(token)
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": random.randint(0, 100),
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course}",
|
|
},
|
|
headers=headers,
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
new_index = random.randint(0, 100)
|
|
new_in_use = bool(random.randint(0, 1))
|
|
new_name = "New" + course
|
|
|
|
response = client.patch(f"/learnables/{course}", json={"index": new_index, "in_use": new_in_use, "name": new_name}, headers=headers)
|
|
|
|
assert response.status_code == 200
|
|
|
|
response = client.get(f"/learnables/{course}", headers=headers)
|
|
|
|
assert response.status_code == 200
|
|
|
|
response = response.json()[0]
|
|
|
|
assert response["index"] == new_index
|
|
assert response["in_use"] == new_in_use
|
|
assert response["name"] == new_name
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_learnables_without_name_should_fail():
|
|
"""Test whether creating a new learnable without name fails"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
headers = get_headers(token)
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": random.randint(0, 100),
|
|
"in_use": bool(random.randint(0, 1)),
|
|
},
|
|
headers=headers,
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_learnables_without_index_should_fail():
|
|
"""Test whether creating a new learnable without index fails"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
headers = get_headers(token)
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"name": course,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
},
|
|
headers=headers,
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_learnables_without_in_use_should_fail():
|
|
"""Test whether creating a new learnable without in_use fails"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
headers = get_headers(token)
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": random.randint(0, 100),
|
|
"name": course,
|
|
},
|
|
headers=headers,
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_learnable_without_auth_should_fail():
|
|
"""Test whether creating learnables without authentication fails"""
|
|
clear_db()
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": 0,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course}",
|
|
},
|
|
headers=get_headers(),
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_learnables_of_nonexisting_course_should_fail():
|
|
"""Test whether learnables of a nonexisting course fails"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
course = "FakeCourse"
|
|
|
|
response = client.get(f"/learnables/{course}", headers=get_headers(token))
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_post_learnable_to_nonexisting_course_should_fail():
|
|
"""Test whether creating a learnable for a nonexisting course fails fails"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
course = "FakeCourse"
|
|
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": 0,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course}",
|
|
},
|
|
headers=get_headers(token),
|
|
)
|
|
|
|
assert response.status_code == 422
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_get_learnables_without_auth_should_fail():
|
|
"""Test whether fetching learnables without authentication fails"""
|
|
clear_db()
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
response = client.get(f"/learnables/{course}", headers=get_headers())
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_patch_learnable_without_auth_should_fail():
|
|
"""Test whether patching learnables without authentication fails"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": 0,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course}",
|
|
},
|
|
headers=get_headers(token),
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
response = client.patch(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": 0,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course}",
|
|
},
|
|
headers=get_headers(),
|
|
)
|
|
|
|
assert response.status_code == 403
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_create_learnable_with_existing_name_should_fail():
|
|
"""Test whether putting high scores without authentication fails"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": 0,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course}",
|
|
},
|
|
headers=get_headers(token),
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
|
|
response = client.post(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": 1,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course}",
|
|
},
|
|
headers=get_headers(token),
|
|
)
|
|
|
|
assert response.status_code == 400
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
async def test_patch_nonexisting_learnable_should_fail():
|
|
"""Test whether patching nonexisting learnables fails"""
|
|
clear_db()
|
|
token = await register_user()
|
|
|
|
for course in CourseEnum:
|
|
if course != CourseEnum.All:
|
|
response = client.patch(
|
|
f"/learnables/{course}",
|
|
json={
|
|
"index": 0,
|
|
"in_use": bool(random.randint(0, 1)),
|
|
"name": f"{course}",
|
|
},
|
|
headers=get_headers(token),
|
|
)
|
|
|
|
assert response.status_code == 400
|