From 55eb864f77c761dda0283c0875d6672dedece39b Mon Sep 17 00:00:00 2001 From: Xevion Date: Sun, 10 Nov 2024 00:31:52 -0600 Subject: [PATCH] fixup test_auth_login, add expiry assertions --- backend/linkpulse/tests/test_auth.py | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/backend/linkpulse/tests/test_auth.py b/backend/linkpulse/tests/test_auth.py index f7c898f..fba3b15 100644 --- a/backend/linkpulse/tests/test_auth.py +++ b/backend/linkpulse/tests/test_auth.py @@ -1,18 +1,44 @@ +from datetime import datetime, timedelta +import structlog from fastapi import status from fastapi.testclient import TestClient from linkpulse.app import app from linkpulse.tests.test_user import user +from linkpulse.utilities import utc_now + +import pytest + +logger = structlog.get_logger() def test_auth_login(user): args = {"email": user.email, "password": "password"} with TestClient(app) as client: + + def test_expiry(response, expected): + expiry = datetime.fromisoformat(response.json()["expiry"]) + relative_expiry_days = (expiry - utc_now()).total_seconds() / timedelta(days=1).total_seconds() + assert relative_expiry_days == pytest.approx(expected, rel=1e-5) + + # Remember Me, default False response = client.post("/api/login", json=args) assert response.status_code == status.HTTP_200_OK + test_expiry(response, 0.5) + # Remember Me, True + response = client.post("/api/login", json={**args, "remember_me": True}) + assert response.status_code == status.HTTP_200_OK + test_expiry(response, 14) + + # Invalid Email response = client.post("/api/login", json={**args, "email": "invalid_email"}) assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY - response = client.post("/api/login", json={**args, "password": "invalid_password"}) + # Wrong Email + response = client.post("/api/login", json={**args, "email": "bad@email.com"}) + assert response.status_code == status.HTTP_401_UNAUTHORIZED + + # Wrong Password + response = client.post("/api/login", json={**args, "password": "bad_password"}) assert response.status_code == status.HTTP_401_UNAUTHORIZED