unifi_access/tests/live/test_access_policy.py

88 lines
2.5 KiB
Python
Raw Normal View History

from collections.abc import Generator
import pytest
from unifi_access import AccessClient
from unifi_access.schemas import AccessPolicy, HolidayGroup, Schedule, WeekSchedule
from unifi_access.schemas._base import DoorResource
@pytest.fixture
def holiday_group(live_access_client: AccessClient) -> Generator[HolidayGroup]:
_holiday_group = (
live_access_client.create_holiday_group("Test Holiday Group")
.success_or_raise()
.data
)
yield _holiday_group
live_access_client.delete_holiday_group(_holiday_group.id)
@pytest.fixture
def schedule(
live_access_client: AccessClient, holiday_group: HolidayGroup
) -> Generator[Schedule]:
_schedule = (
live_access_client.create_schedule(
"Test Schedule",
week_schedule=WeekSchedule(
sunday=[],
monday=[],
tuesday=[],
wednesday=[],
thursday=[],
friday=[],
saturday=[],
),
holiday_group_id=holiday_group.id,
)
.success_or_raise()
.data
)
yield _schedule
live_access_client.delete_schedule(_schedule.id)
@pytest.fixture
def access_policy(
live_access_client: AccessClient, schedule: Schedule
) -> Generator[AccessPolicy]:
_access_policy = (
live_access_client.create_access_policy("Test Access Policy", schedule.id)
.success_or_raise()
.data
)
yield _access_policy
live_access_client.delete_access_policy(_access_policy.id)
def test_access_policy_lifecycle(
live_access_client: AccessClient,
holiday_group: HolidayGroup,
schedule: Schedule,
access_policy: AccessPolicy,
) -> None:
assert access_policy
doors = live_access_client.fetch_all_doors().success_or_raise().data
resources = [DoorResource(id=door.id) for door in doors]
updated_access_policy = (
live_access_client.update_access_policy(access_policy.id, resources=resources)
.success_or_raise()
.data
)
assert updated_access_policy.resources == resources
updated_holiday_group = (
live_access_client.update_holiday_group(
holiday_group.id, description="Test Description"
)
.success_or_raise()
.data
)
assert updated_holiday_group.description == "Test Description"
holiday_groups = (
live_access_client.fetch_all_holiday_groups().success_or_raise().data
)
assert any(g.id == updated_holiday_group.id for g in holiday_groups)