Lines
96.7 %
Functions
90.91 %
Branches
100 %
use super::*;
use crate::form_render::fixture::assert_autocomplete;
use axum::{
Router,
body::to_bytes,
middleware,
routing::{get, post},
};
use tower::ServiceExt;
async fn body_of(response: Response) -> String {
let bytes = to_bytes(response.into_body(), usize::MAX)
.await
.expect("body reads");
String::from_utf8(bytes.to_vec()).expect("utf8 body")
}
#[tokio::test]
async fn the_page_shows_the_error_and_the_email_but_no_password() {
let response = login_page(StatusCode::BAD_REQUEST, Some("Nope <b>"), "me@example.com");
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
let body = body_of(response).await;
assert!(body.contains(r#"<div class="error-alert">Nope <b></div>"#));
assert!(body.contains(r#"value="me@example.com""#));
assert!(body.contains(r#"name="password""#));
assert!(!body.contains(r#"name="password" value"#));
async fn the_login_fields_keep_their_credential_autocomplete() {
let body = body_of(login_page(StatusCode::OK, None, "")).await;
assert_autocomplete(
&body,
&[
("name=\"email\"", "username"),
("name=\"password\"", "current-password"),
],
);
async fn the_page_without_an_error_has_no_alert() {
assert!(!body.contains("error-alert"));
async fn throttled() -> Result<&'static str, WebError> {
Err(WebError::too_many_requests("Slow down"))
async fn fine() -> &'static str {
"fine"
fn app() -> Router {
Router::new()
.route("/throttled", get(throttled))
.route("/fine", get(fine))
.route_layer(middleware::from_fn(reshow_login))
async fn a_refusal_outside_the_handler_reshows_the_login_page() {
let response = app()
.oneshot(
Request::get("/throttled")
.body(Body::empty())
.expect("request"),
)
.expect("answers");
assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
assert!(body.contains("Slow down"));
assert!(body.contains(r#"id="loginform""#));
async fn other_responses_pass_through() {
.oneshot(Request::get("/fine").body(Body::empty()).expect("request"))
assert_eq!(body_of(response).await, "fine");
#[test]
fn login_cookies_split_into_headers_and_token() {
let cookies = LoginCookies {
cookies: ["a=1".to_string(), "b=2".to_string()],
access_token: "tok".to_string(),
let (headers, token) = cookie_headers(cookies);
assert_eq!(headers.get_all(header::SET_COOKIE).iter().count(), 2);
assert_eq!(token, "tok");
async fn form_only(axum::Form(_): axum::Form<LoginUserSchema>) -> &'static str {
"ok"
async fn a_malformed_login_submission_reshows_the_login_page() {
let response = Router::new()
.route("/login", post(form_only))
Request::post("/login")
.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
.body(Body::from("email=me%40example.com"))
assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
assert!(body.contains("error-alert"));