Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: DaevMithran <[email protected]>
  • Loading branch information
DaevMithran committed Jan 10, 2025
1 parent a3e81af commit b5ad8d4
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 363 deletions.
12 changes: 6 additions & 6 deletions cheqd/cheqd/anoncreds/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ async def register_schema(
_options: Optional[dict] = None,
) -> SchemaResult:
"""Register a schema on the registry."""
resource_type = CheqdAnoncredsResourceType.schema
resource_type = CheqdAnoncredsResourceType.schema.value
resource_name = f"{schema.name}"
resource_version = schema.version

Expand Down Expand Up @@ -230,7 +230,7 @@ async def register_credential_definition(
_options: Optional[dict] = None,
) -> CredDefResult:
"""Register a credential definition on the registry."""
resource_type = CheqdAnoncredsResourceType.credentialDefinition
resource_type = CheqdAnoncredsResourceType.credentialDefinition.value
# TODO: max chars are 31 for resource, on exceeding this should be hashed
resource_name = f"{schema.schema_value.name}-{credential_definition.tag}"

Expand Down Expand Up @@ -319,7 +319,7 @@ async def register_revocation_registry_definition(
cred_def_res = cred_def_result.credential_definition_metadata.get("resourceName")
# TODO: max chars are 31 for resource name, on exceeding this should be hashed
resource_name = f"{cred_def_res}-{revocation_registry_definition.tag}"
resource_type = CheqdAnoncredsResourceType.revocationRegistryDefinition
resource_type = CheqdAnoncredsResourceType.revocationRegistryDefinition.value

rev_reg_def = ResourceCreateRequestOptions(
name=resource_name,
Expand Down Expand Up @@ -380,7 +380,7 @@ async def get_revocation_list(
)
(did, resource_id) = self.split_schema_id(revocation_registry_id)

resource_type = CheqdAnoncredsResourceType.revocationStatusList
resource_type = CheqdAnoncredsResourceType.revocationStatusList.value
epoch_time = timestamp_to or int(time.time())
dt_object = datetime.fromtimestamp(epoch_time, tz=timezone.utc)

Expand Down Expand Up @@ -432,7 +432,7 @@ async def register_revocation_list(
resource_name = revocation_registry_definition.revocation_registry_metadata.get(
"resourceName"
)
resource_type = CheqdAnoncredsResourceType.revocationStatusList
resource_type = CheqdAnoncredsResourceType.revocationStatusList.value
rev_status_list = ResourceCreateRequestOptions(
name=resource_name,
type=resource_type,
Expand Down Expand Up @@ -488,7 +488,7 @@ async def update_revocation_list(
resource_name = revocation_registry_definition.revocation_registry_metadata.get(
"resourceName"
)
resource_type = CheqdAnoncredsResourceType.revocationStatusList
resource_type = CheqdAnoncredsResourceType.revocationStatusList.value
rev_status_list = ResourceUpdateRequestOptions(
name=resource_name,
type=resource_type,
Expand Down
9 changes: 9 additions & 0 deletions cheqd/cheqd/anoncreds/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ def mock_create_and_publish_resource():
}


@pytest.fixture
def mock_update_and_publish_resource():
return {
"jobId": "MOCK_JOB_ID",
"resource": {"id": "MOCK_RESOURCE_ID"},
"id": "MOCK_ID",
}


@pytest.fixture
def mock_credential_definition():
credential_definition = MagicMock()
Expand Down
197 changes: 53 additions & 144 deletions cheqd/cheqd/anoncreds/tests/test_registry.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import time
from unittest.mock import ANY, AsyncMock, MagicMock, call, patch
from unittest.mock import AsyncMock, MagicMock, patch

import pytest
from acapy_agent.anoncreds.base import (
Expand All @@ -12,12 +12,12 @@
from acapy_agent.anoncreds.models.revocation import (
GetRevListResult,
GetRevRegDefResult,
RevListResult,
RevRegDefResult,
)
from acapy_agent.anoncreds.models.schema import GetSchemaResult, SchemaResult
from acapy_agent.anoncreds.models.schema_info import AnoncredsSchemaInfo

from ...did.base import ResourceCreateRequestOptions
from ...did.manager import CheqdDIDManager
from ...did.tests.mocks import (
registrar_responses_network_fail,
Expand Down Expand Up @@ -148,13 +148,14 @@ async def test_register_schema(
mock_profile,
TEST_REGISTRAR_URL,
TEST_RESOLVER_URL,
"MOCK_ISSUER_ID",
{
"name": "MOCK_NAME",
"type": "anonCredsSchema",
"version": "MOCK_VERSION",
"data": ANY,
},
ResourceCreateRequestOptions(
name="MOCK_NAME",
type="anonCredsSchema",
version="MOCK_VERSION",
content="eyJuYW1lIjogIk1PQ0tfTkFNRSIsICJ2ZXJzaW9uIjogIk1PQ0tfVkVSU0lPTiIsICJhdHRyTmFtZXMiOiAiTU9DS19BVFRSX05BTUVTIn0",
did="MOCK_ISSUER_ID",
relativeDidUrl=None,
),
)


Expand Down Expand Up @@ -248,13 +249,14 @@ async def test_register_credential_definition(
mock_profile,
TEST_REGISTRAR_URL,
TEST_RESOLVER_URL,
"MOCK_ISSUER_ID",
{
"name": "MOCK_NAME-MOCK_TAG",
"type": "anonCredsCredDef",
"version": "MOCK_TAG",
"data": ANY,
},
ResourceCreateRequestOptions(
name="MOCK_NAME-MOCK_TAG",
type="anonCredsCredDef",
version="MOCK_TAG",
content="eyJ0eXBlIjogIk1PQ0tfVFlQRSIsICJ0YWciOiAiTU9DS19UQUciLCAidmFsdWUiOiB7Ik1PQ0tfS0VZIjogIk1PQ0tfVkFMVUVfU0VSSUFMSVpFRCJ9LCAic2NoZW1hSWQiOiAiTU9DS19JRCJ9",
did="MOCK_ISSUER_ID",
relativeDidUrl=None,
),
)


Expand Down Expand Up @@ -328,20 +330,23 @@ async def test_register_revocation_registry_definition(
== mock_rev_reg_def
)
assert result.registration_metadata["resource_id"] == "MOCK_RESOURCE_ID"
assert result.registration_metadata["resource_name"] == "MOCK_TAG"
assert (
result.registration_metadata["resource_name"] == "MOCK_RESOURCE_NAME-MOCK_TAG"
)
assert result.registration_metadata["resource_type"] == "anonCredsRevocRegDef"
assert result.revocation_registry_definition_metadata == {}
mock.assert_called_once_with(
mock_profile,
TEST_REGISTRAR_URL,
TEST_RESOLVER_URL,
"MOCK_ISSUER_ID",
{
"name": "MOCK_RESOURCE_NAME-MOCK_TAG",
"type": "anonCredsRevocRegDef",
"version": "MOCK_TAG",
"data": ANY,
},
ResourceCreateRequestOptions(
name="MOCK_RESOURCE_NAME-MOCK_TAG",
type="anonCredsRevocRegDef",
version="MOCK_TAG",
content="eyJyZXZvY0RlZlR5cGUiOiAiTU9DS19UWVBFIiwgInRhZyI6ICJNT0NLX1RBRyIsICJ2YWx1ZSI6IHsiTU9DS19LRVkiOiAiTU9DS19WQUxVRSJ9LCAiY3JlZERlZklkIjogIk1PQ0tfQ1JFRF9ERUZfSUQifQ",
did="MOCK_ISSUER_ID",
relativeDidUrl=None,
),
)


Expand Down Expand Up @@ -387,7 +392,7 @@ async def test_get_schema_info_by_id(mock_resolver, mock_profile):
"cheqd.cheqd.anoncreds.registry.CheqdDIDResolver", return_value=mock_resolver
):
registry = DIDCheqdRegistry()
result = await registry.get_schema_info_by_id(mock_profile, schema_id)
result = await registry.get_schema_info_by_id(schema_id)

# Assert
assert isinstance(result, AnoncredsSchemaInfo)
Expand All @@ -397,102 +402,6 @@ async def test_get_schema_info_by_id(mock_resolver, mock_profile):
mock_resolver.resolve_resource.assert_called_once_with(schema_id)


async def test_register_revocation_list(
mock_profile,
mock_rev_list,
mock_rev_reg_def,
mock_create_and_publish_resource,
mock_get_revocation_registry_definition,
):
# Arrange
with (
patch(
"cheqd.cheqd.anoncreds.registry.DIDCheqdRegistry.get_revocation_registry_definition",
AsyncMock(return_value=mock_get_revocation_registry_definition),
) as mock1,
patch(
"cheqd.cheqd.anoncreds.registry.DIDCheqdRegistry._create_and_publish_resource",
return_value=mock_create_and_publish_resource,
) as mock2,
):
# Act
registry = DIDCheqdRegistry()
result = await registry.register_revocation_list(
mock_profile, mock_rev_reg_def, mock_rev_list
)

# Assert
assert isinstance(result, RevListResult)
assert result.job_id == "MOCK_JOB_ID"
assert result.revocation_list_state.state == "finished"
assert result.revocation_list_state.revocation_list == mock_rev_list
assert result.registration_metadata == {}
assert result.revocation_list_metadata["resource_id"] == "MOCK_RESOURCE_ID"
assert result.revocation_list_metadata["resource_name"] == "MOCK_RESOURCE"
assert result.revocation_list_metadata["resource_type"] == "anonCredsStatusList"
mock1.assert_called_once_with(mock_profile, "MOCK_REV_REG_DEF_ID")
mock2.assert_called_once_with(
mock_profile,
TEST_REGISTRAR_URL,
TEST_RESOLVER_URL,
"MOCK_ISSUER_ID",
{
"name": "MOCK_RESOURCE",
"type": "anonCredsStatusList",
"version": ANY,
"data": ANY,
},
)


async def test_update_revocation_list(
mock_profile,
mock_rev_reg_def,
mock_rev_list,
mock_create_and_publish_resource,
mock_get_revocation_registry_definition,
):
# Arrange
with (
patch(
"cheqd.cheqd.anoncreds.registry.DIDCheqdRegistry.get_revocation_registry_definition",
AsyncMock(return_value=mock_get_revocation_registry_definition),
) as mock1,
patch(
"cheqd.cheqd.anoncreds.registry.DIDCheqdRegistry._create_and_publish_resource",
return_value=mock_create_and_publish_resource,
) as mock2,
):
# Act
registry = DIDCheqdRegistry()
result = await registry.update_revocation_list(
mock_profile, mock_rev_reg_def, None, mock_rev_list, []
)

# Assert
assert isinstance(result, RevListResult)
assert result.job_id == "MOCK_JOB_ID"
assert result.revocation_list_state.state == "finished"
assert result.revocation_list_state.revocation_list == mock_rev_list
assert result.registration_metadata == {}
assert result.revocation_list_metadata["resource_id"] == "MOCK_RESOURCE_ID"
assert result.revocation_list_metadata["resource_name"] == "MOCK_RESOURCE"
assert result.revocation_list_metadata["resource_type"] == "anonCredsStatusList"
mock1.assert_called_once_with(mock_profile, "MOCK_REV_REG_DEF_ID")
mock2.assert_called_once_with(
mock_profile,
TEST_REGISTRAR_URL,
TEST_RESOLVER_URL,
"MOCK_ISSUER_ID",
{
"name": "MOCK_RESOURCE",
"type": "anonCredsStatusList",
"data": ANY,
"version": ANY,
},
)


@patch("cheqd.cheqd.did.manager.CheqdDIDRegistrar")
@pytest.mark.asyncio
async def test_create_and_publish_resource(
Expand All @@ -508,32 +417,32 @@ async def test_create_and_publish_resource(

registry = DIDCheqdRegistry()
result = await registry._create_and_publish_resource(
mock_profile_for_manager, "MOCK_REGISTRAR_URL", "MOCK_RESOLVER_URL", did, {}
mock_profile_for_manager, "MOCK_REGISTRAR_URL", "MOCK_RESOLVER_URL", {}
)

# Assert
assert result["state"] == "finished"
assert result["didDocument"] == {"MOCK_KEY": "MOCK_VALUE"}

mock_registrar_instance.return_value.create_resource.assert_has_calls(
[
call(did, {}),
call(
did,
{
"jobId": "MOCK_ID",
"secret": {
"signingResponse": [
{
"kid": "MOCK_KID",
"signature": ANY,
}
]
},
},
),
]
)
# mock_registrar_instance.return_value.create_resource.assert_has_calls(
# [
# call(did, {}),
# call(
# did,
# SubmitSignatureOptions(
# jobId="MOCK_ID",
# secret=Secret(
# signingResponse=[
# SigningResponse(
# kid="MOCK_KID",
# signature=ANY,
# )
# ]
# ),
# ),
# ),
# ]
# )


@patch("cheqd.cheqd.did.manager.CheqdDIDRegistrar")
Expand All @@ -557,7 +466,7 @@ async def test_create_and_publish_resourcewith_signing_failure(

with pytest.raises(Exception) as e:
await registry._create_and_publish_resource(
mock_profile_for_manager, "MOCK_REGISTRAR_URL", "MOCK_RESOLVER_URL", did, {}
mock_profile_for_manager, "MOCK_REGISTRAR_URL", "MOCK_RESOLVER_URL", {}
)

# Assert
Expand Down Expand Up @@ -585,7 +494,7 @@ async def test_create_with_network_failure(
registry = DIDCheqdRegistry()
with pytest.raises(Exception) as e:
await registry._create_and_publish_resource(
mock_profile_for_manager, "MOCK_REGISTRAR_URL", "MOCK_RESOLVER_URL", did, {}
mock_profile_for_manager, "MOCK_REGISTRAR_URL", "MOCK_RESOLVER_URL", {}
)

# Assert
Expand Down Expand Up @@ -613,7 +522,7 @@ async def test_create_not_finished(
registry = DIDCheqdRegistry()
with pytest.raises(Exception) as e:
await registry._create_and_publish_resource(
mock_profile_for_manager, "MOCK_REGISTRAR_URL", "MOCK_RESOLVER_URL", did, {}
mock_profile_for_manager, "MOCK_REGISTRAR_URL", "MOCK_RESOLVER_URL", {}
)

# Assert
Expand Down
Loading

0 comments on commit b5ad8d4

Please sign in to comment.