2024-11-16 00:26:00 -05:00
|
|
|
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]:
|
2024-11-16 01:38:07 -05:00
|
|
|
_holiday_group = live_access_client.create_holiday_group("Test Holiday Group")
|
2024-11-16 00:26:00 -05:00
|
|
|
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]:
|
2024-11-16 01:38:07 -05:00
|
|
|
_schedule = live_access_client.create_schedule(
|
|
|
|
"Test Schedule",
|
|
|
|
week_schedule=WeekSchedule(
|
|
|
|
sunday=[],
|
|
|
|
monday=[],
|
|
|
|
tuesday=[],
|
|
|
|
wednesday=[],
|
|
|
|
thursday=[],
|
|
|
|
friday=[],
|
|
|
|
saturday=[],
|
|
|
|
),
|
|
|
|
holiday_group_id=holiday_group.id,
|
2024-11-16 00:26:00 -05:00
|
|
|
)
|
|
|
|
yield _schedule
|
|
|
|
live_access_client.delete_schedule(_schedule.id)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def access_policy(
|
|
|
|
live_access_client: AccessClient, schedule: Schedule
|
|
|
|
) -> Generator[AccessPolicy]:
|
2024-11-16 01:38:07 -05:00
|
|
|
_access_policy = live_access_client.create_access_policy(
|
|
|
|
"Test Access Policy", schedule.id
|
2024-11-16 00:26:00 -05:00
|
|
|
)
|
|
|
|
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
|
2024-11-16 01:38:07 -05:00
|
|
|
doors = live_access_client.fetch_all_doors()
|
2024-11-16 00:26:00 -05:00
|
|
|
resources = [DoorResource(id=door.id) for door in doors]
|
2024-11-16 01:38:07 -05:00
|
|
|
updated_access_policy = live_access_client.update_access_policy(
|
|
|
|
access_policy.id, resources=resources
|
2024-11-16 00:26:00 -05:00
|
|
|
)
|
|
|
|
assert updated_access_policy.resources == resources
|
|
|
|
|
2024-11-16 01:38:07 -05:00
|
|
|
updated_holiday_group = live_access_client.update_holiday_group(
|
|
|
|
holiday_group.id, description="Test Description"
|
2024-11-16 00:26:00 -05:00
|
|
|
)
|
|
|
|
assert updated_holiday_group.description == "Test Description"
|
|
|
|
|
2024-11-16 01:38:07 -05:00
|
|
|
holiday_groups = live_access_client.fetch_all_holiday_groups()
|
2024-11-16 00:26:00 -05:00
|
|
|
assert any(g.id == updated_holiday_group.id for g in holiday_groups)
|