From d7511ceda88aa77a992832fd3956f69c239806cc Mon Sep 17 00:00:00 2001 From: jamshale <31809382+jamshale@users.noreply.github.com> Date: Wed, 23 Oct 2024 14:49:05 -0700 Subject: [PATCH] Fix endorsement setup with existing connection (#3309) * Fix endorsement setup with existing connection Signed-off-by: Jamie Hale * Add test for no invitation or previous connection Signed-off-by: Jamie Hale --------- Signed-off-by: Jamie Hale --- acapy_agent/utils/endorsement_setup.py | 16 +++++--- .../utils/tests/test_endorsement_setup.py | 39 +++++++++++++++++++ 2 files changed, 49 insertions(+), 6 deletions(-) diff --git a/acapy_agent/utils/endorsement_setup.py b/acapy_agent/utils/endorsement_setup.py index ca02d9e4d1..685cdda3e6 100644 --- a/acapy_agent/utils/endorsement_setup.py +++ b/acapy_agent/utils/endorsement_setup.py @@ -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.") @@ -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 @@ -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." ) diff --git a/acapy_agent/utils/tests/test_endorsement_setup.py b/acapy_agent/utils/tests/test_endorsement_setup.py index 80164dd42f..bea85eda01 100644 --- a/acapy_agent/utils/tests/test_endorsement_setup.py +++ b/acapy_agent/utils/tests/test_endorsement_setup.py @@ -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)