|
| 1 | +from tests.integration.integration_test_case import IntegrationTestCase |
| 2 | +from tests.integration.it_utils import ( |
| 3 | + sign_and_reliable_submission_async, |
| 4 | + test_async_and_sync, |
| 5 | +) |
| 6 | +from tests.integration.reusable_values import DESTINATION, WALLET |
| 7 | +from xrpl.models import AccountObjects, AccountObjectType |
| 8 | +from xrpl.models.requests.ledger_entry import Credential, LedgerEntry |
| 9 | +from xrpl.models.response import ResponseStatus |
| 10 | +from xrpl.models.transactions.credential_accept import CredentialAccept |
| 11 | +from xrpl.models.transactions.credential_create import CredentialCreate |
| 12 | +from xrpl.models.transactions.credential_delete import CredentialDelete |
| 13 | +from xrpl.utils import str_to_hex |
| 14 | + |
| 15 | +_URI = "www.my-id.com/username" |
| 16 | + |
| 17 | + |
| 18 | +def is_cred_object_present( |
| 19 | + result: dict, issuer: str, subject: str, cred_type: str |
| 20 | +) -> bool: |
| 21 | + """ |
| 22 | + Args: |
| 23 | + result: JSON response from account_objects RPC |
| 24 | + issuer: Address of the credential issuer |
| 25 | + subject: Address of the credential subject |
| 26 | + cred_type: Type of the credential |
| 27 | +
|
| 28 | + Returns: |
| 29 | + bool: True if credential exists, False otherwise |
| 30 | + """ |
| 31 | + |
| 32 | + for val in result["account_objects"]: |
| 33 | + if ( |
| 34 | + val["Issuer"] == issuer |
| 35 | + and val["Subject"] == subject |
| 36 | + and val["CredentialType"] == cred_type |
| 37 | + ): |
| 38 | + return True |
| 39 | + |
| 40 | + return False |
| 41 | + |
| 42 | + |
| 43 | +class TestCredentials(IntegrationTestCase): |
| 44 | + @test_async_and_sync(globals()) |
| 45 | + async def test_valid(self, client): |
| 46 | + # Define entities helpful for Credential lifecycle |
| 47 | + _ISSUER = WALLET.address |
| 48 | + _SUBJECT = DESTINATION.address |
| 49 | + _SUBJECT_WALLET = DESTINATION |
| 50 | + |
| 51 | + # Disambiguate the sync/async, json/websocket tests with different |
| 52 | + # credential type values -- this avoids tecDUPLICATE error |
| 53 | + # self.value is defined inside the above decorator |
| 54 | + cred_type = str_to_hex("Passport_" + str(self.value)) |
| 55 | + |
| 56 | + tx = CredentialCreate( |
| 57 | + account=_ISSUER, |
| 58 | + subject=_SUBJECT, |
| 59 | + credential_type=cred_type, |
| 60 | + uri=str_to_hex(_URI), |
| 61 | + ) |
| 62 | + response = await sign_and_reliable_submission_async(tx, WALLET, client) |
| 63 | + self.assertEqual(response.status, ResponseStatus.SUCCESS) |
| 64 | + self.assertEqual(response.result["engine_result"], "tesSUCCESS") |
| 65 | + |
| 66 | + # Use the LedgerEntry RPC to validate the creation of the credential object |
| 67 | + ledger_entry_response = await client.request( |
| 68 | + LedgerEntry( |
| 69 | + credential=Credential( |
| 70 | + subject=_SUBJECT, issuer=_ISSUER, credential_type=cred_type |
| 71 | + ) |
| 72 | + ) |
| 73 | + ) |
| 74 | + |
| 75 | + self.assertEqual(ledger_entry_response.status, ResponseStatus.SUCCESS) |
| 76 | + |
| 77 | + # Execute the CredentialAccept transaction on the above Credential ledger object |
| 78 | + tx = CredentialAccept( |
| 79 | + issuer=_ISSUER, account=_SUBJECT, credential_type=cred_type |
| 80 | + ) |
| 81 | + # CredentialAccept transaction is submitted by SUBJECT |
| 82 | + response = await sign_and_reliable_submission_async(tx, _SUBJECT_WALLET, client) |
| 83 | + self.assertEqual(response.status, ResponseStatus.SUCCESS) |
| 84 | + self.assertEqual(response.result["engine_result"], "tesSUCCESS") |
| 85 | + |
| 86 | + # Execute the CredentialDelete transaction |
| 87 | + # Subject initiates the deletion of the Credential ledger object |
| 88 | + tx = CredentialDelete( |
| 89 | + issuer=_ISSUER, account=_SUBJECT, credential_type=cred_type |
| 90 | + ) |
| 91 | + |
| 92 | + response = await sign_and_reliable_submission_async(tx, _SUBJECT_WALLET, client) |
| 93 | + self.assertEqual(response.status, ResponseStatus.SUCCESS) |
| 94 | + self.assertEqual(response.result["engine_result"], "tesSUCCESS") |
| 95 | + |
| 96 | + # The credential ledger object must be deleted from both the Issuer and Subject |
| 97 | + # account's directory pages |
| 98 | + account_objects_response = await client.request( |
| 99 | + AccountObjects(account=_ISSUER, type=AccountObjectType.CREDENTIAL) |
| 100 | + ) |
| 101 | + self.assertFalse( |
| 102 | + is_cred_object_present( |
| 103 | + account_objects_response.result, |
| 104 | + issuer=_ISSUER, |
| 105 | + subject=_SUBJECT, |
| 106 | + cred_type=cred_type, |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + # Verify that the Credential object has been deleted from the Subject's |
| 111 | + # directory page as well |
| 112 | + account_objects_response = await client.request( |
| 113 | + AccountObjects(account=_SUBJECT, type=AccountObjectType.CREDENTIAL) |
| 114 | + ) |
| 115 | + self.assertFalse( |
| 116 | + is_cred_object_present( |
| 117 | + account_objects_response.result, |
| 118 | + issuer=_ISSUER, |
| 119 | + subject=_SUBJECT, |
| 120 | + cred_type=cred_type, |
| 121 | + ) |
| 122 | + ) |
0 commit comments