unifi_access/tests/test_unifi_identity.py
Adam Goldsmith 5b0c72dbef Initial Commit
Really should have added this to version control earlier, but was
trying to fix all the pre-commit issues first...
2024-11-26 12:50:13 -05:00

250 lines
8.9 KiB
Python

import responses
from responses import matchers
from unifi_access.schemas import (
IdentityInvitationUser,
IdentityResourceType,
UserGroupId,
UserId,
)
from unifi_access.schemas._base import IdentityResourceId
from .base import UnifiAccessTests
class UnifiIdentityTests(UnifiAccessTests):
@responses.activate
def test_send_unifi_identity_invitations(self) -> None:
"""10.1 Send UniFi Identity Invitations"""
responses.post(
f"https://{self.host}/api/v1/developer/users/identity/invitations",
match=[
matchers.header_matcher(self.common_headers),
matchers.json_params_matcher(
[
{
"user_id": "e0051e08-c4d5-43db-87c8-a9b19cb66513",
"email": "example@*.com",
}
]
),
],
json={"code": "SUCCESS", "data": [], "msg": "success"},
)
resp = self.client.send_unifi_identity_invitations(
[
IdentityInvitationUser(
user_id=UserId("e0051e08-c4d5-43db-87c8-a9b19cb66513"),
email="example@*.com",
)
]
).success_or_raise()
@responses.activate
def test_send_unifi_identity_invitations_email_failure(self) -> None:
"""10.1 Send UniFi Identity Invitations"""
responses.post(
f"https://{self.host}/api/v1/developer/users/identity/invitations",
match=[
matchers.header_matcher(self.common_headers),
matchers.json_params_matcher(
[
{
"user_id": "e0051e08-c4d5-43db-87c8-a9b19cb66513",
"email": "example@*.com",
}
]
),
],
json={
"code": "SUCCESS",
"data": [
{
"error_code": "",
"error_msg": "invalid email",
"user_email": "example@*.com",
"user_id": "e0051e08-c4d5-43db-87c8-a9b19cb66513",
}
],
"msg": "success",
},
)
# XXX: not sure what behavior here should be...
resp = self.client.send_unifi_identity_invitations(
[
IdentityInvitationUser(
user_id=UserId("e0051e08-c4d5-43db-87c8-a9b19cb66513"),
email="example@*.com",
)
]
).success_or_raise()
@responses.activate
def test_fetch_available_resources(self) -> None:
"""10.2 Fetch Available Resources"""
responses.get(
f"https://{self.host}/api/v1/developer/users/identity/assignments",
match=[
matchers.header_matcher(self.common_headers),
matchers.query_param_matcher({"resource_type": "ev_station,wifi,vpn"}),
],
json={
"code": "SUCCESS",
"data": {
"ev_station": [],
"vpn": [
{
"deleted": False,
"id": "65dff9a9c188cb71cfac8e9d",
"metadata": None,
"name": "UDM Pro",
"short_name": "",
}
],
"wifi": [
{
"deleted": False,
"id": "65dff9a8c188cb71cfac8e9a",
"metadata": None,
"name": "UniFi Identity",
"short_name": "",
}
],
},
"msg": "success",
},
)
resp = self.client.fetch_available_resources(
resource_type=[
IdentityResourceType.EV_STATION,
IdentityResourceType.WIFI,
IdentityResourceType.VPN,
]
).success_or_raise()
@responses.activate
def test_assign_resources_to_user(self) -> None:
"""10.3 Assign Resources to Users"""
user_id = UserId("b602879b-b857-400b-970b-336d4cb881ad")
responses.post(
f"https://{self.host}/api/v1/developer/users/{user_id}/identity/assignments",
match=[
matchers.header_matcher(self.common_headers),
matchers.json_params_matcher(
{
"resource_type": "wifi",
"resource_ids": ["65dff9a8c188cb71cfac8e9a"],
}
),
],
json={"code": "SUCCESS", "data": None, "msg": "success"},
)
resp = self.client.assign_resources_to_user(
user_id=user_id,
resource_type=IdentityResourceType.WIFI,
resource_ids=[IdentityResourceId("65dff9a8c188cb71cfac8e9a")],
).success_or_raise()
@responses.activate
def test_fetch_resources_assigned_to_user(self) -> None:
"""10.4 Fetch Resources Assigned to Users"""
user_id = UserId("b602879b-b857-400b-970b-336d4cb881ad")
responses.get(
f"https://{self.host}/api/v1/developer/users/{user_id}/identity/assignments",
match=[matchers.header_matcher(self.common_headers)],
json={
"code": "SUCCESS",
"data": {
"ev_station": [],
"vpn": [
{
"deleted": False,
"id": "65dff9a9c188cb71cfac8e9d",
"metadata": {"has_ip": True},
"name": "UDM Pro",
"short_name": "",
}
],
"wifi": [
{
"deleted": False,
"id": "65dff9a8c188cb71cfac8e9a",
"metadata": None,
"name": "UniFi Identity",
"short_name": "",
}
],
},
"msg": "success",
},
)
resp = self.client.fetch_resources_assigned_to_user(
user_id=user_id
).success_or_raise()
@responses.activate
def test_assign_resources_to_user_group(self) -> None:
"""10.5 Assign Resources to User Groups"""
user_group_id = UserGroupId("7476c839-8e10-472e-894f-c5b8254c35b5")
responses.post(
f"https://{self.host}/api/v1/developer/user_groups/{user_group_id}/identity/assignments",
match=[
matchers.header_matcher(self.common_headers),
matchers.json_params_matcher(
{
"resource_type": "wifi",
"resource_ids": ["65dff9a8c188cb71cfac8e9a"],
}
),
],
json={"code": "SUCCESS", "data": None, "msg": "success"},
)
resp = self.client.assign_resources_to_user_group(
user_group_id=user_group_id,
resource_type=IdentityResourceType.WIFI,
resource_ids=[IdentityResourceId("65dff9a8c188cb71cfac8e9a")],
).success_or_raise()
@responses.activate
def test_fetch_resources_assigned_to_user_group(self) -> None:
"""10.6 Fetch Resources Assigned to User Groups"""
user_group_id = UserGroupId("b602879b-b857-400b-970b-336d4cb881ad")
responses.get(
f"https://{self.host}/api/v1/developer/user_groups/{user_group_id}/identity/assignments",
match=[matchers.header_matcher(self.common_headers)],
json={
"code": "SUCCESS",
"data": {
"ev_station": [],
"vpn": [
{
"deleted": False,
"id": "65dff9a9c188cb71cfac8e9d",
"metadata": {"has_ip": True},
"name": "UDM Pro",
"short_name": "",
}
],
"wifi": [
{
"deleted": False,
"id": "65dff9a8c188cb71cfac8e9a",
"metadata": None,
"name": "UniFi Identity",
"short_name": "",
}
],
},
"msg": "success",
},
)
resp = self.client.fetch_resources_assigned_to_user_group(
user_group_id=user_group_id
).success_or_raise()