fix(task7): invalidate tokens on password change or reset
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
import pytest
|
||||
from app.models import User
|
||||
from app.seed.seed import seed_database
|
||||
|
||||
|
||||
@pytest.fixture(scope="module", autouse=True)
|
||||
def seeded_task7_db(db, tmp_path_factory):
|
||||
upload_dir = tmp_path_factory.mktemp("task7_test_uploads")
|
||||
seed_database(db, upload_dir=upload_dir)
|
||||
return upload_dir
|
||||
|
||||
|
||||
def test_token_invalidation_on_change_password_and_admin_reset(client, db):
|
||||
# 1. Login as user and save the initial token
|
||||
user_email = "bio@poona.demo"
|
||||
login_res = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": user_email, "password": "Demo@123"},
|
||||
)
|
||||
assert login_res.status_code == 200
|
||||
old_token = login_res.json()["access_token"]
|
||||
|
||||
# Verify initial token works
|
||||
me_res1 = client.get(
|
||||
"/api/auth/me",
|
||||
headers={"Authorization": f"Bearer {old_token}"},
|
||||
)
|
||||
assert me_res1.status_code == 200
|
||||
assert me_res1.json()["email"] == user_email
|
||||
|
||||
# 2. Call POST /api/auth/change-password
|
||||
change_res = client.post(
|
||||
"/api/auth/change-password",
|
||||
headers={"Authorization": f"Bearer {old_token}"},
|
||||
json={"current_password": "Demo@123", "new_password": "NewSecurePass99"},
|
||||
)
|
||||
assert change_res.status_code == 200, change_res.text
|
||||
change_data = change_res.json()
|
||||
assert "access_token" in change_data
|
||||
assert change_data["token_type"] == "bearer"
|
||||
new_token = change_data["access_token"]
|
||||
|
||||
# 3. Assert the old token now gets 401 on an authenticated endpoint
|
||||
old_token_res = client.get(
|
||||
"/api/auth/me",
|
||||
headers={"Authorization": f"Bearer {old_token}"},
|
||||
)
|
||||
assert old_token_res.status_code == 401
|
||||
assert "Token has been invalidated" in old_token_res.json()["detail"]
|
||||
|
||||
# 4. Assert the new token returned by change-password works
|
||||
new_token_res = client.get(
|
||||
"/api/auth/me",
|
||||
headers={"Authorization": f"Bearer {new_token}"},
|
||||
)
|
||||
assert new_token_res.status_code == 200
|
||||
assert new_token_res.json()["email"] == user_email
|
||||
|
||||
# 5. Login as admin, reset that user's password
|
||||
admin_login = client.post(
|
||||
"/api/auth/login",
|
||||
json={"email": "admin@poona.demo", "password": "Demo@123"},
|
||||
)
|
||||
assert admin_login.status_code == 200
|
||||
admin_token = admin_login.json()["access_token"]
|
||||
|
||||
target_user = db.query(User).filter(User.email == user_email).first()
|
||||
assert target_user is not None
|
||||
|
||||
reset_res = client.post(
|
||||
f"/api/users/{target_user.id}/reset-password",
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
json={"new_password": "AdminResetPass88"},
|
||||
)
|
||||
assert reset_res.status_code == 200, reset_res.text
|
||||
|
||||
# 6. Assert the user's token now gets 401
|
||||
user_after_reset_res = client.get(
|
||||
"/api/auth/me",
|
||||
headers={"Authorization": f"Bearer {new_token}"},
|
||||
)
|
||||
assert user_after_reset_res.status_code == 401
|
||||
assert "Token has been invalidated" in user_after_reset_res.json()["detail"]
|
||||
|
||||
# 7. Assert the admin's own token is still valid
|
||||
admin_me_res = client.get(
|
||||
"/api/auth/me",
|
||||
headers={"Authorization": f"Bearer {admin_token}"},
|
||||
)
|
||||
assert admin_me_res.status_code == 200
|
||||
assert admin_me_res.json()["role"] == "system_admin"
|
||||
Reference in New Issue
Block a user