summaryrefslogtreecommitdiff
path: root/src/test/fixtures/identity.rs
blob: e438f2bc7fe77c4c7005e38f3286be4d7d6487d7 (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
use crate::{
    app::App,
    clock::RequestedAt,
    login::Password,
    name::Name,
    test::fixtures,
    token::{
        self,
        extract::{Identity, IdentityCookie},
    },
};

pub async fn create(app: &App, created_at: &RequestedAt) -> Identity {
    let credentials = fixtures::login::create_with_password(app, created_at).await;
    logged_in(app, &credentials, created_at).await
}

pub async fn from_cookie(app: &App, token: &IdentityCookie, issued_at: &RequestedAt) -> Identity {
    let secret = token.secret().expect("identity token has a secret");
    let (token, login) = app
        .tokens()
        .validate(&secret, issued_at)
        .await
        .expect("always validates newly-issued secret");

    Identity { token, login }
}

pub async fn logged_in(
    app: &App,
    credentials: &(Name, Password),
    issued_at: &RequestedAt,
) -> Identity {
    let secret = fixtures::cookie::logged_in(app, credentials, issued_at).await;
    from_cookie(app, &secret, issued_at).await
}

pub fn fictitious() -> Identity {
    let token = token::Id::generate();
    let login = fixtures::login::fictitious();

    Identity { token, login }
}