Skip to content

Commit ab45cd4

Browse files
committed
backend: fix fmt and clippy
1 parent ea774de commit ab45cd4

File tree

2 files changed

+78
-26
lines changed

2 files changed

+78
-26
lines changed

backend/src/routes/auth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ pub mod jwt_refresh;
2525
pub mod login;
2626
pub mod recovery;
2727
pub mod register;
28+
pub mod resend_verification;
2829
pub mod start_recovery;
2930
pub mod verify;
30-
pub mod resend_verification;
3131

3232
pub const VERIFICATION_EXPIRATION_DAYS: u64 = 1;
3333

backend/src/routes/auth/resend_verification.rs

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ use diesel::prelude::*;
55
use serde::{Deserialize, Serialize};
66
use utoipa::ToSchema;
77

8-
use crate::{error::Error, routes::auth::VERIFICATION_EXPIRATION_DAYS, utils::{get_connection, web_block_unpacked}, AppState};
8+
use crate::{
9+
error::Error,
10+
routes::auth::VERIFICATION_EXPIRATION_DAYS,
11+
utils::{get_connection, web_block_unpacked},
12+
AppState,
13+
};
914

1015
use db_connector::models::{users::User, verification::Verification};
1116

@@ -40,12 +45,30 @@ fn send_verification_mail(
4045

4146
let (body, subject) = match lang.as_str() {
4247
"de" | "de-DE" => {
43-
let template = VerifyEmailDETemplate { name: &name, link: &link };
44-
match template.render() { Ok(body) => (body, "Email verifizieren"), Err(e) => { log::error!("Failed to render German verification email template for user '{name}': {e}"); return Err(Error::InternalError.into()); } }
48+
let template = VerifyEmailDETemplate {
49+
name: &name,
50+
link: &link,
51+
};
52+
match template.render() {
53+
Ok(body) => (body, "Email verifizieren"),
54+
Err(e) => {
55+
log::error!("Failed to render German verification email template for user '{name}': {e}");
56+
return Err(Error::InternalError.into());
57+
}
58+
}
4559
}
4660
_ => {
47-
let template = VerifyEmailENTemplate { name: &name, link: &link };
48-
match template.render() { Ok(body) => (body, "Verify email"), Err(e) => { log::error!("Failed to render English verification email template for user '{name}': {e}"); return Err(Error::InternalError.into()); } }
61+
let template = VerifyEmailENTemplate {
62+
name: &name,
63+
link: &link,
64+
};
65+
match template.render() {
66+
Ok(body) => (body, "Verify email"),
67+
Err(e) => {
68+
log::error!("Failed to render English verification email template for user '{name}': {e}");
69+
return Err(Error::InternalError.into());
70+
}
71+
}
4972
}
5073
};
5174

@@ -69,7 +92,6 @@ pub async fn resend_verification(
6992
) -> actix_web::Result<impl Responder> {
7093
use db_connector::schema::users::dsl as u_dsl;
7194

72-
7395
let mut conn = get_connection(&state)?;
7496
let user_email = data.email.to_lowercase();
7597

@@ -78,14 +100,17 @@ pub async fn resend_verification(
78100
match u_dsl::users
79101
.filter(u_dsl::email.eq(&user_email))
80102
.select(User::as_select())
81-
.get_result(&mut conn) {
82-
Ok(u) => Ok(u),
83-
Err(diesel::result::Error::NotFound) => Err(Error::UserDoesNotExist),
84-
Err(_) => Err(Error::InternalError)
85-
}
86-
}).await?;
103+
.get_result(&mut conn)
104+
{
105+
Ok(u) => Ok(u),
106+
Err(diesel::result::Error::NotFound) => Err(Error::UserDoesNotExist),
107+
Err(_) => Err(Error::InternalError),
108+
}
109+
})
110+
.await?;
87111

