register demo user only when it's absent

This commit is contained in:
realaravinth 2021-08-12 08:26:49 +05:30
parent 9269539a8a
commit 78ebc46c64
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
2 changed files with 21 additions and 21 deletions

View file

@ -20,6 +20,7 @@ use actix::clock::sleep;
use actix::spawn;
use crate::api::v1::account::delete::runners::delete_user;
use crate::api::v1::account::{username::runners::username_exists, AccountCheckPayload};
use crate::api::v1::auth::runners::{register_runner, Register};
use crate::*;
@ -32,17 +33,25 @@ pub const DEMO_PASSWORD: &str = "password";
/// register demo user runner
async fn register_demo_user(data: &AppData) -> ServiceResult<()> {
let payload = Register {
username: DEMO_USER.into(),
password: DEMO_PASSWORD.into(),
confirm_password: DEMO_PASSWORD.into(),
email: None,
let user_exists_payload = AccountCheckPayload {
val: DEMO_USER.into(),
};
log::info!("Registering demo user");
match register_runner(&payload, data).await {
Err(ServiceError::UsernameTaken) | Ok(_) => Ok(()),
Err(e) => Err(e),
if !username_exists(&user_exists_payload, &data).await?.exists {
let register_payload = Register {
username: DEMO_USER.into(),
password: DEMO_PASSWORD.into(),
confirm_password: DEMO_PASSWORD.into(),
email: None,
};
log::info!("Registering demo user");
match register_runner(&register_payload, data).await {
Err(ServiceError::UsernameTaken) | Ok(_) => Ok(()),
Err(e) => Err(e),
}
} else {
Ok(())
}
}
@ -77,9 +86,6 @@ mod tests {
use libmcaptcha::defense::Level;
use super::*;
use crate::api::v1::account::{
username::runners::username_exists, AccountCheckPayload,
};
use crate::tests::*;
const DURATION: u64 = 5;

View file

@ -1,12 +1,6 @@
<. if crate::SETTINGS.allow_demo && crate::SETTINGS.allow_registration { .>
<p class="auth__demo-user__banner">
Try mCaptcha without joining
<br />
<span class="auth__demo-user__cred">
user: <.= crate::demo::DEMO_USER .>
</span>
<span class="auth__demo-user__cred">
password: <.= crate::demo::DEMO_PASSWORD .>
</span>
<p class="auth__demo-user__banner">Try mCaptcha without joining<br />
<span class="auth__demo-user__cred">user: <.= crate::demo::DEMO_USER .></span>
<span class="auth__demo-user__cred">password: <.= crate::demo::DEMO_PASSWORD .></span>
</p>
<. } .>