1
use super::*;
2
use crate::form_render::fixture::{assert_autocomplete, assert_fixture};
3
use crate::form_render::{References, render_form};
4
use chrono::{TimeZone, Utc};
5
use forms::FormValues;
6
use server::command::ssh_key::SshKeyRecord;
7
use uuid::Uuid;
8

            
9
6
fn record(last_used_at: Option<chrono::DateTime<Utc>>) -> SshKeyRecord {
10
6
    SshKeyRecord {
11
6
        id: Uuid::from_u128(1),
12
6
        user_id: Uuid::from_u128(2),
13
6
        key_type: "ssh-ed25519".to_string(),
14
6
        key_blob: vec![1, 2, 3],
15
6
        fingerprint: "SHA256:abc".to_string(),
16
6
        annotation: "laptop".to_string(),
17
6
        created_at: Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap(),
18
6
        last_used_at,
19
6
    }
20
6
}
21

            
22
#[test]
23
2
fn key_row_formats_the_last_use() {
24
2
    let used = Utc.with_ymd_and_hms(2024, 3, 5, 14, 7, 0).unwrap();
25
2
    let row = key_row(record(Some(used)), Tz::UTC);
26
2
    assert_eq!(row.fingerprint, "SHA256:abc");
27
2
    assert_eq!(row.key_type, "ssh-ed25519");
28
2
    assert_eq!(row.annotation, "laptop");
29
2
    assert_eq!(row.last_used.as_deref(), Some("2024-03-05 14:07"));
30
2
}
31

            
32
#[test]
33
2
fn password_change_fields_keep_their_credential_autocomplete() {
34
2
    let html = SshKeyPage {
35
2
        form: String::new(),
36
2
        keys: Vec::new(),
37
2
        config_snippet: None,
38
2
    }
39
2
    .render()
40
2
    .unwrap();
41
2
    assert_autocomplete(
42
2
        &html,
43
2
        &[
44
2
            ("name=\"current_password\"", "current-password"),
45
2
            ("name=\"new_password\"", "new-password"),
46
2
            ("name=\"confirm_password\"", "new-password"),
47
2
        ],
48
2
    );
49
2
}
50

            
51
#[test]
52
2
fn key_row_prints_the_last_use_in_the_user_zone() {
53
2
    let used = Utc.with_ymd_and_hms(2024, 3, 5, 23, 30, 0).unwrap();
54
2
    let row = key_row(record(Some(used)), Tz::Asia__Tokyo);
55
2
    assert_eq!(row.last_used.as_deref(), Some("2024-03-06 08:30"));
56
2
}
57

            
58
#[test]
59
2
fn key_row_without_use_has_no_timestamp() {
60
2
    assert!(key_row(record(None), Tz::UTC).last_used.is_none());
61
2
}
62

            
63
#[test]
64
2
fn the_browser_fixture_of_the_ssh_key_add_form_is_up_to_date() {
65
2
    let html = render_form(
66
2
        &SSH_KEY_ADD,
67
2
        &FormValues::default(),
68
2
        &[],
69
2
        SUBMIT_PATH,
70
2
        &References::default(),
71
2
    )
72
2
    .expect("renders");
73
2
    assert_fixture("ssh_key_add.html", &html);
74
2
}
75

            
76
#[test]
77
2
fn the_query_value_keeps_only_unreserved_characters() {
78
2
    assert_eq!(encode_query("SHA256:a+b/c="), "SHA256%3Aa%2Bb%2Fc%3D");
79
2
}