Skip to content

Commit

Permalink
ADD: tests for VCDI handler
Browse files Browse the repository at this point in the history
- test_store_credential

Signed-off-by: EmadAnwer <emadanwer.official@gmail.com>
  • Loading branch information
EmadAnwer committed Jun 9, 2024
1 parent 077b889 commit 61166ad
Showing 1 changed file with 148 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,21 @@
CredDef,
GetCredDefResult,
)
from aries_cloudagent.anoncreds.models.anoncreds_revocation import (
GetRevRegDefResult,
RevRegDef,
)
from aries_cloudagent.anoncreds.registry import AnonCredsRegistry
from aries_cloudagent.askar.profile_anon import AskarAnoncredsProfile
from aries_cloudagent.protocols.issue_credential.v2_0.messages.cred_issue import (
V20CredIssue,
)
from aries_cloudagent.protocols.issue_credential.v2_0.models.cred_ex_record import (
V20CredExRecord,
)
from aries_cloudagent.wallet.did_info import DIDInfo
import pytest
from .......anoncreds.holder import AnonCredsHolder
from .......anoncreds.holder import AnonCredsHolder, AnonCredsHolderError
from .......messaging.credential_definitions.util import (
CRED_DEF_SENT_RECORD_TYPE,
)
Expand Down Expand Up @@ -746,3 +753,143 @@ async def test_match_sent_cred_def_id_error(self):
assert "Issuer has no operable cred def for proposal spec " in str(
context.exception
)

@pytest.mark.asyncio
async def test_store_credential(self):
attr_values = {
"legalName": "value",
"jurisdictionId": "value",
"incorporationDate": "value",
}
cred_preview = V20CredPreview(
attributes=[
V20CredAttrSpec(name=k, value=v) for (k, v) in attr_values.items()
]
)
cred_offer = V20CredOffer(
credential_preview=cred_preview,
formats=[
V20CredFormat(
attach_id="0",
format_=ATTACHMENT_FORMAT[CRED_20_OFFER][
V20CredFormat.Format.VC_DI.api
],
)
],
offers_attach=[AttachDecorator.data_base64(VCDI_OFFER, ident="0")],
)
cred_request = V20CredRequest(
formats=[
V20CredFormat(
attach_id="0",
format_=ATTACHMENT_FORMAT[CRED_20_REQUEST][
V20CredFormat.Format.VC_DI.api
],
)
],
requests_attach=[AttachDecorator.data_base64(VCDI_CRED_REQ, ident="0")],
)
cred_issue = V20CredIssue(
formats=[
V20CredFormat(
attach_id="0",
format_=ATTACHMENT_FORMAT[CRED_20_ISSUE][
V20CredFormat.Format.VC_DI.api
],
)
],
credentials_attach=[AttachDecorator.data_base64(VCDI_CRED, ident="0")],
)
cred_ex_record = V20CredExRecord(
cred_ex_id="dummy-cxid",
cred_offer=cred_offer.serialize(),
cred_request=cred_request.serialize(),
cred_issue=cred_issue.serialize(),
initiator=V20CredExRecord.INITIATOR_SELF,
role=V20CredExRecord.ROLE_ISSUER,
state=V20CredExRecord.STATE_REQUEST_RECEIVED,
)
cred_id = "dummy-cred-id"

# Define your mock credential definition
mock_credential_definition_result = GetCredDefResult(
credential_definition=CredDef(
issuer_id=TEST_DID, schema_id=SCHEMA_ID, type="CL", tag="tag1", value={}
),
credential_definition_id=CRED_DEF_ID,
resolution_metadata={},
credential_definition_metadata={},
)
mock_creds_registry = mock.AsyncMock()
mock_creds_registry.get_credential_definition = mock.AsyncMock(
return_value=mock_credential_definition_result
)

revocation_registry = RevRegDef(
cred_def_id=CRED_DEF_ID,
issuer_id=TEST_DID,
tag="tag1",
type="CL_ACCUM",
value={},
)

mock_creds_registry.get_revocation_registry_definition = mock.AsyncMock(
return_value=GetRevRegDefResult(
revocation_registry=revocation_registry,
revocation_registry_id="rr-id",
resolution_metadata={},
revocation_registry_metadata={},
)
)
# Inject the MagicMock into the context
self.context.injector.bind_instance(AnonCredsRegistry, mock_creds_registry)
self.profile = mock.AsyncMock(AskarAnoncredsProfile)
self.context.injector.bind_instance(AskarAnoncredsProfile, self.profile)
with mock.patch.object(
test_module.AnonCredsRevocation,
"get_or_fetch_local_tails_path",
mock.CoroutineMock(),
) as mock_get_or_fetch_local_tails_path:
with self.assertRaises(V20CredFormatError) as context:
await self.handler.store_credential(cred_ex_record, cred_id)
assert (
"No credential exchange didcomm/ detail record found for cred ex id dummy-cxid"
in str(context.exception)
)

record = V20CredExRecordIndy(
cred_ex_indy_id="dummy-cxid",
rev_reg_id="rr-id",
cred_ex_id="dummy-cxid",
cred_id_stored=cred_id,
cred_request_metadata="dummy-metadata",
cred_rev_id="0",
)

record.save = mock.CoroutineMock()
self.handler.get_detail_record = mock.AsyncMock(return_value=record)
with mock.patch.object(
AnonCredsHolder,
"store_credential_w3c",
mock.AsyncMock(),
) as mock_store_credential:
# Error case: no cred ex record found

await self.handler.store_credential(cred_ex_record, cred_id)

mock_store_credential.assert_called_once_with(
mock_credential_definition_result.credential_definition.serialize(),
VCDI_CRED["credential"],
record.cred_request_metadata,
None,
credential_id=cred_id,
rev_reg_def=revocation_registry.serialize(),
)

with mock.patch.object(
AnonCredsHolder,
"store_credential_w3c",
mock.AsyncMock(side_effect=AnonCredsHolderError),
) as mock_store_credential:
with self.assertRaises(AnonCredsHolderError) as context:
await self.handler.store_credential(cred_ex_record, cred_id)

0 comments on commit 61166ad

Please sign in to comment.