async def request_login(data: models.AuthRequest = Body(...)): user = await crud.get_user_by_email(data.email) if not user: raise HTTPException(status_code=HTTP_400_BAD_REQUEST, detail="No user with that email.") otp = security.generate_otp(data.email) await send_email(data.email, "Your One Time Password", f"Your password is {otp}") return "Please check your email for a single use password."
async def send_otp(email): otp = security.generate_otp(email) await send_email( email, "Your One Time Password", f"Your password is {otp}", from_address, from_name, )
def test_confirm_login_wrong_email_fails( test_client: TestClient, user1: dict, monkeypatch: MonkeyPatch, async_db ): monkeypatch.setattr("app.auth.crud.db", async_db) otp = security.generate_otp(user1["email"]) response = test_client.post( "/auth/confirm", json={"email": "*****@*****.**", "code": otp} ) assert response.status_code == 400 assert response.cookies.get("token") is None
def test_logged_in_get_user_info( test_client: TestClient, user1: dict, monkeypatch: MonkeyPatch, async_db ): monkeypatch.setattr("app.auth.crud.db", async_db) otp = security.generate_otp(user1["email"]) _response = test_client.post( "/auth/confirm", json={"email": user1["email"], "code": otp} ) response2 = test_client.get("/auth/me") assert response2.status_code == 200
def test_log_out( test_client: TestClient, user1: dict, monkeypatch: MonkeyPatch, async_db ): monkeypatch.setattr("app.auth.crud.db", async_db) otp = security.generate_otp(user1["email"]) response = test_client.post( "/auth/confirm", json={"email": user1["email"], "code": otp} ) assert response.cookies.get("token") is not None response2 = test_client.get("/auth/sign-out") assert response2.status_code == 200 assert response2.cookies.get("token") == '""' response3 = test_client.get("/auth/me") assert response3.status_code == 401