Skip to content

Commit

Permalink
Fix up unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Ian Costanzo <[email protected]>
  • Loading branch information
ianco committed Aug 25, 2022
1 parent 064a32d commit e5bc71e
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 35 deletions.
16 changes: 8 additions & 8 deletions aries_cloudagent/indy/credx/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,13 @@ async def verify_presentation(
msgs = []
try:
msgs += self.non_revoc_intervals(pres_req, pres, credential_definitions)
msgs += await self.check_timestamps(self.profile, pres_req, pres, rev_reg_defs)
msgs += await self.check_timestamps(
self.profile, pres_req, pres, rev_reg_defs
)
msgs += await self.pre_verify(pres_req, pres)
except ValueError as err:
msgs.append(
f"{PresVerifyMsg.PRES_VALUE_ERROR.value}::{err}"
)
s = str(err)
msgs.append(f"{PresVerifyMsg.PRES_VALUE_ERROR.value}::{s}")
LOGGER.error(
f"Presentation on nonce={pres_req['nonce']} "
f"cannot be validated: {str(err)}"
Expand All @@ -72,10 +73,9 @@ async def verify_presentation(
rev_reg_defs.values(),
rev_reg_entries,
)
except CredxError:
msgs.append(
f"{PresVerifyMsg.PRES_VERIFY_ERROR.value}::{err}"
)
except CredxError as err:
s = str(err)
msgs.append(f"{PresVerifyMsg.PRES_VERIFY_ERROR.value}::{s}")
LOGGER.exception(
f"Validation of presentation on nonce={pres_req['nonce']} "
"failed with error"
Expand Down
44 changes: 35 additions & 9 deletions aries_cloudagent/indy/sdk/tests/test_verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ async def test_verify_presentation(self, mock_verify):
) as mock_get_ledger:
mock_get_ledger.return_value = (None, self.ledger)
INDY_PROOF_REQ_X = deepcopy(INDY_PROOF_REQ_PRED_NAMES)
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
INDY_PROOF_REQ_X,
INDY_PROOF_PRED_NAMES,
"schemas",
Expand Down Expand Up @@ -370,7 +370,7 @@ async def test_verify_presentation_x_indy(self, mock_verify):
IndyLedgerRequestsExecutor, "get_ledger_for_identifier"
) as mock_get_ledger:
mock_get_ledger.return_value = ("test", self.ledger)
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
INDY_PROOF_REQ_NAME,
INDY_PROOF_NAME,
"schemas",
Expand All @@ -397,7 +397,7 @@ async def test_check_encoding_attr(self, mock_verify):
) as mock_get_ledger:
mock_get_ledger.return_value = (None, self.ledger)
mock_verify.return_value = True
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
INDY_PROOF_REQ_NAME,
INDY_PROOF_NAME,
"schemas",
Expand All @@ -415,6 +415,8 @@ async def test_check_encoding_attr(self, mock_verify):
json.dumps("rev_reg_entries"),
)
assert verified is True
assert len(msgs) == 1
assert "TS_OUT_NRI::19_uuid" in msgs

@async_mock.patch("indy.anoncreds.verifier_verify_proof")
async def test_check_encoding_attr_tamper_raw(self, mock_verify):
Expand All @@ -426,7 +428,7 @@ async def test_check_encoding_attr_tamper_raw(self, mock_verify):
IndyLedgerRequestsExecutor, "get_ledger_for_identifier"
) as mock_get_ledger:
mock_get_ledger.return_value = ("test", self.ledger)
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
INDY_PROOF_REQ_NAME,
INDY_PROOF_X,
"schemas",
Expand All @@ -438,6 +440,9 @@ async def test_check_encoding_attr_tamper_raw(self, mock_verify):
mock_verify.assert_not_called()

assert verified is False
assert len(msgs) == 2
assert "TS_OUT_NRI::19_uuid" in msgs
assert "VALUE_ERROR::Encoded representation mismatch for 'Preferred Name'" in msgs

@async_mock.patch("indy.anoncreds.verifier_verify_proof")
async def test_check_encoding_attr_tamper_encoded(self, mock_verify):
Expand All @@ -449,7 +454,7 @@ async def test_check_encoding_attr_tamper_encoded(self, mock_verify):
IndyLedgerRequestsExecutor, "get_ledger_for_identifier"
) as mock_get_ledger:
mock_get_ledger.return_value = (None, self.ledger)
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
INDY_PROOF_REQ_NAME,
INDY_PROOF_X,
"schemas",
Expand All @@ -461,6 +466,9 @@ async def test_check_encoding_attr_tamper_encoded(self, mock_verify):
mock_verify.assert_not_called()

assert verified is False
assert len(msgs) == 2
assert "TS_OUT_NRI::19_uuid" in msgs
assert "VALUE_ERROR::Encoded representation mismatch for 'Preferred Name'" in msgs

