Files
fastapi-users/tests/test_router.py
2019-10-06 08:53:13 +02:00

57 lines
1.5 KiB
Python

import pytest
from starlette import status
from starlette.testclient import TestClient
from fastapi_users.db import UserDBInterface
from fastapi_users.models import UserDB
class MockUserDBInterface(UserDBInterface):
async def create(self, user: UserDB) -> UserDB:
return user
@pytest.fixture
def test_app_client() -> TestClient:
from fastapi import FastAPI
from fastapi_users.router import UserRouter
userRouter = UserRouter(MockUserDBInterface())
app = FastAPI()
app.include_router(userRouter)
return TestClient(app)
def test_register_empty_body(test_app_client: TestClient):
response = test_app_client.post('/register', json={})
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_register_missing_password(test_app_client: TestClient):
json = {
'email': 'king.arthur@camelot.bt',
}
response = test_app_client.post('/register', json=json)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_register_wrong_email(test_app_client: TestClient):
json = {
'email': 'king.arthur',
'password': 'guinevere',
}
response = test_app_client.post('/register', json=json)
assert response.status_code == status.HTTP_422_UNPROCESSABLE_ENTITY
def test_register_valid_body(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_200_OK