Files
zero2prod/tests/api/change_password.rs
Alphonse Paix 33281132c6
Some checks failed
Rust / Test (push) Has been cancelled
Rust / Rustfmt (push) Has been cancelled
Rust / Clippy (push) Has been cancelled
Rust / Code coverage (push) Has been cancelled
Update test suite to drop database automatically when test is successfull
2025-09-24 02:55:18 +02:00

104 lines
3.3 KiB
Rust

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.post_login(&serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password,
}))
.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.post_login(&serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password,
}))
.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;
let login_body = &serde_json::json!({
"username": app.test_user.username,
"password": app.test_user.password,
});
let response = app.post_login(login_body).await;
assert_is_redirect_to(&response, "/admin/dashboard");
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.post_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");
}