Skip to content

Commit

Permalink
feat: nais and smooth test logging
Browse files Browse the repository at this point in the history
Co-authored-by: Trong Huu Nguyen <[email protected]>
Co-authored-by: Tommy Trøen <[email protected]>
  • Loading branch information
3 people committed Nov 5, 2024
1 parent 9837fe2 commit 76dd39e
Showing 1 changed file with 51 additions and 30 deletions.
81 changes: 51 additions & 30 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,56 @@ mod tests {
use crate::app::App;
use crate::config::Config;
use crate::identity_provider::{IdentityProvider, IntrospectRequest, TokenExchangeRequest, TokenRequest, TokenResponse};
use log::{info, LevelFilter};
use reqwest::{Error, Response};
use serde::Serialize;
use serde_json::Value;
use std::collections::HashMap;
use testcontainers::{ContainerAsync, GenericImage};
// TODO: add some error case tests

struct DockerRuntimeParams {
container: Option<ContainerAsync<GenericImage>>,
host: String,
port: u16,
}

impl DockerRuntimeParams {
/// Runs Docker from Rust, no external setup needed
#[cfg(feature = "docker")]
async fn init() -> DockerRuntimeParams {
use testcontainers::core::{IntoContainerPort, WaitFor};
use testcontainers::core::wait::HttpWaitStrategy;
use testcontainers::runners::AsyncRunner;
use reqwest::StatusCode;

// Set up Docker container
let container = GenericImage::new("ghcr.io/navikt/mock-oauth2-server", "2.1.10")
.with_exposed_port(8080.tcp())
.with_wait_for(WaitFor::Http(HttpWaitStrategy::new("/maskinporten/.well-known/openid-configuration").with_expected_status_code(StatusCode::OK)))
.start()
.await
.unwrap();
let host = container.get_host().await.unwrap().to_string();
let port = container.get_host_port_ipv4(8080).await.unwrap();
Self {
container: Some(container),
host,
port,
}
}

/// Requires docker-compose up to be running.
#[cfg(not(feature = "docker"))]
async fn init() -> DockerRuntimeParams {
Self {
container: None,
host: "localhost".to_string(),
port: 8080,
}
}
}

/// Test a full round-trip of the `/token`, `/token/exchange`, and `/introspect` endpoints.
///
/// Content-type encodings tested are both `application/json` and `application/x-www-form-urlencoded`.
Expand All @@ -69,39 +112,17 @@ mod tests {
/// 2. Introspect the resulting token and check parameters
#[tokio::test]
async fn test_roundtrip() {
#[allow(unused_mut)]
let mut host;
#[allow(unused_mut)]
let mut host_port;
#[allow(unused_variables, unused_mut)]
let mut container: ContainerAsync<GenericImage>;
env_logger::builder().filter_level(LevelFilter::Info).init();

#[cfg(feature = "docker")]
{
use testcontainers::core::{IntoContainerPort, WaitFor};
use testcontainers::core::wait::HttpWaitStrategy;
use testcontainers::runners::AsyncRunner;
use reqwest::StatusCode;
let docker = DockerRuntimeParams::init().await;

// Set up Docker container
container = GenericImage::new("ghcr.io/navikt/mock-oauth2-server", "2.1.10")
.with_exposed_port(8080.tcp())
.with_wait_for(WaitFor::Http(HttpWaitStrategy::new("/maskinporten/.well-known/openid-configuration").with_expected_status_code(StatusCode::OK)))
.start()
.await
.unwrap();
host = container.get_host().await.unwrap().to_string();
host_port = container.get_host_port_ipv4(8080).await.unwrap();
}
#[cfg(not(feature = "docker"))]
// Requires docker-compose up to be running
{
host = "localhost".to_string();
host_port = 8080;
match docker.container {
None => info!("Expecting mock-oauth2-server natively in docker-compose on localhost:8080"),
Some(_) => info!("Running mock-oauth2-server on {}:{}", docker.host, docker.port, ),
}

// Set up Texas
let cfg = Config::mock(host.to_string(), host_port);
let cfg = Config::mock(docker.host.clone(), docker.port);
let app = App::new(cfg.clone()).await;
let address = app.address().unwrap();
let join_handler = tokio::spawn(async move {
Expand All @@ -112,8 +133,8 @@ mod tests {
machine_to_machine_token(cfg.maskinporten_issuer.clone(), address.to_string(), IdentityProvider::Maskinporten, format.clone()).await;
machine_to_machine_token(cfg.azure_ad_issuer.clone(), address.to_string(), IdentityProvider::AzureAD, format.clone()).await;

token_exchange_token(cfg.azure_ad_issuer.clone(), address.to_string(), format!("{}:{}", host, host_port), IdentityProvider::AzureAD, format.clone()).await;
token_exchange_token(cfg.token_x_issuer.clone(), address.to_string(), format!("{}:{}", host, host_port), IdentityProvider::TokenX, format).await;
token_exchange_token(cfg.azure_ad_issuer.clone(), address.to_string(), format!("{}:{}", docker.host.clone(), docker.port), IdentityProvider::AzureAD, format.clone()).await;
token_exchange_token(cfg.token_x_issuer.clone(), address.to_string(), format!("{}:{}", docker.host.clone(), docker.port), IdentityProvider::TokenX, format).await;
}

// TODO: implement these tests:
Expand Down

0 comments on commit 76dd39e

Please sign in to comment.