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

Return 404 when schema not found #2683

Merged
merged 1 commit into from
Dec 18, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 8 additions & 7 deletions aries_cloudagent/anoncreds/default/legacy_indy/registry.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""Legacy Indy Registry."""
from asyncio import shield
import json
import logging
import re
from asyncio import shield
from typing import List, Optional, Pattern, Sequence, Tuple

from base58 import alphabet

from ....cache.base import BaseCache
from ....config.injection_context import InjectionContext
from ....core.profile import Profile
Expand Down Expand Up @@ -40,14 +42,14 @@
GetCredDefResult,
)
from ...models.anoncreds_revocation import (
GetRevRegDefResult,
GetRevListResult,
RevRegDef,
RevRegDefResult,
RevRegDefState,
GetRevRegDefResult,
RevList,
RevListResult,
RevListState,
RevRegDef,
RevRegDefResult,
RevRegDefState,
RevRegDefValue,
)
from ...models.anoncreds_schema import (
Expand All @@ -56,7 +58,6 @@
SchemaResult,
SchemaState,
)
from base58 import alphabet

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -158,7 +159,7 @@ async def get_schema(self, profile: Profile, schema_id: str) -> GetSchemaResult:
schema = await ledger.get_schema(schema_id)
if schema is None:
raise AnonCredsObjectNotFound(
f"Credential definition not found: {schema_id}",
f"Schema not found: {schema_id}",
{"ledger_id": ledger_id},
)

Expand Down
14 changes: 8 additions & 6 deletions aries_cloudagent/anoncreds/routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Anoncreds admin routes."""
from asyncio import shield
import logging
from asyncio import shield

from aiohttp import web
from aiohttp_apispec import (
Expand All @@ -21,13 +21,13 @@
from ..revocation_anoncreds.manager import RevocationManager, RevocationManagerError
from ..revocation_anoncreds.routes import (
PublishRevocationsSchema,
RevRegIdMatchInfoSchema,
RevocationModuleResponseSchema,
RevokeRequestSchema,
RevRegIdMatchInfoSchema,
TxnOrPublishRevocationsResultSchema,
)
from ..storage.error import StorageError, StorageNotFoundError
from .base import AnonCredsRegistrationError
from .base import AnonCredsObjectNotFound, AnonCredsRegistrationError
from .issuer import AnonCredsIssuer, AnonCredsIssuerError
from .models.anoncreds_cred_def import CredDefResultSchema, GetCredDefResultSchema
from .models.anoncreds_revocation import RevListResultSchema, RevRegDefResultSchema
Expand Down Expand Up @@ -186,9 +186,11 @@ async def schema_get(request: web.BaseRequest):
context: AdminRequestContext = request["context"]
anoncreds_registry = context.inject(AnonCredsRegistry)
schema_id = request.match_info["schemaId"]
result = await anoncreds_registry.get_schema(context.profile, schema_id)

return web.json_response(result.serialize())
try:
schema = await anoncreds_registry.get_schema(context.profile, schema_id)
return web.json_response(schema.serialize())
except AnonCredsObjectNotFound:
raise web.HTTPNotFound(reason=f"Schema not found: {schema_id}")


class SchemasQueryStringSchema(OpenAPISchema):
Expand Down
14 changes: 13 additions & 1 deletion aries_cloudagent/anoncreds/tests/test_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from asynctest import TestCase as AsyncTestCase

from aries_cloudagent.admin.request_context import AdminRequestContext
from aries_cloudagent.anoncreds.base import AnonCredsObjectNotFound
from aries_cloudagent.anoncreds.issuer import AnonCredsIssuer
from aries_cloudagent.anoncreds.revocation import AnonCredsRevocation
from aries_cloudagent.anoncreds.revocation_setup import DefaultRevocationSetup
Expand Down Expand Up @@ -95,16 +96,27 @@ async def test_get_schema(self):
self.request.match_info = {"schemaId": "schema_id"}
self.context.inject = mock.Mock(
return_value=mock.MagicMock(
get_schema=mock.CoroutineMock(return_value=MockSchema("schemaId"))
get_schema=mock.CoroutineMock(
side_effect=[
MockSchema("schemaId"),
AnonCredsObjectNotFound("test"),
]
)
)
)
result = await test_module.schema_get(self.request)
assert json.loads(result.body)["schema_id"] == "schemaId"

# missing schema_id
self.request.match_info = {}
with self.assertRaises(KeyError):
await test_module.schema_get(self.request)

# schema not found
self.request.match_info = {"schemaId": "schema_id"}
with self.assertRaises(web.HTTPNotFound):
await test_module.schema_get(self.request)

@mock.patch.object(
AnonCredsIssuer,
"get_created_schemas",
Expand Down
3 changes: 2 additions & 1 deletion aries_cloudagent/messaging/schemas/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
request_schema,
response_schema,
)

from marshmallow import fields
from marshmallow.validate import Regexp

Expand Down Expand Up @@ -397,6 +396,8 @@ async def schemas_get_schema(request: web.BaseRequest):
async with ledger:
try:
schema = await ledger.get_schema(schema_id)
if not schema:
raise web.HTTPNotFound(reason=f"Schema not found: {schema_id}")
except LedgerError as err:
raise web.HTTPBadRequest(reason=err.roll_up) from err

Expand Down
12 changes: 9 additions & 3 deletions aries_cloudagent/messaging/schemas/tests/test_routes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from unittest import IsolatedAsyncioTestCase

from aries_cloudagent.tests import mock

from ....admin.request_context import AdminRequestContext
from ....connections.models.conn_record import ConnRecord
from ....core.in_memory import InMemoryProfile
from ....indy.issuer import IndyIssuer
from ....ledger.base import BaseLedger
Expand All @@ -11,10 +13,7 @@
from ....multitenant.base import BaseMultitenantManager
from ....multitenant.manager import MultitenantManager
from ....storage.base import BaseStorage

from .. import routes as test_module
from ....connections.models.conn_record import ConnRecord


SCHEMA_ID = "WgWxqztrNooG92RXvxSTWv:2:schema_name:1.0"

Expand Down Expand Up @@ -304,6 +303,9 @@ async def test_created(self):
mock_response.assert_called_once_with({"schema_ids": [SCHEMA_ID]})

async def test_get_schema(self):
self.ledger.get_schema = mock.CoroutineMock(
side_effect=[{"schema": "def", "signed_txn": "..."}, None]
)
self.profile_injector.bind_instance(
IndyLedgerRequestsExecutor,
mock.MagicMock(
Expand All @@ -323,6 +325,10 @@ async def test_get_schema(self):
}
)

# test schema not found
with self.assertRaises(test_module.web.HTTPNotFound):
await test_module.schemas_get_schema(self.request)

async def test_get_schema_multitenant(self):
self.profile_injector.bind_instance(
BaseMultitenantManager,
Expand Down