summaryrefslogtreecommitdiff
path: root/src/channel/routes/test/on_create.rs
blob: 5e62d7fd0f4f0a4d5a36debd78637621c363f82f (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
use axum::extract::{Json, State};
use futures::{future, stream::StreamExt as _};

use crate::{
    channel::{app, routes},
    events::types,
    test::fixtures::{self, future::Immediately as _},
};

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

    let app = fixtures::scratch_app().await;
    let creator = fixtures::login::create(&app).await;

    // Call the endpoint

    let name = fixtures::channel::propose();
    let request = routes::CreateRequest { name };
    let Json(response_channel) = routes::on_create(
        State(app.clone()),
        creator,
        fixtures::now(),
        Json(request.clone()),
    )
    .await
    .expect("new channel in an empty app");

    // Verify the structure of the response

    assert_eq!(request.name, response_channel.name);

    // Verify the semantics

    let channels = app.channels().all().await.expect("always succeeds");
    assert!(channels.contains(&response_channel));

    let mut events = app
        .events()
        .subscribe(types::ResumePoint::default())
        .await
        .expect("subscribing never fails")
        .filter(|types::ResumableEvent(_, event)| future::ready(event.channel == response_channel));

    let types::ResumableEvent(_, event) = events
        .next()
        .immediately()
        .await
        .expect("creation event published");

    assert_eq!(types::Sequence::default(), event.sequence);
    assert_eq!(types::ChannelEventData::Created, event.data);
}

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

    let app = fixtures::scratch_app().await;
    let creator = fixtures::login::create(&app).await;
    let channel = fixtures::channel::create(&app, &fixtures::now()).await;

    // Call the endpoint

    let request = routes::CreateRequest { name: channel.name };
    let routes::CreateError(error) = routes::on_create(
        State(app.clone()),
        creator,
        fixtures::now(),
        Json(request.clone()),
    )
    .await
    .expect_err("duplicate channel name");

    // Verify the structure of the response

    assert!(matches!(
        error,
        app::CreateError::DuplicateName(name) if request.name == name
    ));
}