summaryrefslogtreecommitdiff
path: root/src/push/handlers/ping/test.rs
blob: 70ba4bf88924ce9daa19c7b1a49f21b24f2a782c (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
use std::{collections::HashSet, io};

use axum::{
    extract::{Json, State},
    http::StatusCode,
};
use itertools::Itertools;
use web_push::{SubscriptionInfo, WebPushError};

use crate::{event::Heartbeat, test::fixtures};

#[tokio::test]
async fn ping_without_subscriptions() {
    let app = fixtures::scratch_app().await;

    let recipient = fixtures::identity::create(&app, &fixtures::now()).await;

    let response = super::handler(State(app.push()), recipient, Json(super::Request {}))
        .await
        .expect("sending a ping with no subscriptions always succeeds");

    assert_eq!(StatusCode::ACCEPTED, response);

    assert!(
        app.publisher()
            .sent()
            .into_iter()
            .filter(|publish| publish.message_eq(&Heartbeat::Heartbeat)
                && publish.recipients.is_empty())
            .exactly_one()
            .is_ok()
    );
}

#[tokio::test]
async fn ping() {
    let app = fixtures::scratch_app().await;

    let recipient = fixtures::identity::create(&app, &fixtures::now()).await;
    let vapid = fixtures::vapid::key(&app).await;

    // Create a subscription
    let subscription = SubscriptionInfo::new(
        "https://push.example.com/endpoint",
        "testing-p256dh-key",
        "testing-auth",
    );
    app.push()
        .subscribe(&recipient, &subscription, &vapid)
        .await
        .expect("creating a subscription succeeds");

    // Send a ping
    let response = super::handler(State(app.push()), recipient, Json(super::Request {}))
        .await
        .expect("sending a ping succeeds");

    assert_eq!(StatusCode::ACCEPTED, response);

    // Confirm that it was actually sent
    let subscriptions = HashSet::from([subscription]);
    assert!(
        app.publisher()
            .sent()
            .into_iter()
            .filter(|publish| publish.message_eq(&Heartbeat::Heartbeat)
                && publish.recipients == subscriptions)
            .exactly_one()
            .is_ok()
    );
}

#[tokio::test]
async fn ping_multiple_subscriptions() {
    let app = fixtures::scratch_app().await;

    let recipient = fixtures::identity::create(&app, &fixtures::now()).await;
    let vapid = fixtures::vapid::key(&app).await;

    // Create a subscription
    let subscriptions = HashSet::from([
        SubscriptionInfo::new(
            "https://push.example.com/endpoint-1",
            "testing-p256dh-key-1",
            "testing-auth-1",
        ),
        SubscriptionInfo::new(
            "https://push.example.com/endpoint-2",
            "testing-p256dh-key-2",
            "testing-auth-2",
        ),
    ]);
    for subscription in &subscriptions {
        app.push()
            .subscribe(&recipient, subscription, &vapid)
            .await
            .expect("creating a subscription succeeds");
    }

    // Send a ping
    let response = super::handler(State(app.push()), recipient, Json(super::Request {}))
        .await
        .expect("sending a ping succeeds");

    assert_eq!(StatusCode::ACCEPTED, response);

    // Confirm that it was actually sent
    assert!(
        app.publisher()
            .sent()
            .into_iter()
            .filter(|publish| publish.message_eq(&Heartbeat::Heartbeat)
                && publish.recipients == subscriptions)
            .exactly_one()
            .is_ok()
    );
}

#[tokio::test]
async fn ping_recipient_only() {
    let app = fixtures::scratch_app().await;

    let recipient = fixtures::identity::create(&app, &fixtures::now()).await;
    let spectator = fixtures::identity::create(&app, &fixtures::now()).await;

    let vapid = fixtures::vapid::key(&app).await;

    // Create subscriptions for each user
    let recipient_subscription = SubscriptionInfo::new(
        "https://push.example.com/recipient/endpoint",
        "recipient-p256dh-key",
        "recipient-auth",
    );
    app.push()
        .subscribe(&recipient, &recipient_subscription, &vapid)
        .await
        .expect("creating a subscription succeeds");

    let spectator_subscription = SubscriptionInfo::new(
        "https://push.example.com/spectator/endpoint",
        "spectator-p256dh-key",
        "spectator-auth",
    );
    app.push()
        .subscribe(&spectator, &spectator_subscription, &vapid)
        .await
        .expect("creating a subscription succeeds");

    // Send a ping
    let response = super::handler(State(app.push()), recipient, Json(super::Request {}))
        .await
        .expect("sending a ping succeeds");

    assert_eq!(StatusCode::ACCEPTED, response);

    // Confirm that it was actually sent to the recipient
    let sent = app.publisher().sent();

    let recipient_subscriptions = HashSet::from([recipient_subscription]);
    assert!(
        sent.iter()
            .filter(|publish| publish.message_eq(&Heartbeat::Heartbeat)
                && publish.recipients == recipient_subscriptions)
            .exactly_one()
            .is_ok()
    );

    // Confirm that it was not sent to the spectator
    assert!(
        !sent
            .iter()
            .any(|publish| publish.recipients.contains(&spectator_subscription))
    );
}