Finished test for user-router

This commit is contained in:
Mawoka
2022-04-16 17:56:44 +02:00
parent 78df99d837
commit a561537f05
5 changed files with 48 additions and 6 deletions
+2 -2
View File
@@ -205,11 +205,11 @@ class ForgotPassword(BaseModel):
@router.post("/forgot-password") @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() user = await User.objects.filter(email=forgot_password.email, verified=True).get_or_none()
if user is None: if user is None:
raise HTTPException(status_code=404, detail="User not found") 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"} return {"message": "Password reset email sent"}
-1
View File
@@ -12,5 +12,4 @@ test_url = (
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_download_image(): async def test_download_image():
image = await _download_image(test_url) image = await _download_image(test_url)
print(image)
assert image == ddg_robots_txt assert image == ddg_robots_txt
-1
View File
@@ -6,7 +6,6 @@ from classquiz.kahoot_importer.search import search
@pytest.mark.asyncio @pytest.mark.asyncio
async def test_search(): async def test_search():
res = await search(query="Python", limit=100) res = await search(query="Python", limit=100)
print(res)
assert len(res.entities) == 100 assert len(res.entities) == 100
await search(query="Test Quiz", limit=100) await search(query="Test Quiz", limit=100)
await search(query="Biologie", limit=100) await search(query="Biologie", limit=100)
+45 -1
View File
@@ -1,9 +1,11 @@
import uuid
from typing import Generator from typing import Generator
import pytest import pytest
from redis import Redis
from fastapi.testclient import TestClient from fastapi.testclient import TestClient
from classquiz.db import database, models from classquiz.db import database, models
from classquiz.config import settings
from classquiz import app 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}) resp = test_client.get("/api/v1/users/sessions/list", cookies={"access_token": token})
assert resp.status_code == 200 assert resp.status_code == 200
assert len(resp.json()) >= 1 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
+1 -1
View File
@@ -1,5 +1,5 @@
docker run --rm -d -p 6379:6379 --name test_redis redis:alpine docker run --rm -d -p 6379:6379 --name test_redis redis:alpine
python3 init_db.py 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 rm classquiz.db
docker container stop test_redis docker container stop test_redis