Skip to content

Commit

Permalink
Address PR review comments
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 Nov 3, 2020
1 parent 582b43d commit e3a4869
Show file tree
Hide file tree
Showing 21 changed files with 72 additions and 75 deletions.
28 changes: 4 additions & 24 deletions libvcx/src/agency_comm/agency_settings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::borrow::Borrow;
use std::collections::HashMap;
use std::path::Path;
use std::sync::RwLock;

use serde_json::Value;
Expand Down Expand Up @@ -29,11 +28,6 @@ pub static VALID_AGENCY_CONFIG_KEYS: &[&str] = &[
CONFIG_ENABLE_TEST_MODE
];


pub static DEFAULT_DID: &str = "2hoqvcwupRTUNkXn6ArYzs";
pub static DEFAULT_VERKEY: &str = "FuN98eH2eZybECWkofW6A9BKJxxnTatBCopfUiNxo6ZB";
pub static DEFAULT_URL: &str = "http://127.0.0.1:8080";

lazy_static! {
static ref AGENCY_SETTINGS: RwLock<HashMap<String, String>> = RwLock::new(HashMap::new());
}
Expand All @@ -60,6 +54,10 @@ fn validate_optional_config_val<F, S, E>(val: Option<&String>, err: VcxErrorKind
pub fn set_testing_defaults_agency() -> u32 {
trace!("set_testing_defaults_agency >>>");

let DEFAULT_DID= "2hoqvcwupRTUNkXn6ArYzs";
let DEFAULT_VERKEY= "FuN98eH2eZybECWkofW6A9BKJxxnTatBCopfUiNxo6ZB";
let DEFAULT_URL= "http://127.0.0.1:8080";

// if this fails the test should exit
let mut agency_settings = AGENCY_SETTINGS.write().unwrap();

Expand Down Expand Up @@ -153,21 +151,3 @@ pub fn set_config_value(key: &str, value: &str) {
}


pub fn agency_mocks_enabled() -> bool {
let config = AGENCY_SETTINGS.read().unwrap();

match config.get(CONFIG_ENABLE_TEST_MODE) {
None => false,
Some(value) => value == "true" || value == "agency"
}
}

pub fn agency_decrypted_mocks_enabled() -> bool {
let config = AGENCY_SETTINGS.read().unwrap();

match config.get(CONFIG_ENABLE_TEST_MODE) {
None => false,
Some(value) => value == "true"
}
}

9 changes: 5 additions & 4 deletions libvcx/src/agency_comm/create_key.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use agency_comm::{A2AMessage, A2AMessageKinds, A2AMessageV2, agency_settings, parse_response_from_agency, prepare_message_for_agency};
use agency_comm::message_type::MessageTypes;
use agency_comm::mocking::AgencyMock;
use agency_comm::util::post_u8;
use agency_comm::utils::comm::post_to_agency;
use error::prelude::*;
use utils::{constants, httpclient, validation};
use crate::agency_comm::mocking;

#[derive(Deserialize, Serialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -55,15 +56,15 @@ impl CreateKeyBuilder {
}

pub fn send_secure(&self) -> VcxResult<(String, String)> {
trace!("CreateKeyMsg::send >>>");
trace!("CreateKeyBuilder::send_secure >>>");

if agency_settings::agency_mocks_enabled() {
if mocking::agency_mocks_enabled() {
AgencyMock::set_next_response(constants::CREATE_KEYS_V2_RESPONSE.to_vec());
}

let data = self.prepare_request()?;

let response = post_u8(&data)?;
let response = post_to_agency(&data)?;

self.parse_response(&response)
}
Expand Down
9 changes: 5 additions & 4 deletions libvcx/src/agency_comm/get_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ use std::collections::HashMap;

use agency_comm::{A2AMessage, A2AMessageKinds, A2AMessageV2, agency_settings, GeneralMessage, get_messages, MessageStatusCode, parse_response_from_agency, prepare_message_for_agency, prepare_message_for_agent, RemoteMessageType};
use agency_comm::message_type::MessageTypes;
use agency_comm::util::post_u8;
use agency_comm::utils::comm::post_to_agency;
use aries::handlers::connection::agent_info::AgentInfo;
use aries::messages::a2a::A2AMessage as AriesA2AMessage;
use aries::utils::encryption_envelope::EncryptionEnvelope;
use error::{VcxError, VcxErrorKind, VcxResult};
use settings;
use settings::ProtocolTypes;
use utils::{constants, httpclient};
use crate::agency_comm::mocking;

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -127,7 +128,7 @@ impl GetMessagesBuilder {

let data = self.prepare_request()?;

let response = post_u8(&data)?;
let response = post_to_agency(&data)?;

self.parse_response(response)
}
Expand All @@ -153,9 +154,9 @@ impl GetMessagesBuilder {

let data = self.prepare_download_request()?;

let response = post_u8(&data)?;
let response = post_to_agency(&data)?;

if agency_settings::agency_mocks_enabled() && response.len() == 0 {
if mocking::agency_mocks_enabled() && response.len() == 0 {
return Ok(Vec::new());
}

Expand Down
25 changes: 21 additions & 4 deletions libvcx/src/agency_comm/mocking.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::sync::Mutex;

use agency_comm::agency_settings;
use settings;
use settings::CONFIG_ENABLE_TEST_MODE;

lazy_static! {
static ref AGENCY_MOCK: Mutex<AgencyMock> = Mutex::new(AgencyMock::default());
Expand All @@ -25,7 +27,7 @@ pub struct AgencyMockDecrypted {

impl AgencyMock {
pub fn set_next_response(body: Vec<u8>) {
if agency_settings::agency_mocks_enabled() {
if agency_mocks_enabled() {
AGENCY_MOCK.lock().unwrap().responses.push(body);
}
}
Expand All @@ -37,7 +39,7 @@ impl AgencyMock {

impl AgencyMockDecrypted {
pub fn set_next_decrypted_response(body: &str) {
if agency_settings::agency_mocks_enabled() {
if agency_mocks_enabled() {
AGENCY_MOCK_DECRYPTED_RESPONSES.lock().unwrap().responses.push(body.into());
} else {
warn!("Attempting to set mocked decrypted response when mocks are not enabled!");
Expand All @@ -58,7 +60,7 @@ impl AgencyMockDecrypted {
}

pub fn set_next_decrypted_message(message: &str) {
if agency_settings::agency_mocks_enabled() {
if agency_mocks_enabled() {
AGENCY_MOCK_DECRYPTED_MESSAGES.lock().unwrap().messages.push(message.into());
} else {
warn!("Attempting to set mocked decrypted message when mocks are not enabled!");
Expand All @@ -77,4 +79,19 @@ impl AgencyMockDecrypted {
AGENCY_MOCK_DECRYPTED_MESSAGES.lock().unwrap().messages.clear();
AGENCY_MOCK_DECRYPTED_RESPONSES.lock().unwrap().responses.clear();
}
}
}

pub fn agency_mocks_enabled() -> bool {
match agency_settings::get_config_value(CONFIG_ENABLE_TEST_MODE).ok() {
None => false,
Some(value) => value == "true" || value == "agency"
}
}

pub fn agency_decrypted_mocks_enabled() -> bool {
match agency_settings::get_config_value(CONFIG_ENABLE_TEST_MODE).ok() {
None => false,
Some(value) => value == "true"
}
}

5 changes: 2 additions & 3 deletions libvcx/src/agency_comm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use error::prelude::*;
use libindy::utils::crypto;
use utils::validation;

use self::agent_utils::{ComMethodUpdated, Connect, ConnectResponse, CreateAgent, CreateAgentResponse, SignUp, SignUpResponse, UpdateComMethod};
use self::utils::agent_utils::{ComMethodUpdated, Connect, ConnectResponse, CreateAgent, CreateAgentResponse, SignUp, SignUpResponse, UpdateComMethod};
use self::create_key::{CreateKey, CreateKeyBuilder, CreateKeyResponse};
use self::get_message::{GetMessages, GetMessagesBuilder, GetMessagesResponse, MessagesByConnections};
use self::message_type::*;
Expand All @@ -20,7 +20,7 @@ use agency_comm::mocking::AgencyMockDecrypted;
pub mod create_key;
pub mod get_message;
pub mod update_profile;
pub mod agent_utils;
pub mod utils;
pub mod update_connection;
pub mod update_message;
pub mod message_type;
Expand All @@ -29,7 +29,6 @@ pub mod payload;
pub mod thread;
pub mod agency_settings;
pub mod mocking;
mod util;

#[derive(Debug, Serialize)]
#[serde(untagged)]
Expand Down
2 changes: 1 addition & 1 deletion libvcx/src/agency_comm/payload.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde_json::Value;

use agency_comm::get_message::MessagePayload;
use agency_comm::message_type::*;
use agency_comm::thread::Thread;
use error::{VcxError, VcxErrorKind, VcxResult};
use libindy::utils::crypto;
use agency_comm::message_type::MessageTypeV2;

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq)]
#[serde(untagged)]
Expand Down
4 changes: 2 additions & 2 deletions libvcx/src/agency_comm/update_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde_json::Value;

use agency_comm::{A2AMessage, A2AMessageKinds, A2AMessageV2, delete_connection, GeneralMessage, parse_response_from_agency, prepare_message_for_agent};
use agency_comm::message_type::MessageTypes;
use agency_comm::util::post_u8;
use agency_comm::utils::comm::post_to_agency;
use error::prelude::*;
use settings;
use utils::httpclient;
Expand Down Expand Up @@ -82,7 +82,7 @@ impl DeleteConnectionBuilder {

let data = self.prepare_request()?;

let response = post_u8(&data)?;
let response = post_to_agency(&data)?;

self.parse_response(&response)
}
Expand Down
7 changes: 4 additions & 3 deletions libvcx/src/agency_comm/update_message.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use agency_comm::{A2AMessage, A2AMessageKinds, A2AMessageV2, agency_settings, MessageStatusCode, parse_response_from_agency, prepare_message_for_agency};
use agency_comm::message_type::MessageTypes;
use agency_comm::mocking::AgencyMock;
use agency_comm::util::post_u8;
use agency_comm::utils::comm::post_to_agency;
use error::{VcxError, VcxErrorKind, VcxResult};
use settings;
use utils::{constants, httpclient};
use crate::agency_comm::mocking;

#[derive(Clone, Serialize, Deserialize, Debug, PartialEq)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -66,7 +67,7 @@ impl UpdateMessageStatusByConnectionsBuilder {

let data = self.prepare_request()?;

let response = post_u8(&data)?;
let response = post_to_agency(&data)?;

self.parse_response(&response)
}
Expand Down Expand Up @@ -115,7 +116,7 @@ pub fn update_agency_messages(status_code: &str, msg_json: &str) -> VcxResult<()
pub fn update_messages(status_code: MessageStatusCode, uids_by_conns: Vec<UIDsByConn>) -> VcxResult<()> {
trace!("update_messages >>> ");

if agency_settings::agency_mocks_enabled() {
if mocking::agency_mocks_enabled() {
trace!("update_messages >>> agency mocks enabled, returning empty response");
return Ok(());
};
Expand Down
4 changes: 2 additions & 2 deletions libvcx/src/agency_comm/update_profile.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use agency_comm::{A2AMessage, A2AMessageKinds, A2AMessageV2, agency_settings, parse_response_from_agency, prepare_message_for_agency};
use agency_comm::message_type::MessageTypes;
use agency_comm::mocking::AgencyMock;
use agency_comm::util::post_u8;
use agency_comm::utils::comm::post_to_agency;
use error::{VcxError, VcxErrorKind, VcxResult};
use settings;
use utils::{httpclient, validation};
Expand Down Expand Up @@ -87,7 +87,7 @@ impl UpdateProfileDataBuilder {

let data = self.prepare_request()?;

let response = post_u8(&data)?;
let response = post_to_agency(&data)?;

self.parse_response(response)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;

use agency_comm::{A2AMessage, A2AMessageKinds, A2AMessageV2, agency_settings, parse_response_from_agency, prepare_message_for_agency};
use agency_comm::agency_settings::agency_mocks_enabled;
use crate::agency_comm::mocking::agency_mocks_enabled;
use agency_comm::message_type::MessageTypes;
use agency_comm::mocking::AgencyMockDecrypted;
use agency_comm::util::post_u8;
use error::prelude::*;
use libindy::utils::{anoncreds, wallet};
use libindy::utils::signus::create_and_store_my_did;
use libindy::utils::wallet::get_wallet_handle;
use settings;
use utils::{constants, error, httpclient};
use utils::option_util::get_or_default;
use agency_comm::utils::comm::post_to_agency;

#[derive(Serialize, Deserialize, Debug)]
pub struct Connect {
Expand Down Expand Up @@ -411,7 +411,7 @@ fn update_agent_webhook_v2(to_did: &str, com_method: ComMethod) -> VcxResult<()>
pub fn send_message_to_agency(message: &A2AMessage, did: &str) -> VcxResult<Vec<A2AMessage>> {
let data = prepare_message_for_agency(message, &did)?;

let response = post_u8(&data)
let response = post_to_agency(&data)
.map_err(|err| err.map(VcxErrorKind::InvalidHttpResponse, error::INVALID_HTTP_RESPONSE.message))?;

parse_response_from_agency(&response)
Expand All @@ -421,9 +421,9 @@ pub fn send_message_to_agency(message: &A2AMessage, did: &str) -> VcxResult<Vec<
mod tests {
use std::env;

use agency_comm::agent_utils::{ComMethodType, Config, configure_wallet, connect_register_provision, update_agent_webhook};
use api::vcx::vcx_shutdown;
use utils::devsetup::{SetupDefaults, SetupLibraryAgencyV2, SetupMocks};
use agency_comm::utils::agent_utils::{connect_register_provision, configure_wallet, Config, ComMethodType, update_agent_webhook};

#[test]
#[cfg(feature = "agency")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use agency_comm::agency_settings;
use error::VcxResult;
use utils;

//Todo: change this RC to a u32
pub fn post_u8(body_content: &Vec<u8>) -> VcxResult<Vec<u8>> {
pub fn post_to_agency(body_content: &Vec<u8>) -> VcxResult<Vec<u8>> {
let endpoint = format!("{}/agency/msg", agency_settings::get_config_value(agency_settings::CONFIG_AGENCY_ENDPOINT)?);
utils::httpclient::post_message(body_content, &endpoint)
}
2 changes: 2 additions & 0 deletions libvcx/src/agency_comm/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod agent_utils;
pub(super) mod comm;
4 changes: 2 additions & 2 deletions libvcx/src/api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub extern fn vcx_provision_agent(config: *const c_char) -> *mut c_char {

trace!("vcx_provision_agent(config: {})", config);

match agency_comm::agent_utils::connect_register_provision(&config) {
match agency_comm::utils::agent_utils::connect_register_provision(&config) {
Err(e) => {
error!("Provision Agent Error {}.", e);
let _res: u32 = e.into();
Expand Down Expand Up @@ -84,7 +84,7 @@ pub extern fn vcx_agent_provision_async(command_handle: CommandHandle,
command_handle, config);

thread::spawn(move || {
match agency_comm::agent_utils::connect_register_provision(&config) {
match agency_comm::utils::agent_utils::connect_register_provision(&config) {
Err(e) => {
error!("vcx_agent_provision_async_cb(command_handle: {}, rc: {}, config: NULL", command_handle, e);
cb(command_handle, e.into(), ptr::null_mut());
Expand Down
4 changes: 2 additions & 2 deletions libvcx/src/api/vcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ fn _finish_init(command_handle: CommandHandle, cb: extern fn(xcommand_handle: Co

info!("_finish_init: getting and setting webhook url");
match settings::get_config_value(settings::CONFIG_WEBHOOK_URL) {
Ok(webhook_url) => match ::agency_comm::agent_utils::update_agent_webhook(&webhook_url) {
Ok(webhook_url) => match ::agency_comm::utils::agent_utils::update_agent_webhook(&webhook_url) {
Ok(()) => {
info!("Agent webhook url updated on init, webhook_url={}", webhook_url);
cb(command_handle, error::SUCCESS.code_num);
Expand Down Expand Up @@ -515,7 +515,7 @@ pub extern fn vcx_update_webhook_url(command_handle: CommandHandle,
settings::set_config_value(::settings::CONFIG_WEBHOOK_URL, &notification_webhook_url);

spawn(move || {
match ::agency_comm::agent_utils::update_agent_webhook(&notification_webhook_url[..]) {
match ::agency_comm::utils::agent_utils::update_agent_webhook(&notification_webhook_url[..]) {
Ok(()) => {
trace!("vcx_update_webhook_url_cb(command_handle: {}, rc: {})",
command_handle, error::SUCCESS.message);
Expand Down
2 changes: 1 addition & 1 deletion libvcx/src/aries/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod test {
use ::{rand, settings};
use rand::Rng;

use agency_comm::agent_utils::connect_register_provision;
use agency_comm::utils::agent_utils::connect_register_provision;
use agency_comm::payload::{PayloadKinds};
use utils::devsetup::*;
use libindy::utils::wallet::*;
Expand Down
Loading

0 comments on commit e3a4869

Please sign in to comment.