diff --git a/modules/analysis/src/endpoints.rs b/modules/analysis/src/endpoints.rs index 43fa9056f..888cf6517 100644 --- a/modules/analysis/src/endpoints.rs +++ b/modules/analysis/src/endpoints.rs @@ -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)] diff --git a/modules/analysis/src/test.rs b/modules/analysis/src/test.rs index d8dfa0205..0dc5f8576 100644 --- a/modules/analysis/src/test.rs +++ b/modules/analysis/src/test.rs @@ -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(&self, r: Request) -> T; -} - -#[async_trait(?Send)] -impl CallService for S -where - S: Service, -{ - 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(&self, r: Request) -> T { - actix_web::test::call_and_read_body_json(self, r).await - } -} - -pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result { - 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 { + call::caller(|svc| configure(svc, ctx.db.clone())).await } diff --git a/modules/fundamental/src/test/common.rs b/modules/fundamental/src/test/common.rs index 90006eb45..cc430a48f 100644 --- a/modules/fundamental/src/test/common.rs +++ b/modules/fundamental/src/test/common.rs @@ -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 { +pub async fn caller(ctx: &TrustifyContext) -> anyhow::Result { caller_with(ctx, Config::default()).await } -pub async fn caller_with( +async fn caller_with( ctx: &TrustifyContext, config: Config, -) -> anyhow::Result { - 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 { + call::caller(|svc| configure(svc, config, ctx.db.clone(), ctx.storage.clone())).await } diff --git a/modules/ingestor/tests/common.rs b/modules/ingestor/tests/common.rs index 2f4281b6e..809193166 100644 --- a/modules/ingestor/tests/common.rs +++ b/modules/ingestor/tests/common.rs @@ -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 { - 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 { - 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 { + call::caller(|svc| configure(svc, config, ctx.db.clone(), ctx.storage.clone())).await } diff --git a/test-context/src/call.rs b/test-context/src/call.rs index b9dd4bc2a..31c17584f 100644 --- a/test-context/src/call.rs +++ b/test-context/src/call.rs @@ -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 { @@ -30,3 +32,18 @@ where actix_web::test::call_and_read_body_json(self, r).await } } + +pub async fn caller(cfg_fn: F) -> anyhow::Result +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) +}