-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: dry up redundant test app creation logic
Signed-off-by: Jim Crossley <[email protected]>
- Loading branch information
1 parent
26e33cb
commit 6eea981
Showing
5 changed files
with
39 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters