Skip to content

Commit

Permalink
Fix endorsement setup with existing connection (#3309)
Browse files Browse the repository at this point in the history
* Fix endorsement setup with existing connection

Signed-off-by: Jamie Hale <[email protected]>

* Add test for no invitation or previous connection

Signed-off-by: Jamie Hale <[email protected]>

---------

Signed-off-by: Jamie Hale <[email protected]>
  • Loading branch information
jamshale authored Oct 23, 2024
1 parent 6a50ec8 commit d7511ce
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
16 changes: 10 additions & 6 deletions acapy_agent/utils/endorsement_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@
LOGGER = logging.getLogger(__name__)


class EndorsementSetupError(Exception):
"""Endorsement setup error."""


async def attempt_auto_author_with_endorser_setup(profile: Profile):
"""Automatically setup the author's endorser connection if possible."""

if not is_author_role(profile):
return

endorser_invitation = profile.settings.get_value("endorser.endorser_invitation")
if not endorser_invitation:
LOGGER.info("No endorser invitation, can't connect automatically.")
return

endorser_alias = profile.settings.get_value("endorser.endorser_alias")
if not endorser_alias:
LOGGER.info("No endorser alias, alias is required if invitation is specified.")
Expand All @@ -46,6 +45,11 @@ async def attempt_auto_author_with_endorser_setup(profile: Profile):
LOGGER.info("No endorser DID, can connect, but can't setup connection metadata.")
return

endorser_invitation = profile.settings.get_value("endorser.endorser_invitation")
if not endorser_invitation:
LOGGER.info("No endorser invitation, can't create connection automatically.")
return

try:
# OK, we are an author, we have no endorser connection but we have enough info
# to automatically initiate the connection
Expand All @@ -71,7 +75,7 @@ async def attempt_auto_author_with_endorser_setup(profile: Profile):
alias=endorser_alias,
)
else:
raise Exception(
raise EndorsementSetupError(
"Failed to establish endorser connection, invalid "
"invitation format."
)
Expand Down
39 changes: 39 additions & 0 deletions acapy_agent/utils/tests/test_endorsement_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,42 @@ async def test_create_connection_with_valid_invitation(
assert "Error accepting endorser invitation" not in call[0][0]

assert mock_conn_record.called

@mock.patch.object(
ConnRecord,
"retrieve_by_alias",
return_value=[ConnRecord(connection_id="test-connection-id")],
)
@mock.patch.object(endorsement_setup.LOGGER, "info", return_value=mock.MagicMock())
async def test_has_previous_connection(self, mock_logger, *_):
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

# No invitation
self.profile.settings.set_value("endorser.author", True)
self.profile.settings.set_value("endorser.endorser_alias", "test-alias")
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

assert mock_logger.call_count == 1
assert (
mock_logger.call_args_list[0][0][0]
== "Connected to endorser from previous connection."
)

@mock.patch.object(
ConnRecord,
"retrieve_by_alias",
side_effect=Exception("No connection"),
)
@mock.patch.object(endorsement_setup.LOGGER, "info", return_value=mock.MagicMock())
async def test_does_not_have_previous_connection_with_did_but_no_invitation(
self, mock_logger, *_
):
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

# No invitation
self.profile.settings.set_value("endorser.author", True)
self.profile.settings.set_value("endorser.endorser_alias", "test-alias")
self.profile.settings.set_value(
"endorser.endorser_public_did", "WuE7ndRJgAGbJYnwDXV7Pz"
)
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

0 comments on commit d7511ce

Please sign in to comment.