Add inactive user handling

This commit is contained in:
François Voron
2019-10-06 13:23:08 +02:00
parent a81b438ec9
commit 4ff6e2ad2b
3 changed files with 22 additions and 4 deletions

View File

@@ -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

View File

@@ -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:

View File

@@ -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