Skip to content

Commit

Permalink
Merge pull request #1718 from shaangill025/delete_pres_exch
Browse files Browse the repository at this point in the history
Allow deletion of invalid V20PresExRecord - failing schema validation
  • Loading branch information
andrewwhitehead authored Apr 12, 2022
2 parents 186c3f4 + 034fecd commit 9712d0d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
7 changes: 5 additions & 2 deletions aries_cloudagent/messaging/models/base_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from ..util import datetime_to_str, time_now
from ..valid import INDY_ISO8601_DATETIME

from .base import BaseModel, BaseModelSchema
from .base import BaseModel, BaseModelSchema, BaseModelError

LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -329,7 +329,10 @@ async def query(
positive=False,
alt=alt,
):
result.append(cls.from_storage(record.id, vals))
try:
result.append(cls.from_storage(record.id, vals))
except BaseModelError as err:
raise BaseModelError(f"{err}, for record id {record.id}")
return result

async def save(
Expand Down
21 changes: 21 additions & 0 deletions aries_cloudagent/messaging/models/tests/test_base_record.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
StorageError,
StorageRecord,
)
from ....messaging.models.base import BaseModelError

from ...util import time_now

Expand Down Expand Up @@ -181,6 +182,26 @@ async def test_query(self):
assert result[0]._id == record_id
assert result[0].value == record_value

async def test_query_x(self):
session = InMemoryProfile.test_session()
mock_storage = async_mock.MagicMock(BaseStorage, autospec=True)
session.context.injector.bind_instance(BaseStorage, mock_storage)
record_id = "record_id"
record_value = {"created_at": time_now(), "updated_at": time_now()}
tag_filter = {"tag": "filter"}
stored = StorageRecord(
BaseRecordImpl.RECORD_TYPE, json.dumps(record_value), {}, record_id
)

mock_storage.find_all_records.return_value = [stored]
with async_mock.patch.object(
BaseRecordImpl,
"from_storage",
async_mock.MagicMock(side_effect=BaseModelError),
):
with self.assertRaises(BaseModelError):
await BaseRecordImpl.query(session, tag_filter)

async def test_query_post_filter(self):
session = InMemoryProfile.test_session()
mock_storage = async_mock.MagicMock(BaseStorage, autospec=True)
Expand Down
14 changes: 12 additions & 2 deletions aries_cloudagent/protocols/present_proof/v2_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
UUID4,
)
from ....storage.error import StorageError, StorageNotFoundError
from ....storage.base import BaseStorage
from ....storage.vc_holder.base import VCHolder
from ....storage.vc_holder.vc_record import VCRecord
from ....utils.tracing import trace_event, get_timer, AdminAPIMessageTracingSchema
Expand Down Expand Up @@ -1265,8 +1266,17 @@ async def present_proof_remove(request: web.BaseRequest):
pres_ex_record = None
try:
async with context.profile.session() as session:
pres_ex_record = await V20PresExRecord.retrieve_by_id(session, pres_ex_id)
await pres_ex_record.delete_record(session)
try:
pres_ex_record = await V20PresExRecord.retrieve_by_id(
session, pres_ex_id
)
await pres_ex_record.delete_record(session)
except (BaseModelError, ValidationError):
storage = session.inject(BaseStorage)
storage_record = await storage.get_record(
record_type=V20PresExRecord.RECORD_TYPE, record_id=pres_ex_id
)
await storage.delete_record(storage_record)
except StorageNotFoundError as err:
raise web.HTTPNotFound(reason=err.roll_up) from err
except StorageError as err:
Expand Down

0 comments on commit 9712d0d

Please sign in to comment.