feat: def traits to get captcha config
This commit is contained in:
parent
a15d963c3e
commit
2212b3b974
3 changed files with 33 additions and 2 deletions
|
@ -49,6 +49,7 @@ pub enum DBError {
|
|||
#[error("Traffic pattern not found")]
|
||||
TrafficPatternNotFound,
|
||||
|
||||
/// Notification not found
|
||||
#[error("Notification not found")]
|
||||
NotificationNotFound,
|
||||
}
|
||||
|
|
|
@ -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<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
|
||||
async fn update_captcha_metadata(
|
||||
&self,
|
||||
|
@ -293,7 +299,7 @@ pub struct TrafficPattern {
|
|||
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
|
||||
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
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
Loading…
Reference in a new issue