Fix unit tests of authentication classes

This commit is contained in:
François Voron
2021-09-14 11:24:32 +02:00
parent dade8c263d
commit e2395998e4
4 changed files with 59 additions and 36 deletions

View File

@@ -273,22 +273,6 @@ def mock_user_db(
return MockUserDatabase(UserDB)
@pytest.fixture
def get_mock_user_db(mock_user_db):
def _get_mock_user_db():
yield mock_user_db
return _get_mock_user_db
@pytest.fixture
def get_user_manager(get_mock_user_db, validate_password):
def _get_user_manager(user_db=Depends(get_mock_user_db)):
yield UserManager(UserDB, user_db, validate_password)
return _get_user_manager
@pytest.fixture
def mock_user_db_oauth(
user_oauth,
@@ -355,6 +339,14 @@ def mock_user_db_oauth(
return MockUserDatabase(UserDBOAuth)
@pytest.fixture
def get_mock_user_db(mock_user_db):
def _get_mock_user_db():
yield mock_user_db
return _get_mock_user_db
@pytest.fixture
def get_mock_user_db_oauth(mock_user_db_oauth):
def _get_mock_user_db_oauth():
@@ -363,6 +355,19 @@ def get_mock_user_db_oauth(mock_user_db_oauth):
return _get_mock_user_db_oauth
@pytest.fixture
def user_manager(mock_user_db, validate_password):
return UserManager(UserDB, mock_user_db, validate_password)
@pytest.fixture
def get_user_manager(get_mock_user_db, validate_password):
def _get_user_manager(user_db=Depends(get_mock_user_db)):
yield UserManager(UserDB, user_db, validate_password)
return _get_user_manager
@pytest.fixture
def get_user_manager_oauth(get_mock_user_db_oauth, validate_password):
def _get_user_manager_oauth(user_db=Depends(get_mock_user_db_oauth)):

View File

@@ -12,9 +12,9 @@ def base_authentication():
@pytest.mark.authentication
class TestAuthenticate:
@pytest.mark.asyncio
async def test_not_implemented(self, base_authentication, mock_user_db):
async def test_not_implemented(self, base_authentication, user_manager):
with pytest.raises(NotImplementedError):
await base_authentication(None, mock_user_db)
await base_authentication(None, user_manager)
@pytest.mark.authentication

View File

@@ -52,37 +52,46 @@ def test_default_name(cookie_authentication: CookieAuthentication):
class TestAuthenticate:
@pytest.mark.asyncio
async def test_missing_token(
self, mock_user_db, cookie_authentication: CookieAuthentication
self, user_manager, cookie_authentication: CookieAuthentication
):
authenticated_user = await cookie_authentication(None, mock_user_db)
authenticated_user = await cookie_authentication(None, user_manager)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_invalid_token(
self, mock_user_db, cookie_authentication: CookieAuthentication
self, user_manager, cookie_authentication: CookieAuthentication
):
authenticated_user = await cookie_authentication("foo", mock_user_db)
authenticated_user = await cookie_authentication("foo", user_manager)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_valid_token_missing_user_payload(
self, mock_user_db, token, cookie_authentication: CookieAuthentication
self, user_manager, token, cookie_authentication: CookieAuthentication
):
authenticated_user = await cookie_authentication(token(), mock_user_db)
authenticated_user = await cookie_authentication(token(), user_manager)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_valid_token_invalid_uuid(
self, mock_user_db, token, cookie_authentication: CookieAuthentication
self, user_manager, token, cookie_authentication: CookieAuthentication
):
authenticated_user = await cookie_authentication(token("foo"), mock_user_db)
authenticated_user = await cookie_authentication(token("foo"), user_manager)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_valid_token_not_existing_user(
self, user_manager, token, cookie_authentication: CookieAuthentication
):
authenticated_user = await cookie_authentication(
token("d35d213e-f3d8-4f08-954a-7e0d1bea286f"), user_manager
)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_valid_token(
self, mock_user_db, token, user, cookie_authentication: CookieAuthentication
self, user_manager, token, user, cookie_authentication: CookieAuthentication
):
authenticated_user = await cookie_authentication(token(user.id), mock_user_db)
authenticated_user = await cookie_authentication(token(user.id), user_manager)
assert authenticated_user is not None
assert authenticated_user.id == user.id

View File

@@ -32,27 +32,36 @@ def test_default_name(jwt_authentication):
@pytest.mark.authentication
class TestAuthenticate:
@pytest.mark.asyncio
async def test_missing_token(self, jwt_authentication, mock_user_db):
authenticated_user = await jwt_authentication(None, mock_user_db)
async def test_missing_token(self, jwt_authentication, user_manager):
authenticated_user = await jwt_authentication(None, user_manager)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_invalid_token(self, jwt_authentication, mock_user_db):
authenticated_user = await jwt_authentication("foo", mock_user_db)
async def test_invalid_token(self, jwt_authentication, user_manager):
authenticated_user = await jwt_authentication("foo", user_manager)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_valid_token_missing_user_payload(
self, jwt_authentication, mock_user_db, token
self, jwt_authentication, user_manager, token
):
authenticated_user = await jwt_authentication(token(), mock_user_db)
authenticated_user = await jwt_authentication(token(), user_manager)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_valid_token_invalid_uuid(
self, jwt_authentication, mock_user_db, token
self, jwt_authentication, user_manager, token
):
authenticated_user = await jwt_authentication(token("foo"), mock_user_db)
authenticated_user = await jwt_authentication(token("foo"), user_manager)
assert authenticated_user is None
@pytest.mark.asyncio
async def test_valid_token_not_existing_user(
self, jwt_authentication, user_manager, token
):
authenticated_user = await jwt_authentication(
token("d35d213e-f3d8-4f08-954a-7e0d1bea286f"), user_manager
)
assert authenticated_user is None
@pytest.mark.asyncio