import responses from responses import matchers from unifi_access.schemas import DeviceId, NfcCardEnrollmentSessionId, NfcCardToken from .base import UnifiAccessTests class CredentialTests(UnifiAccessTests): @responses.activate def test_generate_pin_code(self) -> None: """6.1 Generate PIN Code""" responses.post( f"https://{self.host}/api/v1/developer/credentials/pin_codes", match=[matchers.header_matcher(self.common_headers)], json={"code": "SUCCESS", "data": "67203419", "msg": "success"}, ) resp = self.client.generate_pin_code() assert resp == "67203419" @responses.activate def test_begin_enroll_card(self) -> None: """6.2 Enroll NFC Card""" responses.post( f"https://{self.host}/api/v1/developer/credentials/nfc_cards/sessions", match=[ matchers.header_matcher(self.common_headers), matchers.json_params_matcher( {"device_id": "0418d6a2bb7a", "reset_ua_card": False} ), ], json={ "code": "SUCCESS", "msg": "success", "data": {"session_id": "e8a97c52-6676-4c48-8589-bd518afc4094"}, }, ) resp = self.client.begin_enroll_card( device_id=DeviceId("0418d6a2bb7a"), reset_ua_card=False ) assert resp.session_id == "e8a97c52-6676-4c48-8589-bd518afc4094" @responses.activate def test_fetch_enroll_card_status(self) -> None: """6.3 Fetch NFC Card Enrollment Status""" session_id = NfcCardEnrollmentSessionId("e8a97c52-6676-4c48-8589-bd518afc4094") responses.get( f"https://{self.host}/api/v1/developer/credentials/nfc_cards/sessions/{session_id}", match=[ matchers.header_matcher(self.common_headers), ], json={ "code": "SUCCESS", "msg": "success", "data": { # NOTE: altered from API docs to match testing on real instance (was "card_id") "id": "014A3151", "token": "821f90b262e90c5c0fbcddf3d6d2f3b94cc015d6e8104ab4fb96e4c8b8e90cb7", }, }, ) resp = self.client.fetch_enroll_card_status(session_id=session_id) assert resp.id == "014A3151" assert ( resp.token == "821f90b262e90c5c0fbcddf3d6d2f3b94cc015d6e8104ab4fb96e4c8b8e90cb7" ) @responses.activate def test_remove_enrollment_session(self) -> None: """6.4 Remove a Session Created for NFC Card Enrollment""" session_id = NfcCardEnrollmentSessionId("e8a97c52-6676-4c48-8589-bd518afc4094") responses.delete( f"https://{self.host}/api/v1/developer/credentials/nfc_cards/sessions/{session_id}", match=[ matchers.header_matcher(self.common_headers), ], json={"code": "SUCCESS", "msg": "success", "data": "success"}, ) resp = self.client.remove_enrollment_session(session_id=session_id) @responses.activate def test_fetch_nfc_card(self) -> None: """6.7 Fetch NFC Card""" nfc_card_token = NfcCardToken( "f77d69b08eaf5eb5d647ac1a0a73580f1b27494b345f40f54fa022a8741fa15c" ) responses.get( f"https://{self.host}/api/v1/developer/credentials/nfc_cards/tokens/{nfc_card_token}", match=[matchers.header_matcher(self.common_headers)], json={ "code": "SUCCESS", "data": { "alias": "", "card_type": "ua_card", "display_id": "100005", "note": "100005", "status": "assigned", "token": "f77d69b08eaf5eb5d647ac1a0a73580f1b27494b345f40f54fa022a8741fa15c", "user": { "avatar": "", "first_name": "H", "id": "e0051e08-c4d5-43db-87c8-a9b19cb66513", "last_name": "L", "name": "H L", }, "user_id": "e0051e08-c4d5-43db-87c8-a9b19cb66513", "user_type": "USER", }, "msg": "success", }, ) resp = self.client.fetch_nfc_card(nfc_card_token) @responses.activate def test_fetch_all_nfc_cards(self) -> None: """6.8 Fetch All NFC Cards""" responses.get( f"https://{self.host}/api/v1/developer/credentials/nfc_cards/tokens", match=[ matchers.header_matcher(self.common_headers), matchers.query_param_matcher({"page_num": 1, "page_size": 25}), ], json={ "code": "SUCCESS", "data": [ { "alias": "", "card_type": "ua_card", "display_id": "100004", "note": "100004", "status": "assigned", "token": "9e24cdfafebf63e58fd02c5f67732b478948e5793d31124239597d9a86b30dc4", "user": { "avatar": "", "first_name": "H", "id": "e0051e08-c4d5-43db-87c8-a9b19cb66513", "last_name": "L", "name": "H L", }, "user_id": "e0051e08-c4d5-43db-87c8-a9b19cb66513", "user_type": "USER", }, { "alias": "F77D69B03", "card_type": "ua_card", "display_id": "100005", "note": "100005", "status": "assigned", "token": "f77d69b08eaf5eb5d647ac1a0a73580f1b27494b345f40f54fa022a8741fa15c", "user": { "avatar": "", "first_name": "H2", "id": "34dc90a7-409f-4bf8-a5a8-1c59535a21b9", "last_name": "L", "name": "H2 L", }, "user_id": "34dc90a7-409f-4bf8-a5a8-1c59535a21b9", "user_type": "VISITOR", }, ], "msg": "succ", "pagination": {"page_num": 1, "page_size": 2, "total": 2}, }, ) resp = self.client.fetch_all_nfc_cards(page_num=1, page_size=25) # NOTE: not taken from API docs examples @responses.activate def test_fetch_all_nfc_cards__unpaged(self) -> None: """6.8 Fetch All NFC Cards""" responses.get( f"https://{self.host}/api/v1/developer/credentials/nfc_cards/tokens", match=[ matchers.header_matcher(self.common_headers), matchers.query_param_matcher({"page_num": 1, "page_size": 1}), ], json={ "code": "SUCCESS", "data": [ { "alias": "", "card_type": "ua_card", "display_id": "100004", "note": "100004", "status": "assigned", "token": "9e24cdfafebf63e58fd02c5f67732b478948e5793d31124239597d9a86b30dc4", "user": { "avatar": "", "first_name": "H", "id": "e0051e08-c4d5-43db-87c8-a9b19cb66513", "last_name": "L", "name": "H L", }, "user_id": "e0051e08-c4d5-43db-87c8-a9b19cb66513", "user_type": "USER", }, ], "msg": "succ", "pagination": {"page_num": 1, "page_size": 1, "total": 2}, }, ) responses.get( f"https://{self.host}/api/v1/developer/credentials/nfc_cards/tokens", match=[ matchers.header_matcher(self.common_headers), matchers.query_param_matcher({"page_num": 2, "page_size": 1}), ], json={ "code": "SUCCESS", "data": [ { "alias": "F77D69B03", "card_type": "ua_card", "display_id": "100005", "note": "100005", "status": "assigned", "token": "f77d69b08eaf5eb5d647ac1a0a73580f1b27494b345f40f54fa022a8741fa15c", "user": { "avatar": "", "first_name": "H2", "id": "34dc90a7-409f-4bf8-a5a8-1c59535a21b9", "last_name": "L", "name": "H2 L", }, "user_id": "34dc90a7-409f-4bf8-a5a8-1c59535a21b9", "user_type": "VISITOR", }, ], "msg": "succ", "pagination": {"page_num": 1, "page_size": 1, "total": 2}, }, ) resp = list(self.client.fetch_all_nfc_cards__unpaged(page_size=1)) assert resp[0].display_id == "100004" assert resp[1].display_id == "100005" @responses.activate def test_delete_nfc_card(self) -> None: """6.9 Delete NFC Card""" nfc_card_token = NfcCardToken( "f77d69b08eaf5eb5d647ac1a0a73580f1b27494b345f40f54fa022a8741fa15c" ) responses.delete( f"https://{self.host}/api/v1/developer/credentials/nfc_cards/tokens/{nfc_card_token}", match=[matchers.header_matcher(self.common_headers)], json={"code": "SUCCESS", "data": "success", "msg": "success"}, ) resp = self.client.delete_nfc_card(nfc_card_token) assert resp == "success"