diff --git a/mcaptcha/api/v1/pow/get_config/fn.init_mcaptcha.html b/mcaptcha/api/v1/pow/get_config/fn.init_mcaptcha.html index c42994f0..3ca71ad1 100644 --- a/mcaptcha/api/v1/pow/get_config/fn.init_mcaptcha.html +++ b/mcaptcha/api/v1/pow/get_config/fn.init_mcaptcha.html @@ -4,7 +4,7 @@
async fn init_mcaptcha(data: &AppData, key: &str) -> Result<(), ServiceError>
Expand description

Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.

+

Function mcaptcha::api::v1::pow::get_config::init_mcaptcha

source · []
pub async fn init_mcaptcha(
    data: &AppData,
    key: &str
) -> Result<(), ServiceError>
Expand description

Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.

This fn gets mcaptcha config from database, builds [Defense][libmcaptcha::Defense], creates [MCaptcha][libmcaptcha::MCaptcha] and adds it to [Master][libmcaptcha::Defense]

diff --git a/mcaptcha/api/v1/pow/get_config/index.html b/mcaptcha/api/v1/pow/get_config/index.html index 05dee566..0638939c 100644 --- a/mcaptcha/api/v1/pow/get_config/index.html +++ b/mcaptcha/api/v1/pow/get_config/index.html @@ -4,9 +4,9 @@

Structs

+

Module mcaptcha::api::v1::pow::get_config

source · []

Structs

get PoW configuration for an mcaptcha key

Functions

-

Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.

+

Call this when [MCaptcha][libmcaptcha::MCaptcha] is not in master.

\ No newline at end of file diff --git a/mcaptcha/constant.COMPILED_DATE.html b/mcaptcha/constant.COMPILED_DATE.html index b6b8a83d..652d6c9d 100644 --- a/mcaptcha/constant.COMPILED_DATE.html +++ b/mcaptcha/constant.COMPILED_DATE.html @@ -4,5 +4,5 @@
pub const COMPILED_DATE: &str = "22-05-30";
+

Constant mcaptcha::COMPILED_DATE

source · []
pub const COMPILED_DATE: &str = "22-05-31";
\ No newline at end of file diff --git a/mcaptcha/constant.GIT_COMMIT_HASH.html b/mcaptcha/constant.GIT_COMMIT_HASH.html index 0f9fc6fe..be2e0643 100644 --- a/mcaptcha/constant.GIT_COMMIT_HASH.html +++ b/mcaptcha/constant.GIT_COMMIT_HASH.html @@ -4,5 +4,5 @@
pub const GIT_COMMIT_HASH: &str = "be9c6b757e3fe85bc7ba859eec00caa43d03ec04";
+

Constant mcaptcha::GIT_COMMIT_HASH

