Skip to content

Commit

Permalink
Testing: remove macros, return errs instead of panics in
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <[email protected]>
  • Loading branch information
Patrik-Stas committed Oct 31, 2023
1 parent 491170c commit c72360c
Show file tree
Hide file tree
Showing 12 changed files with 2,509 additions and 2,800 deletions.
301 changes: 145 additions & 156 deletions aries_vcx/tests/test_anoncreds.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod utils;
use std::error::Error;

use aries_vcx::common::credentials::get_cred_rev_id;
use aries_vcx_core::{
Expand All @@ -7,191 +7,180 @@ use aries_vcx_core::{
ledger::{base_ledger::AnoncredsLedgerRead, indy::pool::test_utils::get_temp_dir_path},
};
use serde_json::json;
use test_utils::{constants::DEFAULT_SCHEMA_ATTRS, run_setup_test};
use test_utils::{constants::DEFAULT_SCHEMA_ATTRS, devsetup::build_setup_profile};

use crate::utils::{
create_and_publish_test_rev_reg, create_and_write_credential, create_and_write_test_cred_def,
create_and_write_test_schema,
};

pub mod utils;

#[tokio::test]
#[ignore]
async fn test_pool_returns_error_if_proof_request_is_malformed() {
run_setup_test!(|setup| async move {
let proof_req = "{";
let anoncreds = setup.anoncreds;
let result = anoncreds
.prover_get_credentials_for_proof_req(&setup.wallet, proof_req)
.await;
assert_eq!(
result.unwrap_err().kind(),
AriesVcxCoreErrorKind::InvalidProofRequest
);
})
.await;
async fn test_pool_returns_error_if_proof_request_is_malformed() -> Result<(), Box<dyn Error>> {
let setup = build_setup_profile().await;
let proof_req = "{";
let anoncreds = setup.anoncreds;
let result = anoncreds
.prover_get_credentials_for_proof_req(&setup.wallet, proof_req)
.await;
assert_eq!(
result.unwrap_err().kind(),
AriesVcxCoreErrorKind::InvalidProofRequest
);
Ok(())
}

#[tokio::test]
#[ignore]
async fn test_pool_prover_get_credentials() {
run_setup_test!(|setup| async move {
let proof_req = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"address1_1": json!({
"name":"address1",
}),
"zip_2": json!({
"name":"zip",
}),
async fn test_pool_prover_get_credentials() -> Result<(), Box<dyn Error>> {
let setup = build_setup_profile().await;
let proof_req = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"address1_1": json!({
"name":"address1",
}),
"requested_predicates": json!({}),
})
.to_string();

let anoncreds = setup.anoncreds;
let _result = anoncreds
.prover_get_credentials_for_proof_req(&setup.wallet, &proof_req)
.await
.unwrap();

let result_malformed_json = anoncreds
.prover_get_credentials_for_proof_req(&setup.wallet, "{}")
.await
.unwrap_err();
assert_eq!(
result_malformed_json.kind(),
AriesVcxCoreErrorKind::InvalidAttributesStructure
);
"zip_2": json!({
"name":"zip",
}),
}),
"requested_predicates": json!({}),
})
.await;
.to_string();

let anoncreds = setup.anoncreds;
let _result = anoncreds
.prover_get_credentials_for_proof_req(&setup.wallet, &proof_req)
.await?;

let result_malformed_json = anoncreds
.prover_get_credentials_for_proof_req(&setup.wallet, "{}")
.await
.unwrap_err();
assert_eq!(
result_malformed_json.kind(),
AriesVcxCoreErrorKind::InvalidAttributesStructure
);
Ok(())
}

