85 lines
3.0 KiB
Python
85 lines
3.0 KiB
Python
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] = []
|
|
|
|
def _make_visitor(*args, **kwargs) -> Visitor: # type: ignore
|
|
visitor = live_access_client.create_visitor(*args, **kwargs)
|
|
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"
|
|
|
|
fetched_visitor = live_access_client.fetch_visitor(visitor.id)
|
|
assert fetched_visitor.first_name == "Test"
|
|
assert fetched_visitor.pin_code is None
|
|
|
|
# pin code assign/unassign
|
|
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)
|
|
assert fetched_visitor.pin_code
|
|
assert fetched_visitor.pin_code.token
|
|
live_access_client.unassign_pin_code_from_visitor(visitor.id)
|
|
fetched_visitor = live_access_client.fetch_visitor(visitor.id)
|
|
assert fetched_visitor.pin_code is None
|
|
|
|
updated_visitor = live_access_client.update_visitor(
|
|
visitor.id, first_name="Updated Test"
|
|
)
|
|
assert updated_visitor.first_name == "Updated Test"
|
|
|
|
all_visitors = live_access_client.fetch_all_visitors().data
|
|
matched_visitor = next(v for v in all_visitors if v.id == visitor.id)
|
|
assert matched_visitor.first_name == "Updated Test"
|
|
|
|
expanded_all_visitors = live_access_client.fetch_all_visitors(
|
|
expand=[
|
|
FetchAllVisitorsExpansion.ACCESS_POLICY,
|
|
FetchAllVisitorsExpansion.RESOURCE,
|
|
FetchAllVisitorsExpansion.SCHEDULE,
|
|
FetchAllVisitorsExpansion.NFC_CARD,
|
|
FetchAllVisitorsExpansion.PIN_CODE,
|
|
]
|
|
).data
|
|
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
|
|
|
|
non_expanded_all_visitors = live_access_client.fetch_all_visitors(
|
|
expand=[FetchAllVisitorsExpansion.NONE]
|
|
).data
|
|
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
|