Skip to content

Commit 5639ba6

Browse files
committed
MSC4140: test split delayed event mgmt endpoints
1 parent 1f90952 commit 5639ba6

File tree

1 file changed

+37
-57
lines changed

1 file changed

+37
-57
lines changed

tests/msc4140/delayed_event_test.go

Lines changed: 37 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ import (
2020
const hsName = "hs1"
2121
const eventType = "com.example.test"
2222

23+
type DelayedEventAction string
24+
25+
const (
26+
DelayedEventActionCancel = "cancel"
27+
DelayedEventActionRestart = "restart"
28+
DelayedEventActionSend = "send"
29+
)
30+
2331
// TODO: Test pagination of `GET /_matrix/client/v1/delayed_events` once
2432
// it is implemented in a homeserver.
2533

@@ -46,7 +54,7 @@ func TestDelayedEvents(t *testing.T) {
4654
})
4755

4856
t.Run("delayed event lookups are authenticated", func(t *testing.T) {
49-
res := unauthedClient.Do(t, "GET", getPathForUpdateDelayedEvents())
57+
res := unauthedClient.Do(t, "GET", getPathForDelayedEvents())
5058
must.MatchResponse(t, res, match.HTTPResponse{
5159
StatusCode: 401,
5260
})
@@ -173,66 +181,42 @@ func TestDelayedEvents(t *testing.T) {
173181
})
174182
})
175183

176-
t.Run("cannot update a delayed event without a delay ID", func(t *testing.T) {
177-
res := unauthedClient.Do(t, "POST", append(getPathForUpdateDelayedEvents(), ""))
178-
must.MatchResponse(t, res, match.HTTPResponse{
179-
StatusCode: 404,
180-
})
181-
})
182-
183-
t.Run("cannot update a delayed event without a request body", func(t *testing.T) {
184-
res := unauthedClient.Do(t, "POST", append(getPathForUpdateDelayedEvents(), "abc"))
185-
must.MatchResponse(t, res, match.HTTPResponse{
186-
StatusCode: 400,
187-
JSON: []match.JSON{
188-
match.JSONKeyEqual("errcode", "M_NOT_JSON"),
189-
},
190-
})
191-
})
192-
193184
t.Run("cannot update a delayed event without an action", func(t *testing.T) {
194185
res := unauthedClient.Do(
195186
t,
196187
"POST",
197-
append(getPathForUpdateDelayedEvents(), "abc"),
188+
append(getPathForDelayedEvents(), "abc"),
198189
client.WithJSONBody(t, map[string]interface{}{}),
199190
)
200-
must.MatchResponse(t, res, match.HTTPResponse{
201-
StatusCode: 400,
202-
JSON: []match.JSON{
203-
match.JSONKeyEqual("errcode", "M_MISSING_PARAM"),
204-
},
205-
})
191+
// TODO: specify failure as 404 when/if Synapse removes the action-in-body version of this endpoint
192+
must.MatchFailure(t, res)
206193
})
207194

208195
t.Run("cannot update a delayed event with an invalid action", func(t *testing.T) {
209196
res := unauthedClient.Do(
210197
t,
211198
"POST",
212-
append(getPathForUpdateDelayedEvents(), "abc"),
213-
client.WithJSONBody(t, map[string]interface{}{
214-
"action": "oops",
215-
}),
199+
append(getPathForDelayedEvents(), "abc", "oops"),
200+
client.WithJSONBody(t, map[string]interface{}{}),
216201
)
217202
must.MatchResponse(t, res, match.HTTPResponse{
218-
StatusCode: 400,
219-
JSON: []match.JSON{
220-
match.JSONKeyEqual("errcode", "M_INVALID_PARAM"),
221-
},
203+
StatusCode: 404,
222204
})
223205
})
224206

