Skip to content

Commit

Permalink
refactor: refactor init code of tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Naian <[email protected]>
  • Loading branch information
nain-F49FF806 committed Oct 27, 2023
1 parent 3ca303a commit a441558
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 60 deletions.
11 changes: 8 additions & 3 deletions agents/rust/mediator/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ pub mod agent_and_transport_utils;
pub mod test_setup {
// inspired by
// https://stackoverflow.com/questions/58006033/how-to-run-setup-code-before-any-tests-run-in-rust
use std::sync::Once;

static INIT: Once = Once::new();
static INIT: std::sync::Once = std::sync::Once::new();
pub trait OneTimeInit {
// runs the initialization code if it hasn't been run yet, else does nothing
fn init(&self) {
Expand All @@ -15,6 +13,13 @@ pub mod test_setup {
// your custom initialization code goes here
fn one_time_setup_code(&self);
}

pub fn setup_env_logging() {
// default test setup code
let _ = dotenvy::dotenv();
let env = env_logger::Env::default().default_filter_or("info");
env_logger::init_from_env(env);
}
}

pub mod prelude {
Expand Down
22 changes: 5 additions & 17 deletions agents/rust/mediator/tests/mediator-aries-connection.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
mod common;
use std::collections::VecDeque;

use common::{prelude::*, test_setup::OneTimeInit};
use mediator::aries_agent::transports::AriesReqwest;
use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OOBInvitation;
use reqwest::header::ACCEPT;

const ENDPOINT_ROOT: &str = "http://localhost:8005";
use crate::common::{prelude::*, test_setup::setup_env_logging};

struct TestSetupAries;
impl OneTimeInit for TestSetupAries {
fn one_time_setup_code(&self) {
fn setup_logging() {
let env = env_logger::Env::default().default_filter_or("info");
env_logger::init_from_env(env);
}
fn load_dot_env() {
let _ = dotenvy::dotenv();
}
load_dot_env();
setup_logging();
}
}
static LOGGING_INIT: std::sync::Once = std::sync::Once::new();

const ENDPOINT_ROOT: &str = "http://localhost:8005";

#[tokio::test]
async fn didcomm_connection_succeeds() -> Result<()> {
TestSetupAries.init();
LOGGING_INIT.call_once(setup_env_logging);
let client = reqwest::Client::new();
let base: Url = ENDPOINT_ROOT.parse().unwrap();
let endpoint_register = base.join("register").unwrap();
Expand Down
29 changes: 9 additions & 20 deletions agents/rust/mediator/tests/mediator-coord-protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use std::collections::VecDeque;

use aries_vcx::protocols::connection::invitee::{states::completed::Completed, InviteeConnection};
use aries_vcx_core::wallet::base_wallet::BaseWallet;
use common::{prelude::*, test_setup::OneTimeInit};
use diddoc_legacy::aries::diddoc::AriesDidDoc;
use mediation::{
didcomm_types::mediator_coord_structs::{
Expand All @@ -22,22 +21,12 @@ use mediator::{
use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OOBInvitation;
use reqwest::header::ACCEPT;

use crate::common::{prelude::*, test_setup::setup_env_logging};

static LOGGING_INIT: std::sync::Once = std::sync::Once::new();

const ENDPOINT_ROOT: &str = "http://localhost:8005";

struct TestSetupAries;
impl OneTimeInit for TestSetupAries {
fn one_time_setup_code(&self) {
fn setup_logging() {
let env = env_logger::Env::default().default_filter_or("info");
env_logger::init_from_env(env);
}
fn load_dot_env() {
let _ = dotenvy::dotenv();
}
load_dot_env();
setup_logging();
}
}
async fn didcomm_connection(
agent: &Agent<impl BaseWallet + 'static, impl MediatorPersistence>,
aries_transport: &mut impl AriesTransport,
Expand Down Expand Up @@ -104,7 +93,7 @@ async fn send_message_and_pop_response_message(
#[tokio::test]
#[ignore]
async fn test_init() {
TestSetupAries.init();
LOGGING_INIT.call_once(setup_env_logging);
let agent = mediator::aries_agent::AgentBuilder::new_demo_agent()
.await
.unwrap();
Expand All @@ -118,7 +107,7 @@ async fn test_init() {

#[tokio::test]
async fn test_mediate_grant() -> Result<()> {
TestSetupAries.init();
LOGGING_INIT.call_once(setup_env_logging);
// prepare connection parameters
let (agent, mut aries_transport, our_verkey, their_diddoc) =
gen_mediator_connected_agent().await?;
Expand Down Expand Up @@ -155,7 +144,7 @@ async fn test_mediate_grant() -> Result<()> {

#[tokio::test]
async fn test_mediate_keylist_update_add() -> Result<()> {
TestSetupAries.init();
LOGGING_INIT.call_once(setup_env_logging);
// prepare connection parameters
let (agent, mut aries_transport, our_verkey, their_diddoc) =
gen_mediator_connected_agent().await?;
Expand Down Expand Up @@ -199,7 +188,7 @@ async fn test_mediate_keylist_update_add() -> Result<()> {

#[tokio::test]
async fn test_mediate_keylist_query() -> Result<()> {
TestSetupAries.init();
LOGGING_INIT.call_once(setup_env_logging);
// prepare connection parameters
let (agent, mut aries_transport, our_verkey, their_diddoc) =
gen_mediator_connected_agent().await?;
Expand Down Expand Up @@ -256,7 +245,7 @@ async fn test_mediate_keylist_query() -> Result<()> {

#[tokio::test]
async fn test_mediate_keylist_update_remove() -> Result<()> {
TestSetupAries.init();
LOGGING_INIT.call_once(setup_env_logging);
// prepare connection parameters
let (agent, mut aries_transport, our_verkey, their_diddoc) =
gen_mediator_connected_agent().await?;
Expand Down
22 changes: 18 additions & 4 deletions agents/rust/mediator/tests/mediator-oob-invitation.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
mod common;

use anyhow::Result;
use messages::msg_fields::protocols::out_of_band::invitation::Invitation as OOBInvitation;
use reqwest::header::{ACCEPT, CONTENT_TYPE};
use url::Url;

use crate::common::{prelude::*, test_setup::setup_env_logging};

static LOGGING_INIT: std::sync::Once = std::sync::Once::new();

const ENDPOINT_ROOT: &str = "http://localhost:8005";

#[test]
fn endpoint_register_json_returns_oob() -> Result<()> {
LOGGING_INIT.call_once(setup_env_logging);

let client = reqwest::blocking::Client::new();
let base: Url = ENDPOINT_ROOT.parse().unwrap();
let endpoint_register_json = base.join("/register.json").unwrap();
Expand All @@ -15,7 +23,7 @@ fn endpoint_register_json_returns_oob() -> Result<()> {
.get(endpoint_register_json)
.send()?
.error_for_status()?;
println!("{:?}", res);
info!("{:?}", res);

let _oob: OOBInvitation = res.json()?;

Expand All @@ -24,6 +32,8 @@ fn endpoint_register_json_returns_oob() -> Result<()> {

#[test]
fn endpoint_register_returns_oob_with_correct_accept_header() -> Result<()> {
LOGGING_INIT.call_once(setup_env_logging);

let client = reqwest::blocking::Client::new();
let base: Url = ENDPOINT_ROOT.parse().unwrap();
let endpoint_register = base.join("/register").unwrap();
Expand All @@ -33,7 +43,7 @@ fn endpoint_register_returns_oob_with_correct_accept_header() -> Result<()> {
.header(ACCEPT, "application/json")
.send()?
.error_for_status()?;
println!("{:?}", res);
info!("{:?}", res);

let _oob: OOBInvitation = res.json()?;

Expand All @@ -42,12 +52,14 @@ fn endpoint_register_returns_oob_with_correct_accept_header() -> Result<()> {

#[test]
fn endpoint_register_returns_html_page() -> Result<()> {
LOGGING_INIT.call_once(setup_env_logging);

let client = reqwest::blocking::Client::new();
let base: Url = ENDPOINT_ROOT.parse().unwrap();
let endpoint_register = base.join("/register").unwrap();

let res = client.get(endpoint_register).send()?.error_for_status()?;
println!("{:?}", res);
info!("{:?}", res);

assert!(res
.headers()
Expand All @@ -63,12 +75,14 @@ fn endpoint_register_returns_html_page() -> Result<()> {
#[test]
#[ignore]
fn endpoint_register_returns_html_page_with_valid_oob_qr() -> Result<()> {
LOGGING_INIT.call_once(setup_env_logging);

let client = reqwest::blocking::Client::new();
let base: Url = ENDPOINT_ROOT.parse().unwrap();
let endpoint_register = base.join("/register").unwrap();

let res = client.get(endpoint_register).send()?.error_for_status()?;
println!("{:?}", res);
info!("{:?}", res);

let _html = res.text()?;
// validate qr of html page
Expand Down
19 changes: 3 additions & 16 deletions agents/rust/mediator/tests/mediator-routing-forward.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,10 @@ use crate::common::{
gen_mediator_connected_agent, send_message_and_pop_response_message,
},
prelude::*,
test_setup::OneTimeInit,
test_setup::setup_env_logging,
};

struct TestSetupAries;
impl OneTimeInit for TestSetupAries {
fn one_time_setup_code(&self) {
fn setup_logging() {
let env = env_logger::Env::default().default_filter_or("info");
env_logger::init_from_env(env);
}
fn load_dot_env() {
let _ = dotenvy::dotenv();
}
load_dot_env();
setup_logging();
}
}
static LOGGING_INIT: std::sync::Once = std::sync::Once::new();

async fn get_mediator_grant_data(
agent: &Agent<impl BaseWallet + 'static, impl MediatorPersistence>,
Expand Down Expand Up @@ -83,7 +70,7 @@ async fn get_mediator_grant_data(

#[tokio::test]
async fn test_forward_flow() -> Result<()> {
TestSetupAries.init();
LOGGING_INIT.call_once(setup_env_logging);
// prepare receiver connection parameters
let (mut agent, mut agent_aries_transport, agent_verkey, mediator_diddoc) =
gen_mediator_connected_agent().await?;
Expand Down

0 comments on commit a441558

Please sign in to comment.