Skip to content

Commit d7511ce

Browse files
authored
Fix endorsement setup with existing connection (openwallet-foundation#3309)
* 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]>
1 parent 6a50ec8 commit d7511ce

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

acapy_agent/utils/endorsement_setup.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@
2020
LOGGER = logging.getLogger(__name__)
2121

2222

23+
class EndorsementSetupError(Exception):
24+
"""Endorsement setup error."""
25+
26+
2327
async def attempt_auto_author_with_endorser_setup(profile: Profile):
2428
"""Automatically setup the author's endorser connection if possible."""
2529

2630
if not is_author_role(profile):
2731
return
2832

29-
endorser_invitation = profile.settings.get_value("endorser.endorser_invitation")
30-
if not endorser_invitation:
31-
LOGGER.info("No endorser invitation, can't connect automatically.")
32-
return
33-
3433
endorser_alias = profile.settings.get_value("endorser.endorser_alias")
3534
if not endorser_alias:
3635
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):
4645
LOGGER.info("No endorser DID, can connect, but can't setup connection metadata.")
4746
return
4847

48+
endorser_invitation = profile.settings.get_value("endorser.endorser_invitation")
49+
if not endorser_invitation:
50+
LOGGER.info("No endorser invitation, can't create connection automatically.")
51+
return
52+
4953
try:
5054
# OK, we are an author, we have no endorser connection but we have enough info
5155
# to automatically initiate the connection
@@ -71,7 +75,7 @@ async def attempt_auto_author_with_endorser_setup(profile: Profile):
7175
alias=endorser_alias,
7276
)
7377
else:
74-
raise Exception(
78+
raise EndorsementSetupError(
7579
"Failed to establish endorser connection, invalid "
7680
"invitation format."
7781
)

acapy_agent/utils/tests/test_endorsement_setup.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,42 @@ async def test_create_connection_with_valid_invitation(
6262
assert "Error accepting endorser invitation" not in call[0][0]
6363

6464
assert mock_conn_record.called
65+
66+
@mock.patch.object(
67+
ConnRecord,
68+
"retrieve_by_alias",
69+
return_value=[ConnRecord(connection_id="test-connection-id")],
70+
)
71+
@mock.patch.object(endorsement_setup.LOGGER, "info", return_value=mock.MagicMock())
72+
async def test_has_previous_connection(self, mock_logger, *_):
73+
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)
74+
75+
# No invitation
76+
self.profile.settings.set_value("endorser.author", True)
77+
self.profile.settings.set_value("endorser.endorser_alias", "test-alias")
78+
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)
79+
80+
assert mock_logger.call_count == 1
81+
assert (
82+
mock_logger.call_args_list[0][0][0]
83+
== "Connected to endorser from previous connection."
84+
)
85+
86+
@mock.patch.object(
87+
ConnRecord,
88+
"retrieve_by_alias",
89+
side_effect=Exception("No connection"),
90+
)
91+
@mock.patch.object(endorsement_setup.LOGGER, "info", return_value=mock.MagicMock())
92+
async def test_does_not_have_previous_connection_with_did_but_no_invitation(
93+
self, mock_logger, *_
94+
):
95+
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)
96+
97+
# No invitation
98+
self.profile.settings.set_value("endorser.author", True)
99+
self.profile.settings.set_value("endorser.endorser_alias", "test-alias")
100+
self.profile.settings.set_value(
101+
"endorser.endorser_public_did", "WuE7ndRJgAGbJYnwDXV7Pz"
102+
)
103+
await endorsement_setup.attempt_auto_author_with_endorser_setup(self.profile)

0 commit comments

Comments
 (0)