From 2212b3b9744e13e733f00cf984c9a9891348c567 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Fri, 27 May 2022 18:14:51 +0530 Subject: [PATCH] feat: def traits to get captcha config --- db/db-core/src/errors.rs | 1 + db/db-core/src/lib.rs | 23 +++++++++++++++++++++-- db/db-core/src/tests.rs | 11 +++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/db/db-core/src/errors.rs b/db/db-core/src/errors.rs index f3c93dcc..140d8436 100644 --- a/db/db-core/src/errors.rs +++ b/db/db-core/src/errors.rs @@ -49,6 +49,7 @@ pub enum DBError { #[error("Traffic pattern not found")] TrafficPatternNotFound, + /// Notification not found #[error("Notification not found")] NotificationNotFound, } diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index aaccd9d6..6af3f49b 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -140,6 +140,12 @@ pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { /// create new captcha async fn create_captcha(&self, username: &str, p: &CreateCaptcha) -> DBResult<()>; + /// Get captcha config + async fn get_captcha_config(&self, username: &str, key: &str) -> DBResult; + + /// Get all captchas belonging to user + async fn get_all_user_captchas(&self, username: &str) -> DBResult>; + /// update captcha metadata; doesn't change captcha key async fn update_captcha_metadata( &self, @@ -293,7 +299,7 @@ pub struct TrafficPattern { pub broke_my_site_traffic: Option, } -#[derive(Clone, Debug, Default, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)] /// data requried to create new captcha pub struct CreateCaptcha<'a> { /// cool down duration @@ -304,7 +310,20 @@ pub struct CreateCaptcha<'a> { pub key: &'a str, } -#[derive(Clone, Debug, Deserialize, Serialize)] +#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)] +/// Data representing a captcha +pub struct Captcha { + /// Database assigned ID + pub config_id: i32, + /// cool down duration + pub duration: i32, + /// description of the captcha + pub description: String, + /// secret key of the captcha + pub key: String, +} + +#[derive(Clone, Debug, Deserialize, PartialEq, Default, Serialize)] /// datastructure representing a user's secret pub struct Secret { /// user's secret diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index e5b351c4..204c61fb 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -176,6 +176,17 @@ pub async fn database_works<'a, T: MCDatabase>( assert!(db.captcha_exists(None, c.key).await.unwrap()); assert!(db.captcha_exists(Some(p.username), c.key).await.unwrap()); + // get captcha configuration + let captcha = db.get_captcha_config(p.username, c.key).await.unwrap(); + assert_eq!(captcha.key, c.key); + assert_eq!(captcha.duration, c.duration); + assert_eq!(captcha.description, c.description); + + // get all captchas that belong to user + let all_user_captchas = db.get_all_user_captchas(p.username).await.unwrap(); + assert_eq!(all_user_captchas.len(), 1); + assert_eq!(all_user_captchas[0], captcha); + // get captcha cooldown duration assert_eq!(db.get_captcha_cooldown(c.key).await.unwrap(), c.duration);