Buckets:
AnonymousSpottedToad/candidate_bucket / codebase /python /fastapi-users_fastapi-users /tests /test_fastapi_users.py
| from collections.abc import AsyncGenerator | |
| import httpx | |
| import pytest | |
| import pytest_asyncio | |
| from fastapi import Depends, FastAPI, status | |
| from fastapi_users import FastAPIUsers | |
| from tests.conftest import IDType, User, UserCreate, UserModel, UserUpdate | |
| async def test_app_client( | |
| secret, | |
| get_user_manager, | |
| mock_authentication, | |
| oauth_client, | |
| get_test_client, | |
| ) -> AsyncGenerator[httpx.AsyncClient, None]: | |
| fastapi_users = FastAPIUsers[UserModel, IDType]( | |
| get_user_manager, [mock_authentication] | |
| ) | |
| app = FastAPI() | |
| app.include_router(fastapi_users.get_register_router(User, UserCreate)) | |
| app.include_router(fastapi_users.get_reset_password_router()) | |
| app.include_router(fastapi_users.get_auth_router(mock_authentication)) | |
| app.include_router( | |
| fastapi_users.get_oauth_router(oauth_client, mock_authentication, secret) | |
| ) | |
| app.include_router( | |
| fastapi_users.get_oauth_associate_router(oauth_client, User, secret) | |
| ) | |
| def custom_users_route(): | |
| return None | |
| app.include_router( | |
| fastapi_users.get_users_router(User, UserUpdate), prefix="/users" | |
| ) | |
| app.include_router(fastapi_users.get_verify_router(User)) | |
| def current_user(user: UserModel = Depends(fastapi_users.current_user())): | |
| return user | |
| def current_active_user( | |
| user: UserModel = Depends(fastapi_users.current_user(active=True)), | |
| ): | |
| return user | |
| def current_verified_user( | |
| user: UserModel = Depends(fastapi_users.current_user(verified=True)), | |
| ): | |
| return user | |
| def current_superuser( | |
| user: UserModel = Depends( | |
| fastapi_users.current_user(active=True, superuser=True) | |
| ), | |
| ): | |
| return user | |
| def current_verified_superuser( | |
| user: UserModel = Depends( | |
| fastapi_users.current_user(active=True, verified=True, superuser=True) | |
| ), | |
| ): | |
| return user | |
| def optional_current_user( | |
| user: UserModel | None = Depends(fastapi_users.current_user(optional=True)), | |
| ): | |
| return User.model_validate(user) if user else None | |
| def optional_current_active_user( | |
| user: UserModel | None = Depends( | |
| fastapi_users.current_user(optional=True, active=True) | |
| ), | |
| ): | |
| return User.model_validate(user) if user else None | |
| def optional_current_verified_user( | |
| user: UserModel | None = Depends( | |
| fastapi_users.current_user(optional=True, verified=True) | |
| ), | |
| ): | |
| return User.model_validate(user) if user else None | |
| def optional_current_superuser( | |
| user: UserModel | None = Depends( | |
| fastapi_users.current_user(optional=True, active=True, superuser=True) | |
| ), | |
| ): | |
| return User.model_validate(user) if user else None | |
| def optional_current_verified_superuser( | |
| user: UserModel | None = Depends( | |
| fastapi_users.current_user( | |
| optional=True, active=True, verified=True, superuser=True | |
| ) | |
| ), | |
| ): | |
| return User.model_validate(user) if user else None | |
| async for client in get_test_client(app): | |
| yield client | |
| async def test_route_exists(test_app_client: httpx.AsyncClient, path: str, method: str): | |
| response = await test_app_client.request(method, path) | |
| assert response.status_code not in ( | |
| status.HTTP_404_NOT_FOUND, | |
| status.HTTP_405_METHOD_NOT_ALLOWED, | |
| ) | |
| async def test_custom_users_route_not_catched(test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.request("DELETE", "/users/me") | |
| assert response.status_code == status.HTTP_200_OK | |
| class TestGetCurrentUser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/current-user") | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/current-user", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_valid_token( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-user", headers={"Authorization": f"Bearer {user.id}"} | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| class TestGetCurrentActiveUser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/current-active-user") | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/current-active-user", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_valid_token_inactive_user( | |
| self, test_app_client: httpx.AsyncClient, inactive_user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-active-user", | |
| headers={"Authorization": f"Bearer {inactive_user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_valid_token( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-active-user", headers={"Authorization": f"Bearer {user.id}"} | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| class TestGetCurrentVerifiedUser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/current-verified-user") | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/current-verified-user", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_valid_token_unverified_user( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-verified-user", | |
| headers={"Authorization": f"Bearer {user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_403_FORBIDDEN | |
| async def test_valid_token_verified_user( | |
| self, test_app_client: httpx.AsyncClient, verified_user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-verified-user", | |
| headers={"Authorization": f"Bearer {verified_user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| class TestGetCurrentSuperuser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/current-superuser") | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/current-superuser", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_valid_token_regular_user( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-superuser", headers={"Authorization": f"Bearer {user.id}"} | |
| ) | |
| assert response.status_code == status.HTTP_403_FORBIDDEN | |
| async def test_valid_token_superuser( | |
| self, test_app_client: httpx.AsyncClient, superuser: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-superuser", headers={"Authorization": f"Bearer {superuser.id}"} | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| class TestGetCurrentVerifiedSuperuser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/current-verified-superuser") | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/current-verified-superuser", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_401_UNAUTHORIZED | |
| async def test_valid_token_regular_user( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-verified-superuser", | |
| headers={"Authorization": f"Bearer {user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_403_FORBIDDEN | |
| async def test_valid_token_verified_user( | |
| self, test_app_client: httpx.AsyncClient, verified_user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-verified-superuser", | |
| headers={"Authorization": f"Bearer {verified_user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_403_FORBIDDEN | |
| async def test_valid_token_superuser( | |
| self, test_app_client: httpx.AsyncClient, superuser: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-verified-superuser", | |
| headers={"Authorization": f"Bearer {superuser.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_403_FORBIDDEN | |
| async def test_valid_token_verified_superuser( | |
| self, test_app_client: httpx.AsyncClient, verified_superuser: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/current-verified-superuser", | |
| headers={"Authorization": f"Bearer {verified_superuser.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| class TestOptionalGetCurrentUser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/optional-current-user") | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/optional-current-user", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-user", headers={"Authorization": f"Bearer {user.id}"} | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is not None | |
| class TestOptionalGetCurrentVerifiedUser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/optional-current-verified-user") | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/optional-current-verified-user", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_unverified_user( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-verified-user", | |
| headers={"Authorization": f"Bearer {user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_verified_user( | |
| self, test_app_client: httpx.AsyncClient, verified_user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-verified-user", | |
| headers={"Authorization": f"Bearer {verified_user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is not None | |
| class TestOptionalGetCurrentActiveUser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/optional-current-active-user") | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/optional-current-active-user", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_inactive_user( | |
| self, test_app_client: httpx.AsyncClient, inactive_user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-active-user", | |
| headers={"Authorization": f"Bearer {inactive_user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-active-user", | |
| headers={"Authorization": f"Bearer {user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is not None | |
| class TestOptionalGetCurrentSuperuser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/optional-current-superuser") | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/optional-current-superuser", headers={"Authorization": "Bearer foo"} | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_regular_user( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-superuser", | |
| headers={"Authorization": f"Bearer {user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_superuser( | |
| self, test_app_client: httpx.AsyncClient, superuser: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-superuser", | |
| headers={"Authorization": f"Bearer {superuser.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is not None | |
| class TestOptionalGetCurrentVerifiedSuperuser: | |
| async def test_missing_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get("/optional-current-verified-superuser") | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_invalid_token(self, test_app_client: httpx.AsyncClient): | |
| response = await test_app_client.get( | |
| "/optional-current-verified-superuser", | |
| headers={"Authorization": "Bearer foo"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_regular_user( | |
| self, test_app_client: httpx.AsyncClient, user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-verified-superuser", | |
| headers={"Authorization": f"Bearer {user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_verified_user( | |
| self, test_app_client: httpx.AsyncClient, verified_user: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-verified-superuser", | |
| headers={"Authorization": f"Bearer {verified_user.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_superuser( | |
| self, test_app_client: httpx.AsyncClient, superuser: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-verified-superuser", | |
| headers={"Authorization": f"Bearer {superuser.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is None | |
| async def test_valid_token_verified_superuser( | |
| self, test_app_client: httpx.AsyncClient, verified_superuser: UserModel | |
| ): | |
| response = await test_app_client.get( | |
| "/optional-current-verified-superuser", | |
| headers={"Authorization": f"Bearer {verified_superuser.id}"}, | |
| ) | |
| assert response.status_code == status.HTTP_200_OK | |
| assert response.json() is not None | |
Xet Storage Details
- Size:
- 19 kB
- Xet hash:
- df681857f6a047b23385f802ad91ba3893868e4d5384d5d439a044572b7fc169
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.