aboutsummaryrefslogtreecommitdiffstats
path: root/rust/tests/test_auth.rs
blob: 2faf78ec4f7cfef0446803a3feadf58e5ef15c34 (plain)
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
use fatcat::auth::AuthConfectionary;
use fatcat::identifiers::FatcatId;
use fatcat::{auth, server};
use std::str::FromStr;

#[test]
fn test_macaroons() {
    // Test everything we can without connecting to database

    let c = AuthConfectionary::new_dummy();
    let editor_id = FatcatId::from_str("q3nouwy3nnbsvo3h5klxsx4a7y").unwrap();

    // create token w/o expiration
    c.create_token(editor_id, None).unwrap();

    // create token w/ expiration
    c.create_token(editor_id, Some(chrono::Duration::days(1)))
        .unwrap();
}

#[test]
fn test_auth_db() {
    // Test things that require database

    let server = server::create_test_server().unwrap();
    let conn = server.db_pool.get().expect("db_pool error");
    let c = AuthConfectionary::new_dummy();
    let editor_id = FatcatId::from_str("aaaaaaaaaaaabkvkaaaaaaaaae").unwrap();

    // create token
    let token = c.create_token(editor_id, None).unwrap();

    // verify token
    let editor_row = c.parse_macaroon_token(&conn, &token, None).unwrap();
    assert_eq!(editor_row.id, editor_id.to_uuid());

    // create token w/ expiration
    let token = c.create_token(editor_id, Some(chrono::Duration::days(1))).unwrap();

    // verify token w/ expiration
    let editor_row = c.parse_macaroon_token(&conn, &token, None).unwrap();
    assert_eq!(editor_row.id, editor_id.to_uuid());

    // revoke token
    auth::revoke_tokens(&conn, editor_id).unwrap();

    // verification should fail
    // XXX: one-second slop breaks this
    //assert!(c.parse_macaroon_token(&conn, &token, None).is_err());
}