@async_mock.patch("indy.anoncreds.verifier_verify_proof")
async def test_check_pred_names(self, mock_verify):
Expand All @@ -470,7 +478,7 @@ async def test_check_pred_names(self, mock_verify):
mock_get_ledger.return_value = ("test", self.ledger)
mock_verify.return_value = True
INDY_PROOF_REQ_X = deepcopy(INDY_PROOF_REQ_PRED_NAMES)
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
INDY_PROOF_REQ_X,
INDY_PROOF_PRED_NAMES,
"schemas",
Expand All @@ -491,6 +499,10 @@ async def test_check_pred_names(self, mock_verify):
)

assert verified is True
assert len(msgs) == 3
assert "TS_OUT_NRI::18_uuid" in msgs
assert "TS_OUT_NRI::18_id_GE_uuid" in msgs
assert "TS_OUT_NRI::18_busid_GE_uuid" in msgs

@async_mock.patch("indy.anoncreds.verifier_verify_proof")
async def test_check_pred_names_tamper_pred_value(self, mock_verify):
Expand All @@ -502,7 +514,7 @@ async def test_check_pred_names_tamper_pred_value(self, mock_verify):
IndyLedgerRequestsExecutor, "get_ledger_for_identifier"
) as mock_get_ledger:
mock_get_ledger.return_value = (None, self.ledger)
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
deepcopy(INDY_PROOF_REQ_PRED_NAMES),
INDY_PROOF_X,
"schemas",
Expand All @@ -514,6 +526,11 @@ async def test_check_pred_names_tamper_pred_value(self, mock_verify):
mock_verify.assert_not_called()

assert verified is False
assert len(msgs) == 4
assert "RMV_RFNT_NRI::18_uuid" in msgs
assert "RMV_RFNT_NRI::18_busid_GE_uuid" in msgs
assert "RMV_RFNT_NRI::18_id_GE_uuid" in msgs
assert "VALUE_ERROR::Timestamp on sub-proof #0 is superfluous vs. requested attribute group 18_uuid" in msgs

@async_mock.patch("indy.anoncreds.verifier_verify_proof")
async def test_check_pred_names_tamper_pred_req_attr(self, mock_verify):
Expand All @@ -523,7 +540,7 @@ async def test_check_pred_names_tamper_pred_req_attr(self, mock_verify):
IndyLedgerRequestsExecutor, "get_ledger_for_identifier"
) as mock_get_ledger:
mock_get_ledger.return_value = (None, self.ledger)
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
INDY_PROOF_REQ_X,
INDY_PROOF_PRED_NAMES,
"schemas",
Expand All @@ -535,6 +552,11 @@ async def test_check_pred_names_tamper_pred_req_attr(self, mock_verify):
mock_verify.assert_not_called()

assert verified is False
assert len(msgs) == 4
assert "RMV_RFNT_NRI::18_uuid" in msgs
assert "RMV_RFNT_NRI::18_busid_GE_uuid" in msgs
assert "RMV_RFNT_NRI::18_id_GE_uuid" in msgs
assert "VALUE_ERROR::Timestamp on sub-proof #0 is superfluous vs. requested attribute group 18_uuid" in msgs