88-
if db_user.email_verified { // silently return success
112+
if db_user.email_verified {
113+
// silently return success
89114
return Ok(HttpResponse::Ok());
90115
}
91116

@@ -98,25 +123,37 @@ pub async fn resend_verification(
98123
// remove old tokens
99124
let _ = diesel::delete(verification.filter(user.eq(user_id))).execute(&mut conn);
100125

101-
let exp = chrono::Utc::now().checked_add_days(Days::new(VERIFICATION_EXPIRATION_DAYS)).ok_or(Error::InternalError)?;
102-
103-
let verify = Verification { id: uuid::Uuid::new_v4(), user: user_id, expiration: exp.naive_utc() };
104-
diesel::insert_into(verification).values(&verify).execute(&mut conn).map_err(|_| Error::InternalError)?;
126+
let exp = chrono::Utc::now()
127+
.checked_add_days(Days::new(VERIFICATION_EXPIRATION_DAYS))
128+
.ok_or(Error::InternalError)?;
129+
130+
let verify = Verification {
131+
id: uuid::Uuid::new_v4(),
132+
user: user_id,
133+
expiration: exp.naive_utc(),
134+
};
135+
diesel::insert_into(verification)
136+
.values(&verify)
137+
.execute(&mut conn)
138+
.map_err(|_| Error::InternalError)?;
105139
Ok(verify)
106-
}).await.and_then(|_verify| {
140+
})
141+
.await
142+
.map(|_verify| {
107143
#[cfg(not(test))]
108144
{
109145
let user_name = db_user.name.clone();
110146
let lang: String = lang.into();
111147
let state_cpy = state.clone();
112148
let email_cpy = data.email.clone();
113149
std::thread::spawn(move || {
114-
if let Err(e) = send_verification_mail(user_name, _verify, email_cpy, state_cpy, lang) {
150+
if let Err(e) =
151+
send_verification_mail(user_name, _verify, email_cpy, state_cpy, lang)
152+
{
115153
log::error!("Failed to resend verification mail: {e:?}");
116154
}
117155
});
118156
}
119-
Ok(())
120157
})?;
121158

122159
Ok(HttpResponse::Ok())
@@ -125,18 +162,23 @@ pub async fn resend_verification(
125162
#[cfg(test)]
126163
mod tests {
127164
use super::*;
128-
use actix_web::{test, App};
129-
use crate::tests::configure;
130165
use crate::routes::auth::register::tests::{create_user, delete_user};
131166
use crate::routes::auth::verify::tests::fast_verify;
167+
use crate::tests::configure;
168+
use actix_web::{test, App};
132169

133170
#[actix_web::test]
134171
async fn test_resend_unverified() {
135172
let mail = "[email protected]";
136173
create_user(mail).await;
137174
let app = App::new().configure(configure).service(resend_verification);
138175
let app = test::init_service(app).await;
139-
let req = test::TestRequest::post().uri("/resend_verification").set_json(&ResendSchema{ email: mail.to_string() }).to_request();
176+
let req = test::TestRequest::post()
177+
.uri("/resend_verification")
178+
.set_json(&ResendSchema {
179+
email: mail.to_string(),
180+
})
181+
.to_request();
140182
let resp = test::call_service(&app, req).await;
141183
assert!(resp.status().is_success());
142184
delete_user(mail);
@@ -149,7 +191,12 @@ mod tests {
149191
fast_verify(mail);
150192
let app = App::new().configure(configure).service(resend_verification);
151193
let app = test::init_service(app).await;
152-
let req = test::TestRequest::post().uri("/resend_verification").set_json(&ResendSchema{ email: mail.to_string() }).to_request();
194+
let req = test::TestRequest::post()
195+
.uri("/resend_verification")
196+
.set_json(&ResendSchema {
197+
email: mail.to_string(),
198+
})
199+
.to_request();
153200
let resp = test::call_service(&app, req).await;
154201
assert!(resp.status().is_success());
155202
delete_user(mail);
@@ -160,7 +207,12 @@ mod tests {
160207
let mail = "[email protected]";
161208
let app = App::new().configure(configure).service(resend_verification);
162209
let app = test::init_service(app).await;
163-
let req = test::TestRequest::post().uri("/resend_verification").set_json(&ResendSchema{ email: mail.to_string() }).to_request();
210+
let req = test::TestRequest::post()
211+
.uri("/resend_verification")
212+
.set_json(&ResendSchema {
213+
email: mail.to_string(),
214+
})
215+
.to_request();
164216
let resp = test::call_service(&app, req).await;
165217
assert_eq!(resp.status().as_u16(), 400); // mapped from UserDoesNotExist
166218
}

0 commit comments

Comments
 (0)