mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-12 02:25:41 +00:00
fix and chore: refactor tests to minimize initializing DB connections
SUMMARY
The test suite was spinning up way too many database connections that what's
strictly needed and so the test suite was failing with[0]:
code: "53300", message: "sorry, too many clients already"
EXPERIMENTS
Tried sharing database connection pool across all tests with
async_once[0] but faced:
- IO errors
The connections were probably getting dropped in between tests
- actix Actor errors
The actor was probably not getting initialized before a
a reference to the async_once initialized app
context(crate::data::Data) is retrieved and used
FIX
crate::tests was spinning up an App context
instance(crate::data::Data) for most utility functions, which was
unnecessarily excessive.
Each test now creates an instance of the application context at the
beginning and shared a reference with all test utility functions. So
number of database connections/app context instance = number of unit
tests.
[0]: permanently fixes #22
[1]: https://docs.rs/async_once/latest/async_once/
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use actix_web::test;
|
||||
use actix_web::{
|
||||
body::{BoxBody, EitherBody},
|
||||
@@ -16,8 +14,8 @@ use crate::api::v1::auth::runners::{Login, Register};
|
||||
use crate::api::v1::mcaptcha::create::CreateCaptcha;
|
||||
use crate::api::v1::mcaptcha::create::MCaptchaDetails;
|
||||
use crate::api::v1::ROUTES;
|
||||
use crate::data::Data;
|
||||
use crate::errors::*;
|
||||
use crate::ArcData;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! get_cookie {
|
||||
@@ -26,14 +24,6 @@ macro_rules! get_cookie {
|
||||
};
|
||||
}
|
||||
|
||||
pub async fn delete_user(name: &str, data: &Data) {
|
||||
let x = data.dblib.delete_user(name).await;
|
||||
println!();
|
||||
println!();
|
||||
println!();
|
||||
println!("Deleting user: {:?}", &x);
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! post_request {
|
||||
($uri:expr) => {
|
||||
@@ -81,19 +71,27 @@ macro_rules! get_app {
|
||||
};
|
||||
}
|
||||
|
||||
pub async fn delete_user(data: &ArcData, name: &str) {
|
||||
let x = data.dblib.delete_user(name).await;
|
||||
println!();
|
||||
println!();
|
||||
println!();
|
||||
println!("Deleting user: {:?}", &x);
|
||||
}
|
||||
|
||||
/// register and signin utility
|
||||
pub async fn register_and_signin(
|
||||
data: &ArcData,
|
||||
name: &str,
|
||||
email: &str,
|
||||
password: &str,
|
||||
) -> (Arc<data::Data>, Login, ServiceResponse<EitherBody<BoxBody>>) {
|
||||
register(name, email, password).await;
|
||||
signin(name, password).await
|
||||
) -> (Login, ServiceResponse<EitherBody<BoxBody>>) {
|
||||
register(data, name, email, password).await;
|
||||
signin(data, name, password).await
|
||||
}
|
||||
|
||||
/// register utility
|
||||
pub async fn register(name: &str, email: &str, password: &str) {
|
||||
let data = Data::new().await;
|
||||
pub async fn register(data: &ArcData, name: &str, email: &str, password: &str) {
|
||||
let app = get_app!(data).await;
|
||||
|
||||
// 1. Register
|
||||
@@ -111,10 +109,10 @@ pub async fn register(name: &str, email: &str, password: &str) {
|
||||
|
||||
/// signin util
|
||||
pub async fn signin(
|
||||
data: &ArcData,
|
||||
name: &str,
|
||||
password: &str,
|
||||
) -> (Arc<Data>, Login, ServiceResponse<EitherBody<BoxBody>>) {
|
||||
let data = Data::new().await;
|
||||
) -> (Login, ServiceResponse<EitherBody<BoxBody>>) {
|
||||
let app = get_app!(data.clone()).await;
|
||||
|
||||
// 2. signin
|
||||
@@ -126,18 +124,19 @@ pub async fn signin(
|
||||
test::call_service(&app, post_request!(&creds, ROUTES.auth.login).to_request())
|
||||
.await;
|
||||
assert_eq!(signin_resp.status(), StatusCode::OK);
|
||||
(data, creds, signin_resp)
|
||||
(creds, signin_resp)
|
||||
}
|
||||
|
||||
/// pub duplicate test
|
||||
pub async fn bad_post_req_test<T: Serialize>(
|
||||
data: &ArcData,
|
||||
name: &str,
|
||||
password: &str,
|
||||
url: &str,
|
||||
payload: &T,
|
||||
err: ServiceError,
|
||||
) {
|
||||
let (data, _, signin_resp) = signin(name, password).await;
|
||||
let (_, signin_resp) = signin(data, name, password).await;
|
||||
let cookies = get_cookie!(signin_resp);
|
||||
let app = get_app!(data).await;
|
||||
|
||||
@@ -158,6 +157,31 @@ pub async fn bad_post_req_test<T: Serialize>(
|
||||
assert_eq!(resp_err.error, format!("{}", err));
|
||||
}
|
||||
|
||||
pub async fn add_levels_util(
|
||||
data: &ArcData,
|
||||
name: &str,
|
||||
password: &str,
|
||||
) -> (Login, ServiceResponse<EitherBody<BoxBody>>, MCaptchaDetails) {
|
||||
let (creds, signin_resp) = signin(data, name, password).await;
|
||||
let cookies = get_cookie!(signin_resp);
|
||||
let app = get_app!(data).await;
|
||||
|
||||
let add_level = get_level_data();
|
||||
|
||||
// 1. add level
|
||||
let add_token_resp = test::call_service(
|
||||
&app,
|
||||
post_request!(&add_level, ROUTES.captcha.create)
|
||||
.cookie(cookies.clone())
|
||||
.to_request(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(add_token_resp.status(), StatusCode::OK);
|
||||
let token_key: MCaptchaDetails = test::read_body_json(add_token_resp).await;
|
||||
|
||||
(creds, signin_resp, token_key)
|
||||
}
|
||||
|
||||
pub const L1: Level = Level {
|
||||
difficulty_factor: 50,
|
||||
visitor_threshold: 50,
|
||||
@@ -176,32 +200,3 @@ pub fn get_level_data() -> CreateCaptcha {
|
||||
description: "dummy".into(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn add_levels_util(
|
||||
name: &str,
|
||||
password: &str,
|
||||
) -> (
|
||||
Arc<data::Data>,
|
||||
Login,
|
||||
ServiceResponse<EitherBody<BoxBody>>,
|
||||
MCaptchaDetails,
|
||||
) {
|
||||
let (data, creds, signin_resp) = signin(name, password).await;
|
||||
let cookies = get_cookie!(signin_resp);
|
||||
let app = get_app!(data).await;
|
||||
|
||||
let add_level = get_level_data();
|
||||
|
||||
// 1. add level
|
||||
let add_token_resp = test::call_service(
|
||||
&app,
|
||||
post_request!(&add_level, ROUTES.captcha.create)
|
||||
.cookie(cookies.clone())
|
||||
.to_request(),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(add_token_resp.status(), StatusCode::OK);
|
||||
let token_key: MCaptchaDetails = test::read_body_json(add_token_resp).await;
|
||||
|
||||
(data, creds, signin_resp, token_key)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user