source · []
pub const GIT_COMMIT_HASH: &str = "5daeffd6fbd421b0bffa4111f62ae3ac8f663bff";
\ No newline at end of file diff --git a/src/mcaptcha/api/v1/pow/get_config.rs.html b/src/mcaptcha/api/v1/pow/get_config.rs.html index cb8fa456..f5341762 100644 --- a/src/mcaptcha/api/v1/pow/get_config.rs.html +++ b/src/mcaptcha/api/v1/pow/get_config.rs.html @@ -188,6 +188,102 @@ 183 184 185 +186 +187 +188 +189 +190 +191 +192 +193 +194 +195 +196 +197 +198 +199 +200 +201 +202 +203 +204 +205 +206 +207 +208 +209 +210 +211 +212 +213 +214 +215 +216 +217 +218 +219 +220 +221 +222 +223 +224 +225 +226 +227 +228 +229 +230 +231 +232 +233 +234 +235 +236 +237 +238 +239 +240 +241 +242 +243 +244 +245 +246 +247 +248 +249 +250 +251 +252 +253 +254 +255 +256 +257 +258 +259 +260 +261 +262 +263 +264 +265 +266 +267 +268 +269 +270 +271 +272 +273 +274 +275 +276 +277 +278 +279 +280 +281
/*
  * Copyright (C) 2022  Aravinth Manivannan <realaravinth@batsense.net>
  *
@@ -288,7 +384,8 @@
 ///
 /// This fn gets mcaptcha config from database, builds [Defense][libmcaptcha::Defense],
 /// creates [MCaptcha][libmcaptcha::MCaptcha] and adds it to [Master][libmcaptcha::Defense]
-async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> {
+pub async fn init_mcaptcha(data: &AppData, key: &str) -> ServiceResult<()> {
+    println!("Initializing captcha");
     // get levels
     let levels = data.db.get_captcha_levels(None, key).await?;
     let duration = data.db.get_captcha_cooldown(key).await?;
@@ -307,6 +404,7 @@
     }
 
     let defense = defense.build()?;
+    println!("{:?}", defense);
 
     // create captcha
     let mcaptcha = MCaptchaBuilder::default()
@@ -372,6 +470,100 @@
         let config: PoWConfig = test::read_body_json(get_config_resp).await;
         assert_eq!(config.difficulty_factor, L1.difficulty_factor);
     }
+
+    #[actix_rt::test]
+    pub async fn pow_difficulty_factor_increases_on_visitor_count_increase() {
+        use super::*;
+        use crate::tests::*;
+        use crate::*;
+        use actix_web::test;
+
+        use libmcaptcha::defense::Level;
+
+        use crate::api::v1::mcaptcha::create::CreateCaptcha;
+        use crate::api::v1::mcaptcha::create::MCaptchaDetails;
+
+        const NAME: &str = "powusrworks2";
+        const PASSWORD: &str = "testingpas";
+        const EMAIL: &str = "randomuser2@a.com";
+        pub const L1: Level = Level {
+            difficulty_factor: 10,
+            visitor_threshold: 10,
+        };
+        pub const L2: Level = Level {
+            difficulty_factor: 20,
+            visitor_threshold: 20,
+        };
+
+        pub const L3: Level = Level {
+            difficulty_factor: 30,
+            visitor_threshold: 30,
+        };
+
+        let data = get_data().await;
+        let data = &data;
+        let levels = [L1, L2, L3];
+
+        delete_user(data, NAME).await;
+
+        let (_, signin_resp) = register_and_signin(data, NAME, EMAIL, PASSWORD).await;
+        let cookies = get_cookie!(signin_resp);
+        let app = get_app!(data).await;
+
+        let create_captcha = CreateCaptcha {
+            levels: levels.into(),
+            duration: 30,
+            description: "dummy".into(),
+        };
+
+        // 1. add level
+        let add_token_resp = test::call_service(
+            &app,
+            post_request!(&create_captcha, V1_API_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;
+
+        let get_config_payload = GetConfigPayload {
+            key: token_key.key.clone(),
+        };
+
+        let url = V1_API_ROUTES.pow.get_config;
+        let mut prev = 0;
+        for (count, l) in levels.iter().enumerate() {
+            for l in prev..l.visitor_threshold * 2 {
+                let get_config_resp = test::call_service(
+                    &app,
+                    post_request!(&get_config_payload, V1_API_ROUTES.pow.get_config)
+                        .to_request(),
+                )
+                .await;
+            }
+
+            let get_config_resp = test::call_service(
+                &app,
+                post_request!(&get_config_payload, V1_API_ROUTES.pow.get_config)
+                    .to_request(),
+            )
+            .await;
+
+            let config: PoWConfig = test::read_body_json(get_config_resp).await;
+            println!(
+                "[{count}] received difficulty_factor: {} prev difficulty_factor {}",
+                config.difficulty_factor, prev
+            );
+            if count == levels.len() - 1 {
+                assert!(config.difficulty_factor == prev);
+            } else {
+                assert!(config.difficulty_factor > prev);
+            }
+            prev = config.difficulty_factor;
+        }
+        // update and check changes
+    }
 }