summaryrefslogtreecommitdiff
path: root/src/setup/routes/test.rs
blob: 5794b786b9cffb9f9e9d950dafb050a592a41099 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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));
}

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

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

    // Call the endpoint

    let name = fixtures::login::propose_invalid_name();
    let password = fixtures::login::propose_password();
    let identity = fixtures::cookie::not_logged_in();
    let request = post::Request {
        name: name.clone(),
        password: password.clone(),
    };
    let post::Error(error) =
        post::handler(State(app.clone()), fixtures::now(), identity, Json(request))
            .await
            .expect_err("setup with an invalid name fails");

    // Verify the response

    assert!(matches!(error, app::Error::InvalidName(error_name) if name == error_name));
}