summaryrefslogtreecommitdiffstats
path: root/tests/storage.rs
blob: b6e1369aecaafb7e71641e66992b3ddd95adcb12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Tests for storage layers that already exist. These must PASS.

use std::path::PathBuf;

use zagan_bet::client::database as client_database;
use zagan_bet::server::database as server_database;

fn tmp_db(name: &str) -> (String, rusqlite::Connection) {
    let path = PathBuf::from(env!("CARGO_TARGET_TMPDIR"))
        .join(format!("storage-{name}.db"))
        .to_string_lossy()
        .into_owned();
    let _ = std::fs::remove_file(&path);
    let conn = rusqlite::Connection::open(&path).unwrap();
    (path, conn)
}

#[test]
fn client_credentials_roundtrip() {
    let (_, conn) = tmp_db("creds");
    client_database::init_db(&conn);

    assert_eq!(client_database::get_credentials(&conn, "h1"), None);
    client_database::save_credentials(&conn, "h1", "alice", "pw").unwrap();
    assert_eq!(
        client_database::get_credentials(&conn, "h1"),
        Some(("alice".to_string(), "pw".to_string()))
    );
    // Overwrite works.
    client_database::save_credentials(&conn, "h1", "alice", "pw2").unwrap();
    assert_eq!(
        client_database::get_credentials(&conn, "h1"),
        Some(("alice".to_string(), "pw2".to_string()))
    );
}

#[test]
fn client_last_host_tracking() {
    let (_, conn) = tmp_db("hosts");
    client_database::init_db(&conn);

    assert_eq!(client_database::get_last_host(&conn), None);
    client_database::save_host(&conn, "example.com:9000");
    assert_eq!(
        client_database::get_last_host(&conn),
        Some("example.com:9000".to_string())
    );
}

#[test]
fn server_user_registration_and_auth_lookup() {
    let (_, conn) = tmp_db("users");
    server_database::init_db(&conn);

    server_database::add_user(&conn, "bob", "cafebabe").unwrap();
    assert_eq!(
        server_database::get_hashed_password(&conn, "bob"),
        Some("cafebabe".to_string())
    );
    assert_eq!(server_database::get_hashed_password(&conn, "nobody"), None);
}