mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Implement variant of dep injections to get active/super user
This commit is contained in:
@@ -20,8 +20,16 @@ def test_app_client(fastapi_users: FastAPIUsers) -> TestClient:
|
||||
app = FastAPI()
|
||||
app.include_router(fastapi_users.router)
|
||||
|
||||
@app.get("/authenticated")
|
||||
def authenticated(user=Depends(fastapi_users.get_current_user)):
|
||||
@app.get("/current-user")
|
||||
def current_user(user=Depends(fastapi_users.get_current_user)):
|
||||
return user
|
||||
|
||||
@app.get("/current-active-user")
|
||||
def current_active_user(user=Depends(fastapi_users.get_current_active_user)):
|
||||
return user
|
||||
|
||||
@app.get("/current-superuser")
|
||||
def current_superuser(user=Depends(fastapi_users.get_current_superuser)):
|
||||
return user
|
||||
|
||||
return TestClient(app)
|
||||
@@ -38,17 +46,72 @@ class TestRouter:
|
||||
|
||||
class TestGetCurrentUser:
|
||||
def test_missing_token(self, test_app_client: TestClient):
|
||||
response = test_app_client.get("/authenticated")
|
||||
response = test_app_client.get("/current-user")
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
def test_invalid_token(self, test_app_client: TestClient):
|
||||
response = test_app_client.get(
|
||||
"/authenticated", headers={"Authorization": "Bearer foo"}
|
||||
"/current-user", headers={"Authorization": "Bearer foo"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
def test_valid_token(self, test_app_client: TestClient, user: BaseUserDB):
|
||||
response = test_app_client.get(
|
||||
"/authenticated", headers={"Authorization": f"Bearer {user.id}"}
|
||||
"/current-user", headers={"Authorization": f"Bearer {user.id}"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
|
||||
class TestGetCurrentActiveUser:
|
||||
def test_missing_token(self, test_app_client: TestClient):
|
||||
response = test_app_client.get("/current-active-user")
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
def test_invalid_token(self, test_app_client: TestClient):
|
||||
response = test_app_client.get(
|
||||
"/current-active-user", headers={"Authorization": "Bearer foo"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
def test_valid_token_inactive_user(
|
||||
self, test_app_client: TestClient, inactive_user: BaseUserDB
|
||||
):
|
||||
response = test_app_client.get(
|
||||
"/current-active-user",
|
||||
headers={"Authorization": f"Bearer {inactive_user.id}"},
|
||||
)
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
def test_valid_token(self, test_app_client: TestClient, user: BaseUserDB):
|
||||
response = test_app_client.get(
|
||||
"/current-active-user", headers={"Authorization": f"Bearer {user.id}"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
|
||||
class TestGetCurrentSuperuser:
|
||||
def test_missing_token(self, test_app_client: TestClient):
|
||||
response = test_app_client.get("/current-superuser")
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
def test_invalid_token(self, test_app_client: TestClient):
|
||||
response = test_app_client.get(
|
||||
"/current-superuser", headers={"Authorization": "Bearer foo"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_401_UNAUTHORIZED
|
||||
|
||||
def test_valid_token_regular_user(
|
||||
self, test_app_client: TestClient, user: BaseUserDB
|
||||
):
|
||||
response = test_app_client.get(
|
||||
"/current-superuser", headers={"Authorization": f"Bearer {user.id}"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_403_FORBIDDEN
|
||||
|
||||
def test_valid_token_superuser(
|
||||
self, test_app_client: TestClient, superuser: BaseUserDB
|
||||
):
|
||||
response = test_app_client.get(
|
||||
"/current-superuser", headers={"Authorization": f"Bearer {superuser.id}"}
|
||||
)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
Reference in New Issue
Block a user