rename from_user_provided_traffic_pattern to create_easy

isolate non-async test in separate module
This commit is contained in:
realaravinth
2021-12-16 17:47:30 +05:30
parent 5ac0b36255
commit 05f7e81c21

View File

@@ -34,7 +34,9 @@ pub mod routes {
pub delete: &'static str, pub delete: &'static str,
pub update_key: &'static str, pub update_key: &'static str,
pub stats: &'static str, pub stats: &'static str,
pub user_provided_traffic_pattern: &'static str, /// easy is using defaults
pub create_easy: &'static str,
pub update_easy: &'static str,
} }
impl MCaptcha { impl MCaptcha {
@@ -43,8 +45,8 @@ pub mod routes {
update_key: "/api/v1/mcaptcha/update/key", update_key: "/api/v1/mcaptcha/update/key",
delete: "/api/v1/mcaptcha/delete", delete: "/api/v1/mcaptcha/delete",
stats: "/api/v1/mcaptcha/stats", stats: "/api/v1/mcaptcha/stats",
user_provided_traffic_pattern: create_easy: "/api/v1/mcaptcha/add/easy",
"/api/v1/mcaptcha/add/user-provided-traffic-pattern", update_easy: "/api/v1/mcaptcha/update/easy",
} }
} }
} }
@@ -54,7 +56,7 @@ pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(update_token); cfg.service(update_token);
cfg.service(delete_mcaptcha); cfg.service(delete_mcaptcha);
cfg.service(get_stats); cfg.service(get_stats);
cfg.service(from_user_provided_traffic_pattern); cfg.service(create_easy);
} }
#[derive(Clone, Debug, Deserialize, Serialize)] #[derive(Clone, Debug, Deserialize, Serialize)]
@@ -221,14 +223,14 @@ async fn get_stats(
} }
#[derive(Serialize, Deserialize, Clone, Debug)] #[derive(Serialize, Deserialize, Clone, Debug)]
pub struct UserProvidedTrafficPattern { pub struct TrafficPattern {
pub avg_traffic: u32, pub avg_traffic: u32,
pub peak_sustainable_traffic: u32, pub peak_sustainable_traffic: u32,
pub broke_my_site_traffic: Option<u32>, pub broke_my_site_traffic: Option<u32>,
pub description: String, pub description: String,
} }
impl UserProvidedTrafficPattern { impl TrafficPattern {
pub fn calculate( pub fn calculate(
&self, &self,
strategy: &DefaultDifficultyStrategy, strategy: &DefaultDifficultyStrategy,
@@ -268,11 +270,11 @@ impl UserProvidedTrafficPattern {
} }
#[my_codegen::post( #[my_codegen::post(
path = "crate::V1_API_ROUTES.mcaptcha.user_provided_traffic_pattern", path = "crate::V1_API_ROUTES.mcaptcha.create_easy",
wrap = "crate::CheckLogin" wrap = "crate::CheckLogin"
)] )]
async fn from_user_provided_traffic_pattern( async fn create_easy(
payload: web::Json<UserProvidedTrafficPattern>, payload: web::Json<TrafficPattern>,
data: AppData, data: AppData,
id: Identity, id: Identity,
) -> ServiceResult<impl Responder> { ) -> ServiceResult<impl Responder> {
@@ -380,78 +382,83 @@ mod tests {
assert_eq!(get_statis_resp.status(), StatusCode::OK); assert_eq!(get_statis_resp.status(), StatusCode::OK);
} }
#[actix_rt::test] #[cfg(test)]
async fn user_provided_traffic_pattern_calculate_works() { mod isoloated_test {
const NAME: &str = "defaultuserconfgworks"; use super::{LevelBuilder, TrafficPattern};
let mut payload = UserProvidedTrafficPattern { #[test]
avg_traffic: 100_000, fn easy_configuration_works() {
peak_sustainable_traffic: 1_000_000, const NAME: &str = "defaultuserconfgworks";
broke_my_site_traffic: Some(10_000_000),
description: NAME.into(),
};
let strategy = &crate::SETTINGS.captcha.default_difficulty_strategy; let mut payload = TrafficPattern {
let l1 = LevelBuilder::default() avg_traffic: 100_000,
.difficulty_factor(strategy.avg_traffic_difficulty) peak_sustainable_traffic: 1_000_000,
.unwrap() broke_my_site_traffic: Some(10_000_000),
.visitor_threshold(payload.avg_traffic) description: NAME.into(),
.build() };
.unwrap();
let l2 = LevelBuilder::default() let strategy = &crate::SETTINGS.captcha.default_difficulty_strategy;
.difficulty_factor(strategy.peak_sustainable_traffic_difficulty) let l1 = LevelBuilder::default()
.unwrap() .difficulty_factor(strategy.avg_traffic_difficulty)
.visitor_threshold(payload.peak_sustainable_traffic) .unwrap()
.build() .visitor_threshold(payload.avg_traffic)
.unwrap(); .build()
let l3 = LevelBuilder::default() .unwrap();
.difficulty_factor(strategy.broke_my_site_traffic_difficulty)
.unwrap()
.visitor_threshold(payload.broke_my_site_traffic.unwrap())
.build()
.unwrap();
let levels = vec![l1, l2, l3]; let l2 = LevelBuilder::default()
assert_eq!(payload.calculate(strategy).unwrap(), levels); .difficulty_factor(strategy.peak_sustainable_traffic_difficulty)
.unwrap()
.visitor_threshold(payload.peak_sustainable_traffic)
.build()
.unwrap();
let l3 = LevelBuilder::default()
.difficulty_factor(strategy.broke_my_site_traffic_difficulty)
.unwrap()
.visitor_threshold(payload.broke_my_site_traffic.unwrap())
.build()
.unwrap();
let estimated_lmax = LevelBuilder::default() let levels = vec![l1, l2, l3];
.difficulty_factor(strategy.broke_my_site_traffic_difficulty) assert_eq!(payload.calculate(strategy).unwrap(), levels);
.unwrap()
.visitor_threshold(1500000)
.build()
.unwrap();
payload.broke_my_site_traffic = None;
assert_eq!(
payload.calculate(strategy).unwrap(),
vec![l1, l2, estimated_lmax]
);
let lmax = LevelBuilder::default() let estimated_lmax = LevelBuilder::default()
.difficulty_factor(strategy.broke_my_site_traffic_difficulty) .difficulty_factor(strategy.broke_my_site_traffic_difficulty)
.unwrap() .unwrap()
.visitor_threshold(u32::MAX) .visitor_threshold(1500000)
.build() .build()
.unwrap(); .unwrap();
payload.broke_my_site_traffic = None;
assert_eq!(
payload.calculate(strategy).unwrap(),
vec![l1, l2, estimated_lmax]
);
let very_large_l2_peak_traffic = u32::MAX - 1; let lmax = LevelBuilder::default()
let very_large_l2 = LevelBuilder::default() .difficulty_factor(strategy.broke_my_site_traffic_difficulty)
.difficulty_factor(strategy.peak_sustainable_traffic_difficulty) .unwrap()
.unwrap() .visitor_threshold(u32::MAX)
.visitor_threshold(very_large_l2_peak_traffic) .build()
.build() .unwrap();
.unwrap();
// payload.broke_my_site_traffic = Some(very_large_l2_peak_traffic); let very_large_l2_peak_traffic = u32::MAX - 1;
payload.peak_sustainable_traffic = very_large_l2_peak_traffic; let very_large_l2 = LevelBuilder::default()
assert_eq!( .difficulty_factor(strategy.peak_sustainable_traffic_difficulty)
payload.calculate(strategy).unwrap(), .unwrap()
vec![l1, very_large_l2, lmax] .visitor_threshold(very_large_l2_peak_traffic)
); .build()
.unwrap();
// payload.broke_my_site_traffic = Some(very_large_l2_peak_traffic);
payload.peak_sustainable_traffic = very_large_l2_peak_traffic;
assert_eq!(
payload.calculate(strategy).unwrap(),
vec![l1, very_large_l2, lmax]
);
}
} }
#[actix_rt::test] #[actix_rt::test]
async fn from_user_provided_traffic_pattern_works() { async fn easy_works() {
const NAME: &str = "defaultuserconfgworks"; const NAME: &str = "defaultuserconfgworks";
const PASSWORD: &str = "longpassworddomain"; const PASSWORD: &str = "longpassworddomain";
const EMAIL: &str = "defaultuserconfgworks@a.com"; const EMAIL: &str = "defaultuserconfgworks@a.com";
@@ -466,7 +473,7 @@ mod tests {
let cookies = get_cookie!(signin_resp); let cookies = get_cookie!(signin_resp);
let app = get_app!(data).await; let app = get_app!(data).await;
let payload = UserProvidedTrafficPattern { let payload = TrafficPattern {
avg_traffic: 100_000, avg_traffic: 100_000,
peak_sustainable_traffic: 1_000_000, peak_sustainable_traffic: 1_000_000,
broke_my_site_traffic: Some(10_000_000), broke_my_site_traffic: Some(10_000_000),
@@ -479,7 +486,7 @@ mod tests {
let add_token_resp = test::call_service( let add_token_resp = test::call_service(
&app, &app,
post_request!(&payload, ROUTES.mcaptcha.user_provided_traffic_pattern) post_request!(&payload, ROUTES.mcaptcha.create_easy)
.cookie(cookies.clone()) .cookie(cookies.clone())
.to_request(), .to_request(),
) )