Files
celtic-life-science/tests/e2e/test_user_credentials.py
Shubhankar45 8fdd20b29f Production
2026-09-22 06:23:46 +00:00

79 lines
4.9 KiB
Python

"""An Administrator can change any user's sign-in credentials; nobody else can."""
from tests.conftest import DEFAULT_PASSWORD
API = "/api/v1"
NEW_PASSWORD = "Brand-New-Pass-42"
async def _login(client, email, password):
return await client.post(f"{API}/auth/login", json={"email": email, "password": password})
class TestAdminChangesCredentials:
async def test_password_reset_replaces_the_old_password(self, client, auth, org):
resp = await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"password": NEW_PASSWORD})
assert resp.status_code == 200, resp.text
assert "password" not in resp.json() and "password_hash" not in resp.json()
assert (await _login(client, org["mr1"].email, DEFAULT_PASSWORD)).status_code == 401
assert (await _login(client, org["mr1"].email, NEW_PASSWORD)).status_code == 200
async def test_password_reset_signs_the_user_out_everywhere(self, client, org, auth):
first = await _login(client, org["mr1"].email, DEFAULT_PASSWORD)
old_refresh = first.json()["refresh_token"]
await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"password": NEW_PASSWORD})
again = await client.post(f"{API}/auth/refresh", json={"refresh_token": old_refresh})
assert again.status_code == 401
async def test_password_reset_unlocks_a_locked_account(self, client, auth, org, db):
org["mr1"].failed_login_attempts = 99
await db.commit()
assert (await _login(client, org["mr1"].email, DEFAULT_PASSWORD)).status_code == 401
await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"password": NEW_PASSWORD})
assert (await _login(client, org["mr1"].email, NEW_PASSWORD)).status_code == 200
async def test_email_change_moves_the_login(self, client, auth, org):
resp = await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"email": "Ravi.New@Acmepharma.in"})
assert resp.status_code == 200, resp.text
assert resp.json()["email"] == "ravi.new@acmepharma.in" # stored lower-case, as at creation
assert (await _login(client, org["mr1"].email, DEFAULT_PASSWORD)).status_code == 401
assert (await _login(client, "ravi.new@acmepharma.in", DEFAULT_PASSWORD)).status_code == 200
async def test_employee_code_and_phone_can_change(self, client, auth, org):
resp = await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"employee_code": "MR777", "phone": "9000000001"})
assert resp.status_code == 200, resp.text
assert resp.json()["employee_code"] == "MR777"
async def test_duplicate_email_or_code_is_a_clean_409(self, client, auth, org):
dup_email = await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"email": org["mr2"].email})
assert dup_email.status_code == 409, dup_email.text
dup_code = await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"employee_code": org["mr2"].employee_code})
assert dup_code.status_code == 409, dup_code.text
# ...and the user is untouched afterwards.
assert (await _login(client, org["mr1"].email, DEFAULT_PASSWORD)).status_code == 200
async def test_weak_password_and_bad_email_are_rejected(self, client, auth, org):
assert (await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"password": "short"})).status_code == 422
assert (await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"email": "not-an-email"})).status_code == 422
async def test_only_the_admin_can_change_credentials(self, client, auth, org):
for who in ("gm", "am1", "mr1", "mr2"):
resp = await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth[who], json={"password": NEW_PASSWORD})
assert resp.status_code == 403, (who, resp.text)
async def test_the_password_never_reaches_the_audit_log(self, client, auth, org, db):
from sqlalchemy import select
from app.audit.models import AuditLog
await client.patch(f"{API}/users/{org['mr1'].id}", headers=auth["admin"], json={"password": NEW_PASSWORD})
rows = (await db.execute(select(AuditLog).where(AuditLog.entity_id == org["mr1"].id))).scalars().all()
blob = " ".join(f"{r.before_data} {r.after_data}" for r in rows)
assert NEW_PASSWORD not in blob and "password_hash" not in blob
assert any(r.after_data and r.after_data.get("password_reset") for r in rows)
async def test_admin_can_reset_their_own_password(self, client, auth, org):
resp = await client.patch(f"{API}/users/{org['admin'].id}", headers=auth["admin"], json={"password": NEW_PASSWORD})
assert resp.status_code == 200, resp.text
assert (await _login(client, org["admin"].email, NEW_PASSWORD)).status_code == 200