Skip to content

Commit

Permalink
Update integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: DaevMithran <[email protected]>
  • Loading branch information
DaevMithran committed Jan 13, 2025
1 parent b5ad8d4 commit 906f918
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 188 deletions.
98 changes: 46 additions & 52 deletions cheqd/cheqd/anoncreds/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def make_revocation_registry_id(
return f"{revocation_registry_definition.issuer_id}/resources/{resource_id}"

@staticmethod
def split_schema_id(schema_id: str) -> (str, str):
def split_did_url(schema_id: str) -> (str, str):
"""Derive the ID for a schema."""
ids = schema_id.split("/")
return ids[0], ids[2]
Expand Down Expand Up @@ -126,7 +126,7 @@ async def get_schema(self, _profile: Profile, schema_id: str) -> GetSchemaResult
schema = resource_with_metadata.resource
metadata = resource_with_metadata.metadata

(did, resource_id) = self.split_schema_id(schema_id)
(did, resource_id) = self.split_did_url(schema_id)

anoncreds_schema = AnonCredsSchema(
issuer_id=did,
Expand Down Expand Up @@ -170,20 +170,19 @@ async def register_schema(

LOGGER.debug("schema value: %s", cheqd_schema)
try:
resource_state = await self._create_and_publish_resource(
(job_id, resource_state) = await self._create_and_publish_resource(
profile,
self.registrar.DID_REGISTRAR_BASE_URL,
self.resolver.DID_RESOLVER_BASE_URL,
cheqd_schema,
)
job_id = resource_state.get("jobId")
resource = resource_state.get("resource")
resource_id = resource.get("id")
schema_id = self.make_schema_id(schema, resource_id)
LOGGER.debug("resource state: %s", resource_state)
schema_id = resource_state.get("didUrl")
(_, resource_id) = self.split_did_url(schema_id)
except Exception as err:
raise AnonCredsRegistrationError(f"{err}")
return SchemaResult(
job_id=job_id,
job_id=schema_id,
schema_state=SchemaState(
state=SchemaState.STATE_FINISHED,
schema_id=schema_id,
Expand All @@ -205,7 +204,7 @@ async def get_credential_definition(
)
credential_definition = resource_with_metadata.resource
metadata = resource_with_metadata.metadata
(did, resource_id) = self.split_schema_id(credential_definition_id)
(did, resource_id) = self.split_did_url(credential_definition_id)

anoncreds_credential_definition = CredDef(
issuer_id=did,
Expand Down Expand Up @@ -249,22 +248,17 @@ async def register_credential_definition(
did=credential_definition.issuer_id,
)

resource_state = await self._create_and_publish_resource(
(job_id, resource_state) = await self._create_and_publish_resource(
profile,
self.registrar.DID_REGISTRAR_BASE_URL,
self.resolver.DID_RESOLVER_BASE_URL,
cred_def,
)
job_id = resource_state.get("jobId")
resource = resource_state.get("resource")
resource_id = resource.get("id")

credential_definition_id = self.make_credential_definition_id(
credential_definition, resource_id
)
credential_definition_id = resource_state.get("didUrl")
(_, resource_id) = self.split_did_url(credential_definition_id)

return CredDefResult(
job_id=job_id,
job_id=credential_definition_id,
credential_definition_state=CredDefState(
state=CredDefState.STATE_FINISHED,
credential_definition_id=credential_definition_id,
Expand All @@ -288,7 +282,7 @@ async def get_revocation_registry_definition(
revocation_registry_definition = resource_with_metadata.resource
metadata = resource_with_metadata.metadata

(did, resource_id) = self.split_schema_id(revocation_registry_id)
(did, resource_id) = self.split_did_url(revocation_registry_id)

anoncreds_revocation_registry_definition = RevRegDef(
issuer_id=did,
Expand Down Expand Up @@ -336,23 +330,20 @@ async def register_revocation_registry_definition(
did=revocation_registry_definition.issuer_id,
)

resource_state = await self._create_and_publish_resource(
(job_id, resource_state) = await self._create_and_publish_resource(
profile,
self.registrar.DID_REGISTRAR_BASE_URL,
self.resolver.DID_RESOLVER_BASE_URL,
rev_reg_def,
)
job_id = resource_state.get("jobId")
resource = resource_state.get("resource")
resource_id = resource.get("id")
revocation_registry_definition_id = resource_state.get("didUrl")
(_, resource_id) = self.split_did_url(revocation_registry_definition_id)

return RevRegDefResult(
job_id=job_id,
job_id=revocation_registry_definition_id,
revocation_registry_definition_state=RevRegDefState(
state=RevRegDefState.STATE_FINISHED,
revocation_registry_definition_id=self.make_revocation_registry_id(
revocation_registry_definition, resource_id
),
revocation_registry_definition_id=revocation_registry_definition_id,
revocation_registry_definition=revocation_registry_definition,
),
registration_metadata={
Expand All @@ -378,7 +369,7 @@ async def get_revocation_list(
resource_name = revocation_registry_definition.revocation_registry_metadata.get(
"resourceName"
)
(did, resource_id) = self.split_schema_id(revocation_registry_id)
(did, resource_id) = self.split_did_url(revocation_registry_id)

resource_type = CheqdAnoncredsResourceType.revocationStatusList.value
epoch_time = timestamp_to or int(time.time())
Expand Down Expand Up @@ -409,7 +400,7 @@ async def get_schema_info_by_id(self, schema_id: str) -> AnoncredsSchemaInfo:
"""Get a schema info from the registry."""
resource_with_metadata = await self.resolver.resolve_resource(schema_id)
schema = resource_with_metadata.resource
(did, resource_id) = self.split_schema_id(schema_id)
(did, resource_id) = self.split_did_url(schema_id)
anoncreds_schema = AnoncredsSchemaInfo(
issuer_id=did,
name=schema["name"],
Expand Down Expand Up @@ -447,18 +438,17 @@ async def register_revocation_list(
did=rev_reg_def.issuer_id,
)

resource_state = await self._create_and_publish_resource(
(job_id, resource_state) = await self._create_and_publish_resource(
profile,
self.registrar.DID_REGISTRAR_BASE_URL,
self.resolver.DID_RESOLVER_BASE_URL,
rev_status_list,
)
job_id = resource_state.get("jobId")
resource = resource_state.get("resource")
resource_id = resource.get("id")
did_url = resource_state.get("didUrl")
(_, resource_id) = self.split_did_url(did_url)

return RevListResult(
job_id=job_id,
job_id=did_url,
revocation_list_state=RevListState(
state=RevListState.STATE_FINISHED,
revocation_list=rev_list,
Expand Down Expand Up @@ -503,18 +493,17 @@ async def update_revocation_list(
did=rev_reg_def.issuer_id,
)

resource_state = await self._update_and_publish_resource(
job_id, resource_state = await self._update_and_publish_resource(
profile,
self.registrar.DID_REGISTRAR_BASE_URL,
self.resolver.DID_RESOLVER_BASE_URL,
rev_status_list,
)
job_id = resource_state.get("jobId")
resource = resource_state.get("resource")
resource_id = resource.get("id")
did_url = resource_state.get("didUrl")
(_, resource_id) = self.split_did_url(did_url)

return RevListResult(
job_id=job_id,
job_id=did_url,
revocation_list_state=RevListState(
state=RevListState.STATE_FINISHED,
revocation_list=curr_list,
Expand All @@ -533,7 +522,7 @@ async def _create_and_publish_resource(
registrar_url: str,
resolver_url: str,
options: ResourceCreateRequestOptions,
) -> dict:
) -> (str, dict):
"""Create, Sign and Publish a Resource."""
cheqd_manager = CheqdDIDManager(profile, registrar_url, resolver_url)
async with profile.session() as session:
Expand All @@ -547,7 +536,9 @@ async def _create_and_publish_resource(
)

job_id: str = create_request_res.get("jobId")
resource_state = create_request_res.get("resourceState")
resource_state = create_request_res.get("didUrlState")
if not resource_state:
raise Exception("No signing requests available for update.")

LOGGER.debug("JOBID %s", job_id)
if resource_state.get("state") == "action":
Expand All @@ -558,23 +549,25 @@ async def _create_and_publish_resource(
signed_responses = await CheqdDIDManager.sign_requests(
wallet, signing_requests
)
LOGGER.debug("Signed Responses %s", signed_responses)

# publish resource
publish_resource_res = await cheqd_manager.registrar.create_resource(
SubmitSignatureOptions(
jobId=job_id,
secret=Secret(signingResponse=signed_responses),
did=options.did,
),
)
resource_state = publish_resource_res.get("resourceState")
resource_state = publish_resource_res.get("didUrlState")
if resource_state.get("state") != "finished":
raise AnonCredsRegistrationError(
f"Error publishing Resource {resource_state.get("reason")}"
f"Error publishing Resource {resource_state.get('reason')}"
)
return resource_state
return job_id, resource_state
else:
raise AnonCredsRegistrationError(
f"Error publishing Resource {resource_state.get("reason")}"
f"Error publishing Resource {resource_state.get('reason')}"
)
except Exception as err:
raise AnonCredsRegistrationError(f"{err}")
Expand All @@ -585,7 +578,7 @@ async def _update_and_publish_resource(
registrar_url: str,
resolver_url: str,
options: ResourceUpdateRequestOptions,
) -> dict:
) -> (str, dict):
"""Update, Sign and Publish a Resource."""
cheqd_manager = CheqdDIDManager(profile, registrar_url, resolver_url)
async with profile.session() as session:
Expand All @@ -599,7 +592,7 @@ async def _update_and_publish_resource(
)

job_id: str = create_request_res.get("jobId")
resource_state = create_request_res.get("resourceState")
resource_state = create_request_res.get("didUrlState")

LOGGER.debug("JOBID %s", job_id)
if resource_state.get("state") == "action":
Expand All @@ -610,23 +603,24 @@ async def _update_and_publish_resource(
signed_responses = await CheqdDIDManager.sign_requests(
wallet, signing_requests
)

LOGGER.debug("Signed Responses %s", signed_responses)
# publish resource
publish_resource_res = await cheqd_manager.registrar.update_resource(
SubmitSignatureOptions(
jobId=job_id,
secret=Secret(signingResponse=signed_responses),
did=options.did,
),
)
resource_state = publish_resource_res.get("resourceState")
resource_state = publish_resource_res.get("didUrlState")
if resource_state.get("state") != "finished":
raise AnonCredsRegistrationError(
f"Error publishing Resource {resource_state.get("reason")}"
f"Error publishing Resource {resource_state.get('reason')}"
)
return resource_state
return job_id, resource_state
else:
raise AnonCredsRegistrationError(
f"Error publishing Resource {resource_state.get("reason")}"
f"Error publishing Resource {resource_state.get('reason')}"
)
except Exception as err:
raise AnonCredsRegistrationError(f"{err}")
27 changes: 22 additions & 5 deletions cheqd/cheqd/anoncreds/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from acapy_agent.wallet.key_type import KeyTypes

from ...did_method import CHEQD
from ...did.base import ResourceCreateRequestOptions, ResourceUpdateRequestOptions


@pytest.fixture
Expand Down Expand Up @@ -54,19 +55,21 @@ def mock_schema():

@pytest.fixture
def mock_create_and_publish_resource():
return {
"jobId": "MOCK_JOB_ID",
"resource": {"id": "MOCK_RESOURCE_ID"},
return "MOCK_JOB_ID", {
"id": "MOCK_ID",
"didUrl": "MOCK_ISSUER_ID/resources/MOCK_RESOURCE_ID",
"state": "finished",
"content": "MOCK_VALUE",
}


@pytest.fixture
def mock_update_and_publish_resource():
return {
"jobId": "MOCK_JOB_ID",
return "MOCK_JOB_ID", {
"resource": {"id": "MOCK_RESOURCE_ID"},
"id": "MOCK_ID",
"didUrl": "MOCK_ISSUER_ID/resources/MOCK_RESOURCE_ID",
"state": "finished",
}


Expand Down Expand Up @@ -139,3 +142,17 @@ async def mock_profile_for_manager():
profile.context.injector.bind_instance(BaseCache, InMemoryCache())

return profile


@pytest.fixture
def mock_resource_create_options():
return ResourceCreateRequestOptions(
did="MOCK_VALUE", content="MOCK_VALUE", name="MOCK_VALUE", type="MOCK_VALUE"
)


@pytest.fixture
def mock_resource_update_options():
return ResourceUpdateRequestOptions(
did="MOCK_VALUE", content="MOCK_VALUE", name="MOCK_VALUE", type="MOCK_VALUE"
)
Loading

0 comments on commit 906f918

Please sign in to comment.