|
| 1 | +use super::update::UserConfirmEmail; |
| 2 | +use crate::app::AppState; |
| 3 | +use crate::auth::AuthCheck; |
| 4 | +use crate::controllers::helpers::ok_true; |
| 5 | +use crate::models::Email; |
| 6 | +use crate::tasks::spawn_blocking; |
| 7 | +use crate::util::errors::bad_request; |
| 8 | +use crate::util::errors::AppResult; |
| 9 | +use axum::extract::Path; |
| 10 | +use axum::response::Response; |
| 11 | +use crates_io_database::schema::emails; |
| 12 | +use diesel::dsl::sql; |
| 13 | +use diesel::prelude::*; |
| 14 | +use diesel_async::async_connection_wrapper::AsyncConnectionWrapper; |
| 15 | +use http::request::Parts; |
| 16 | + |
| 17 | +/// Handles `PUT /user/:user_id/resend` route |
| 18 | +pub async fn regenerate_token_and_send( |
| 19 | + state: AppState, |
| 20 | + Path(param_user_id): Path<i32>, |
| 21 | + req: Parts, |
| 22 | +) -> AppResult<Response> { |
| 23 | + let mut conn = state.db_write().await?; |
| 24 | + let auth = AuthCheck::default().check(&req, &mut conn).await?; |
| 25 | + spawn_blocking(move || { |
| 26 | + let conn: &mut AsyncConnectionWrapper<_> = &mut conn.into(); |
| 27 | + |
| 28 | + let user = auth.user(); |
| 29 | + |
| 30 | + // need to check if current user matches user to be updated |
| 31 | + if user.id != param_user_id { |
| 32 | + return Err(bad_request("current user does not match requested user")); |
| 33 | + } |
| 34 | + |
| 35 | + conn.transaction(|conn| -> AppResult<_> { |
| 36 | + let email: Email = diesel::update(Email::belonging_to(user)) |
| 37 | + .set(emails::token.eq(sql("DEFAULT"))) |
| 38 | + .get_result(conn) |
| 39 | + .optional()? |
| 40 | + .ok_or_else(|| bad_request("Email could not be found"))?; |
| 41 | + |
| 42 | + let email1 = UserConfirmEmail { |
| 43 | + user_name: &user.gh_login, |
| 44 | + domain: &state.emails.domain, |
| 45 | + token: email.token, |
| 46 | + }; |
| 47 | + |
| 48 | + state.emails.send(&email.email, email1).map_err(Into::into) |
| 49 | + })?; |
| 50 | + |
| 51 | + ok_true() |
| 52 | + }) |
| 53 | + .await |
| 54 | +} |
| 55 | + |
| 56 | +#[cfg(test)] |
| 57 | +mod tests { |
| 58 | + use crate::tests::util::{RequestHelper, TestApp}; |
| 59 | + use http::StatusCode; |
| 60 | + use insta::assert_snapshot; |
| 61 | + |
| 62 | + #[tokio::test(flavor = "multi_thread")] |
| 63 | + async fn test_no_auth() { |
| 64 | + let (app, anon, user) = TestApp::init().with_user(); |
| 65 | + |
| 66 | + let url = format!("/api/v1/users/{}/resend", user.as_model().id); |
| 67 | + let response = anon.put::<()>(&url, "").await; |
| 68 | + assert_eq!(response.status(), StatusCode::FORBIDDEN); |
| 69 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"this action requires authentication"}]}"#); |
| 70 | + |
| 71 | + assert_eq!(app.emails().len(), 0); |
| 72 | + } |
| 73 | + |
| 74 | + #[tokio::test(flavor = "multi_thread")] |
| 75 | + async fn test_wrong_user() { |
| 76 | + let (app, _anon, user) = TestApp::init().with_user(); |
| 77 | + let user2 = app.db_new_user("bar"); |
| 78 | + |
| 79 | + let url = format!("/api/v1/users/{}/resend", user2.as_model().id); |
| 80 | + let response = user.put::<()>(&url, "").await; |
| 81 | + assert_eq!(response.status(), StatusCode::BAD_REQUEST); |
| 82 | + assert_snapshot!(response.text(), @r#"{"errors":[{"detail":"current user does not match requested user"}]}"#); |
| 83 | + |
| 84 | + assert_eq!(app.emails().len(), 0); |
| 85 | + } |
| 86 | + |
| 87 | + #[tokio::test(flavor = "multi_thread")] |
| 88 | + async fn test_happy_path() { |
| 89 | + let (app, _anon, user) = TestApp::init().with_user(); |
| 90 | + |
| 91 | + let url = format!("/api/v1/users/{}/resend", user.as_model().id); |
| 92 | + let response = user.put::<()>(&url, "").await; |
| 93 | + assert_eq!(response.status(), StatusCode::OK); |
| 94 | + assert_snapshot!(response.text(), @r###"{"ok":true}"###); |
| 95 | + |
| 96 | + assert_snapshot!(app.emails_snapshot()); |
| 97 | + } |
| 98 | +} |
0 commit comments