From a561537f0547a08f7c3a22106a1083a1b5d556b5 Mon Sep 17 00:00:00 2001 From: Mawoka Date: Sat, 16 Apr 2022 17:56:44 +0200 Subject: [PATCH] :white_check_mark: Finished test for user-router --- classquiz/routers/users.py | 4 +-- classquiz/tests/test_kahoot_import.py | 1 - classquiz/tests/test_kahoot_search.py | 1 - classquiz/tests/test_users.py | 46 ++++++++++++++++++++++++++- run_tests.sh | 2 +- 5 files changed, 48 insertions(+), 6 deletions(-) diff --git a/classquiz/routers/users.py b/classquiz/routers/users.py index ac5bc4b..507d045 100644 --- a/classquiz/routers/users.py +++ b/classquiz/routers/users.py @@ -205,11 +205,11 @@ class ForgotPassword(BaseModel): @router.post("/forgot-password") -async def forgotten_password(forgot_password: ForgotPassword): +async def forgotten_password(forgot_password: ForgotPassword, background_task: BackgroundTasks): user = await User.objects.filter(email=forgot_password.email, verified=True).get_or_none() if user is None: raise HTTPException(status_code=404, detail="User not found") - await send_forgotten_password_email(user.email) + background_task.add_task(send_forgotten_password_email, email=user.email) return {"message": "Password reset email sent"} diff --git a/classquiz/tests/test_kahoot_import.py b/classquiz/tests/test_kahoot_import.py index ddeaf2a..0bdb3a8 100644 --- a/classquiz/tests/test_kahoot_import.py +++ b/classquiz/tests/test_kahoot_import.py @@ -12,5 +12,4 @@ test_url = ( @pytest.mark.asyncio async def test_download_image(): image = await _download_image(test_url) - print(image) assert image == ddg_robots_txt diff --git a/classquiz/tests/test_kahoot_search.py b/classquiz/tests/test_kahoot_search.py index 7900ed7..ad78018 100644 --- a/classquiz/tests/test_kahoot_search.py +++ b/classquiz/tests/test_kahoot_search.py @@ -6,7 +6,6 @@ from classquiz.kahoot_importer.search import search @pytest.mark.asyncio async def test_search(): res = await search(query="Python", limit=100) - print(res) assert len(res.entities) == 100 await search(query="Test Quiz", limit=100) await search(query="Biologie", limit=100) diff --git a/classquiz/tests/test_users.py b/classquiz/tests/test_users.py index ba17c87..d18ae12 100644 --- a/classquiz/tests/test_users.py +++ b/classquiz/tests/test_users.py @@ -1,9 +1,11 @@ +import uuid from typing import Generator import pytest +from redis import Redis from fastapi.testclient import TestClient from classquiz.db import database, models - +from classquiz.config import settings from classquiz import app @@ -194,3 +196,45 @@ async def test_list_sessions(test_client): resp = test_client.get("/api/v1/users/sessions/list", cookies={"access_token": token}) assert resp.status_code == 200 assert len(resp.json()) >= 1 + + +@pytest.mark.asyncio +async def test_reset_password_with_token(test_client): + resp = test_client.post( + "/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.set("reset_passwd:_1token_", str(me["id"])) + redis.set("reset_passwd:_2token_", str(uuid.uuid4())) + # test wtith wrong token + resp = test_client.post("/api/v1/users/reset-password", json={"token": "doesnt_exist", "password": "new_password"}) + assert resp.status_code == 400 + resp = test_client.post("/api/v1/users/reset-password", json={"token": "_2token_", "password": "new_password"}) + assert resp.status_code == 400 + resp = test_client.post("/api/v1/users/reset-password", json={"token": "_1token_", "password": "new_password"}) + assert resp.status_code == 200 + resp = test_client.post( + "/api/v1/users/token/cookie", data={"username": test_user_email, "password": "new_password"} + ) + token = resp.cookies["access_token"] + test_client.put( + "/api/v1/users/password/update", + json={"new_password": test_user_password, "old_password": "new_password"}, + cookies={"access_token": f"Bearer {token}"}, + ) + + +@pytest.mark.asyncio +async def test_forgotten_password(test_client): + resp = test_client.post("/api/v1/users/forgot-password", json={"email": test_user_email}) + assert resp.status_code == 200 + resp = test_client.post("/api/v1/users/forgot-password", json={"email": "ddassad@dsa.ads"}) + assert resp.status_code == 404 + + +@pytest.mark.asyncio +async def test_signout_everywhere(test_client): + resp = test_client.delete("/api/v1/users/signout-everywhere") + assert resp.status_code == 200 diff --git a/run_tests.sh b/run_tests.sh index 6d068b1..002a4ac 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -1,5 +1,5 @@ docker run --rm -d -p 6379:6379 --name test_redis redis:alpine python3 init_db.py -coverage run -m pytest -s -v --asyncio-mode=strict --cache-clear classquiz/tests +coverage run -m pytest -s -v --asyncio-mode=strict classquiz/tests/test_users.py rm classquiz.db docker container stop test_redis