mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2025-08-17 04:26:47 +08:00
Fix #18: check for existing user on registration
This commit is contained in:
@ -26,6 +26,9 @@ Register a new user.
|
||||
|
||||
!!! fail "`422 Validation Error`"
|
||||
|
||||
!!! fail "`400 Bad Request`"
|
||||
A user already exists with this email.
|
||||
|
||||
## `POST /login`
|
||||
|
||||
Login a user.
|
||||
|
@ -34,6 +34,11 @@ def get_user_router(
|
||||
|
||||
@router.post("/register", response_model=models.User)
|
||||
async def register(user: models.UserCreate): # type: ignore
|
||||
existing_user = await user_db.get_by_email(user.email)
|
||||
|
||||
if existing_user is not None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
hashed_password = get_password_hash(user.password)
|
||||
db_user = models.UserDB(**user.dict(), hashed_password=hashed_password)
|
||||
created_user = await user_db.create(db_user)
|
||||
|
@ -76,9 +76,14 @@ class TestRegister:
|
||||
response = test_app_client.post("/register", json=json)
|
||||
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
|
||||
|
||||
def test_valid_body(self, test_app_client: TestClient):
|
||||
def test_existing_user(self, test_app_client: TestClient):
|
||||
json = {"email": "king.arthur@camelot.bt", "password": "guinevere"}
|
||||
response = test_app_client.post("/register", json=json)
|
||||
assert response.status_code == status.HTTP_400_BAD_REQUEST
|
||||
|
||||
def test_valid_body(self, test_app_client: TestClient):
|
||||
json = {"email": "lancelot@camelot.bt", "password": "guinevere"}
|
||||
response = test_app_client.post("/register", json=json)
|
||||
assert response.status_code == status.HTTP_200_OK
|
||||
|
||||
response_json = response.json()
|
||||
|
Reference in New Issue
Block a user