Skip to content

Commit

Permalink
Enable mocking specific parts of code
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Stas <patrik.stas@absa.africa>
  • Loading branch information
Patrik-Stas committed Sep 23, 2020
1 parent 291ffbf commit 976e26d
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 21 deletions.
31 changes: 23 additions & 8 deletions libvcx/src/disclosed_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use v3::{
};
use settings::indy_mocks_enabled;
use utils::mockdata::mockdata_proof::ARIES_PROOF_REQUEST_PRESENTATION;
use mock_settings::get_mock_generate_indy_proof;

lazy_static! {
static ref HANDLE_MAP: ObjectCache<DisclosedProofs> = ObjectCache::<DisclosedProofs>::new("disclosed-proofs-cache");
Expand Down Expand Up @@ -303,7 +304,6 @@ impl DisclosedProof {

fn retrieve_credentials(&self) -> VcxResult<String> {
trace!("DisclosedProof::set_state >>>");
if settings::indy_mocks_enabled() { return Ok(CREDS_FROM_PROOF_REQ.to_string()); }

let proof_req = self.proof_request
.as_ref()
Expand Down Expand Up @@ -405,6 +405,16 @@ impl DisclosedProof {
}

pub fn generate_indy_proof(credentials: &str, self_attested_attrs: &str, proof_req_data_json: &str) -> VcxResult<String> {
trace!("generate_indy_proof >>> credentials: {}, self_attested_attrs: {}", secret!(&credentials), secret!(&self_attested_attrs));

match get_mock_generate_indy_proof() {
None => {}
Some(mocked_indy_proof) => {
warn!("generate_indy_proof :: returning mocked response");
return Ok(mocked_indy_proof)
}
}

let proof_request: ProofRequestData = serde_json::from_str(&proof_req_data_json)
.map_err(|err| VcxError::from_msg(VcxErrorKind::InvalidJson, format!("Cannot deserialize proof request: {}", err)))?;

Expand Down Expand Up @@ -1007,7 +1017,8 @@ mod tests {
use utils::devsetup::*;

use super::*;
use utils::mockdata::mockdata_proof::ARIES_PROOF_REQUEST_PRESENTATION;
use utils::mockdata::mockdata_proof::{ARIES_PROOF_REQUEST_PRESENTATION, ARIES_PROOF_PRESENTATION_ACK};
use mock_settings::{set_mock_generate_indy_proof, reset_mock_settings};

fn proof_req_no_interval() -> ProofRequestData {
let proof_req = json!({
Expand Down Expand Up @@ -1066,10 +1077,14 @@ mod tests {
let handle_proof = create_proof("TEST_CREDENTIAL", &request).unwrap();
assert_eq!(VcxStateType::VcxStateRequestReceived as u32, get_state(handle_proof).unwrap());

// todo: mock dependencies to make generate_proof work
// generate_proof(proof_handle, selected_credentials.into(), "{}".to_string()).unwrap();
// send_proof(handle_proof, connection_h).unwrap();
// assert_eq!(VcxStateType::VcxStateAccepted as u32, get_state(handle_proof).unwrap());
set_mock_generate_indy_proof("{\"selected\":\"credentials\"}");
generate_proof(handle_proof, String::from("{\"selected\":\"credentials\"}"), "{}".to_string()).unwrap();
send_proof(handle_proof, connection_h).unwrap();
assert_eq!(VcxStateType::VcxStateOfferSent as u32, get_state(handle_proof).unwrap());

update_state(handle_proof, Some(String::from(ARIES_PROOF_PRESENTATION_ACK)), Some(connection_h)).unwrap();
assert_eq!(VcxStateType::VcxStateAccepted as u32, get_state(handle_proof).unwrap());
reset_mock_settings()
}

#[test]
Expand Down Expand Up @@ -1164,9 +1179,9 @@ mod tests {
let _setup = SetupDefaults::init();
::settings::set_config_value(::settings::CONFIG_PROTOCOL_TYPE, "4.0");

let handle_1 = create_proof("id", ARIES_PROOF_REQUEST_PRESENTATION).unwrap();
let handle = create_proof("id", ARIES_PROOF_REQUEST_PRESENTATION).unwrap();

let serialized = to_string(handle_1).unwrap();
let serialized = to_string(handle).unwrap();
from_string(&serialized).unwrap();
}

Expand Down
2 changes: 1 addition & 1 deletion libvcx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub mod error;
pub mod credential;
pub mod object_cache;
pub mod disclosed_proof;

pub mod mock_settings;

pub mod v3;

Expand Down
34 changes: 34 additions & 0 deletions libvcx/src/mock_settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::collections::HashMap;
use std::sync::RwLock;

pub static MOCKED_GENERATED_PROOF: &str = "mocked_proof";
pub static MOCKED_RETRIEVED_CREDS: &str = "mocked_retrieved_creds";

lazy_static! {
static ref MOCK_SETTINGS: RwLock<HashMap<String, String>> = RwLock::new(HashMap::new());
}

pub fn set_mock_generate_indy_proof(generated_proof: &str) {
let mut settings = MOCK_SETTINGS.write().unwrap();
settings.insert(String::from(MOCKED_GENERATED_PROOF), generated_proof.into());
}

pub fn get_mock_generate_indy_proof() -> Option<String> {
let config = MOCK_SETTINGS.read().unwrap();
config.get(MOCKED_GENERATED_PROOF).map(|s| String::from(s))
}

pub fn set_mock_creds_retrieved_for_proof_request(retrieve_creds: &str) {
let mut settings = MOCK_SETTINGS.write().unwrap();
settings.insert(String::from(MOCKED_RETRIEVED_CREDS), retrieve_creds.into());
}

pub fn get_mock_creds_retrieved_for_proof_request() -> Option<String> {
let config = MOCK_SETTINGS.read().unwrap();
config.get(MOCKED_RETRIEVED_CREDS).map(|s| String::from(s))
}

pub fn reset_mock_settings() {
let mut config = MOCK_SETTINGS.write().unwrap();
config.clear();
}
18 changes: 9 additions & 9 deletions libvcx/src/proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,15 +1053,15 @@ pub mod tests {

let handle_conn = build_test_connection_inviter_requested();

let hande_proof = create_proof("1".to_string(),
REQUESTED_ATTRS.to_owned(),
REQUESTED_PREDICATES.to_owned(),
r#"{"support_revocation":false}"#.to_string(),
"Optional".to_owned()).unwrap();
let request = generate_proof_request_msg(hande_proof).unwrap();
send_proof_request(hande_proof, handle_conn).unwrap();
update_state(hande_proof, Some(ARIES_PROOF_PRESENTATION.to_string()), Some(handle_conn)).unwrap();
assert_eq!(::proof::get_state(hande_proof).unwrap(), VcxStateType::VcxStateAccepted as u32);
let handle_proof = create_proof("1".to_string(),
REQUESTED_ATTRS.to_owned(),
REQUESTED_PREDICATES.to_owned(),
r#"{"support_revocation":false}"#.to_string(),
"Optional".to_owned()).unwrap();
let request = generate_proof_request_msg(handle_proof).unwrap();
send_proof_request(handle_proof, handle_conn).unwrap();
update_state(handle_proof, Some(ARIES_PROOF_PRESENTATION.to_string()), Some(handle_conn)).unwrap();
assert_eq!(::proof::get_state(handle_proof).unwrap(), VcxStateType::VcxStateAccepted as u32);
}

#[test]
Expand Down
4 changes: 4 additions & 0 deletions libvcx/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ pub fn agency_decrypted_mocks_enabled() -> bool {
}
}

pub fn enable_mock_generate_indy_proof() {

}

pub fn process_config_string(config: &str, do_validation: bool) -> VcxResult<u32> {
trace!("process_config_string >>> config {}", config);

Expand Down
9 changes: 9 additions & 0 deletions libvcx/src/utils/libindy/anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use utils::libindy::{LibindyMock, wallet::get_wallet_handle};
use utils::libindy::cache::{clear_rev_reg_delta_cache, get_rev_reg_delta_cache, set_rev_reg_delta_cache};
use utils::libindy::ledger::*;
use utils::libindy::payments::{pay_for_txn, PaymentTxn};
use mock_settings::get_mock_creds_retrieved_for_proof_request;

const BLOB_STORAGE_TYPE: &str = "default";
const REVOCATION_REGISTRY_TYPE: &str = "ISSUANCE_BY_DEFAULT";
Expand Down Expand Up @@ -145,6 +146,14 @@ fn close_search_handle(search_handle: i32) -> VcxResult<()> {
}

pub fn libindy_prover_get_credentials_for_proof_req(proof_req: &str) -> VcxResult<String> {
match get_mock_creds_retrieved_for_proof_request() {
None => {}
Some(mocked_creds) => {
warn!("get_mock_creds_retrieved_for_proof_request :: returning mocked response");
return Ok(mocked_creds)
}
}

let wallet_handle = get_wallet_handle();

// this may be too redundant since Prover::search_credentials will validate the proof reqeuest already.
Expand Down
3 changes: 0 additions & 3 deletions libvcx/src/v3/handlers/proof_presentation/prover/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ impl Prover {
pub fn retrieve_credentials(&self) -> VcxResult<String> {
trace!("Prover::retrieve_credentials >>>");
let presentation_request = self.prover_sm.presentation_request().request_presentations_attach.content()?;
if settings::indy_mocks_enabled() {
return Ok(CREDS_FROM_PROOF_REQ.to_string());
}
anoncreds::libindy_prover_get_credentials_for_proof_req(&presentation_request)
}

Expand Down
9 changes: 9 additions & 0 deletions libvcx/src/v3/handlers/proof_presentation/prover/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,8 @@ pub mod test {

mod step {
use super::*;
use utils::constants::CREDS_FROM_PROOF_REQ;
use mock_settings::{set_mock_creds_retrieved_for_proof_request, reset_mock_settings};

#[test]
#[cfg(feature = "general_test")]
Expand All @@ -535,11 +537,13 @@ pub mod test {
#[cfg(feature = "general_test")]
fn test_prover_handle_prepare_presentation_message_from_initiated_state_for_invalid_credentials() {
let _setup = SetupAriesMocks::init();
set_mock_creds_retrieved_for_proof_request(CREDS_FROM_PROOF_REQ);

let mut prover_sm = _prover_sm();
prover_sm = prover_sm.step(ProverMessages::PreparePresentation(("invalid".to_string(), _self_attested()))).unwrap();

assert_match!(ProverState::PresentationPreparationFailed(_), prover_sm.state);
reset_mock_settings()
}

#[test]
Expand Down Expand Up @@ -642,6 +646,7 @@ pub mod test {
#[cfg(feature = "general_test")]
fn test_prover_handle_send_presentation_message_from_presentation_preparation_failed_state() {
let _setup = SetupAriesMocks::init();
set_mock_creds_retrieved_for_proof_request(CREDS_FROM_PROOF_REQ);

let mut prover_sm = _prover_sm();
prover_sm = prover_sm.step(ProverMessages::PreparePresentation(("invalid".to_string(), _self_attested()))).unwrap();
Expand All @@ -650,12 +655,14 @@ pub mod test {
prover_sm = prover_sm.step(ProverMessages::SendPresentation(mock_connection())).unwrap();
assert_match!(ProverState::Finished(_), prover_sm.state);
assert_eq!(Status::Failed(ProblemReport::default()).code(), prover_sm.presentation_status());
reset_mock_settings()
}

#[test]
#[cfg(feature = "general_test")]
fn test_prover_handle_other_messages_from_presentation_preparation_failed_state() {
let _setup = SetupAriesMocks::init();
set_mock_creds_retrieved_for_proof_request(CREDS_FROM_PROOF_REQ);

let mut prover_sm = _prover_sm();
prover_sm = prover_sm.step(ProverMessages::PreparePresentation(("invalid".to_string(), _self_attested()))).unwrap();
Expand All @@ -665,6 +672,8 @@ pub mod test {

prover_sm = prover_sm.step(ProverMessages::PresentationAckReceived(_ack())).unwrap();
assert_match!(ProverState::PresentationPreparationFailed(_), prover_sm.state);

reset_mock_settings()
}

#[test]
Expand Down

0 comments on commit 976e26d

Please sign in to comment.