use crate::helpers::{TestApp, assert_is_redirect_to}; use sqlx::PgPool; use uuid::Uuid; #[sqlx::test] async fn you_must_be_logged_in_to_change_your_password(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; let new_password = Uuid::new_v4().to_string(); let response = app .post_change_password(&serde_json::json!({ "current_password": Uuid::new_v4().to_string(), "new_password": new_password, "new_password_check": new_password, })) .await; assert_is_redirect_to(&response, "/login"); } #[sqlx::test] async fn new_password_fields_must_match(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; app.admin_login().await; let new_password = Uuid::new_v4().to_string(); let another_new_password = Uuid::new_v4().to_string(); let response = app .post_change_password(&serde_json::json!({ "current_password": app.test_user.password, "new_password": new_password, "new_password_check": another_new_password, })) .await; assert!(response.status().is_success()); let html_fragment = response.text().await.unwrap(); assert!(html_fragment.contains("You entered two different passwords")); } #[sqlx::test] async fn current_password_is_invalid(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; app.admin_login().await; let new_password = Uuid::new_v4().to_string(); let response = app .post_change_password(&serde_json::json!({ "current_password": Uuid::new_v4().to_string(), "new_password": new_password, "new_password_check": new_password, })) .await; assert!(response.status().is_success()); let html_fragment = response.text().await.unwrap(); assert!(html_fragment.contains("The current password is incorrect")); } #[sqlx::test] async fn changing_password_works(connection_pool: PgPool) { let app = TestApp::spawn(connection_pool).await; app.admin_login().await; let new_password = Uuid::new_v4().to_string(); let response = app .post_change_password(&serde_json::json!({ "current_password": app.test_user.password, "new_password": new_password, "new_password_check": new_password, })) .await; assert!(response.status().is_success()); let html_page_fragment = response.text().await.unwrap(); assert!(html_page_fragment.contains("Your password has been changed")); let response = app.logout().await; assert_is_redirect_to(&response, "/login"); let login_body = &serde_json::json!({ "username": app.test_user.username, "password": new_password, }); let response = app.post_login(login_body).await; assert_is_redirect_to(&response, "/admin/dashboard"); }