summaryrefslogtreecommitdiff
path: root/src/event/handlers/stream/test/setup.rs
blob: 297162e0dc99c82d4cb5371f7ab853ca5f78f8b5 (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
use axum::extract::State;
use axum_extra::extract::Query;
use futures::{future, stream::StreamExt as _};

use crate::test::fixtures::{self, future::Expect as _};

// There's no test for this in subscribe-then-setup order because creating an
// identity to subscribe with also completes initial setup, preventing the
// test from running. That is also a can't-happen scenario in reality.
#[tokio::test]
async fn previously_completed() {
    // Set up the environment

    let app = fixtures::scratch_app().await;
    let resume_point = fixtures::boot::resume_point(&app).await;

    // Complete initial setup

    let (name, password) = fixtures::user::propose();
    let secret = app
        .setup()
        .initial(&name, &password, &fixtures::now())
        .await
        .expect("initial setup in an empty app succeeds");
    let (_, owner) = app
        .tokens()
        .validate(&secret, &fixtures::now())
        .await
        .expect("secret returned by initial setup should be valid");

    // Subscribe to events

    let subscriber = fixtures::identity::create(&app, &fixtures::now()).await;
    let super::Response(events) = super::handler(
        State(app.clone()),
        subscriber,
        None,
        Query(super::QueryParams { resume_point }),
    )
    .await
    .expect("subscribe never fails");

    // Expect a login created event

    let _ = events
        .filter_map(fixtures::event::stream::user)
        .filter_map(fixtures::event::stream::user::created)
        .filter(|event| future::ready(event.user == owner))
        .next()
        .expect_some("a login created event is sent")
        .await;
}