#[tokio::test]
#[ignore]
async fn test_pool_proof_req_attribute_names() {
run_setup_test!(|setup| async move {
let proof_req = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"multiple_attrs": {
"names": ["name_1", "name_2"]
},
"address1_1": json!({
"name":"address1",
"restrictions": [json!({ "issuer_did": "some_did" })]
}),
"self_attest_3": json!({
"name":"self_attest",
}),
async fn test_pool_proof_req_attribute_names() -> Result<(), Box<dyn Error>> {
let setup = build_setup_profile().await;
let proof_req = json!({
"nonce":"123432421212",
"name":"proof_req_1",
"version":"0.1",
"requested_attributes": json!({
"multiple_attrs": {
"names": ["name_1", "name_2"]
},
"address1_1": json!({
"name":"address1",
"restrictions": [json!({ "issuer_did": "some_did" })]
}),
"requested_predicates": json!({
"zip_3": {"name":"zip", "p_type":">=", "p_value":18}
"self_attest_3": json!({
"name":"self_attest",
}),
})
.to_string();

let anoncreds = setup.anoncreds;
let _result = anoncreds
.prover_get_credentials_for_proof_req(&setup.wallet, &proof_req)
.await
.unwrap();
}),
"requested_predicates": json!({
"zip_3": {"name":"zip", "p_type":">=", "p_value":18}
}),
})
.await;
.to_string();

let anoncreds = setup.anoncreds;
anoncreds
.prover_get_credentials_for_proof_req(&setup.wallet, &proof_req)
.await?;
Ok(())
}

#[tokio::test]
#[ignore]
async fn test_pool_revoke_credential() {
run_setup_test!(|setup| async move {
let schema = create_and_write_test_schema(
&setup.wallet,
&setup.anoncreds,
&setup.ledger_write,
&setup.institution_did,
DEFAULT_SCHEMA_ATTRS,
)
.await;
let cred_def = create_and_write_test_cred_def(
async fn test_pool_revoke_credential() -> Result<(), Box<dyn Error>> {
let setup = build_setup_profile().await;
let schema = create_and_write_test_schema(
&setup.wallet,
&setup.anoncreds,
&setup.ledger_write,
&setup.institution_did,
DEFAULT_SCHEMA_ATTRS,
)
.await;
let cred_def = create_and_write_test_cred_def(
&setup.wallet,
&setup.anoncreds,
&setup.ledger_read,
&setup.ledger_write,
&setup.institution_did,
&schema.schema_id,
true,
)
.await;
let rev_reg = create_and_publish_test_rev_reg(
&setup.wallet,
&setup.anoncreds,
&setup.ledger_write,
&setup.institution_did,
&cred_def.get_cred_def_id(),
)
.await;
let cred_id = create_and_write_credential(
&setup.wallet,
&setup.wallet,
&setup.anoncreds,
&setup.anoncreds,
&setup.institution_did,
&cred_def,
Some(&rev_reg),
)
.await;
let cred_rev_id = get_cred_rev_id(&setup.wallet, &setup.anoncreds, &cred_id).await?;

let ledger = setup.ledger_read;

let (_, first_rev_reg_delta, first_timestamp) = ledger
.get_rev_reg_delta_json(&rev_reg.rev_reg_id, None, None)
.await?;

let (_, test_same_delta, test_same_timestamp) = ledger
.get_rev_reg_delta_json(&rev_reg.rev_reg_id, None, None)
.await?;

assert_eq!(first_rev_reg_delta, test_same_delta);
assert_eq!(first_timestamp, test_same_timestamp);

let anoncreds = &setup.anoncreds;

anoncreds
.revoke_credential_local(
&setup.wallet,
&setup.anoncreds,
&setup.ledger_read,
&setup.ledger_write,
&setup.institution_did,
&schema.schema_id,
true,
get_temp_dir_path().to_str().unwrap(),
&rev_reg.rev_reg_id,
&cred_rev_id,
)
.await;
let rev_reg = create_and_publish_test_rev_reg(
.await?;

rev_reg
.publish_local_revocations(
&setup.wallet,
&setup.anoncreds,
&setup.ledger_write,
&setup.institution_did,
&cred_def.get_cred_def_id(),
)
.await;
let cred_id = create_and_write_credential(
&setup.wallet,
&setup.wallet,
&setup.anoncreds,
&setup.anoncreds,
&setup.institution_did,
&cred_def,
Some(&rev_reg),
)
.await;
let cred_rev_id = get_cred_rev_id(&setup.wallet, &setup.anoncreds, &cred_id)
.await
.unwrap();

let ledger = setup.ledger_read;

let (_, first_rev_reg_delta, first_timestamp) = ledger
.get_rev_reg_delta_json(&rev_reg.rev_reg_id, None, None)
.await
.unwrap();

let (_, test_same_delta, test_same_timestamp) = ledger
.get_rev_reg_delta_json(&rev_reg.rev_reg_id, None, None)
.await
.unwrap();

assert_eq!(first_rev_reg_delta, test_same_delta);
assert_eq!(first_timestamp, test_same_timestamp);

let anoncreds = &setup.anoncreds;

anoncreds
.revoke_credential_local(
&setup.wallet,
get_temp_dir_path().to_str().unwrap(),
&rev_reg.rev_reg_id,
&cred_rev_id,
)
.await
.unwrap();

rev_reg
.publish_local_revocations(
&setup.wallet,
&setup.anoncreds,
&setup.ledger_write,
&setup.institution_did,
)
.await
.unwrap();

// Delta should change after revocation
let (_, second_rev_reg_delta, _) = ledger
.get_rev_reg_delta_json(&rev_reg.rev_reg_id, Some(first_timestamp + 1), None)
.await
.unwrap();

assert_ne!(first_rev_reg_delta, second_rev_reg_delta);
})
.await;
.await?;

// Delta should change after revocation
let (_, second_rev_reg_delta, _) = ledger
.get_rev_reg_delta_json(&rev_reg.rev_reg_id, Some(first_timestamp + 1), None)
.await?;

assert_ne!(first_rev_reg_delta, second_rev_reg_delta);
Ok(())
}
Loading

0 comments on commit c72360c

Please sign in to comment.