mirror of
https://github.com/mCaptcha/mCaptcha.git
synced 2026-02-11 10:05:41 +00:00
feat: def traits to get captcha config
This commit is contained in:
@@ -49,6 +49,7 @@ pub enum DBError {
|
|||||||
#[error("Traffic pattern not found")]
|
#[error("Traffic pattern not found")]
|
||||||
TrafficPatternNotFound,
|
TrafficPatternNotFound,
|
||||||
|
|
||||||
|
/// Notification not found
|
||||||
#[error("Notification not found")]
|
#[error("Notification not found")]
|
||||||
NotificationNotFound,
|
NotificationNotFound,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -140,6 +140,12 @@ pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase {
|
|||||||
/// create new captcha
|
/// create new captcha
|
||||||
async fn create_captcha(&self, username: &str, p: &CreateCaptcha) -> DBResult<()>;
|
async fn create_captcha(&self, username: &str, p: &CreateCaptcha) -> DBResult<()>;
|
||||||
|
|
||||||
|
/// Get captcha config
|
||||||
|
async fn get_captcha_config(&self, username: &str, key: &str) -> DBResult<Captcha>;
|
||||||
|
|
||||||
|
/// Get all captchas belonging to user
|
||||||
|
async fn get_all_user_captchas(&self, username: &str) -> DBResult<Vec<Captcha>>;
|
||||||
|
|
||||||
/// update captcha metadata; doesn't change captcha key
|
/// update captcha metadata; doesn't change captcha key
|
||||||
async fn update_captcha_metadata(
|
async fn update_captcha_metadata(
|
||||||
&self,
|
&self,
|
||||||
@@ -293,7 +299,7 @@ pub struct TrafficPattern {
|
|||||||
pub broke_my_site_traffic: Option<u32>,
|
pub broke_my_site_traffic: Option<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)]
|
||||||
/// data requried to create new captcha
|
/// data requried to create new captcha
|
||||||
pub struct CreateCaptcha<'a> {
|
pub struct CreateCaptcha<'a> {
|
||||||
/// cool down duration
|
/// cool down duration
|
||||||
@@ -304,7 +310,20 @@ pub struct CreateCaptcha<'a> {
|
|||||||
pub key: &'a str,
|
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
|
/// datastructure representing a user's secret
|
||||||
pub struct Secret {
|
pub struct Secret {
|
||||||
/// user's secret
|
/// user's secret
|
||||||
|
|||||||
@@ -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(None, c.key).await.unwrap());
|
||||||
assert!(db.captcha_exists(Some(p.username), 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
|
// get captcha cooldown duration
|
||||||
assert_eq!(db.get_captcha_cooldown(c.key).await.unwrap(), c.duration);
|
assert_eq!(db.get_captcha_cooldown(c.key).await.unwrap(), c.duration);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user