unifi_access/tests/live/test_user_lifecycle.py

119 lines
3.9 KiB
Python
Raw Normal View History

import functools
from collections.abc import Generator
from typing import Protocol
import pytest
from unifi_access import AccessClient
from unifi_access.schemas import User, UserGroup, UserGroupId, UserStatus
@pytest.fixture(scope="module")
def user(live_access_client: AccessClient) -> User:
# XXX: maybe should test email and employeeNumber, but they are
# unique on the server, and I can't delete users. Maybe
# should fetch user if it already exists?
# TODO: add an onboard time?
return live_access_client.register_user("Test", "User")
def test_user_lifecycle(live_access_client: AccessClient, user: User) -> None:
assert user.id
assert user.first_name == "Test"
assert user.last_name == "User"
assert user.status == UserStatus.ACTIVE
# Fetch user
fetched_user = live_access_client.fetch_user(user.id)
assert fetched_user.id == user.id
assert fetched_user.first_name == "Test"
assert fetched_user.last_name == "User"
assert fetched_user.status == UserStatus.ACTIVE
# Update user
live_access_client.update_user(
user.id, last_name="Updated User", status=UserStatus.DEACTIVATED
)
# Fetch again, and check changes
fetched_user = live_access_client.fetch_user(user.id)
assert fetched_user.id == user.id
assert fetched_user.first_name == "Test"
assert fetched_user.last_name == "Updated User"
assert fetched_user.status == UserStatus.DEACTIVATED
# Check for the user in full user list
# TODO: test pagination
all_users = live_access_client.fetch_all_users__unpaged()
matching_user = next(u for u in all_users if u.id == user.id)
assert matching_user.id == user.id
assert matching_user.first_name == "Test"
assert matching_user.last_name == "Updated User"
assert matching_user.status == UserStatus.DEACTIVATED
class UserGroupFactory(Protocol):
def __call__(
self, name: str, up_id: UserGroupId | None = None
) -> UserGroup | None: ...
@pytest.fixture
def make_user_group(
live_access_client: AccessClient,
) -> Generator[UserGroupFactory]:
created_groups = []
@functools.wraps(live_access_client.create_user_group)
def _make_user_group(
name: str,
up_id: UserGroupId | None = None,
) -> UserGroup | None:
group = live_access_client.create_user_group(name, up_id)
created_groups.append(group)
return group
yield _make_user_group
for group in reversed(created_groups):
if group is not None:
live_access_client.delete_user_group(group.id)
def test_user_groups_lifecycle(
live_access_client: AccessClient,
make_user_group: UserGroupFactory,
user: User,
) -> None:
# Create group
group = make_user_group("Test Group")
assert group, "group 'Test Group' already existed, please delete"
# Fetch group
fetched_group = live_access_client.fetch_user_group(group.id)
assert fetched_group.name == "Test Group"
# Check for group in in full group list
groups = live_access_client.fetch_all_user_groups()
matching_group = next(g for g in groups if g.id == group.id)
assert matching_group
# Create child group
child_group = make_user_group("Test Group Child", group.id)
assert child_group, "group 'Test Group / Test Group Child' already existed, which shouldn't be possible"
assert child_group.up_id == group.id
assert child_group.up_ids[-1] == group.id
# Add user to group
live_access_client.assign_user_to_user_group(group.id, [user.id])
# Fetch users in the group
users_in_group = live_access_client.fetch_users_in_a_user_group(group.id)
assert len(users_in_group) == 1
assert users_in_group[0].id == user.id
# Fetch all users in the group
all_users_in_group = live_access_client.fetch_all_users_in_a_user_group(group.id)
assert len(all_users_in_group) == 1
assert all_users_in_group[0].id == user.id