Files
fastapi-users/tests/test_authentication.py
François Voron 49deb437a6 Fix #42: multiple authentication backends (#47)
* Revamp authentication to allow multiple backends

* Make router generate a login route for each backend

* Apply black

* Remove unused imports

* Complete docstrings

* Update documentation

* WIP add cookie auth

* Complete cookie auth unit tests

* Add documentation for cookie auth

* Fix cookie backend default name

* Don't make cookie return a Response
2019-12-04 13:32:49 +01:00

46 lines
1.3 KiB
Python

from typing import Optional
import pytest
from starlette import status
from starlette.requests import Request
from fastapi_users.authentication import BaseAuthentication
from fastapi_users.db import BaseUserDatabase
from fastapi_users.models import BaseUserDB
@pytest.fixture()
def auth_backend_none():
class BackendNone(BaseAuthentication):
async def __call__(
self, request: Request, user_db: BaseUserDatabase
) -> Optional[BaseUserDB]:
return None
return BackendNone()
@pytest.fixture()
def auth_backend_user(user):
class BackendUser(BaseAuthentication):
async def __call__(
self, request: Request, user_db: BaseUserDatabase
) -> Optional[BaseUserDB]:
return user
return BackendUser()
@pytest.mark.authentication
def test_authenticator(get_test_auth_client, auth_backend_none, auth_backend_user):
client = get_test_auth_client([auth_backend_none, auth_backend_user])
response = client.get("/test-current-user")
assert response.status_code == status.HTTP_200_OK
@pytest.mark.authentication
def test_authenticator_none(get_test_auth_client, auth_backend_none):
client = get_test_auth_client([auth_backend_none, auth_backend_none])
response = client.get("/test-current-user")
assert response.status_code == status.HTTP_401_UNAUTHORIZED