Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anoncreds-rs pytest update #2541

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"vscode": {
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
"ms-python.vscode-pylance",
"ms-python.black-formatter"
],
"settings": {
"python.testing.pytestArgs": [
Expand All @@ -26,17 +27,15 @@
"editor.defaultFormatter": null,
"editor.formatOnSave": false, // enable per language
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter",
"editor.formatOnSave": true
},
"python.formatting.provider": "black",
"python.formatting.blackPath": "/usr/local/py-utils/bin/black",
"python.formatting.blackArgs": []
}
}
}
},

"features": {
"docker-in-docker": "latest",
"docker-in-docker": "latest"
},

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
Expand Down
7 changes: 4 additions & 3 deletions aries_cloudagent/anoncreds/issuer.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
Schema,
)


from ..askar.profile import AskarProfile, AskarProfileSession
from ..core.error import BaseError
from ..core.event_bus import Event, EventBus
from ..core.profile import Profile
from .base import AnonCredsSchemaAlreadyExists
from .base import AnonCredsSchemaAlreadyExists, BaseAnonCredsError
from .events import CredDefFinishedEvent
from .models.anoncreds_cred_def import CredDef, CredDefResult
from .models.anoncreds_schema import AnonCredsSchema, SchemaResult, SchemaState
Expand Down Expand Up @@ -220,7 +221,7 @@ async def create_and_register_schema(
raise AnonCredsIssuerError(
"Schema already exists but was not in wallet; stored in wallet"
) from err
except AnoncredsError as err:
except (AnoncredsError, BaseAnonCredsError) as err:
raise AnonCredsIssuerError("Error creating schema") from err

async def finish_schema(self, job_id: str, schema_id: str):
Expand Down Expand Up @@ -335,7 +336,7 @@ async def create_and_register_credential_definition(
CredDef.from_native(cred_def),
options,
)
except AnoncredsError as err:
except (AnoncredsError, BaseAnonCredsError) as err:
raise AnonCredsIssuerError("Error creating credential definition") from err

# Store the cred def and it's components
Expand Down
1 change: 0 additions & 1 deletion aries_cloudagent/anoncreds/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ async def register_credential_definition(
registrar = await self._registrar_for_identifier(
credential_definition.issuer_id
)

return await registrar.register_credential_definition(
profile,
schema,
Expand Down
1 change: 1 addition & 0 deletions aries_cloudagent/askar/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class AskarProfile(Profile):
"""Provide access to Aries-Askar profile interaction methods."""

BACKEND_NAME = "askar"
TEST_PROFILE_NAME = "test-profile"

def __init__(
self,
Expand Down
21 changes: 19 additions & 2 deletions aries_cloudagent/core/in_memory/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Mapping, Type
from weakref import ref


from ...config.injection_context import InjectionContext
from ...config.provider import ClassProvider
from ...storage.base import BaseStorage
Expand All @@ -27,7 +28,13 @@ class InMemoryProfile(Profile):
BACKEND_NAME = "in_memory"
TEST_PROFILE_NAME = "test-profile"

def __init__(self, *, context: InjectionContext = None, name: str = None):
def __init__(
self,
*,
context: InjectionContext = None,
name: str = None,
profile_class: Any = None
):
"""Create a new InMemoryProfile instance."""
global STORAGE_CLASS, WALLET_CLASS
super().__init__(context=context, name=name, created=True)
Expand All @@ -36,6 +43,12 @@ def __init__(self, *, context: InjectionContext = None, name: str = None):
self.pair_dids = {}
self.records = OrderedDict()
self.bind_providers()
self.profile_class = profile_class if profile_class else InMemoryProfile

@property
def __class__(self):
"""Return the given profile class."""
return self.profile_class

def bind_providers(self):
"""Initialize the profile-level instance providers."""
Expand Down Expand Up @@ -64,12 +77,16 @@ def transaction(self, context: InjectionContext = None) -> "ProfileSession":

@classmethod
def test_profile(
cls, settings: Mapping[str, Any] = None, bind: Mapping[Type, Any] = None
cls,
settings: Mapping[str, Any] = None,
bind: Mapping[Type, Any] = None,
profile_class: Any = None,
) -> "InMemoryProfile":
"""Used in tests to create a standard InMemoryProfile."""
profile = InMemoryProfile(
context=InjectionContext(enforce_typing=False, settings=settings),
name=InMemoryProfile.TEST_PROFILE_NAME,
profile_class=profile_class,
)
if bind:
for k, v in bind.items():
Expand Down
26 changes: 25 additions & 1 deletion aries_cloudagent/messaging/credential_definitions/routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Credential definition admin routes."""

import functools
import json


Expand All @@ -13,7 +14,9 @@
)

from marshmallow import fields
from ...anoncreds.issuer import AnonCredsIssuer

from ...anoncreds.base import AnonCredsResolutionError
from ...anoncreds.issuer import AnonCredsIssuer, AnonCredsIssuerError
from ...anoncreds.registry import AnonCredsRegistry

from ...wallet.base import BaseWallet
Expand Down Expand Up @@ -138,6 +141,24 @@ class CredDefConnIdMatchInfoSchema(OpenAPISchema):
)


def error_handler(func):
"""API/Routes Error handler function."""

@functools.wraps(func)
async def wrapper(request):
try:
ret = await func(request)
return ret
except AnonCredsResolutionError as err:
raise web.HTTPForbidden(reason=err.roll_up) from err
except AnonCredsIssuerError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err
except Exception as err:
raise err

return wrapper


@docs(
tags=["credential-definition"],
summary="Sends a credential definition to the ledger",
Expand All @@ -146,6 +167,7 @@ class CredDefConnIdMatchInfoSchema(OpenAPISchema):
@querystring_schema(CreateCredDefTxnForEndorserOptionSchema())
@querystring_schema(CredDefConnIdMatchInfoSchema())
@response_schema(TxnOrCredentialDefinitionSendResultSchema(), 200, description="")
@error_handler
async def credential_definitions_send_credential_definition(request: web.BaseRequest):
"""
Request handler for sending a credential definition to the ledger.
Expand Down Expand Up @@ -280,6 +302,7 @@ async def credential_definitions_send_credential_definition(request: web.BaseReq
)
@querystring_schema(CredDefQueryStringSchema())
@response_schema(CredentialDefinitionsCreatedResultSchema(), 200, description="")
@error_handler
async def credential_definitions_created(request: web.BaseRequest):
"""
Request handler for retrieving credential definitions that current agent created.
Expand Down Expand Up @@ -312,6 +335,7 @@ async def credential_definitions_created(request: web.BaseRequest):
)
@match_info_schema(CredDefIdMatchInfoSchema())
@response_schema(CredentialDefinitionGetResultSchema(), 200, description="")
@error_handler
async def credential_definitions_get_credential_definition(request: web.BaseRequest):
"""
Request handler for getting a credential definition from the ledger.
Expand Down
Loading