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

Fix endorsement setup with existing connection #3309

Merged
merged 2 commits into from
Oct 23, 2024
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
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)
Loading