Files
fastapi-users/tests/test_authentication.py
dependabot-preview[bot] 639217bb08 Make unit tests full async (#160)
* Bump pytest-asyncio from 0.10.0 to 0.11.0

Bumps [pytest-asyncio](https://github.com/pytest-dev/pytest-asyncio) from 0.10.0 to 0.11.0.
- [Release notes](https://github.com/pytest-dev/pytest-asyncio/releases)
- [Commits](https://github.com/pytest-dev/pytest-asyncio/compare/v0.10.0...v0.11.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Bump pytest-asyncio from 0.10.0 to 0.11.0

Bumps [pytest-asyncio](https://github.com/pytest-dev/pytest-asyncio) from 0.10.0 to 0.11.0.
- [Release notes](https://github.com/pytest-dev/pytest-asyncio/releases)
- [Commits](https://github.com/pytest-dev/pytest-asyncio/compare/v0.10.0...v0.11.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Convert tests to full async

* Change deprecated yield_fixture

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: François Voron <fvoron@gmail.com>
2020-04-25 11:34:11 +02:00

50 lines
1.4 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
@pytest.mark.asyncio
async def test_authenticator(
get_test_auth_client, auth_backend_none, auth_backend_user
):
client = await get_test_auth_client([auth_backend_none, auth_backend_user])
response = await client.get("/test-current-user")
assert response.status_code == status.HTTP_200_OK
@pytest.mark.authentication
@pytest.mark.asyncio
async def test_authenticator_none(get_test_auth_client, auth_backend_none):
client = await get_test_auth_client([auth_backend_none, auth_backend_none])
response = await client.get("/test-current-user")
assert response.status_code == status.HTTP_401_UNAUTHORIZED