mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-06-28 06:41:43 +00:00
simplify login ttl processing
This commit is contained in:
@ -13,7 +13,6 @@ from ahriman.models.package import Package
|
||||
from ahriman.models.package_description import PackageDescription
|
||||
from ahriman.models.package_source import PackageSource
|
||||
from ahriman.models.remote_source import RemoteSource
|
||||
from ahriman.models.user_identity import UserIdentity
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -149,14 +148,3 @@ def pyalpm_package_description_ahriman(package_description_ahriman: PackageDescr
|
||||
type(mock).provides = PropertyMock(return_value=package_description_ahriman.provides)
|
||||
type(mock).url = PropertyMock(return_value=package_description_ahriman.url)
|
||||
return mock
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def user_identity() -> UserIdentity:
|
||||
"""
|
||||
identity fixture
|
||||
|
||||
Returns:
|
||||
UserIdentity: user identity test instance
|
||||
"""
|
||||
return UserIdentity("username", int(time.time()) + 30)
|
||||
|
@ -1,64 +0,0 @@
|
||||
from ahriman.models.user_identity import UserIdentity
|
||||
|
||||
|
||||
def test_from_identity(user_identity: UserIdentity) -> None:
|
||||
"""
|
||||
must construct identity object from string
|
||||
"""
|
||||
identity = UserIdentity.from_identity(f"{user_identity.username} {user_identity.expire_at}")
|
||||
assert identity == user_identity
|
||||
|
||||
|
||||
def test_from_identity_expired(user_identity: UserIdentity) -> None:
|
||||
"""
|
||||
must construct None from expired identity
|
||||
"""
|
||||
user_identity = UserIdentity(username=user_identity.username, expire_at=user_identity.expire_at - 60)
|
||||
assert UserIdentity.from_identity(f"{user_identity.username} {user_identity.expire_at}") is None
|
||||
|
||||
|
||||
def test_from_identity_no_split() -> None:
|
||||
"""
|
||||
must construct None from invalid string
|
||||
"""
|
||||
assert UserIdentity.from_identity("username") is None
|
||||
|
||||
|
||||
def test_from_identity_not_int() -> None:
|
||||
"""
|
||||
must construct None from invalid timestamp
|
||||
"""
|
||||
assert UserIdentity.from_identity("username timestamp") is None
|
||||
|
||||
|
||||
def test_from_username() -> None:
|
||||
"""
|
||||
must construct identity from username
|
||||
"""
|
||||
identity = UserIdentity.from_username("username", 0)
|
||||
assert identity.username == "username"
|
||||
# we want to check timestamp too, but later
|
||||
|
||||
|
||||
def test_expire_when() -> None:
|
||||
"""
|
||||
must return correct expiration time
|
||||
"""
|
||||
assert UserIdentity.expire_when(-1) < UserIdentity.expire_when(0) < UserIdentity.expire_when(1)
|
||||
|
||||
|
||||
def test_is_expired(user_identity: UserIdentity) -> None:
|
||||
"""
|
||||
must return expired flag for expired identities
|
||||
"""
|
||||
assert not user_identity.is_expired()
|
||||
|
||||
user_identity = UserIdentity(username=user_identity.username, expire_at=user_identity.expire_at - 60)
|
||||
assert user_identity.is_expired()
|
||||
|
||||
|
||||
def test_to_identity(user_identity: UserIdentity) -> None:
|
||||
"""
|
||||
must return correct identity string
|
||||
"""
|
||||
assert user_identity == UserIdentity.from_identity(user_identity.to_identity())
|
@ -5,42 +5,28 @@ from aiohttp import web
|
||||
from aiohttp.test_utils import TestClient
|
||||
from cryptography import fernet
|
||||
from pytest_mock import MockerFixture
|
||||
from unittest.mock import AsyncMock
|
||||
from unittest.mock import AsyncMock, call as MockCall
|
||||
|
||||
from ahriman.core.auth import Auth
|
||||
from ahriman.core.configuration import Configuration
|
||||
from ahriman.models.user import User
|
||||
from ahriman.models.user_access import UserAccess
|
||||
from ahriman.models.user_identity import UserIdentity
|
||||
from ahriman.web.middlewares.auth_handler import AuthorizationPolicy, auth_handler, cookie_secret_key, setup_auth
|
||||
|
||||
|
||||
def _identity(username: str) -> str:
|
||||
"""
|
||||
generate identity from user
|
||||
|
||||
Args:
|
||||
username(str): name of the user
|
||||
|
||||
Returns:
|
||||
str: user identity string
|
||||
"""
|
||||
return f"{username} {UserIdentity.expire_when(60)}"
|
||||
|
||||
|
||||
async def test_authorized_userid(authorization_policy: AuthorizationPolicy, user: User, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
must return authorized user id
|
||||
"""
|
||||
mocker.patch("ahriman.core.database.SQLite.user_get", return_value=user)
|
||||
assert await authorization_policy.authorized_userid(_identity(user.username)) == user.username
|
||||
assert await authorization_policy.authorized_userid(user.username) == user.username
|
||||
|
||||
|
||||
async def test_authorized_userid_unknown(authorization_policy: AuthorizationPolicy, user: User) -> None:
|
||||
"""
|
||||
must not allow unknown user id for authorization
|
||||
"""
|
||||
assert await authorization_policy.authorized_userid(_identity("somerandomname")) is None
|
||||
assert await authorization_policy.authorized_userid("somerandomname") is None
|
||||
assert await authorization_policy.authorized_userid("somerandomname") is None
|
||||
|
||||
|
||||
@ -51,11 +37,13 @@ async def test_permits(authorization_policy: AuthorizationPolicy, user: User) ->
|
||||
authorization_policy.validator = AsyncMock()
|
||||
authorization_policy.validator.verify_access.side_effect = lambda username, *args: username == user.username
|
||||
|
||||
assert await authorization_policy.permits(_identity(user.username), user.access, "/endpoint")
|
||||
authorization_policy.validator.verify_access.assert_called_once_with(user.username, user.access, "/endpoint")
|
||||
assert await authorization_policy.permits(user.username, user.access, "/endpoint")
|
||||
assert not await authorization_policy.permits("somerandomname", user.access, "/endpoint")
|
||||
|
||||
assert not await authorization_policy.permits(_identity("somerandomname"), user.access, "/endpoint")
|
||||
assert not await authorization_policy.permits(user.username, user.access, "/endpoint")
|
||||
authorization_policy.validator.verify_access.assert_has_calls([
|
||||
MockCall(user.username, user.access, "/endpoint"),
|
||||
MockCall("somerandomname", user.access, "/endpoint"),
|
||||
])
|
||||
|
||||
|
||||
async def test_auth_handler_unix_socket(client_with_auth: TestClient, mocker: MockerFixture) -> None:
|
||||
|
Reference in New Issue
Block a user