@async_mock.patch("indy.anoncreds.verifier_verify_proof")
async def test_check_pred_names_tamper_attr_groups(self, mock_verify):
Expand All @@ -546,7 +568,7 @@ async def test_check_pred_names_tamper_attr_groups(self, mock_verify):
IndyLedgerRequestsExecutor, "get_ledger_for_identifier"
) as mock_get_ledger:
mock_get_ledger.return_value = ("test", self.ledger)
verified = await self.verifier.verify_presentation(
(verified, msgs) = await self.verifier.verify_presentation(
deepcopy(INDY_PROOF_REQ_PRED_NAMES),
INDY_PROOF_X,
"schemas",
Expand All @@ -558,3 +580,7 @@ async def test_check_pred_names_tamper_attr_groups(self, mock_verify):
mock_verify.assert_not_called()

assert verified is False
assert len(msgs) == 3
assert "RMV_RFNT_NRI::18_busid_GE_uuid" in msgs
assert "RMV_RFNT_NRI::18_id_GE_uuid" in msgs
assert "VALUE_ERROR::Missing requested attribute group 18_uuid" in msgs
16 changes: 8 additions & 8 deletions aries_cloudagent/indy/sdk/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,13 @@ async def verify_presentation(
msgs = []
try:
msgs += self.non_revoc_intervals(pres_req, pres, credential_definitions)
msgs += await self.check_timestamps(self.profile, pres_req, pres, rev_reg_defs)
msgs += await self.check_timestamps(
self.profile, pres_req, pres, rev_reg_defs
)
msgs += await self.pre_verify(pres_req, pres)
except ValueError as err:
msgs.append(
f"{PresVerifyMsg.PRES_VALUE_ERROR.value}::{err}"
)
s = str(err)
msgs.append(f"{PresVerifyMsg.PRES_VALUE_ERROR.value}::{s}")
LOGGER.error(
f"Presentation on nonce={pres_req['nonce']} "
f"cannot be validated: {str(err)}"
Expand All @@ -75,10 +76,9 @@ async def verify_presentation(
json.dumps(rev_reg_defs),
json.dumps(rev_reg_entries),
)
except IndyError:
msgs.append(
f"{PresVerifyMsg.PRES_VERIFY_ERROR.value}::{err}"
)
except IndyError as err:
s = str(err)
msgs.append(f"{PresVerifyMsg.PRES_VERIFY_ERROR.value}::{s}")
LOGGER.exception(
f"Validation of presentation on nonce={pres_req['nonce']} "
"failed with error"
Expand Down
12 changes: 6 additions & 6 deletions aries_cloudagent/indy/verifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@

LOGGER = logging.getLogger(__name__)


class PresVerifyMsg(str, Enum):
"""Credential verification codes."""

RMV_REFERENT_NON_REVOC_INTERVAL = "RMV_RFNT_NRI"
RMV_GLOBAL_NON_REVOC_INTERVAL = "RMV_GLB_NRI"
TSTMP_OUT_NON_REVOC_INTRVAL = "TS_OUT_NRI"
Expand Down Expand Up @@ -216,8 +219,7 @@ async def check_timestamps(
elif uuid in unrevealed_attrs:
# nothing to do, attribute value is not revealed
msgs.append(
f"{PresVerifyMsg.CT_UNREVEALED_ATTRIBUTES.value}::"
f"{uuid}"
f"{PresVerifyMsg.CT_UNREVEALED_ATTRIBUTES.value}::" f"{uuid}"
)
elif uuid not in self_attested:
raise ValueError(
Expand Down Expand Up @@ -277,8 +279,7 @@ async def check_timestamps(
< non_revoc_intervals[uuid].get("to", now)
):
msgs.append(
f"{PresVerifyMsg.TSTMP_OUT_NON_REVOC_INTRVAL.value}::"
f"{uuid}"
f"{PresVerifyMsg.TSTMP_OUT_NON_REVOC_INTRVAL.value}::" f"{uuid}"
)
LOGGER.warning(
f"Best-effort timestamp {timestamp} "
Expand Down Expand Up @@ -347,8 +348,7 @@ async def pre_verify(self, pres_req: dict, pres: dict) -> list:
# unrevealed attribute, nothing to do
pres_req_attr_spec = {}
msgs.append(
f"{PresVerifyMsg.CT_UNREVEALED_ATTRIBUTES.value}::"
f"{uuid}"
f"{PresVerifyMsg.CT_UNREVEALED_ATTRIBUTES.value}::" f"{uuid}"
)
elif uuid in self_attested:
if not req_attr.get("restrictions"):
Expand Down
2 changes: 1 addition & 1 deletion aries_cloudagent/protocols/present_proof/v1_0/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ async def verify_presentation(
rev_reg_entries,
)
presentation_exchange_record.verified = json.dumps(verified_bool)
presentation_exchange_record.verified_msgs = verified_msgs
presentation_exchange_record.verified_msgs = list(set(verified_msgs))
presentation_exchange_record.state = V10PresentationExchange.STATE_VERIFIED

async with self._profile.session() as session:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ async def test_record(self):
"auto_verify": False,
"error_msg": None,
"verified": None,
"verified_msgs": None,
"trace": False,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ async def setUp(self):
Verifier = async_mock.MagicMock(IndyVerifier, autospec=True)
self.verifier = Verifier()
self.verifier.verify_presentation = async_mock.CoroutineMock(
return_value="true"
return_value=("true", [])
)
injector.bind_instance(IndyVerifier, self.verifier)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,5 +338,5 @@ async def verify_pres(self, pres_ex_record: V20PresExRecord) -> V20PresExRecord:
rev_reg_entries,
)
pres_ex_record.verified = json.dumps(verified)
pres_ex_record.verified_msgs = verified_msgs
pres_ex_record.verified_msgs = list(set(verified_msgs))
return pres_ex_record
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ async def test_record(self):
"state": "state",
"pres_proposal": pres_proposal.serialize(),
"verified": "false",
"verified_msgs": None,
"auto_present": True,
"auto_verify": False,
"error_msg": "error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ async def setUp(self):
Verifier = async_mock.MagicMock(IndyVerifier, autospec=True)
self.verifier = Verifier()
self.verifier.verify_presentation = async_mock.CoroutineMock(
return_value="true"
return_value=("true", [])
)
injector.bind_instance(IndyVerifier, self.verifier)

Expand Down

0 comments on commit e5bc71e

Please sign in to comment.