import datetime import responses from responses import matchers from unifi_access.schemas import ( AccessPolicyId, DoorGroupId, DoorGroupResource, DoorId, DoorResource, HolidayGroupId, HolidayId, PartialHoliday, ScheduleId, TimePeriod, WeekSchedule, ) from .base import UnifiAccessTests class AccessPolicyTests(UnifiAccessTests): @responses.activate def test_create_access_policy(self) -> None: """5.2 Create Access Policy""" responses.post( f"https://{self.host}/api/v1/developer/access_policies", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( { "name": "test", "resource": [ { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door", }, { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "type": "door_group", }, { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "type": "door_group", }, ], "schedule_id": "4e108aeb-ec9a-4822-bf86-170ea986f934", } ), ], json={ "code": "SUCCESS", "data": { "id": "bb5eb965-42dc-4206-9654-88a2d1c3aaa5", "name": "test", "resources": [ {"id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door"}, { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "type": "door_group", }, { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "type": "door_group", }, ], "schedule_id": "4e108aeb-ec9a-4822-bf86-170ea986f934", }, "msg": "success", }, ) resp = self.client.create_access_policy( name="test", resources=[ DoorResource( id=DoorId("6ff875d2-af87-470b-9cb5-774c6596afc8"), ), DoorGroupResource( id=DoorGroupId("5c496423-6d25-4e4f-8cdf-95ad5135188a"), ), DoorGroupResource( id=DoorGroupId("d5573467-d6b3-4e8f-8e48-8a322b91664a"), ), ], schedule_id=ScheduleId("4e108aeb-ec9a-4822-bf86-170ea986f934"), ) assert resp.id == "bb5eb965-42dc-4206-9654-88a2d1c3aaa5" @responses.activate def test_update_access_policy(self) -> None: """5.3 Update Access Policy""" access_policy_id = AccessPolicyId("242c88e3-0524-42de-8447-45891c5df714") responses.put( f"https://{self.host}/api/v1/developer/access_policies/{access_policy_id}", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( { "name": "test", "resource": [ { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door", }, { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "type": "door_group", }, { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "type": "door_group", }, ], "schedule_id": "4e108aeb-ec9a-4822-bf86-170ea986f934", } ), ], # NOTE: no example provided in docs, using the same one as `create` json={ "code": "SUCCESS", "data": { "id": "bb5eb965-42dc-4206-9654-88a2d1c3aaa5", "name": "test", "resources": [ {"id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door"}, { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "type": "door_group", }, { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "type": "door_group", }, ], "schedule_id": "4e108aeb-ec9a-4822-bf86-170ea986f934", }, "msg": "success", }, ) resp = self.client.update_access_policy( access_policy_id=access_policy_id, name="test", resources=[ DoorResource( id=DoorId("6ff875d2-af87-470b-9cb5-774c6596afc8"), ), DoorGroupResource( id=DoorGroupId("5c496423-6d25-4e4f-8cdf-95ad5135188a"), ), DoorGroupResource( id=DoorGroupId("d5573467-d6b3-4e8f-8e48-8a322b91664a"), ), ], schedule_id=ScheduleId("4e108aeb-ec9a-4822-bf86-170ea986f934"), ) assert resp.id == "bb5eb965-42dc-4206-9654-88a2d1c3aaa5" @responses.activate def test_delete_access_policy(self) -> None: """5.4 Delete Access Policy""" access_policy_id = AccessPolicyId("60d0bcc-5d4f-4e7b-8a3c-8d4502765e11") responses.delete( f"https://{self.host}/api/v1/developer/access_policies/{access_policy_id}", match=[matchers.header_matcher(self.common_headers)], json={"code": "SUCCESS", "msg": "success", "data": "success"}, ) resp = self.client.delete_access_policy(access_policy_id=access_policy_id) assert resp == "success" @responses.activate def test_fetch_access_policy(self) -> None: """5.5 Fetch Access Policy""" access_policy_id = AccessPolicyId("ed09985f-cf52-486e-bc33-377b6ed7bbf2") responses.get( f"https://{self.host}/api/v1/developer/access_policies/{access_policy_id}", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": { "id": "ed09985f-cf52-486e-bc33-377b6ed7bbf2", "name": "test11", "resources": [ {"id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door"}, { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "type": "door_group", }, { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "type": "door_group", }, ], "schedule_id": "4e108aeb-ec9a-4822-bf86-170ea986f934", }, "msg": "success", }, ) resp = self.client.fetch_access_policy(access_policy_id=access_policy_id) assert resp.id == "ed09985f-cf52-486e-bc33-377b6ed7bbf2" @responses.activate def test_fetch_all_access_policies(self) -> None: """5.6 Fetch All Access Policies""" responses.get( f"https://{self.host}/api/v1/developer/access_policies", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": [ { "id": "73f15cab-c725-4a76-a419-a4026d131e96", "name": "Default Admin Policy", "resources": [ { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "type": "door_group", }, { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "type": "door_group", }, ], "schedule_id": "73facd6c-839e-4521-a4f4-c07e1d44e748", }, { "id": "b96948a4-fed9-40a3-9c4a-e473822a3db7", "name": "Default UNVR Policy", "resources": [ { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "type": "door_group", }, { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door", }, ], "schedule_id": "58c0f89b-f35c-4d2c-af7b-8b8918df2c31", }, { "id": "edbc80df-3698-49fd-8b53-f1867f104947", "name": "TEST", "resources": [ { "id": "d5573467-d6b3-4e8f-8e48-8a322b91664a", "type": "door_group", }, { "id": "5c496423-6d25-4e4f-8cdf-95ad5135188a", "type": "door_group", }, { "id": "6ff875d2-af87-470b-9cb5-774c6596afc8", "type": "door", }, ], "schedule_id": "73facd6c-839e-4521-a4f4-c07e1d44e748", }, ], "msg": "success", }, ) resp = self.client.fetch_all_access_policies() assert resp[0].id == "73f15cab-c725-4a76-a419-a4026d131e96" @responses.activate def test_create_holiday_group(self) -> None: """5.8 Create Holiday Group""" responses.post( f"https://{self.host}/api/v1/developer/access_policies/holiday_groups", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( { "name": "Holiday Group-169286791557142", "holidays": [ { "name": "Holiday Name 1", "description": "", "repeat": False, "start_time": "2023-08-25T00:00:00Z", "end_time": "2023-08-26T00:00:00Z", }, { "name": "Holiday Name 2", "description": "", "repeat": False, "start_time": "2023-08-26T00:00:00Z", "end_time": "2023-08-27T00:00:00Z", }, ], } ), ], json={ "code": "SUCCESS", "data": { "description": "", "holidays": [ { "description": "", "end_time": "2023-08-26 00:00:00Z", "id": "8900533d-03be-4f84-832d-54ff59905759", "name": "Holiday Name 1", "repeat": False, "start_time": "2023-08-25 00:00:00Z", }, { # NOTE: duplicated key in sample data # "name": "holiday-2023-08-26", "end_time": "2023-08-27 00:00:00Z", "id": "9fff81cc-d476-40c4-80f9-d510451ce2cd", "name": "Holiday Name 2", "repeat": False, "start_time": "2023-08-26 00:00:00Z", }, ], "id": "7be7a7a0-818f-4f76-98c3-1c38957f4dca", "is_default": False, "name": "Holiday Group-169286791557142", "template_name": "", }, "msg": "success", }, ) resp = self.client.create_holiday_group( name="Holiday Group-169286791557142", holidays=[ PartialHoliday( name="Holiday Name 1", description="", repeat=False, start_time=datetime.datetime(2023, 8, 25, tzinfo=datetime.UTC), end_time=datetime.datetime(2023, 8, 26, tzinfo=datetime.UTC), ), PartialHoliday( name="Holiday Name 2", description="", repeat=False, start_time=datetime.datetime(2023, 8, 26, tzinfo=datetime.UTC), end_time=datetime.datetime(2023, 8, 27, tzinfo=datetime.UTC), ), ], ) assert resp.id == "7be7a7a0-818f-4f76-98c3-1c38957f4dca" @responses.activate def test_update_holiday_group(self) -> None: """5.9 Update Holiday Group""" holiday_group_id = HolidayGroupId("7be7a7a0-818f-4f76-98c3-1c38957f4dca") responses.put( f"https://{self.host}/api/v1/developer/access_policies/holiday_groups/{holiday_group_id}", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( { "name": "Holiday Group-169286791557142", "holidays": [ # add a new holiday { "name": "Holiday Name 1", "description": "", "repeat": False, "start_time": "2023-08-25T00:00:00Z", "end_time": "2023-08-26T00:00:00Z", }, # update an existing holiday { "id": "d23a4226-765f-4967-b84f-6dfd53f33c89", "name": "Holiday Name 2", "description": "", "repeat": False, "start_time": "2023-08-26T00:00:00Z", "end_time": "2023-08-27T00:00:00Z", }, ], } ), ], json={ "code": "SUCCESS", "data": { "description": "", "holidays": [ { "description": "", "end_time": "2023-08-26 00:00:00Z", "id": "8900533d-03be-4f84-832d-54ff59905759", "name": "Holiday Name 1", "repeat": False, "start_time": "2023-08-25 00:00:00Z", }, { "description": "", "end_time": "2023-08-27 00:00:00Z", "id": "9fff81cc-d476-40c4-80f9-d510451ce2cd", "name": "Holiday Name 2", "repeat": False, "start_time": "2023-08-26 00:00:00Z", }, ], "id": "7be7a7a0-818f-4f76-98c3-1c38957f4dca", "is_default": False, "name": "Holiday Group-169286791557142", "template_name": "", }, "msg": "success", }, ) resp = self.client.update_holiday_group( holiday_group_id=holiday_group_id, name="Holiday Group-169286791557142", holidays=[ # add a new holiday PartialHoliday( name="Holiday Name 1", description="", repeat=False, start_time=datetime.datetime(2023, 8, 25, tzinfo=datetime.UTC), end_time=datetime.datetime(2023, 8, 26, tzinfo=datetime.UTC), ), # update an existing holiday PartialHoliday( id=HolidayId("d23a4226-765f-4967-b84f-6dfd53f33c89"), name="Holiday Name 2", description="", repeat=False, start_time=datetime.datetime(2023, 8, 26, tzinfo=datetime.UTC), end_time=datetime.datetime(2023, 8, 27, tzinfo=datetime.UTC), ), ], ) assert resp.id == "7be7a7a0-818f-4f76-98c3-1c38957f4dca" @responses.activate def test_delete_holiday_group(self) -> None: """5.10 Delete Holiday Group""" holiday_group_id = HolidayGroupId("7be7a7a0-818f-4f76-98c3-1c38957f4dca") responses.delete( f"https://{self.host}/api/v1/developer/access_policies/holiday_groups/{holiday_group_id}", match=[matchers.header_matcher(self.common_headers)], json={"code": "SUCCESS", "msg": "success", "data": "success"}, ) resp = self.client.delete_holiday_group(holiday_group_id=holiday_group_id) assert resp == "success" @responses.activate def test_fetch_holiday_group(self) -> None: """5.11 Fetch Holiday Group""" holiday_group_id = HolidayGroupId("7be7a7a0-818f-4f76-98c3-1c38957f4dca") responses.get( f"https://{self.host}/api/v1/developer/access_policies/holiday_groups/{holiday_group_id}", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": { "description": "", "holidays": [ { "description": "", "end_time": "2023-08-26 00:00:00Z", "id": "8900533d-03be-4f84-832d-54ff59905759", "name": "Holiday Name 1", "repeat": False, "start_time": "2023-08-25 00:00:00Z", }, { "description": "", "end_time": "2023-08-27 00:00:00Z", "id": "9fff81cc-d476-40c4-80f9-d510451ce2cd", "name": "Holiday Name 2", "repeat": False, "start_time": "2023-08-26 00:00:00Z", }, ], "id": "7be7a7a0-818f-4f76-98c3-1c38957f4dca", "is_default": False, "name": "Holiday Group-169286791557142", "template_name": "", }, "msg": "success", }, ) resp = self.client.fetch_holiday_group(holiday_group_id=holiday_group_id) assert resp.id == "7be7a7a0-818f-4f76-98c3-1c38957f4dca" @responses.activate def test_fetch_all_holiday_groups(self) -> None: """5.12 Fetch All Holiday Groups""" responses.get( f"https://{self.host}/api/v1/developer/access_policies/holiday_groups", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": [ { "count": 0, "description": "", "id": "8cc22b49-a7f4-49a6-9f04-044444992d6c", "is_default": True, "name": "No Holidays", }, { "count": 2, "description": "", "id": "86c634da-7b2c-411c-a2c1-1495d089c719", "is_default": False, "name": "Holiday Group-1692867312225", }, ], "msg": "success", }, ) resp = self.client.fetch_all_holiday_groups() assert resp[0].id == "8cc22b49-a7f4-49a6-9f04-044444992d6c" @responses.activate def test_create_schedule(self) -> None: """5.14 Create Schedule""" responses.post( f"https://{self.host}/api/v1/developer/access_policies/schedules", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( { "name": "schedule-1688977094169", "week_schedule": { "sunday": [], "monday": [ {"start_time": "10:00:00", "end_time": "17:00:59"} ], "tuesday": [ {"start_time": "10:00:00", "end_time": "17:00:59"} ], "wednesday": [ {"start_time": "10:00:00", "end_time": "17:00:59"} ], "thursday": [], "friday": [ {"start_time": "10:00:00", "end_time": "17:00:59"} ], "saturday": [], }, "holiday_group_id": "75660081-431b-4dbe-9b98-e0257877118e", "holiday_schedule": [ {"start_time": "03:15:00", "end_time": "11:45:59"}, {"start_time": "15:00:00", "end_time": "19:00:59"}, ], } ), ], json={ "code": "SUCCESS", "data": { "id": "1d31b648-b8ff-4bd1-b742-60dbd70592cd", "is_default": False, "name": "schedule-1688977094169", "type": "access", "weekly": { "friday": [{"end_time": "17:00:59", "start_time": "10:00:00"}], "monday": [{"end_time": "17:00:59", "start_time": "10:00:00"}], "saturday": [], "sunday": [], "thursday": [], "tuesday": [{"end_time": "17:00:59", "start_time": "10:00:00"}], "wednesday": [ {"end_time": "17:00:59", "start_time": "10:00:00"} ], }, "holiday_group_id": "75660081-431b-4dbe-9b98-e0257877118e", "holiday_group": { "description": "", "holidays": [ { "description": "", "end_time": "2023-08-26 00:00:00Z", "id": "d51777c4-9559-45aa-8e23-434995d9d2a1", "is_template": False, "name": "Holiday Name 1", "repeat": False, "start_time": "2023-08-25 00:00:00Z", }, { "description": "", "end_time": "2023-08-27 00:00:00Z", "id": "d23a4226-765f-4967-b84f-6dfd53f33c89", "is_template": False, "name": "Holiday Name 2", "repeat": False, "start_time": "2023-08-26 00:00:00Z", }, ], "id": "75660081-431b-4dbe-9b98-e0257877118e", "is_default": False, "name": "Holiday Group-1692867915571423", "template_name": "", }, "holiday_schedule": [ {"start_time": "03:15:00", "end_time": "11:45:59"}, {"start_time": "15:00:00", "end_time": "19:00:59"}, ], }, "msg": "success", }, ) resp = self.client.create_schedule( name="schedule-1688977094169", week_schedule=WeekSchedule( sunday=[], monday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 0, 59), ) ], tuesday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 0, 59), ) ], wednesday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 0, 59), ) ], thursday=[], friday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 0, 59), ) ], saturday=[], ), holiday_group_id=HolidayGroupId("75660081-431b-4dbe-9b98-e0257877118e"), holiday_schedule=[ TimePeriod( start_time=datetime.time(3, 15, 0), end_time=datetime.time(11, 45, 59), ), TimePeriod( start_time=datetime.time(15, 0, 0), end_time=datetime.time(19, 0, 59), ), ], ) @responses.activate def test_update_schedule(self) -> None: """5.15 Update Schedule""" schedule_id = ScheduleId("1d31b648-b8ff-4bd1-b742-60dbd70592cd") responses.put( f"https://{self.host}/api/v1/developer/access_policies/schedules/{schedule_id}", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( { "name": "schedule-1688977094169", "holiday_group_id": "75660081-431b-4dbe-9b98-e0257877118e", "week_schedule": { "sunday": [], "monday": [ {"start_time": "10:00:00", "end_time": "17:00:59"} ], "tuesday": [ {"start_time": "10:00:00", "end_time": "17:00:59"} ], "wednesday": [ {"start_time": "10:00:00", "end_time": "17:00:59"} ], "thursday": [ {"start_time": "10:00:00", "end_time": "17:01:59"} ], "friday": [ {"start_time": "10:00:00", "end_time": "17:00:59"} ], "saturday": [], }, "holiday_schedule": [ {"start_time": "03:15:00", "end_time": "11:45:59"} ], } ), ], json={"code": "SUCCESS", "data": {}, "msg": "success"}, ) resp = self.client.update_schedule( schedule_id=schedule_id, name="schedule-1688977094169", holiday_group_id=HolidayGroupId("75660081-431b-4dbe-9b98-e0257877118e"), week_schedule=WeekSchedule( sunday=[], monday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 0, 59), ) ], tuesday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 0, 59), ) ], wednesday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 0, 59), ) ], thursday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 1, 59), ) ], friday=[ TimePeriod( start_time=datetime.time(10, 0, 0), end_time=datetime.time(17, 0, 59), ) ], saturday=[], ), holiday_schedule=[ TimePeriod( start_time=datetime.time(3, 15, 0), end_time=datetime.time(11, 45, 59), ) ], ) @responses.activate def test_fetch_schedule(self) -> None: """5.16 Fetch Schedule""" schedule_id = ScheduleId("1d31b648-b8ff-4bd1-b742-60dbd70592cd") responses.get( f"https://{self.host}/api/v1/developer/access_policies/schedules/{schedule_id}", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": { "id": "1d31b648-b8ff-4bd1-b742-60dbd70592cd", "is_default": False, "name": "schedule-1688977094169", "type": "access", "weekly": { "friday": [{"end_time": "17:00:59", "start_time": "10:00:00"}], "monday": [{"end_time": "17:00:59", "start_time": "10:00:00"}], "saturday": [], "sunday": [], "thursday": [ {"end_time": "17:01:59", "start_time": "10:00:00"} ], "tuesday": [{"end_time": "17:00:59", "start_time": "10:00:00"}], "wednesday": [ {"end_time": "17:00:59", "start_time": "10:00:00"} ], }, "holiday_group_id": "75660081-431b-4dbe-9b98-e0257877118e", "holiday_group": { "description": "", "holidays": [ { "description": "", "end_time": "2023-08-26 00:00:00Z", "id": "d51777c4-9559-45aa-8e23-434995d9d2a1", "is_template": False, "name": "Holiday Name 1", "repeat": False, "start_time": "2023-08-25 00:00:00Z", }, { "description": "", "end_time": "2023-08-27 00:00:00Z", "id": "d23a4226-765f-4967-b84f-6dfd53f33c89", "is_template": False, "name": "Holiday Name 2", "repeat": False, "start_time": "2023-08-26 00:00:00Z", }, ], "id": "75660081-431b-4dbe-9b98-e0257877118e", "is_default": False, "name": "Holiday Group-16928679155714", "template_name": "", }, "holiday_schedule": [ {"end_time": "11:45:59", "start_time": "09:15:00"} ], }, "msg": "success", }, ) resp = self.client.fetch_schedule(schedule_id=schedule_id) @responses.activate def test_fetch_all_schedules(self) -> None: """5.17 Fetch All Schedules""" responses.get( f"https://{self.host}/api/v1/developer/access_policies/schedules", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": [ { "id": "73facd6c-839e-4521-a4f4-c07e1d44e748", "holiday_group_id": "75660081-431b-4dbe-9b98-e0257877118e", "is_default": True, "name": "Always Access", "status": 1, "type": "access", }, { "id": "58c0f89b-f35c-4d2c-af7b-8b8918df2c31", "holiday_group_id": "75660081-431b-4dbe-9b98-e0257877118e", "is_default": False, "name": "UNVR Schedule", "status": 1, "type": "access", }, ], "msg": "success", }, ) resp = self.client.fetch_all_schedules() @responses.activate def test_delete_schedule(self) -> None: """5.18 Delete Schedule""" schedule_id = ScheduleId("1d31b648-b8ff-4bd1-b742-60dbd70592cd") responses.delete( f"https://{self.host}/api/v1/developer/access_policies/schedules/{schedule_id}", match=[matchers.header_matcher(self.common_headers)], json={"code": "SUCCESS", "msg": "success", "data": "success"}, ) resp = self.client.delete_schedule(schedule_id=schedule_id)