mirror of
https://github.com/arcan1s/ahriman.git
synced 2025-06-28 06:41:43 +00:00
expiration on server side support (#33)
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
import pytest
|
||||
import time
|
||||
|
||||
from unittest.mock import MagicMock, PropertyMock
|
||||
|
||||
@ -8,6 +9,7 @@ from ahriman.models.counters import Counters
|
||||
from ahriman.models.internal_status import InternalStatus
|
||||
from ahriman.models.package import Package
|
||||
from ahriman.models.package_description import PackageDescription
|
||||
from ahriman.models.user_identity import UserIdentity
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
@ -104,3 +106,12 @@ 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
|
||||
:return: user identity test instance
|
||||
"""
|
||||
return UserIdentity("username", int(time.time()) + 30)
|
||||
|
64
tests/ahriman/models/test_user_identity.py
Normal file
64
tests/ahriman/models/test_user_identity.py
Normal file
@ -0,0 +1,64 @@
|
||||
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.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.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())
|
@ -7,15 +7,26 @@ from unittest.mock import AsyncMock
|
||||
from ahriman.core.auth.auth import Auth
|
||||
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 auth_handler, AuthorizationPolicy, setup_auth
|
||||
|
||||
|
||||
def _identity(username: str) -> str:
|
||||
"""
|
||||
generate identity from user
|
||||
:param user: user fixture object
|
||||
:return: user identity string
|
||||
"""
|
||||
return f"{username} {UserIdentity.expire_when(60)}"
|
||||
|
||||
|
||||
async def test_authorized_userid(authorization_policy: AuthorizationPolicy, user: User) -> None:
|
||||
"""
|
||||
must return authorized user id
|
||||
"""
|
||||
assert await authorization_policy.authorized_userid(user.username) == user.username
|
||||
assert await authorization_policy.authorized_userid("some random name") is None
|
||||
assert await authorization_policy.authorized_userid(_identity(user.username)) == user.username
|
||||
assert await authorization_policy.authorized_userid(_identity("somerandomname")) is None
|
||||
assert await authorization_policy.authorized_userid("somerandomname") is None
|
||||
|
||||
|
||||
async def test_permits(authorization_policy: AuthorizationPolicy, user: User) -> None:
|
||||
@ -23,11 +34,14 @@ async def test_permits(authorization_policy: AuthorizationPolicy, user: User) ->
|
||||
must call validator check
|
||||
"""
|
||||
authorization_policy.validator = AsyncMock()
|
||||
authorization_policy.validator.verify_access.return_value = True
|
||||
authorization_policy.validator.verify_access.side_effect = lambda username, *args: username == user.username
|
||||
|
||||
assert await authorization_policy.permits(user.username, user.access, "/endpoint")
|
||||
assert await authorization_policy.permits(_identity(user.username), user.access, "/endpoint")
|
||||
authorization_policy.validator.verify_access.assert_called_with(user.username, 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")
|
||||
|
||||
|
||||
async def test_auth_handler_api(auth: Auth, mocker: MockerFixture) -> None:
|
||||
"""
|
||||
|
@ -45,7 +45,8 @@ async def test_get(client_with_auth: TestClient, mocker: MockerFixture) -> None:
|
||||
oauth = client_with_auth.app["validator"] = MagicMock(spec=OAuth)
|
||||
oauth.get_oauth_username.return_value = "user"
|
||||
oauth.known_username.return_value = True
|
||||
oauth.enabled = False # lol
|
||||
oauth.enabled = False # lol\
|
||||
oauth.max_age = 60
|
||||
remember_mock = mocker.patch("aiohttp_security.remember")
|
||||
|
||||
get_response = await client_with_auth.get("/user-api/v1/login", params={"code": "code"})
|
||||
@ -62,6 +63,7 @@ async def test_get_unauthorized(client_with_auth: TestClient, mocker: MockerFixt
|
||||
"""
|
||||
oauth = client_with_auth.app["validator"] = MagicMock(spec=OAuth)
|
||||
oauth.known_username.return_value = False
|
||||
oauth.max_age = 60
|
||||
remember_mock = mocker.patch("aiohttp_security.remember")
|
||||
|
||||
get_response = await client_with_auth.get("/user-api/v1/login", params={"code": "code"})
|
||||
|
Reference in New Issue
Block a user