summaryrefslogtreecommitdiff
path: root/src/login/handlers/logout/test.rs
blob: e7b7dd4c782fb2982ae0a537e951743c0e73f0a4 (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
use axum::extract::{Json, State};

use crate::{
    empty::Empty,
    test::{fixtures, verify},
    token::app,
};

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

    let app = fixtures::scratch_app().await;
    let now = fixtures::now();
    let creds = fixtures::user::create_with_password(&app, &fixtures::now()).await;
    let identity = fixtures::cookie::logged_in(&app, &creds, &now).await;

    // Call the endpoint

    let (response_identity, Empty) = super::handler(
        State(app.clone()),
        fixtures::now(),
        identity.clone(),
        Json::default(),
    )
    .await
    .expect("logged out with a valid token");

    // Verify the return value's basic structure
    assert!(response_identity.secret().is_none());

    // Verify the semantics
    verify::identity::invalid(&app, &identity).await;
}

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

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

    // Call the endpoint

    let identity = fixtures::cookie::not_logged_in();
    let (identity, Empty) = super::handler(State(app), fixtures::now(), identity, Json::default())
        .await
        .expect("logged out with no token succeeds");

    // Verify the return value's basic structure

    assert!(identity.secret().is_none());
}

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

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

    // Call the endpoint

    let identity = fixtures::cookie::fictitious();
    let super::Error(error) =
        super::handler(State(app), fixtures::now(), identity, Json::default())
            .await
            .expect_err("logged out with an invalid token fails");

    // Verify the return value's basic structure

    assert!(matches!(error, app::ValidateError::InvalidToken));
}