summaryrefslogtreecommitdiff
path: root/src/setup/routes/test.rs
blob: f7562ae5a2d4b07ee621ae19aeaec495a2824db6 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use axum::extract::{Json, State};

use super::post;
use crate::{setup::app, test::fixtures};

#[tokio::test]
async fn fresh_instance() {
    // Set up the environment

    let app = fixtures::scratch_app().await;

    // Call the endpoint
    let identity = fixtures::cookie::not_logged_in();
    let (name, password) = fixtures::login::propose();
    let request = post::Request {
        name: name.clone(),
        password: password.clone(),
    };
    let (identity, Json(response)) =
        post::handler(State(app.clone()), fixtures::now(), identity, Json(request))
            .await
            .expect("setup in a fresh app succeeds");

    // Verify the response

    assert_eq!(name, response.name);

    // Verify that the issued token is valid

    let secret = identity
        .secret()
        .expect("newly-issued identity has a token secret");
    let (_, login) = app
        .tokens()
        .validate(&secret, &fixtures::now())
        .await
        .expect("newly-issued identity cookie is valid");
    assert_eq!(response, login);

    // Verify that the given credentials can log in

    let (login, _) = app
        .tokens()
        .login(&name, &password, &fixtures::now())
        .await
        .expect("credentials given on signup are valid");
    assert_eq!(response, login);
}

#[tokio::test]
async fn login_exists() {
    // Set up the environment

    let app = fixtures::scratch_app().await;
    fixtures::login::create(&app, &fixtures::now()).await;

    // Call the endpoint
    let identity = fixtures::cookie::not_logged_in();
    let (name, password) = fixtures::login::propose();
    let request = post::Request { name, password };
    let post::Error(error) =
        post::handler(State(app.clone()), fixtures::now(), identity, Json(request))
            .await
            .expect_err("setup in a populated app fails");

    // Verify the response

    assert!(matches!(error, app::Error::SetupCompleted));
}