Skip to content

Commit

Permalink
refactor: improve mediator routes (#1176)
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Prazak <[email protected]>
Co-authored-by: Ondrej Prazak <[email protected]>
  • Loading branch information
xprazak2 and Ondrej Prazak authored Apr 17, 2024
1 parent 181d5ba commit 58bbc0a
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 113 deletions.
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion aries/agents/mediator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ axum-macros = "0.3.8"
diddoc_legacy = { path = "../../misc/legacy/diddoc_legacy" }
dotenvy = "0.15"
env_logger = "0.10.0"
fast_qr = { version = "0.10.2", features = ["svg"] }
futures = "0.3.28"
log = "0.4.20"
messages = { path = "../../messages" }
Expand Down
9 changes: 3 additions & 6 deletions aries/agents/mediator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,10 @@ cargo build --package mediator --no-default-features --release
Currently exposed endpoints.

```yaml
`/register`:
- **Description** : |
`/invitation`:
- **Description** : |
Returns OOB invitation in json format.
Shows an Aries Out Of Band (OOB) invitation which can be used to connect to the mediator using a conformant Aries Agent.
Use `Accept` header with value "application/json" to receive same in json format.
`/register.json`:
- **Description** : Returns OOB invitation in json format.
```
```yaml
Expand Down
29 changes: 1 addition & 28 deletions aries/agents/mediator/src/http_routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,6 @@ use crate::{
persistence::MediatorPersistence,
};

pub async fn oob_invite_qr(
headers: HeaderMap,
State(agent): State<ArcAgent<impl BaseWallet, impl MediatorPersistence>>,
) -> Response {
let Json(oob_json) = oob_invite_json(State(agent)).await;
match detect_mime_type(&headers) {
"application/json" => Json(oob_json).into_response(),
_ => {
let oob_string = serde_json::to_string_pretty(&oob_json).unwrap();
let qr = fast_qr::QRBuilder::new(oob_string.clone()).build().unwrap();
let oob_qr_svg = fast_qr::convert::svg::SvgBuilder::default().to_str(&qr);
Html(format!(
"<style>
svg {{
width: 50%;
height: 50%;
}}
</style>
{oob_qr_svg} <br>
<pre>{oob_string}</pre>"
))
.into_response()
}
}
}

fn detect_mime_type(headers: &HeaderMap) -> &str {
headers
.get(ACCEPT)
Expand Down Expand Up @@ -89,8 +63,7 @@ pub async fn build_router(
) -> Router {
Router::default()
.route("/", get(readme))
.route("/register", get(oob_invite_qr))
.route("/register.json", get(oob_invite_json))
.route("/invitation", get(oob_invite_json))
.route("/didcomm", get(handle_didcomm).post(handle_didcomm))
.layer(tower_http::catch_panic::CatchPanicLayer::new())
.with_state(Arc::new(agent))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use messages::{
},
AriesMessage,
};
use reqwest::header::ACCEPT;
use test_utils::mockdata::mock_ledger::MockLedger;

use super::prelude::*;
Expand All @@ -36,17 +35,16 @@ pub async fn didcomm_connection(
) -> Result<InviteeConnection<Completed>> {
let client = reqwest::Client::new();
let base: Url = ENDPOINT_ROOT.parse().unwrap();
let endpoint_register = base.join("register").unwrap();
let endpoint_register = base.join("invitation").unwrap();

let oobi: OOBInvitation = client
.get(endpoint_register)
.header(ACCEPT, "application/json")
.send()
.await?
.error_for_status()?
.json()
.await?;
info!("Got invitation from register endpoint {:?}", oobi);
info!("Got invitation {:?}", oobi);

let state: InviteeConnection<Completed> =
agent.establish_connection(oobi, aries_transport).await?;
Expand Down
8 changes: 3 additions & 5 deletions aries/agents/mediator/tests/mediator-aries-connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod common;

use aries_vcx_wallet::wallet::indy::IndySdkWallet;
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};

Expand All @@ -15,18 +14,17 @@ async fn didcomm_connection_succeeds() -> Result<()> {
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();
let endpoint_invitation = base.join("invitation").unwrap();

let oobi: OOBInvitation = client
.get(endpoint_register)
.header(ACCEPT, "application/json")
.get(endpoint_invitation)
.send()
.await?
.error_for_status()?
.json()
.await?;
info!(
"Got invitation from register endpoint {}",
"Got invitation {}",
serde_json::to_string_pretty(&oobi.clone()).unwrap()
);
let agent = mediator::aries_agent::AgentBuilder::<IndySdkWallet>::new_demo_agent().await?;
Expand Down
64 changes: 2 additions & 62 deletions aries/agents/mediator/tests/mediator-oob-invitation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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};
Expand All @@ -12,12 +11,12 @@ 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<()> {
fn endpoint_invitation_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();
let endpoint_register_json = base.join("/invitation").unwrap();

let res = client
.get(endpoint_register_json)
Expand All @@ -29,62 +28,3 @@ fn endpoint_register_json_returns_oob() -> Result<()> {

Ok(())
}

#[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();

let res = client
.get(endpoint_register)
.header(ACCEPT, "application/json")
.send()?
.error_for_status()?;
info!("{:?}", res);

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

Ok(())
}

#[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()?;
info!("{:?}", res);

assert!(res
.headers()
.get(CONTENT_TYPE)
.unwrap()
.to_str()
.unwrap()
.contains("text/html"));

Ok(())
}

#[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()?;
info!("{:?}", res);

let _html = res.text()?;
// validate qr of html page
unimplemented!("validate oob qr of returned html page");
}

0 comments on commit 58bbc0a

Please sign in to comment.