minhtrannhat 979e504c0d
Fix(backend): Updated authenticated Ctxmanager
- After Quart update, we can't test if an user is authenticated anymore
  with only `quart` but we also have to rely on
  `quart_auth.authenticated_client`. All tests passed after changes
  were made.
2023-07-16 23:25:37 -04:00

68 lines
2.5 KiB
Python

import pytest
from freezegun import freeze_time
from itsdangerous import URLSafeTimedSerializer
from quart import Quart
from quart_auth import authenticated_client
from backend.blueprints.members import EMAIL_VERIFICATION_SALT
async def test_register(app: Quart, caplog: pytest.LogCaptureFixture) -> None:
test_client = app.test_client()
data = {
"email": "new@todo.minhtrannhat.com",
"password": "testPassword2$",
}
await test_client.post("/members/", json=data)
response = await test_client.post("/sessions/", json=data)
assert response.status_code == 200
assert "Sending welcome.html to new@todo.minhtrannhat.com" in caplog.text
@pytest.mark.parametrize(
"time, expected",
[("2022-01-01", 403), (None, 200)],
)
async def test_verify_email(app: Quart, time: str | None, expected: int) -> None:
with freeze_time(time):
signer = URLSafeTimedSerializer(app.secret_key, salt=EMAIL_VERIFICATION_SALT)
token = signer.dumps(1)
test_client = app.test_client()
response = await test_client.put("/members/email/", json={"token": token})
assert response.status_code == expected
async def test_verify_email_invalid_token(app: Quart) -> None:
test_client = app.test_client()
response = await test_client.put("/members/email/", json={"token": "invalid"})
assert response.status_code == 400
async def test_change_password(app: Quart, caplog: pytest.LogCaptureFixture) -> None:
test_client = app.test_client()
data = {
"email": "new_password@todo.minhtrannhat.com",
"password": "testPassword2$",
}
response = await test_client.post("/members/", json=data)
async with authenticated_client(test_client, auth_id=2): # type: ignore
response = await test_client.put(
"/members/password/",
json={
"currentPassword": data["password"],
"newPassword": "testPassword3$",
},
)
assert response.status_code == 200
assert "Sending password_changed.html to new@todo.minhtrannhat.com" in caplog.text
async def test_forgotten_password(app: Quart, caplog: pytest.LogCaptureFixture) -> None:
test_client = app.test_client()
data = {"email": "member@todo.minhtrannhat.com"}
response = await test_client.put("/members/forgotten-password/", json=data)
assert response.status_code == 200
assert (
"Sending forgotten_password.html to member@todo.minhtrannhat.com" in caplog.text
)