summaryrefslogtreecommitdiff
path: root/src/invite/handlers/get/test.rs
blob: 0f2f7250f051c8402bdfefb2a2c6c0e0bd2b4897 (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
use axum::extract::{Json, Path, State};

use crate::test::fixtures;

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

    let app = fixtures::scratch_app().await;
    let issuer = fixtures::user::create(&app, &fixtures::now()).await;
    let invite = fixtures::invite::issue(&app, &issuer, &fixtures::now()).await;

    // Call endpoint

    let Json(response) = super::handler(State(app), Path(invite.id))
        .await
        .expect("get for an existing invite succeeds");

    // Verify response

    assert_eq!(issuer.name.display(), &response.issuer);
    assert_eq!(invite.issued_at, response.issued_at);
}

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

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

    // Call endpoint

    let invite = fixtures::invite::fictitious();
    let error = super::handler(State(app), Path(invite.clone()))
        .await
        .expect_err("get for a nonexistent invite fails");

    // Verify response

    assert!(matches!(error, super::Error::NotFound(error_id) if invite == error_id));
}

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

    let app = fixtures::scratch_app().await;
    let issuer = fixtures::user::create(&app, &fixtures::ancient()).await;
    let invite = fixtures::invite::issue(&app, &issuer, &fixtures::ancient()).await;

    app.invites()
        .expire(&fixtures::now())
        .await
        .expect("expiring invites never fails");

    // Call endpoint

    let error = super::handler(State(app), Path(invite.id.clone()))
        .await
        .expect_err("get for an expired invite fails");

    // Verify response

    assert!(matches!(error, super::Error::NotFound(error_id) if invite.id == error_id));
}