2024-11-16 00:26:00 -05:00
|
|
|
import datetime
|
|
|
|
from collections.abc import Callable, Generator
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from unifi_access import AccessClient
|
|
|
|
from unifi_access.schemas import FetchAllVisitorsExpansion, Visitor, VisitReason
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def make_visitor(live_access_client: AccessClient) -> Generator[Callable[..., Visitor]]:
|
|
|
|
created_visitors: list[Visitor] = []
|
|
|
|
|
2024-11-16 01:38:07 -05:00
|
|
|
def _make_visitor(*args, **kwargs) -> Visitor: # type: ignore
|
|
|
|
visitor = live_access_client.create_visitor(*args, **kwargs)
|
2024-11-16 00:26:00 -05:00
|
|
|
created_visitors.append(visitor)
|
|
|
|
return visitor
|
|
|
|
|
|
|
|
yield _make_visitor
|
|
|
|
|
|
|
|
for visitor in created_visitors:
|
|
|
|
live_access_client.delete_visitor(visitor.id, is_force=True)
|
|
|
|
|
|
|
|
|
|
|
|
def test_visitor_lifecycle(
|
|
|
|
live_access_client: AccessClient, make_visitor: Callable[..., Visitor]
|
|
|
|
) -> None:
|
|
|
|
now = datetime.datetime.now(tz=datetime.UTC)
|
|
|
|
visitor = make_visitor(
|
|
|
|
"Test",
|
|
|
|
"Visitor",
|
|
|
|
start_time=now + datetime.timedelta(days=1),
|
|
|
|
end_time=now + datetime.timedelta(days=2),
|
|
|
|
visit_reason=VisitReason.BUSINESS,
|
|
|
|
)
|
|
|
|
assert visitor.first_name == "Test"
|
|
|
|
|
2024-11-16 01:38:07 -05:00
|
|
|
fetched_visitor = live_access_client.fetch_visitor(visitor.id)
|
2024-11-16 00:26:00 -05:00
|
|
|
assert fetched_visitor.first_name == "Test"
|
|
|
|
assert fetched_visitor.pin_code is None
|
|
|
|
|
|
|
|
# pin code assign/unassign
|
2024-11-16 01:38:07 -05:00
|
|
|
pin_code = live_access_client.generate_pin_code()
|
|
|
|
live_access_client.assign_pin_code_to_visitor(visitor.id, pin_code)
|
|
|
|
fetched_visitor = live_access_client.fetch_visitor(visitor.id)
|
2024-11-16 00:26:00 -05:00
|
|
|
assert fetched_visitor.pin_code
|
|
|
|
assert fetched_visitor.pin_code.token
|
2024-11-16 01:38:07 -05:00
|
|
|
live_access_client.unassign_pin_code_from_visitor(visitor.id)
|
|
|
|
fetched_visitor = live_access_client.fetch_visitor(visitor.id)
|
2024-11-16 00:26:00 -05:00
|
|
|
assert fetched_visitor.pin_code is None
|
|
|
|
|
2024-11-16 01:38:07 -05:00
|
|
|
updated_visitor = live_access_client.update_visitor(
|
|
|
|
visitor.id, first_name="Updated Test"
|
2024-11-16 00:26:00 -05:00
|
|
|
)
|
|
|
|
assert updated_visitor.first_name == "Updated Test"
|
|
|
|
|
2024-11-16 01:38:07 -05:00
|
|
|
all_visitors = live_access_client.fetch_all_visitors().data
|
2024-11-16 00:26:00 -05:00
|
|
|
matched_visitor = next(v for v in all_visitors if v.id == visitor.id)
|
|
|
|
assert matched_visitor.first_name == "Updated Test"
|
|
|
|
|
2024-11-16 01:38:07 -05:00
|
|
|
expanded_all_visitors = live_access_client.fetch_all_visitors(
|
|
|
|
expand=[
|
|
|
|
FetchAllVisitorsExpansion.ACCESS_POLICY,
|
|
|
|
FetchAllVisitorsExpansion.RESOURCE,
|
|
|
|
FetchAllVisitorsExpansion.SCHEDULE,
|
|
|
|
FetchAllVisitorsExpansion.NFC_CARD,
|
|
|
|
FetchAllVisitorsExpansion.PIN_CODE,
|
|
|
|
]
|
|
|
|
).data
|
2024-11-16 00:26:00 -05:00
|
|
|
expanded_matched_visitor = next(
|
|
|
|
v for v in expanded_all_visitors if v.id == visitor.id
|
|
|
|
)
|
|
|
|
assert expanded_matched_visitor.first_name == "Updated Test"
|
|
|
|
# TODO: test expanded contents
|
|
|
|
|
2024-11-16 01:38:07 -05:00
|
|
|
non_expanded_all_visitors = live_access_client.fetch_all_visitors(
|
|
|
|
expand=[FetchAllVisitorsExpansion.NONE]
|
|
|
|
).data
|
2024-11-16 00:26:00 -05:00
|
|
|
non_expanded_matched_visitor = next(
|
|
|
|
v for v in non_expanded_all_visitors if v.id == visitor.id
|
|
|
|
)
|
|
|
|
assert non_expanded_matched_visitor.first_name == "Updated Test"
|
|
|
|
assert non_expanded_matched_visitor.schedule is None
|
|
|
|
# TODO: test other non-expanded contents
|