Skip to content

Commit

Permalink
refactor: dry up redundant test app creation logic
Browse files Browse the repository at this point in the history
Signed-off-by: Jim Crossley <[email protected]>
  • Loading branch information
jcrossley3 authored and ctron committed Jan 14, 2025
1 parent 26e33cb commit 6eea981
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 91 deletions.
4 changes: 2 additions & 2 deletions modules/analysis/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ pub async fn get_component_deps(

#[cfg(test)]
mod test {
use crate::test::{caller, CallService};
use crate::test::caller;
use actix_http::Request;
use actix_web::test::TestRequest;
use serde_json::Value;
use test_context::test_context;
use test_log::test;
use trustify_test_context::TrustifyContext;
use trustify_test_context::{call::CallService, TrustifyContext};

#[test_context(TrustifyContext)]
#[test(actix_web::test)]
Expand Down
51 changes: 5 additions & 46 deletions modules/analysis/src/test.rs
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
use crate::endpoints::configure;
use actix_http::Request;
use actix_web::{
dev::{Service, ServiceResponse},
web, App, Error,
use trustify_test_context::{
call::{self, CallService},
TrustifyContext,
};
use bytes::Bytes;
use sea_orm::prelude::async_trait::async_trait;
use serde::de::DeserializeOwned;
use trustify_auth::authorizer::Authorizer;
use trustify_test_context::TrustifyContext;
use utoipa_actix_web::AppExt;

/// A trait wrapping an `impl Service` in a way that we can pass it as a reference.
#[async_trait(?Send)]
pub trait CallService {
async fn call_service(&self, s: Request) -> ServiceResponse;
async fn call_and_read_body(&self, r: Request) -> Bytes;
async fn call_and_read_body_json<T: DeserializeOwned>(&self, r: Request) -> T;
}

#[async_trait(?Send)]
impl<S> CallService for S
where
S: Service<Request, Response = ServiceResponse, Error = Error>,
{
async fn call_service(&self, r: Request) -> ServiceResponse {
actix_web::test::call_service(self, r).await
}
async fn call_and_read_body(&self, r: Request) -> Bytes {
actix_web::test::call_and_read_body(self, r).await
}
async fn call_and_read_body_json<T: DeserializeOwned>(&self, r: Request) -> T {
actix_web::test::call_and_read_body_json(self, r).await
}
}

pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result<impl CallService> {
Ok(actix_web::test::init_service(
App::new()
.into_utoipa_app()
.app_data(web::PayloadConfig::default().limit(5 * 1024 * 1024))
.app_data(web::Data::new(Authorizer::new(None)))
.service(
utoipa_actix_web::scope("/api").configure(|svc| configure(svc, ctx.db.clone())),
)
.into_app(),
)
.await)
pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result<impl CallService + '_> {
call::caller(|svc| configure(svc, ctx.db.clone())).await
}
28 changes: 8 additions & 20 deletions modules/fundamental/src/test/common.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
use actix_web::{web, App};
use trustify_auth::authorizer::Authorizer;
use trustify_test_context::{call::CallService, TrustifyContext};
use utoipa_actix_web::AppExt;
use trustify_test_context::{
call::{self, CallService},
TrustifyContext,
};

#[allow(unused)]
pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result<impl CallService> {
pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result<impl CallService + '_> {
caller_with(ctx, Config::default()).await
}

pub async fn caller_with(
async fn caller_with(
ctx: &TrustifyContext,
config: Config,
) -> anyhow::Result<impl CallService> {
Ok(actix_web::test::init_service(
App::new()
.into_utoipa_app()
.app_data(web::PayloadConfig::default().limit(5 * 1024 * 1024))
.app_data(web::Data::new(Authorizer::new(None)))
.service(
utoipa_actix_web::scope("/api")
.configure(|svc| configure(svc, config, ctx.db.clone(), ctx.storage.clone())),
)
.into_app(),
)
.await)
) -> anyhow::Result<impl CallService + '_> {
call::caller(|svc| configure(svc, config, ctx.db.clone(), ctx.storage.clone())).await
}
28 changes: 6 additions & 22 deletions modules/ingestor/tests/common.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,12 @@
use actix_web::{web, App};
use trustify_auth::authorizer::Authorizer;
use trustify_module_ingestor::endpoints::{configure, Config};
use trustify_test_context::{call::CallService, TrustifyContext};
use utoipa_actix_web::AppExt;

#[allow(unused)]
pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result<impl CallService> {
caller_with(ctx, Config::default()).await
}
use trustify_test_context::{
call::{self, CallService},
TrustifyContext,
};

pub async fn caller_with(
ctx: &TrustifyContext,
config: Config,
) -> anyhow::Result<impl CallService> {
Ok(actix_web::test::init_service(
App::new()
.into_utoipa_app()
.app_data(web::PayloadConfig::default().limit(5 * 1024 * 1024))
.app_data(web::Data::new(Authorizer::new(None)))
.service(
utoipa_actix_web::scope("/api")
.configure(|svc| configure(svc, config, ctx.db.clone(), ctx.storage.clone())),
)
.into_app(),
)
.await)
) -> anyhow::Result<impl CallService + '_> {
call::caller(|svc| configure(svc, config, ctx.db.clone(), ctx.storage.clone())).await
}
19 changes: 18 additions & 1 deletion test-context/src/call.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use actix_http::Request;
use actix_web::{
dev::{Service, ServiceResponse},
Error,
web, App, Error,
};
use bytes::Bytes;
use serde::de::DeserializeOwned;
use std::future::Future;
use trustify_auth::authorizer::Authorizer;
use utoipa_actix_web::{service_config::ServiceConfig, AppExt};

/// A trait wrapping an `impl Service` in a way that we can pass it as a reference.
pub trait CallService {
Expand All @@ -30,3 +32,18 @@ where
actix_web::test::call_and_read_body_json(self, r).await
}
}

pub async fn caller<F>(cfg_fn: F) -> anyhow::Result<impl CallService>
where
F: FnOnce(&mut ServiceConfig),
{
Ok(actix_web::test::init_service(
App::new()
.into_utoipa_app()
.app_data(web::PayloadConfig::default().limit(5 * 1024 * 1024))
.app_data(web::Data::new(Authorizer::new(None)))
.service(utoipa_actix_web::scope("/api").configure(cfg_fn))
.into_app(),
)
.await)
}

0 comments on commit 6eea981

Please sign in to comment.