mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Add inactive user handling
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user