summaryrefslogtreecommitdiff
path: root/src/user/routes/logout/test.rs
blob: ce93760d6295718ec074f8fa4af3089a8c03b2f3 (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
use axum::{
    extract::{Json, State},
    http::StatusCode,
};

use super::post;
use crate::{test::fixtures, 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;
    let secret = fixtures::cookie::secret(&identity);

    // Call the endpoint

    let (response_identity, response_status) = post::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());
    assert_eq!(StatusCode::NO_CONTENT, response_status);

    // Verify the semantics
    let error = app
        .tokens()
        .validate(&secret, &now)
        .await
        .expect_err("secret is invalid");
    assert!(matches!(error, app::ValidateError::InvalidToken));
}

#[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, status) = post::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());
    assert_eq!(StatusCode::NO_CONTENT, status);
}

#[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 post::Error(error) = post::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));
}