1
use super::*;
2
use crate::form_render::fixture::assert_autocomplete;
3
use axum::{
4
    Router,
5
    body::to_bytes,
6
    middleware,
7
    routing::{get, post},
8
};
9
use tower::ServiceExt;
10

            
11
12
async fn body_of(response: Response) -> String {
12
12
    let bytes = to_bytes(response.into_body(), usize::MAX)
13
12
        .await
14
12
        .expect("body reads");
15
12
    String::from_utf8(bytes.to_vec()).expect("utf8 body")
16
12
}
17

            
18
#[tokio::test]
19
2
async fn the_page_shows_the_error_and_the_email_but_no_password() {
20
2
    let response = login_page(StatusCode::BAD_REQUEST, Some("Nope <b>"), "me@example.com");
21
2
    assert_eq!(response.status(), StatusCode::BAD_REQUEST);
22
2
    let body = body_of(response).await;
23
2
    assert!(body.contains(r#"<div class="error-alert">Nope &#60;b&#62;</div>"#));
24
2
    assert!(body.contains(r#"value="me@example.com""#));
25
2
    assert!(body.contains(r#"name="password""#));
26
2
    assert!(!body.contains(r#"name="password" value"#));
27
2
}
28

            
29
#[tokio::test]
30
2
async fn the_login_fields_keep_their_credential_autocomplete() {
31
2
    let body = body_of(login_page(StatusCode::OK, None, "")).await;
32
2
    assert_autocomplete(
33
2
        &body,
34
2
        &[
35
2
            ("name=\"email\"", "username"),
36
2
            ("name=\"password\"", "current-password"),
37
2
        ],
38
2
    );
39
2
}
40

            
41
#[tokio::test]
42
2
async fn the_page_without_an_error_has_no_alert() {
43
2
    let body = body_of(login_page(StatusCode::OK, None, "")).await;
44
2
    assert!(!body.contains("error-alert"));
45
2
}
46

            
47
2
async fn throttled() -> Result<&'static str, WebError> {
48
2
    Err(WebError::too_many_requests("Slow down"))
49
2
}
50

            
51
2
async fn fine() -> &'static str {
52
2
    "fine"
53
2
}
54

            
55
4
fn app() -> Router {
56
4
    Router::new()
57
4
        .route("/throttled", get(throttled))
58
4
        .route("/fine", get(fine))
59
4
        .route_layer(middleware::from_fn(reshow_login))
60
4
}
61

            
62
#[tokio::test]
63
2
async fn a_refusal_outside_the_handler_reshows_the_login_page() {
64
2
    let response = app()
65
2
        .oneshot(
66
2
            Request::get("/throttled")
67
2
                .body(Body::empty())
68
2
                .expect("request"),
69
2
        )
70
2
        .await
71
2
        .expect("answers");
72
2
    assert_eq!(response.status(), StatusCode::TOO_MANY_REQUESTS);
73
2
    let body = body_of(response).await;
74
2
    assert!(body.contains("Slow down"));
75
2
    assert!(body.contains(r#"id="loginform""#));
76
2
}
77

            
78
#[tokio::test]
79
2
async fn other_responses_pass_through() {
80
2
    let response = app()
81
2
        .oneshot(Request::get("/fine").body(Body::empty()).expect("request"))
82
2
        .await
83
2
        .expect("answers");
84
2
    assert_eq!(body_of(response).await, "fine");
85
2
}
86

            
87
#[test]
88
2
fn login_cookies_split_into_headers_and_token() {
89
2
    let cookies = LoginCookies {
90
2
        cookies: ["a=1".to_string(), "b=2".to_string()],
91
2
        access_token: "tok".to_string(),
92
2
    };
93
2
    let (headers, token) = cookie_headers(cookies);
94
2
    assert_eq!(headers.get_all(header::SET_COOKIE).iter().count(), 2);
95
2
    assert_eq!(token, "tok");
96
2
}
97

            
98
async fn form_only(axum::Form(_): axum::Form<LoginUserSchema>) -> &'static str {
99
    "ok"
100
}
101

            
102
#[tokio::test]
103
2
async fn a_malformed_login_submission_reshows_the_login_page() {
104
2
    let response = Router::new()
105
2
        .route("/login", post(form_only))
106
2
        .route_layer(middleware::from_fn(reshow_login))
107
2
        .oneshot(
108
2
            Request::post("/login")
109
2
                .header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
110
2
                .body(Body::from("email=me%40example.com"))
111
2
                .expect("request"),
112
2
        )
113
2
        .await
114
2
        .expect("answers");
115
2
    assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
116
2
    let body = body_of(response).await;
117
2
    assert!(body.contains(r#"id="loginform""#));
118
2
    assert!(body.contains("error-alert"));
119
2
}