Refactor test suite to handle 303 See Other

This commit is contained in:
Alphonse Paix
2025-09-18 20:42:50 +02:00
parent 384d88eee8
commit 13cb477598
4 changed files with 16 additions and 17 deletions

View File

@@ -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<Response, AdminError> {
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())
}

View File

@@ -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)
}
}
}

View File

@@ -152,7 +152,7 @@
stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5.882V19.24a1.76 1.76 0 01-3.417.592l-2.147-6.15M18 13a3 3 0 100-6M5.436 13.683A4.001 4.001 0 017 6h1.832c4.1 0 7.625-1.234 9.168-3v14c-1.543-1.766-5.067-3-9.168-3H7a3.988 3.988 0 01-1.564-.317z" />
</svg>
Contact your subscribers
Send an email
</h2>
<p class="text-sm text-gray-600 mt-1">Contact your subscribers directly.</p>
</div>
@@ -215,7 +215,7 @@
stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 15v2m-6 4h12a2 2 0 002-2v-6a2 2 0 00-2-2H6a2 2 0 00-2 2v6a2 2 0 002 2zm10-10V7a4 4 0 00-8 0v4h8z" />
</svg>
Password
Change your password
</h2>
<p class="text-sm text-gray-600 mt-1">Set a new password for your account.</p>
</div>

View File

@@ -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 {