import time from io import BytesIO from fastapi.testclient import TestClient from PIL import Image from app import mfa from app.main import app from tests.test_v5 import H, user def login(email, password, code=None): c = TestClient(app) c.headers.update(H) body = {"email": email, "password": password} if code is not None: body["code"] = code return c, c.post("/api/auth/login", json=body) def now_code(secret, offset=0): return mfa.code_at(secret, int(time.time() // mfa.STEP) + offset) def test_rfc6238_vectors(): import base64 s = base64.b32encode(b"12345678901234567890").decode().rstrip("=") assert mfa.code_at(s, 59 // 30) == "287082" and mfa.code_at(s, 1111111109 // 30) == "081804" def test_mfa_set_up_sign_in_recovery_and_reset(client): ana = user(client, "ana@t.local", "editor") setup = ana.post("/api/auth/mfa/setup").json() assert setup["uri"].startswith("otpauth://totp/") and "