225207
t.Run("parallel", func(t *testing.T) {
226-
for _, action := range []string{"cancel", "restart", "send"} {
208+
for _, action := range []DelayedEventAction{
209+
DelayedEventActionCancel,
210+
DelayedEventActionRestart,
211+
DelayedEventActionSend,
212+
} {
227213
t.Run(fmt.Sprintf("cannot %s a delayed event without a matching delay ID", action), func(t *testing.T) {
228214
t.Parallel()
229215
res := unauthedClient.Do(
230216
t,
231217
"POST",
232-
append(getPathForUpdateDelayedEvents(), "abc"),
233-
client.WithJSONBody(t, map[string]interface{}{
234-
"action": action,
235-
}),
218+
getPathForUpdateDelayedEvent("abc", action),
219+
client.WithJSONBody(t, map[string]interface{}{}),
236220
)
237221
must.MatchResponse(t, res, match.HTTPResponse{
238222
StatusCode: 404,
@@ -269,10 +253,8 @@ func TestDelayedEvents(t *testing.T) {
269253
unauthedClient.MustDo(
270254
t,
271255
"POST",
272-
append(getPathForUpdateDelayedEvents(), delayID),
273-
client.WithJSONBody(t, map[string]interface{}{
274-
"action": "cancel",
275-
}),
256+
getPathForUpdateDelayedEvent(delayID, DelayedEventActionCancel),
257+
client.WithJSONBody(t, map[string]interface{}{}),
276258
)
277259
matchDelayedEvents(t, user, 0)
278260

@@ -313,10 +295,8 @@ func TestDelayedEvents(t *testing.T) {
313295
unauthedClient.MustDo(
314296
t,
315297
"POST",
316-
append(getPathForUpdateDelayedEvents(), delayID),
317-
client.WithJSONBody(t, map[string]interface{}{
318-
"action": "send",
319-
}),
298+
getPathForUpdateDelayedEvent(delayID, DelayedEventActionSend),
299+
client.WithJSONBody(t, map[string]interface{}{}),
320300
)
321301
matchDelayedEvents(t, user, 0)
322302
res = user.Do(t, "GET", getPathForState(roomID, eventType, stateKey))
@@ -357,10 +337,8 @@ func TestDelayedEvents(t *testing.T) {
357337
unauthedClient.MustDo(
358338
t,
359339
"POST",
360-
append(getPathForUpdateDelayedEvents(), delayID),
361-
client.WithJSONBody(t, map[string]interface{}{
362-
"action": "restart",
363-
}),
340+
getPathForUpdateDelayedEvent(delayID, DelayedEventActionRestart),
341+
client.WithJSONBody(t, map[string]interface{}{}),
364342
)
365343

366344
time.Sleep(1 * time.Second)
@@ -497,10 +475,14 @@ func TestDelayedEvents(t *testing.T) {
497475
})
498476
}
499477

500-
func getPathForUpdateDelayedEvents() []string {
478+
func getPathForDelayedEvents() []string {
501479
return []string{"_matrix", "client", "unstable", "org.matrix.msc4140", "delayed_events"}
502480
}
503481

482+
func getPathForUpdateDelayedEvent(delayId string, action DelayedEventAction) []string {
483+
return append(getPathForDelayedEvents(), delayId, string(action))
484+
}
485+
504486
func getPathForSend(roomID string, eventType string, txnId string) []string {
505487
return []string{"_matrix", "client", "v3", "rooms", roomID, "send", eventType, txnId}
506488
}
@@ -517,7 +499,7 @@ func getDelayQueryParam(delayStr string) client.RequestOpt {
517499

518500
func getDelayedEvents(t *testing.T, user *client.CSAPI) *http.Response {
519501
t.Helper()
520-
return user.MustDo(t, "GET", getPathForUpdateDelayedEvents())
502+
return user.MustDo(t, "GET", getPathForDelayedEvents())
521503
}
522504

523505
// Checks if the number of delayed events match the given number. This will
@@ -526,7 +508,7 @@ func matchDelayedEvents(t *testing.T, user *client.CSAPI, wantNumber int) {
526508
t.Helper()
527509

528510
// We need to retry this as replication can sometimes lag.
529-
user.MustDo(t, "GET", getPathForUpdateDelayedEvents(),
511+
user.MustDo(t, "GET", getPathForDelayedEvents(),
530512
client.WithRetryUntil(
531513
500*time.Millisecond,
532514
func(res *http.Response) bool {
@@ -556,10 +538,8 @@ func cleanupDelayedEvents(t *testing.T, user *client.CSAPI) {
556538
user.MustDo(
557539
t,
558540
"POST",
559-
append(getPathForUpdateDelayedEvents(), delayID),
560-
client.WithJSONBody(t, map[string]interface{}{
561-
"action": "cancel",
562-
}),
541+
getPathForUpdateDelayedEvent(delayID, DelayedEventActionCancel),
542+
client.WithJSONBody(t, map[string]interface{}{}),
563543
)
564544
}
565545

0 commit comments

Comments
 (0)