diff --git a/fastapi_users/router.py b/fastapi_users/router.py index 1c74159c..0fc332df 100644 --- a/fastapi_users/router.py +++ b/fastapi_users/router.py @@ -24,6 +24,8 @@ class UserRouter: if user is None: raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) + elif not user.is_active: + raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) return user diff --git a/tests/conftest.py b/tests/conftest.py index a7429626..52995da6 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -4,22 +4,30 @@ from fastapi_users.db import UserDBInterface from fastapi_users.models import UserDB from fastapi_users.password import get_password_hash -_user = UserDB( +active_user = UserDB( email='king.arthur@camelot.bt', hashed_password=get_password_hash('guinevere'), ) +inactive_user = UserDB( + email='percival@camelot.bt', + hashed_password=get_password_hash('angharad'), + is_active=False +) + @pytest.fixture def user() -> UserDB: - return _user + return active_user class MockUserDBInterface(UserDBInterface): async def get_by_email(self, email: str) -> UserDB: - if email == _user.email: - return _user + if email == active_user.email: + return active_user + elif email == inactive_user.email: + return inactive_user return None async def create(self, user: UserDB) -> UserDB: diff --git a/tests/test_router.py b/tests/test_router.py index 87c1e97d..19d938b5 100644 --- a/tests/test_router.py +++ b/tests/test_router.py @@ -89,3 +89,11 @@ class TestLogin: } response = test_app_client.post('/login', json=json) assert response.status_code == status.HTTP_200_OK + + def test_inactive_user(self, test_app_client: TestClient): + json = { + 'email': 'percival@camelot.bt', + 'password': 'angharad', + } + response = test_app_client.post('/login', json=json) + assert response.status_code == status.HTTP_400_BAD_REQUEST