1
//! A3: the header fragment is the only way into the account page.
2

            
3
use crate::common::{create_mock_jwt_auth, create_mock_user};
4
use axum::Extension;
5

            
6
3
async fn fragment(role: &str) -> String {
7
3
    let mut user = create_mock_user();
8
3
    user.role = role.to_string();
9
3
    let response = web::handler::get_logout_link(Extension(create_mock_jwt_auth(user))).await;
10

            
11
3
    let body = axum::body::to_bytes(response.into_body(), usize::MAX)
12
3
        .await
13
3
        .expect("body");
14
3
    String::from_utf8(body.to_vec()).expect("utf8")
15
3
}
16

            
17
#[supp_macro::shared_runtime_test]
18
1
async fn every_user_gets_a_link_to_their_account_page() {
19
1
    let markup = fragment("user").await;
20
1
    assert!(
21
1
        markup.contains("/account/ssh-key"),
22
        "without this link the password form is unreachable: {markup}"
23
    );
24
1
    assert!(markup.contains("/api/auth/logout"), "got: {markup}");
25
}
26

            
27
#[supp_macro::shared_runtime_test]
28
1
async fn only_an_admin_is_offered_the_registration_link() {
29
1
    assert!(
30
1
        !fragment("user").await.contains("/register"),
31
        "a non-admin must not be shown a link that 401s"
32
    );
33
1
    assert!(fragment("admin").await.contains("/register"));
34
}
35

            
36
/// The middleware must not append a refreshed session on top of a handler that
37
/// just ended one. Logout does not bump `session_generation`, so a refresh
38
/// minted on the way in stays valid, and its rotated Redis UUID is one logout
39
/// never saw and cannot delete.
40
#[test]
41
1
fn a_session_ending_response_is_recognised() {
42
    use axum::body::Body;
43
    use axum::http::{Response, header};
44

            
45
2
    let build = |cookie: String| {
46
2
        let mut response = Response::new(Body::empty());
47
2
        response
48
2
            .headers_mut()
49
2
            .append(header::SET_COOKIE, cookie.parse().expect("header"));
50
2
        response
51
2
    };
52

            
53
1
    let ended = web::session::session_end_headers("/login");
54
1
    let expiry = ended
55
1
        .get_all(header::SET_COOKIE)
56
1
        .iter()
57
1
        .find_map(|v| v.to_str().ok().filter(|c| c.starts_with("access_token")))
58
1
        .expect("an access_token expiry")
59
        .to_string();
60

            
61
1
    assert!(web::jwt_auth::ends_session(&build(expiry)));
62
1
    assert!(!web::jwt_auth::ends_session(&build(
63
1
        "access_token=a.b.c; Path=/; Max-Age=900".to_string()
64
1
    )));
65
1
}