1
use super::*;
2
use crate::model::User;
3
use axum::http::StatusCode;
4

            
5
2
fn user() -> User {
6
2
    User {
7
2
        id: uuid::Uuid::new_v4(),
8
2
        name: "Test".to_string(),
9
2
        email: "test@example.com".to_string(),
10
2
        role: "user".to_string(),
11
2
        photo: String::new(),
12
2
        verified: true,
13
2
        created_at: None,
14
2
        updated_at: None,
15
2
    }
16
2
}
17

            
18
2
fn config() -> Config {
19
2
    Config {
20
2
        site_url: String::new(),
21
2
        redis_url: String::new(),
22
2
        access_token_max_age: 15,
23
2
        refresh_token_max_age: 60,
24
2
        ssh: crate::config::SshConnectInfo {
25
2
            hostname: String::new(),
26
2
            port: 0,
27
2
            host_fingerprint: String::new(),
28
2
        },
29
2
    }
30
2
}
31

            
32
2
fn auth(refreshed: bool) -> JWTAuthMiddleware {
33
2
    JWTAuthMiddleware {
34
2
        user: user(),
35
2
        access_token_uuid: uuid::Uuid::new_v4(),
36
2
        refreshed,
37
2
    }
38
2
}
39

            
40
#[test]
41
2
fn a_wrong_password_is_a_bad_request() {
42
2
    let err = password_change_error(
43
2
        uuid::Uuid::new_v4(),
44
2
        server::command::CmdError::Args("wrong".to_string()),
45
2
    );
46
2
    assert_eq!(err.status, StatusCode::BAD_REQUEST);
47
2
    assert_eq!(err.message, "Invalid email or password");
48
2
}
49

            
50
#[test]
51
2
fn a_storage_failure_is_not_reported_as_a_wrong_password() {
52
2
    let err = password_change_error(
53
2
        uuid::Uuid::new_v4(),
54
2
        server::command::CmdError::Config(server::config::ConfigError::NoConfig("k".into())),
55
2
    );
56
2
    assert_eq!(err.status, StatusCode::INTERNAL_SERVER_ERROR);
57
2
    assert_eq!(err.message, "Internal error");
58
2
}
59

            
60
#[tokio::test]
61
2
async fn mismatched_confirmation_is_refused_before_anything_is_written() {
62
2
    let result = change_password_handler(
63
2
        Extension(auth(false)),
64
2
        Json(ChangePasswordSchema {
65
2
            current_password: "old".to_string(),
66
2
            new_password: "one".to_string(),
67
2
            confirm_password: "two".to_string(),
68
2
        }),
69
2
    )
70
2
    .await;
71
2
    let err = result.err().expect("mismatch refused");
72
2
    assert_eq!(err.status, StatusCode::BAD_REQUEST);
73
2
    assert_eq!(err.message, "The new passwords do not match");
74
2
}
75

            
76
#[tokio::test]
77
2
async fn refresh_without_a_cookie_is_unauthorized() {
78
2
    let err = own_refresh_token(&CookieJar::default(), uuid::Uuid::new_v4())
79
2
        .await
80
2
        .expect_err("no cookie");
81
2
    assert_eq!(err.status, StatusCode::UNAUTHORIZED);
82
2
}
83

            
84
#[test]
85
2
fn session_cookies_hide_the_tokens_from_scripts() {
86
2
    let [access, refresh] = session_cookies(&config(), "a".to_string(), "r".to_string());
87
2
    assert!(access.starts_with("access_token=a") && access.contains("HttpOnly"));
88
2
    assert!(refresh.starts_with("refresh_token=r") && refresh.contains("HttpOnly"));
89
2
}
90

            
91
#[test]
92
2
fn ending_a_session_expires_the_cookies_and_redirects() {
93
2
    let headers = session_end_headers("/login");
94
2
    assert_eq!(headers.get("HX-Redirect").expect("redirect"), "/login");
95
2
    assert_eq!(headers.get_all(header::SET_COOKIE).iter().count(), 2);
96
2
}