From 13cb4775985af835219cd40637126d163e0cf277 Mon Sep 17 00:00:00 2001 From: Alphonse Paix Date: Thu, 18 Sep 2025 20:42:50 +0200 Subject: [PATCH] Refactor test suite to handle 303 See Other --- src/routes/admin/logout.rs | 8 +++----- src/routes/login.rs | 13 ++++++------- templates/dashboard.html | 4 ++-- tests/api/helpers.rs | 8 +++++--- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/routes/admin/logout.rs b/src/routes/admin/logout.rs index cd605d7..2f02292 100644 --- a/src/routes/admin/logout.rs +++ b/src/routes/admin/logout.rs @@ -1,15 +1,13 @@ use crate::{routes::AdminError, session_state::TypedSession}; use axum::{ - http::HeaderMap, + http::{HeaderMap, StatusCode}, response::{IntoResponse, Response}, }; -use reqwest::StatusCode; #[tracing::instrument(name = "Logging out", skip(session))] pub async fn logout(session: TypedSession) -> Result { session.clear().await; let mut headers = HeaderMap::new(); headers.insert("HX-Redirect", "/login".parse().unwrap()); - headers.insert("Location", "/login".parse().unwrap()); - Ok((StatusCode::SEE_OTHER, headers).into_response()) -} + Ok((StatusCode::OK, headers).into_response()) +} \ No newline at end of file diff --git a/src/routes/login.rs b/src/routes/login.rs index dc5138f..ad6a75f 100644 --- a/src/routes/login.rs +++ b/src/routes/login.rs @@ -11,10 +11,7 @@ use axum::{ extract::State, response::{Html, IntoResponse, Response}, }; -use axum::{ - http::{HeaderMap, StatusCode}, - response::Redirect, -}; +use axum::{http::StatusCode, response::Redirect}; use secrecy::SecretString; #[derive(thiserror::Error)] @@ -117,9 +114,11 @@ pub async fn post_login( .await .map_err(|e| LoginError::UnexpectedError(e.into()))?; - let mut headers = HeaderMap::new(); - headers.insert("HX-Redirect", "/admin/dashboard".parse().unwrap()); - Ok((StatusCode::OK, headers).into_response()) + let mut response = Redirect::to("/admin/dashboard").into_response(); + response + .headers_mut() + .insert("HX-Redirect", "/admin/dashboard".parse().unwrap()); + Ok(response) } } } diff --git a/templates/dashboard.html b/templates/dashboard.html index 793fada..75424f2 100644 --- a/templates/dashboard.html +++ b/templates/dashboard.html @@ -152,7 +152,7 @@ stroke="currentColor"> - Contact your subscribers + Send an email

Contact your subscribers directly.

@@ -215,7 +215,7 @@ stroke="currentColor"> - Password + Change your password

Set a new password for your account.

diff --git a/tests/api/helpers.rs b/tests/api/helpers.rs index ff04ed9..c087b6f 100644 --- a/tests/api/helpers.rs +++ b/tests/api/helpers.rs @@ -297,9 +297,11 @@ async fn configure_database(config: &DatabaseSettings) -> PgPool { } pub fn assert_is_redirect_to(response: &reqwest::Response, location: &str) { - dbg!(&response); - assert_eq!(response.status().as_u16(), 200); - assert_eq!(response.headers().get("hx-redirect").unwrap(), location); + assert!( + response.status().as_u16() == 303 + || response.status().as_u16() == 200 + && response.headers().get("hx-redirect").unwrap() == location + ); } pub fn when_sending_an_email() -> MockBuilder {