challenge is wrapper around addvisitorresult

This commit is contained in:
realaravinth 2021-06-09 21:01:28 +05:30
parent ca8709fd93
commit 8490651349
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
3 changed files with 11 additions and 14 deletions

1
Cargo.lock generated
View file

@ -303,7 +303,6 @@ dependencies = [
[[package]]
name = "libmcaptcha"
version = "0.1.4"
source = "git+https://github.com/mCaptcha/libmcaptcha?branch=master#0ff9cf9cb0a43fd6cea8289dcb8df7e707e90ba1"
dependencies = [
"derive_builder",
"derive_more",

View file

@ -17,6 +17,7 @@
use std::time::Duration;
use libmcaptcha::cache::AddChallenge;
use libmcaptcha::master::AddVisitorResult;
use redis_module::native_types::RedisType;
use redis_module::raw::KeyType;
use redis_module::NextArg;
@ -32,17 +33,14 @@ use crate::utils::*;
const MCAPTCHA_CHALLENGE_VERSION: i32 = 0;
#[derive(Serialize, Deserialize)]
pub struct Challenge {
difficulty: usize,
duration: u64,
}
pub struct Challenge(AddVisitorResult);
impl Challenge {
pub fn new(duration: u64, difficulty: usize) -> Self {
Self {
difficulty,
duration,
}
pub fn new(duration: u64, difficulty: u32) -> Self {
Self(AddVisitorResult {
difficulty_factor: difficulty,
duration: duration,
})
}
pub fn create_challenge(ctx: &Context, args: Vec<String>) -> RedisResult {
@ -125,7 +123,7 @@ pub mod type_methods {
let challenge = match encver {
0 => {
let data = raw::load_string(rdb);
let challenge: Result<Challenge, CacheError> = Format::Json.from_str(&data);
let challenge: Result<AddVisitorResult, CacheError> = Format::Json.from_str(&data);
if challenge.is_err() {
panic!(
"Can't load Challenge from old redis RDB, error while serde {}, data received: {}",
@ -133,7 +131,7 @@ pub mod type_methods {
data
);
}
challenge.unwrap()
Challenge(challenge.unwrap())
}
_ => panic!("Can't load mCaptcha from old redis RDB, encver {}", encver),
};
@ -149,7 +147,7 @@ pub mod type_methods {
#[allow(non_snake_case, unused)]
pub unsafe extern "C" fn rdb_save(rdb: *mut raw::RedisModuleIO, value: *mut c_void) {
let challenge = &*(value as *mut Challenge);
match &serde_json::to_string(challenge) {
match &serde_json::to_string(&challenge.0) {
Ok(string) => raw::save_string(rdb, &string),
Err(e) => panic!("error while rdb_save: {}", e),
}

View file

@ -74,7 +74,7 @@ async def add_challenge_works():
add_challenge(key, challenge)
stored_challenge = get_challenge_from_redis(key, challenge_name)
challenge_dict = json.loads(challenge)
assert stored_challenge["difficulty"] == challenge_dict["difficulty"]
assert stored_challenge["difficulty_factor"] == challenge_dict["difficulty"]
assert stored_challenge["duration"] == challenge_dict["duration"]
error = get_challenge_from_redis(key, challenge_name)
assert str(error) == CHALLENGE_NOT_FOUND