🐛 Fixed some tests

This commit is contained in:
Mawoka
2023-06-17 20:45:04 +02:00
parent 1459c6f786
commit 7227cd82e1
5 changed files with 110 additions and 245 deletions
+2 -5
View File
@@ -52,15 +52,12 @@ async def download_file(file_name: str):
else: else:
return RedirectResponse(url=await storage.get_url(file_name, 300), headers=headers_from_storage_item(item)) return RedirectResponse(url=await storage.get_url(file_name, 300), headers=headers_from_storage_item(item))
try: try:
download = await storage.download(file_name) download = storage.download(file_name)
except DownloadingFailedError: except DownloadingFailedError:
raise HTTPException(status_code=404, detail="File not found") raise HTTPException(status_code=404, detail="File not found")
if download is None: if download is None:
raise HTTPException(status_code=404, detail="File not found") raise HTTPException(status_code=404, detail="File not found")
def iter_file():
yield from download
media_type = "image/*" media_type = "image/*"
if item is not None: if item is not None:
media_type = item.mime_type media_type = item.mime_type
@@ -69,7 +66,7 @@ async def download_file(file_name: str):
headers = {**headers, **headers_from_storage_item(item)} headers = {**headers, **headers_from_storage_item(item)}
return StreamingResponse( return StreamingResponse(
iter_file(), download,
media_type=media_type, media_type=media_type,
headers=headers, headers=headers,
) )
+3 -2
View File
@@ -3,6 +3,7 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/. # file, You can obtain one at https://mozilla.org/MPL/2.0/.
import os import os
from shutil import copyfileobj
from typing import BinaryIO, Generator from typing import BinaryIO, Generator
import aiofiles import aiofiles
@@ -29,8 +30,8 @@ class LocalStorage:
# skipcq: PYL-W0613 # skipcq: PYL-W0613
async def upload(self, file_name: str, file: BinaryIO, mime_type: str | None = None) -> None: async def upload(self, file_name: str, file: BinaryIO, mime_type: str | None = None) -> None:
async with aiofiles.open(file=os.path.join(self.base_path, file_name), mode="wb") as f: with open(file=os.path.join(self.base_path, file_name), mode="wb") as f:
await aioshutil_copyfileobj(file, f) copyfileobj(file, f)
async def delete(self, file_names: [str]) -> None: async def delete(self, file_names: [str]) -> None:
for i in file_names: for i in file_names:
+5 -1
View File
@@ -33,6 +33,7 @@ class ValueStorage:
exported_quiz_data = None exported_quiz_data = None
edit_id = None edit_id = None
image_id = None image_id = None
cookies = None
example_quiz = { example_quiz = {
@@ -41,14 +42,16 @@ example_quiz = {
"description": "A description", "description": "A description",
"questions": [ "questions": [
{ {
"type": "ABCD",
"question": "Is ClassQuiz cool?", "question": "Is ClassQuiz cool?",
"time": 10, "time": 10,
"answers": [{"right": True, "answer": "Yes"}, {"right": False, "answer": "No"}], "answers": [{"right": True, "answer": "Yes"}, {"right": False, "answer": "No"}],
}, },
{ {
"type": "ABCD",
"question": "Do you like open source?", "question": "Do you like open source?",
"time": 5, "time": 5,
"image": "https://i.imgur.com/sSNSy77.png", "image": None,
"answers": [ "answers": [
{"right": True, "answer": "Yes"}, {"right": True, "answer": "Yes"},
{"right": False, "answer": "No"}, {"right": False, "answer": "No"},
@@ -60,6 +63,7 @@ example_quiz = {
test_user_email = "sth@byom.de" test_user_email = "sth@byom.de"
test_user_password = "test" test_user_password = "test"
# mock_test_results = {'0': [{'username': 'Player 1', 'answer': 'Byte, Bit, KB, MB, GB, TB', 'right': False}, # mock_test_results = {'0': [{'username': 'Player 1', 'answer': 'Byte, Bit, KB, MB, GB, TB', 'right': False},
# {'username': 'Player 2', 'answer': 'Byte, Bit, KB, MB, GB, TB', 'right': False}, {'username': 'Player 3', # {'username': 'Player 2', 'answer': 'Byte, Bit, KB, MB, GB, TB', 'right': False}, {'username': 'Player 3',
# 'answer': 'Bit, Byte, KB, MB, GB, TB', 'right': True}], '1': [{'username': 'Player 3', 'answer': 'CPU', # 'answer': 'Bit, Byte, KB, MB, GB, TB', 'right': True}], '1': [{'username': 'Player 3', 'answer': 'CPU',
+97 -219
View File
@@ -5,12 +5,10 @@
import uuid import uuid
import pytest import pytest
from httpx import AsyncClient
from redis import Redis from redis import Redis
from classquiz.config import settings from classquiz.config import settings
from classquiz.tests import test_user_email, test_user_password from classquiz.tests import test_user_email, test_user_password
from classquiz.tests import test_client, example_quiz, ValueStorage # noqa : F401 from classquiz.tests import test_client, example_quiz, ValueStorage # noqa : F401
from classquiz.helpers.hashcash import mint
# @pytest.fixture # @pytest.fixture
@@ -25,6 +23,15 @@ from classquiz.helpers.hashcash import mint
class TestUsers: class TestUsers:
def log_in(self, tc, email=test_user_email, password=test_user_password) -> int:
resp = tc.post("/api/v1/login/start", json={"email": email})
session_id = resp.json()["session_id"]
resp = tc.post(
f"/api/v1/login/step/1?session_id={session_id}", json={"auth_type": "PASSWORD", "data": password}
)
ValueStorage.cookies = resp.cookies
return resp.status_code
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_create_test_user(self, test_client): # noqa : F811 async def test_create_test_user(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.post(
@@ -56,138 +63,89 @@ class TestUsers:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_verify_email(self, test_client): # noqa : F811 async def test_verify_email(self, test_client): # noqa : F811
resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
assert resp.status_code == 401
user = test_client.get(f"/api/v1/internal/testing/user/{test_user_email}?secret_key={settings().secret_key}") user = test_client.get(f"/api/v1/internal/testing/user/{test_user_email}?secret_key={settings().secret_key}")
assert (test_client.get("/api/v1/users/verify/dasadsasdadsasdsaddassad")).status_code == 404 assert (test_client.get("/api/v1/users/verify/dasadsasdadsasdsaddassad")).status_code == 404
test_client.get(f"/api/v1/users/verify/{user.json()['verify_key']}") test_client.get(f"/api/v1/users/verify/{user.json()['verify_key']}")
resp = test_client.post("/api/v1/login/start", json={"email": test_user_email})
assert resp.status_code == 200
session_id = resp.json()["session_id"]
resp = test_client.post( resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password} f"/api/v1/login/step/1?session_id={session_id}", json={"auth_type": "PASSWORD", "data": test_user_password}
) )
ValueStorage.cookies = resp.cookies
assert resp.status_code == 200 assert resp.status_code == 200
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_check(self, test_client): # noqa : F811 async def test_check(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.get("/api/v1/users/check", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.json()["access_token"]
resp = test_client.get("/api/v1/users/check", cookies={"access_token": f"Bearer {token}"})
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.get("/api/v1/users/check", cookies={"access_token": "Bearer dasasdasddasadsasdadssadsd"})
assert resp.status_code == 401
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_me(self, test_client): # noqa : F811 async def test_me(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.get("/api/v1/users/me", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.json()["access_token"]
resp = test_client.get("/api/v1/users/me", cookies={"access_token": f"Bearer {token}"})
data = resp.json() data = resp.json()
assert resp.status_code == 200 assert resp.status_code == 200
assert data["verified"] is True assert data["verified"] is True
assert data["email"] == test_user_email assert data["email"] == test_user_email
assert data["username"] == "mawoka" assert data["username"] == "mawoka"
@pytest.mark.asyncio # @pytest.mark.asyncio
async def test_rememberme(self, test_client): # noqa : F811 # async def test_logout(self, test_client): # noqa : F811
resp = test_client.post( # resp = test_client.get("/api/v1/users/me", cookies={"access_token": access_token})
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password} # assert resp.status_code == 200
) # resp = test_client.get(
rememberme_token = resp.cookies["rememberme_token"] # "/api/v1/users/logout", cookies={"rememberme_token": rememberme_token}, allow_redirects=False
resp = test_client.get("/api/v1/users/token/rememberme", cookies={"rememberme_token": rememberme_token}) # )
assert resp.cookies["access_token"] is not None # assert resp.status_code == 302
assert resp.status_code == 200 # resp = test_client.get("/api/v1/users/me", cookies={"rememberme_token": rememberme_token})
resp = test_client.get( # assert resp.status_code == 401
"/api/v1/users/token/rememberme", cookies={"rememberme_token": "dsahgvjadsvsahgxddsvhgdsvhg"}
)
assert resp.status_code == 401
@pytest.mark.asyncio
async def test_logout(self, test_client): # noqa : F811
resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
rememberme_token = resp.cookies["rememberme_token"]
access_token = resp.cookies["access_token"]
resp = test_client.get("/api/v1/users/me", cookies={"access_token": access_token})
assert resp.status_code == 200
resp = test_client.get(
"/api/v1/users/logout", cookies={"rememberme_token": rememberme_token}, allow_redirects=False
)
assert resp.status_code == 302
resp = test_client.get("/api/v1/users/me", cookies={"rememberme_token": rememberme_token})
assert resp.status_code == 401
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_password_update(self, test_client): # noqa : F811 async def test_password_update(self, test_client): # noqa : F811
resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.json()["access_token"]
resp = test_client.put( resp = test_client.put(
"/api/v1/users/password/update", "/api/v1/users/password/update",
json={"new_password": "new_password", "old_password": test_user_password}, json={"new_password": "new_password", "old_password": test_user_password},
cookies={"access_token": f"Bearer {token}"}, cookies=ValueStorage.cookies,
) )
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.put( resp = test_client.put(
"/api/v1/users/password/update", "/api/v1/users/password/update",
json={"new_password": "asdsdadsasdaasd", "old_password": "asdasdsadadsasdsadasdasd"}, json={"new_password": "asdsdadsasdaasd", "old_password": "asdasdsadadsasdsadasdasd"},
cookies={"access_token": f"Bearer {token}"}, cookies=ValueStorage.cookies,
) )
assert resp.status_code == 400 assert resp.status_code == 400
resp = test_client.post( resp_code = self.log_in(test_client, password="new_password")
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": "new_password"} assert resp_code == 200
)
assert resp.status_code == 200
resp = test_client.put( resp = test_client.put(
"/api/v1/users/password/update", "/api/v1/users/password/update",
json={"new_password": test_user_password, "old_password": "new_password"}, json={"new_password": test_user_password, "old_password": "new_password"},
cookies={"access_token": f"Bearer {token}"}, cookies=ValueStorage.cookies,
) )
assert resp.status_code == 200 assert resp.status_code == 200
resp1 = test_client.post( resp_code = self.log_in(test_client)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password} assert resp_code == 200
) response = test_client.get("/api/v1/users/me", cookies=ValueStorage.cookies)
rememberme_token = resp1.cookies["rememberme_token"]
response = test_client.get("/api/v1/users/me", cookies={"rememberme_token": rememberme_token})
assert response.status_code == 200 assert response.status_code == 200
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_session(self, test_client): # noqa : F811 async def test_get_session(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.get("/api/v1/users/session", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["rememberme_token"]
resp = test_client.get("/api/v1/users/session", cookies={"rememberme_token": token})
assert resp.status_code == 200 assert resp.status_code == 200
assert resp.json()["ip_address"] == "testclient" assert resp.json()["ip_address"] == "testclient"
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_delete_session(self, test_client): # noqa : F811 async def test_delete_session(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.get("/api/v1/users/session", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["rememberme_token"]
resp = test_client.get("/api/v1/users/session", cookies={"rememberme_token": token})
session_id = resp.json()["id"] session_id = resp.json()["id"]
resp = test_client.delete("/api/v1/users/sessions/" + str(session_id), cookies={"rememberme_token": token}) resp = test_client.delete("/api/v1/users/sessions/" + str(session_id), cookies=ValueStorage.cookies)
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.delete("/api/v1/users/sessions/asdsadasdasdsad", cookies={"rememberme_token": token}) resp = test_client.delete("/api/v1/users/sessions/asdsadasdasdsad", cookies=ValueStorage.cookies)
assert resp.status_code == 400 assert resp.status_code == 400
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_list_sessions(self, test_client): # noqa : F811 async def test_list_sessions(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.get("/api/v1/users/sessions/list", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["rememberme_token"]
resp = test_client.get("/api/v1/users/sessions/list", cookies={"rememberme_token": token})
assert resp.status_code == 200 assert resp.status_code == 200
assert len(resp.json()) >= 1 assert len(resp.json()) >= 1
@@ -200,11 +158,7 @@ class TestUsers:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_reset_password_with_token(self, test_client): # noqa : F811 async def test_reset_password_with_token(self, test_client): # noqa : F811
resp = test_client.post( me = test_client.get("/api/v1/users/me", cookies=ValueStorage.cookies).json()
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
me = test_client.get("/api/v1/users/me", cookies={"access_token": token}).json()
redis = Redis().from_url(settings().redis) redis = Redis().from_url(settings().redis)
redis.set("reset_passwd:_1token_", str(me["id"])) redis.set("reset_passwd:_1token_", str(me["id"]))
redis.set("reset_passwd:_2token_", str(uuid.uuid4())) redis.set("reset_passwd:_2token_", str(uuid.uuid4()))
@@ -217,35 +171,24 @@ class TestUsers:
assert resp.status_code == 400 assert resp.status_code == 400
resp = test_client.post("/api/v1/users/reset-password", json={"token": "_1token_", "password": "new_password"}) resp = test_client.post("/api/v1/users/reset-password", json={"token": "_1token_", "password": "new_password"})
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.post( self.log_in(test_client, password="new_password")
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": "new_password"}
)
token = resp.cookies["access_token"]
test_client.put( test_client.put(
"/api/v1/users/password/update", "/api/v1/users/password/update",
json={"new_password": test_user_password, "old_password": "new_password"}, json={"new_password": test_user_password, "old_password": "new_password"},
cookies={"access_token": token}, cookies=ValueStorage.cookies,
) )
redis.flushdb() redis.flushdb()
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_signout_everywhere(self, test_client): # noqa : F811 async def test_signout_everywhere(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.delete("/api/v1/users/signout-everywhere", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
resp = test_client.delete("/api/v1/users/signout-everywhere", cookies={"access_token": token})
assert resp.status_code == 200 assert resp.status_code == 200
class TestUtils: class TestUtils:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_ip_data(self, test_client): # noqa : F811 async def test_get_ip_data(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.get("/api/v1/utils/ip-lookup/1.1.1.1", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
resp = test_client.get("/api/v1/utils/ip-lookup/1.1.1.1", cookies={"access_token": token})
assert resp.status_code == 200 assert resp.status_code == 200
assert resp.json()["query"] == "1.1.1.1" assert resp.json()["query"] == "1.1.1.1"
@@ -289,99 +232,84 @@ class TestStats:
class TestQuiz: class TestQuiz:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_create_quiz(self, test_client): # noqa : F811 async def test_create_quiz(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.post("/api/v1/editor/start?edit=false", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
resp = test_client.post("/api/v1/quiz/create", json=example_quiz, cookies={"access_token": token})
assert resp.status_code == 200 assert resp.status_code == 200
ValueStorage.quiz_id = resp.json()["id"] edit_token = resp.json()["token"]
example_quiz["questions"][1]["image"] = "https://imgur.com/sSNSy77.png" assert len(edit_token) == 8
resp = test_client.post("/api/v1/quiz/create", json=example_quiz, cookies={"access_token": token}) resp = test_client.post(
assert resp.status_code == 400 f"/api/v1/editor/finish?edit_id={edit_token}", json=example_quiz, cookies=ValueStorage.cookies
)
assert resp.status_code == 200
resp = test_client.get("/api/v1/quiz/list", cookies=ValueStorage.cookies)
assert resp.status_code == 200
data = resp.json()
assert len(data) == 1
ValueStorage.quiz_id = data[0]["id"]
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_quiz_from_id(self, test_client): # noqa : F811 async def test_get_quiz_from_id(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.get(f"/api/v1/quiz/get/{ValueStorage.quiz_id}", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
resp = test_client.get(f"/api/v1/quiz/get/{ValueStorage.quiz_id}", cookies={"access_token": token})
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.get("/api/v1/quiz/get/dasdsadsadsadsadsa", cookies={"access_token": token}) resp = test_client.get("/api/v1/quiz/get/dasdsadsadsadsadsa", cookies=ValueStorage.cookies)
assert resp.status_code == 400 assert resp.status_code == 400
resp = test_client.get("/api/v1/quiz/get/847c64d3-39f9-4bb7-8f13-fae913f67858", cookies={"access_token": token}) resp = test_client.get("/api/v1/quiz/get/847c64d3-39f9-4bb7-8f13-fae913f67858", cookies=ValueStorage.cookies)
assert resp.status_code == 404 assert resp.status_code == 404
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_list_quizzes(self, test_client): # noqa : F811 async def test_list_quizzes(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.get("/api/v1/quiz/list", cookies=ValueStorage.cookies)
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
resp = test_client.get("/api/v1/quiz/list", cookies={"access_token": token})
assert resp.status_code == 200 assert resp.status_code == 200
assert resp.json()[0]["id"] == ValueStorage.quiz_id assert resp.json()[0]["id"] == ValueStorage.quiz_id
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_update_quiz(self, test_client): # noqa : F811 async def test_update_quiz(self, test_client): # noqa : F811
example_quiz["public"] = True example_quiz["public"] = True
example_quiz["questions"][1]["image"] = "https://i.imgur.com/sSNSy77.png"
resp = test_client.post( resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password} f"/api/v1/editor/start?edit=true&quiz_id={ValueStorage.quiz_id}", cookies=ValueStorage.cookies
) )
token = resp.cookies["access_token"] edit_id = resp.json()["token"]
resp = test_client.put( resp = test_client.post(
f"/api/v1/quiz/update/{ValueStorage.quiz_id}", json=example_quiz, cookies={"access_token": token} f"/api/v1/editor/finish?edit_id={edit_id}", json=example_quiz, cookies=ValueStorage.cookies
) )
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.put( resp = test_client.post(
"/api/v1/quiz/update/f183e091-a863-44ec-a1b7-c70eb92e3f6a", "/api/v1/editor/start?edit=true&quiz_id=f183e091-a863-44ec-a1b7-c70eb92e3f6a", cookies=ValueStorage.cookies
json=example_quiz,
cookies={"access_token": token},
) )
assert resp.status_code == 404 assert resp.status_code == 404
resp = test_client.put( resp = test_client.post("/api/v1/editor/start?edit=true&quiz_id=asddasasdasdads", cookies=ValueStorage.cookies)
"/api/v1/quiz/update/saddsaasddsadsa", json=example_quiz, cookies={"access_token": token} assert resp.status_code == 422
)
assert resp.status_code == 400
example_quiz["public"] = False example_quiz["public"] = False
test_client.put( resp = test_client.post(
f"/api/v1/quiz/update/{ValueStorage.quiz_id}", json=example_quiz, cookies={"access_token": token} f"/api/v1/editor/start?edit=true&quiz_id={ValueStorage.quiz_id}", cookies=ValueStorage.cookies
) )
edit_id = resp.json()["token"]
test_client.post(f"/api/v1/editor/finish?edit_id={edit_id}", json=example_quiz, cookies=ValueStorage.cookies)
example_quiz["public"] = True example_quiz["public"] = True
test_client.put( resp = test_client.post(
f"/api/v1/quiz/update/{ValueStorage.quiz_id}", json=example_quiz, cookies={"access_token": token} f"/api/v1/editor/start?edit=true&quiz_id={ValueStorage.quiz_id}", cookies=ValueStorage.cookies
) )
edit_id = resp.json()["token"]
test_client.post(f"/api/v1/editor/finish?edit_id={edit_id}", json=example_quiz, cookies=ValueStorage.cookies)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_import_quiz(self, test_client): # noqa : F811 async def test_import_quiz(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password} "/api/v1/quiz/import/1f95eb0b-fcf4-4db2-879b-5418ef75116b", cookies=ValueStorage.cookies
)
token = resp.cookies["access_token"]
resp = test_client.post(
"/api/v1/quiz/import/1f95eb0b-fcf4-4db2-879b-5418ef75116b", cookies={"access_token": token}
) )
assert resp.status_code == 200 assert resp.status_code == 200
ValueStorage.imported_quizzes.append(resp.json()["id"]) ValueStorage.imported_quizzes.append(resp.json()["id"])
resp = test_client.post("/api/v1/quiz/import/1f95eb0bdassdadasdas", cookies={"access_token": token}) resp = test_client.post("/api/v1/quiz/import/1f95eb0bdassdadasdas", cookies=ValueStorage.cookies)
assert resp.text == '"quiz not found"' assert resp.text == '"quiz not found"'
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_get_public_quiz(self, test_client): # noqa : F811 async def test_get_public_quiz(self, test_client): # noqa : F811
resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
resp = test_client.get(f"/api/v1/quiz/get/public/{ValueStorage.imported_quizzes[0]}") resp = test_client.get(f"/api/v1/quiz/get/public/{ValueStorage.imported_quizzes[0]}")
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.get( resp = test_client.get(
"/api/v1/quiz/get/public/f183e091-a863-44ec-a1b7-c70eb92e3f6a", cookies={"access_token": token} "/api/v1/quiz/get/public/f183e091-a863-44ec-a1b7-c70eb92e3f6a", cookies=ValueStorage.cookies
) )
assert resp.status_code == 404 assert resp.status_code == 404
resp = test_client.get("/api/v1/quiz/get/public/dadasdas92e3f6a", cookies={"access_token": token}) resp = test_client.get("/api/v1/quiz/get/public/dadasdas92e3f6a", cookies=ValueStorage.cookies)
assert resp.status_code == 400 assert resp.status_code == 400
@pytest.mark.asyncio @pytest.mark.asyncio
@@ -401,10 +329,10 @@ class TestQuiz:
resp = test_client.get(f"/api/v1/quiz/get/public/{ValueStorage.imported_quizzes[0]}") resp = test_client.get(f"/api/v1/quiz/get/public/{ValueStorage.imported_quizzes[0]}")
assert resp.status_code == 200 assert resp.status_code == 200
quiz = resp.json() quiz = resp.json()
image_url = quiz["questions"][0]["image"] image_id = quiz["questions"][0]["image"]
resp = test_client.get(image_url) resp = test_client.get(f"/api/v1/storage/download/{image_id}")
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.get(f"{image_url}sadgvsadgvhsad") resp = test_client.get(f"/api/v1/storage/download/{image_id}sadgvsadgvhsad")
assert resp.status_code == 400 assert resp.status_code == 400
@@ -412,20 +340,16 @@ class TestPlayQuiz:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_start_quiz(self, test_client): # noqa : F811 async def test_start_quiz(self, test_client): # noqa : F811
resp = test_client.post( resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password} "/api/v1/quiz/start/fb5adc91-629e-416e-8b98-ae400e36417c?game_mode=kahoot", cookies=ValueStorage.cookies
)
token = resp.cookies["access_token"]
resp = test_client.post(
"/api/v1/quiz/start/fb5adc91-629e-416e-8b98-ae400e36417c?game_mode=kahoot", cookies={"access_token": token}
) )
assert resp.status_code == 404 assert resp.status_code == 404
resp = test_client.post( resp = test_client.post(
"/api/v1/quiz/start/fb5adc91-629e-416e-8b98-ae400sdadsasadsadasddsae36417c?game_mode=kahoot", "/api/v1/quiz/start/fb5adc91-629e-416e-8b98-ae400sdadsasadsadasddsae36417c?game_mode=kahoot",
cookies={"access_token": token}, cookies=ValueStorage.cookies,
) )
assert resp.status_code == 400 assert resp.status_code == 400
resp = test_client.post( resp = test_client.post(
f"/api/v1/quiz/start/{ValueStorage.quiz_id}?game_mode=kahoot", cookies={"access_token": token} f"/api/v1/quiz/start/{ValueStorage.quiz_id}?game_mode=kahoot", cookies=ValueStorage.cookies
) )
ValueStorage.game_pin = resp.json()["game_pin"] ValueStorage.game_pin = resp.json()["game_pin"]
ValueStorage.game_id = resp.json()["game_id"] ValueStorage.game_id = resp.json()["game_id"]
@@ -462,59 +386,19 @@ class TestCache:
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password} "/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
) )
token = resp.cookies["access_token"] token = resp.cookies["access_token"]
resp = test_client.get("/api/v1/users/me", cookies={"access_token": token}) resp = test_client.get("/api/v1/users/me", cookies=ValueStorage.cookies)
user = await get_user_from_id(resp.json()["id"]) user = await get_user_from_id(resp.json()["id"])
""" """
class TestEditor:
@pytest.mark.asyncio
async def test_start(self, test_client): # noqa : F811
resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
resp = test_client.post("/api/v1/editor/start?edit=false", cookies={"access_token": token})
assert resp.status_code == 200
edit_id = resp.json()["token"]
resp = test_client.get(f"/api/v1/editor/pow?edit_id={edit_id}")
assert resp.status_code == 200
pow_data = resp.json()["data"]
resp = test_client.get("/api/v1/editor/pow?edit_id=loladdfs")
assert resp.status_code == 401
pow_res = mint(pow_data, 8, None, "", 8, False)
print("POW-Res", pow_res)
async with AsyncClient() as ac:
resp = await ac.get("https://i.imgur.com/OE22DNZ.png")
image_bytes = resp.read()
resp = test_client.post(
f"/api/v1/editor/image?edit_id={edit_id}&pow_data={pow_res}", files={"file": image_bytes}
)
assert resp.status_code == 200
ValueStorage.edit_id = edit_id
ValueStorage.image_id = resp.json()["id"]
@pytest.mark.asyncio
async def test_finish(self, test_client): # noqa : F811
local_example_quiz = example_quiz
local_example_quiz["questions"][1][
"image"
] = f"http://localhost:8080/api/v1/storage/download/{ValueStorage.image_id}"
resp = test_client.post(f"/api/v1/editor/finish?edit_id={ValueStorage.edit_id}", json=example_quiz)
assert resp.status_code == 200
class TestExImport: class TestExImport:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_export_quiz(self, test_client): # noqa : F811 async def test_export_quiz(self, test_client): # noqa : F811
resp = test_client.get("/api/v1/eximport/jgfgufgfgfzftzi") resp = test_client.get("/api/v1/eximport/jgfgufgfgfzftzi", cookies=ValueStorage.cookies)
assert resp.status_code == 422 assert resp.status_code == 422
resp = test_client.get("/api/v1/eximport/8bd77201-65ed-46fe-9160-cfe71dad501f") resp = test_client.get("/api/v1/eximport/8bd77201-65ed-46fe-9160-cfe71dad501f", cookies=ValueStorage.cookies)
assert resp.status_code == 404 assert resp.status_code == 404
resp = test_client.get(f"/api/v1/eximport/{ValueStorage.quiz_id}") resp = test_client.get(f"/api/v1/eximport/{ValueStorage.quiz_id}", cookies=ValueStorage.cookies)
assert resp.status_code == 200 assert resp.status_code == 200
exported_data = resp.content exported_data = resp.content
assert len(exported_data) > 3000 assert len(exported_data) > 3000
@@ -522,37 +406,31 @@ class TestExImport:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_import_quiz(self, test_client): # noqa : F811 async def test_import_quiz(self, test_client): # noqa : F811
resp = test_client.post("/api/v1/eximport/", files={"file": ValueStorage.exported_quiz_data}) resp = test_client.post(
"/api/v1/eximport/", files={"file": ValueStorage.exported_quiz_data}, cookies=ValueStorage.cookies
)
assert resp.status_code == 200 assert resp.status_code == 200
class TestDeleteStuff: class TestDeleteStuff:
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_delete_quiz(self, test_client): # noqa : F811 async def test_delete_quiz(self, test_client): # noqa : F811
resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
resp = test_client.delete( resp = test_client.delete(
f"/api/v1/quiz/delete/{ValueStorage.imported_quizzes[0]}", cookies={"access_token": token} f"/api/v1/quiz/delete/{ValueStorage.imported_quizzes[0]}", cookies=ValueStorage.cookies
) )
assert resp.status_code == 200 assert resp.status_code == 200
resp = test_client.delete( resp = test_client.delete(
"/api/v1/quiz/delete/be582c77-da03-4271-929c-5d582056eb78", cookies={"access_token": token} "/api/v1/quiz/delete/be582c77-da03-4271-929c-5d582056eb78", cookies=ValueStorage.cookies
) )
assert resp.status_code == 404 assert resp.status_code == 404
resp = test_client.delete( resp = test_client.delete(
"/api/v1/quiz/delete/be582c77-da03-sdaasdadsasddas4271-929c-5d582056eb78", cookies={"access_token": token} "/api/v1/quiz/delete/be582c77-da03-sdaasdadsasddas4271-929c-5d582056eb78", cookies=ValueStorage.cookies
) )
assert resp.status_code == 400 assert resp.status_code == 400
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_delete_user(self, test_client): # noqa : F811 async def test_delete_user(self, test_client): # noqa : F811
resp = test_client.post(
"/api/v1/users/token/cookie", data={"username": test_user_email, "password": test_user_password}
)
token = resp.cookies["access_token"]
data = {"password": test_user_password} data = {"password": test_user_password}
resp = test_client.delete("/api/v1/users/me", cookies={"access_token": token}, json=data) resp = test_client.delete("/api/v1/users/me", cookies=ValueStorage.cookies, json=data)
assert resp.status_code == 200 assert resp.status_code == 200
+3 -18
View File
@@ -16,14 +16,12 @@ def test_storage_init():
with pytest.raises(NotImplementedError): with pytest.raises(NotImplementedError):
Storage( Storage(
backend="asdsad", backend="asdsad",
deta_key=settings.deta_project_key,
deta_id=settings.deta_project_id,
storage_path=settings.storage_path, storage_path=settings.storage_path,
) )
with pytest.raises(ValueError): with pytest.raises(ValueError):
Storage(backend="deta", deta_key=None, deta_id=None, storage_path=None) Storage(backend="s3", base_url=None, secret_key=None, access_key=None, storage_path=None)
with pytest.raises(ValueError): with pytest.raises(ValueError):
Storage(backend="local", storage_path=None, deta_key=None, deta_id=None) Storage(backend="local", storage_path=None)
async def storage_tester(storage: Storage): async def storage_tester(storage: Storage):
@@ -39,20 +37,9 @@ async def storage_tester(storage: Storage):
assert res is None assert res is None
@pytest.mark.asyncio
async def test_deta():
storage: Storage = Storage(
backend="deta",
deta_key=settings.deta_project_key,
deta_id=settings.deta_project_id,
storage_path=settings.storage_path,
)
await storage_tester(storage)
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_local(): async def test_local():
storage: Storage = Storage(backend="local", storage_path=settings.storage_path, deta_key=None, deta_id=None) storage: Storage = Storage(backend="local", storage_path=settings.storage_path)
await storage_tester(storage) await storage_tester(storage)
@@ -64,8 +51,6 @@ async def test_minio():
secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG", secret_key="zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG",
bucket_name="classquiz", bucket_name="classquiz",
base_url="https://play.min.io", base_url="https://play.min.io",
deta_key=None,
deta_id=None,
storage_path=None, storage_path=None,
) )
await storage_tester(storage) await storage_tester(storage)