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

Use issue-credential protocol in performance demo #243

Merged
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
8 changes: 4 additions & 4 deletions aries_cloudagent/messaging/issue_credential/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def context(self) -> InjectionContext:

async def prepare_send(
self, connection_id: str, credential_proposal: CredentialProposal
) -> V10CredentialExchange:
) -> Tuple[V10CredentialExchange, CredentialOffer]:
"""
Set up a new credential exchange for an automated send.

Expand All @@ -59,7 +59,7 @@ async def prepare_send(
attribute values to use if auto_issue is enabled

Returns:
A new credential exchange record
A tuple of the new credential exchange record and credential offer message

"""

Expand All @@ -77,11 +77,11 @@ async def prepare_send(
credential_definition_id=credential_definition_id,
credential_proposal_dict=credential_proposal.serialize(),
)
(credential_exchange, _) = await self.create_offer(
(credential_exchange, credential_offer) = await self.create_offer(
credential_exchange_record=credential_exchange,
comment="create automated credential exchange",
)
return credential_exchange
return credential_exchange, credential_offer

async def create_proposal(
self,
Expand Down
88 changes: 39 additions & 49 deletions aries_cloudagent/messaging/issue_credential/v1_0/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
from .manager import CredentialManager
from .messages.credential_proposal import CredentialProposal
from .messages.inner.credential_preview import (
CredAttrSpec,
CredentialPreview,
CredentialPreviewSchema,
)
Expand Down Expand Up @@ -51,7 +50,7 @@ class V10CredentialProposalRequestSchema(Schema):
**INDY_CRED_DEF_ID,
)
comment = fields.Str(description="Human-readable comment", required=False)
credential_proposal = fields.Nested(CredentialPreviewSchema, required=True)
credential_preview = fields.Nested(CredentialPreviewSchema, required=True)


class V10CredentialOfferRequestSchema(Schema):
Expand Down Expand Up @@ -190,27 +189,12 @@ async def credential_exchange_send(request: web.BaseRequest):
connection_id = body.get("connection_id")
credential_definition_id = body.get("credential_definition_id")
comment = body.get("comment")
credential_proposal = CredentialProposal(
comment=comment,
credential_proposal=CredentialPreview(
attributes=[
CredAttrSpec(
name=attr_preview["name"],
mime_type=attr_preview.get("mime-type", None),
value=attr_preview["value"],
)
for attr_preview in body.get("credential_proposal")["attributes"]
]
),
cred_def_id=credential_definition_id,
)
preview_spec = body.get("credential_preview")

if not credential_proposal:
raise web.HTTPBadRequest(
reason="credential_proposal must be provided with attribute values."
)

credential_manager = CredentialManager(context)
if not credential_definition_id:
raise web.HTTPBadRequest(reason="credential_definition_id must be provided.")
if not preview_spec:
raise web.HTTPBadRequest(reason="credential_preview must be provided.")

try:
connection_record = await ConnectionRecord.retrieve_by_id(
Expand All @@ -222,18 +206,19 @@ async def credential_exchange_send(request: web.BaseRequest):
if not connection_record.is_ready:
raise web.HTTPForbidden()

credential_exchange_record = await credential_manager.prepare_send(
connection_id, credential_proposal=credential_proposal
credential_proposal = CredentialProposal(
comment=comment,
credential_proposal=CredentialPreview.deserialize(preview_spec),
cred_def_id=credential_definition_id,
)

credential_manager = CredentialManager(context)

(
credential_exchange_record,
credential_offer_message,
) = await credential_manager.create_offer(
credential_exchange_record,
comment="Automated offer creation on cred def id "
f"{credential_exchange_record.credential_definition_id}, "
f"parent thread {credential_exchange_record.parent_thread_id}",
) = await credential_manager.prepare_send(
connection_id, credential_proposal=credential_proposal
)
await outbound_handler(
credential_offer_message, connection_id=credential_exchange_record.connection_id
Expand Down Expand Up @@ -264,14 +249,10 @@ async def credential_exchange_send_proposal(request: web.BaseRequest):
connection_id = body.get("connection_id")
credential_definition_id = body.get("credential_definition_id")
comment = body.get("comment")
proposal_spec = body.get("credential_proposal")

if not proposal_spec:
raise web.HTTPBadRequest(reason="credential_proposal must be provided.")

credential_preview = CredentialPreview.deserialize(proposal_spec)
preview_spec = body.get("credential_preview")

credential_manager = CredentialManager(context)
if not preview_spec:
raise web.HTTPBadRequest(reason="credential_preview must be provided.")

try:
connection_record = await ConnectionRecord.retrieve_by_id(
Expand All @@ -283,6 +264,10 @@ async def credential_exchange_send_proposal(request: web.BaseRequest):
if not connection_record.is_ready:
raise web.HTTPForbidden()

credential_preview = CredentialPreview.deserialize(preview_spec)

credential_manager = CredentialManager(context)

credential_exchange_record = await credential_manager.create_proposal(
connection_id,
comment=comment,
Expand Down Expand Up @@ -332,19 +317,17 @@ async def credential_exchange_send_free_offer(request: web.BaseRequest):
"auto_issue", context.settings.get("debug.auto_respond_credential_request")
)
comment = body.get("comment")
proposal_spec = body.get("credential_proposal")
preview_spec = body.get("credential_preview")

if not credential_definition_id:
raise web.HTTPBadRequest(reason="credential_definition_id is required")

if auto_issue and not proposal_spec:
if auto_issue and not preview_spec:
raise web.HTTPBadRequest(
reason="If auto_issue is set to"
+ " true then credential_preview must also be provided."
)

credential_manager = CredentialManager(context)

try:
connection_record = await ConnectionRecord.retrieve_by_id(
context, connection_id
Expand All @@ -355,8 +338,8 @@ async def credential_exchange_send_free_offer(request: web.BaseRequest):
if not connection_record.is_ready:
raise web.HTTPForbidden()

if proposal_spec:
credential_preview = CredentialPreview.deserialize(proposal_spec)
if preview_spec:
credential_preview = CredentialPreview.deserialize(preview_spec)
credential_proposal = CredentialProposal(
comment=comment,
credential_proposal=credential_preview,
Expand All @@ -374,6 +357,8 @@ async def credential_exchange_send_free_offer(request: web.BaseRequest):
auto_issue=auto_issue,
)

credential_manager = CredentialManager(context)

(
credential_exchange_record,
credential_offer_message,
Expand Down Expand Up @@ -466,8 +451,6 @@ async def credential_exchange_send_request(request: web.BaseRequest):
V10CredentialExchange.STATE_OFFER_RECEIVED
)

credential_manager = CredentialManager(context)

try:
connection_record = await ConnectionRecord.retrieve_by_id(
context, connection_id
Expand All @@ -478,6 +461,8 @@ async def credential_exchange_send_request(request: web.BaseRequest):
if not connection_record.is_ready:
raise web.HTTPForbidden()

credential_manager = CredentialManager(context)

(
credential_exchange_record,
credential_request_message,
Expand Down Expand Up @@ -508,7 +493,10 @@ async def credential_exchange_issue(request: web.BaseRequest):

body = await request.json()
comment = body.get("comment")
credential_preview = CredentialPreview.deserialize(body["credential_preview"])
preview_spec = body.get("credential_preview")

if not preview_spec:
raise web.HTTPBadRequest(reason="credential_preview must be provided.")

credential_exchange_id = request.match_info["cred_ex_id"]
cred_exch_record = await V10CredentialExchange.retrieve_by_id(
Expand All @@ -518,8 +506,6 @@ async def credential_exchange_issue(request: web.BaseRequest):

assert cred_exch_record.state == V10CredentialExchange.STATE_REQUEST_RECEIVED

credential_manager = CredentialManager(context)

try:
connection_record = await ConnectionRecord.retrieve_by_id(
context, connection_id
Expand All @@ -530,6 +516,10 @@ async def credential_exchange_issue(request: web.BaseRequest):
if not connection_record.is_ready:
raise web.HTTPForbidden()

credential_preview = CredentialPreview.deserialize(preview_spec)

credential_manager = CredentialManager(context)

(
cred_exch_record,
credential_issue_message,
Expand Down Expand Up @@ -569,8 +559,6 @@ async def credential_exchange_store(request: web.BaseRequest):
V10CredentialExchange.STATE_CREDENTIAL_RECEIVED
)

credential_manager = CredentialManager(context)

try:
connection_record = await ConnectionRecord.retrieve_by_id(
context, connection_id
Expand All @@ -581,6 +569,8 @@ async def credential_exchange_store(request: web.BaseRequest):
if not connection_record.is_ready:
raise web.HTTPForbidden()

credential_manager = CredentialManager(context)

(
credential_exchange_record,
credential_stored_message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,11 @@ async def test_prepare_send(self):
with async_mock.patch.object(
self.manager, "create_offer", autospec=True
) as create_offer:
create_offer.return_value = (object(), None)
ret_exchange = await self.manager.prepare_send(connection_id, proposal)
create_offer.return_value = (async_mock.MagicMock(), async_mock.MagicMock())
ret_exchange, ret_cred_offer = await self.manager.prepare_send(connection_id, proposal)
create_offer.assert_called_once()
assert ret_exchange is create_offer.return_value[0]
exchange: V10CredentialExchange = create_offer.call_args[1][
"credential_exchange_record"
]
exchange = create_offer.call_args[1]["credential_exchange_record"]
assert exchange.auto_issue
assert exchange.connection_id == connection_id
assert exchange.credential_definition_id == cred_def_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ async def test_credential_exchange_send(self):
test_module, "ConnectionRecord", autospec=True
) as mock_connection_record, async_mock.patch.object(
test_module, "CredentialManager", autospec=True
) as mock_credential_manager:
) as mock_credential_manager, async_mock.patch.object(
test_module.CredentialPreview, "deserialize", autospec=True
):
test_module.web.json_response = async_mock.CoroutineMock()

mock_credential_manager.return_value.create_offer = (
Expand All @@ -34,17 +36,16 @@ async def test_credential_exchange_send(self):
)

mock_cred_ex_record = async_mock.MagicMock()
mock_cred_offer = async_mock.MagicMock()

mock_credential_manager.return_value.prepare_send.return_value = (
mock_cred_ex_record
(mock_cred_ex_record, mock_cred_offer)
)

await test_module.credential_exchange_send(mock)

test_module.web.json_response.assert_called_once_with(
mock_credential_manager.return_value.create_offer.return_value[
0
].serialize.return_value
mock_cred_ex_record.serialize.return_value
)

async def test_credential_exchange_send_no_conn_record(self):
Expand All @@ -65,13 +66,8 @@ async def test_credential_exchange_send_no_conn_record(self):
test_module.web.json_response = async_mock.CoroutineMock()

# Emulate storage not found (bad connection id)
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock(
side_effect=StorageNotFoundError
)
mock_connection_record.retrieve_by_id.side_effect = StorageNotFoundError

mock_credential_manager.return_value.create_offer = (
async_mock.CoroutineMock()
)
mock_credential_manager.return_value.create_offer.return_value = (
async_mock.MagicMock(),
async_mock.MagicMock(),
Expand All @@ -98,12 +94,8 @@ async def test_credential_exchange_send_not_ready(self):
test_module.web.json_response = async_mock.CoroutineMock()

# Emulate connection not ready
mock_connection_record.retrieve_by_id = async_mock.CoroutineMock()
mock_connection_record.retrieve_by_id.return_value.is_ready = False

mock_credential_manager.return_value.create_offer = (
async_mock.CoroutineMock()
)
mock_credential_manager.return_value.create_offer.return_value = (
async_mock.MagicMock(),
async_mock.MagicMock(),
Expand All @@ -114,14 +106,11 @@ async def test_credential_exchange_send_not_ready(self):

async def test_credential_exchange_send_proposal(self):
conn_id = "connection-id"
proposal_spec = {"attributes": [{"name": "attr", "value": "value"}]}
preview_spec = {"attributes": [{"name": "attr", "value": "value"}]}

mock = async_mock.MagicMock()
mock.json = async_mock.CoroutineMock(
return_value={
"connection_id": conn_id,
"credential_proposal": proposal_spec,
}
return_value={"connection_id": conn_id, "credential_preview": preview_spec}
)
mock.app = {
"outbound_message_router": async_mock.CoroutineMock(),
Expand Down Expand Up @@ -218,7 +207,10 @@ async def test_credential_exchange_send_proposal_not_ready(self):
async def test_credential_exchange_send_free_offer(self):
mock = async_mock.MagicMock()
mock.json = async_mock.CoroutineMock(
return_value={"auto_issue": False, "credential_definition_id": "cred-def-id"}
return_value={
"auto_issue": False,
"credential_definition_id": "cred-def-id",
}
)

mock.app = {
Expand Down Expand Up @@ -257,7 +249,10 @@ async def test_credential_exchange_send_free_offer(self):
async def test_credential_exchange_send_free_offer_no_conn_record(self):
mock = async_mock.MagicMock()
mock.json = async_mock.CoroutineMock(
return_value={"auto_issue": False, "credential_definition_id": "cred-def-id"}
return_value={
"auto_issue": False,
"credential_definition_id": "cred-def-id",
}
)

mock.app = {
Expand Down
Loading