Fix #18: check for existing user on registration

This commit is contained in:
François Voron
2019-10-19 18:31:08 +02:00
parent a5618399a1
commit a4171f8bea
3 changed files with 14 additions and 1 deletions

View File

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

View File

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

View File

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