diff --git a/db/db-core/src/lib.rs b/db/db-core/src/lib.rs index f8304999..a233952f 100644 --- a/db/db-core/src/lib.rs +++ b/db/db-core/src/lib.rs @@ -71,6 +71,15 @@ pub struct Register<'a> { pub email: Option<&'a str>, } +#[derive(Clone, Debug, Deserialize, Serialize, PartialEq)] +/// data required to update them email of a user +pub struct UpdateEmail<'a> { + /// username of the user + pub username: &'a str, + /// new email address of the user + pub new_email: &'a str, +} + #[async_trait] /// mCaptcha's database requirements. To implement support for $Database, kindly implement this /// trait. @@ -89,6 +98,9 @@ pub trait MCDatabase: std::marker::Send + std::marker::Sync + CloneSPDatabase { /// check if email exists async fn email_exists(&self, email: &str) -> DBResult; + + /// update a user's email + async fn update_email(&self, p: &UpdateEmail) -> DBResult<()>; } /// Trait to clone MCDatabase diff --git a/db/db-core/src/tests.rs b/db/db-core/src/tests.rs index cdc9f04d..af210be1 100644 --- a/db/db-core/src/tests.rs +++ b/db/db-core/src/tests.rs @@ -55,4 +55,18 @@ pub async fn database_works<'a, T: MCDatabase>(db: &T, p: &Register<'a>) { "user registration with email is deleted; so email shouldn't exsit" ); + let update_email = UpdateEmail { + username: p.username, + new_email: p.email.as_ref().unwrap(), + }; + + db.update_email(&update_email).await.unwrap(); + println!( + "null user email: {}", + db.email_exists(p.email.as_ref().unwrap()).await.unwrap() + ); + assert!( + db.email_exists(p.email.as_ref().unwrap()).await.unwrap(), + "user was with empty email but email is set; so email should exsit" + ); }