Skip to content

Commit c176f4b

Browse files
authored
Merge pull request #817 from AndrewFerr/delayed-event-management-changes
Delayed event management changes
2 parents 1de6412 + 5639ba6 commit c176f4b

File tree

1 file changed

+50
-62
lines changed

1 file changed

+50
-62
lines changed

tests/msc4140/delayed_event_test.go

Lines changed: 50 additions & 62 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

@@ -29,6 +37,7 @@ func TestDelayedEvents(t *testing.T) {
2937

3038
user := deployment.Register(t, hsName, helpers.RegistrationOpts{})
3139
user2 := deployment.Register(t, hsName, helpers.RegistrationOpts{})
40+
unauthedClient := deployment.UnauthenticatedClient(t, hsName)
3241

3342
roomID := user.MustCreateRoom(t, map[string]interface{}{
3443
"preset": "public_chat",
@@ -44,6 +53,13 @@ func TestDelayedEvents(t *testing.T) {
4453
matchDelayedEvents(t, user, 0)
4554
})
4655

56+
t.Run("delayed event lookups are authenticated", func(t *testing.T) {
57+
res := unauthedClient.Do(t, "GET", getPathForDelayedEvents())
58+
must.MatchResponse(t, res, match.HTTPResponse{
59+
StatusCode: 401,
60+
})
61+
})
62+
4763
t.Run("delayed message events are sent on timeout", func(t *testing.T) {
4864
var res *http.Response
4965
var countExpected uint64
@@ -165,66 +181,42 @@ func TestDelayedEvents(t *testing.T) {
165181
})
166182
})
167183

168-
t.Run("cannot update a delayed event without a delay ID", func(t *testing.T) {
169-
res := user.Do(t, "POST", append(getPathForUpdateDelayedEvents(), ""))
170-
must.MatchResponse(t, res, match.HTTPResponse{
171-
StatusCode: 404,
172-
})
173-
})
174-
175-
t.Run("cannot update a delayed event without a request body", func(t *testing.T) {
176-
res := user.Do(t, "POST", append(getPathForUpdateDelayedEvents(), "abc"))
177-
must.MatchResponse(t, res, match.HTTPResponse{
178-
StatusCode: 400,
179-
JSON: []match.JSON{
180-
match.JSONKeyEqual("errcode", "M_NOT_JSON"),
181-
},
182-
})
183-
})
184-
185184
t.Run("cannot update a delayed event without an action", func(t *testing.T) {
186-
res := user.Do(
185+
res := unauthedClient.Do(
187186
t,
188187
"POST",
189-
append(getPathForUpdateDelayedEvents(), "abc"),
188+
append(getPathForDelayedEvents(), "abc"),
190189
client.WithJSONBody(t, map[string]interface{}{}),
191190
)
192-
must.MatchResponse(t, res, match.HTTPResponse{
193-
StatusCode: 400,
194-
JSON: []match.JSON{
195-
match.JSONKeyEqual("errcode", "M_MISSING_PARAM"),
196-
},
197-
})
191+
// TODO: specify failure as 404 when/if Synapse removes the action-in-body version of this endpoint
192+
must.MatchFailure(t, res)
198193
})
199194

200195
t.Run("cannot update a delayed event with an invalid action", func(t *testing.T) {
201-
res := user.Do(
196+
res := unauthedClient.Do(
202197
t,
203198
"POST",
204-
append(getPathForUpdateDelayedEvents(), "abc"),
205-
client.WithJSONBody(t, map[string]interface{}{
206-
"action": "oops",
207-
}),
199+
append(getPathForDelayedEvents(), "abc", "oops"),
200+
client.WithJSONBody(t, map[string]interface{}{}),
208201
)
209202
must.MatchResponse(t, res, match.HTTPResponse{
210-
StatusCode: 400,
211-
JSON: []match.JSON{
212-
match.JSONKeyEqual("errcode", "M_INVALID_PARAM"),
213-
},
203+
StatusCode: 404,
214204
})
215205
})
216206

217207
t.Run("parallel", func(t *testing.T) {
218-
for _, action := range []string{"cancel", "restart", "send"} {
208+
for _, action := range []DelayedEventAction{
209+
DelayedEventActionCancel,
210+
DelayedEventActionRestart,
211+
DelayedEventActionSend,
212+
} {
219213
t.Run(fmt.Sprintf("cannot %s a delayed event without a matching delay ID", action), func(t *testing.T) {
220214
t.Parallel()
221-
res := user.Do(
215+
res := unauthedClient.Do(
222216
t,
223217
"POST",
224-
append(getPathForUpdateDelayedEvents(), "abc"),
225-
client.WithJSONBody(t, map[string]interface{}{
226-
"action": action,
227-
}),
218+
getPathForUpdateDelayedEvent("abc", action),
219+
client.WithJSONBody(t, map[string]interface{}{}),
228220
)
229221
must.MatchResponse(t, res, match.HTTPResponse{
230222
StatusCode: 404,
@@ -258,13 +250,11 @@ func TestDelayedEvents(t *testing.T) {
258250
StatusCode: 404,
259251
})
260252

261-
user.MustDo(
253+
unauthedClient.MustDo(
262254
t,
263255
"POST",
264-
append(getPathForUpdateDelayedEvents(), delayID),
265-
client.WithJSONBody(t, map[string]interface{}{
266-
"action": "cancel",
267-
}),
256+
getPathForUpdateDelayedEvent(delayID, DelayedEventActionCancel),
257+
client.WithJSONBody(t, map[string]interface{}{}),
268258
)
269259
matchDelayedEvents(t, user, 0)
270260

@@ -302,13 +292,11 @@ func TestDelayedEvents(t *testing.T) {
302292
StatusCode: 404,
303293
})
304294

305-
user.MustDo(
295+
unauthedClient.MustDo(
306296
t,
307297
"POST",
308-
append(getPathForUpdateDelayedEvents(), delayID),
309-
client.WithJSONBody(t, map[string]interface{}{
310-
"action": "send",
311-
}),
298+
getPathForUpdateDelayedEvent(delayID, DelayedEventActionSend),
299+
client.WithJSONBody(t, map[string]interface{}{}),
312300
)
313301
matchDelayedEvents(t, user, 0)
314302
res = user.Do(t, "GET", getPathForState(roomID, eventType, stateKey))
@@ -346,13 +334,11 @@ func TestDelayedEvents(t *testing.T) {
346334
StatusCode: 404,
347335
})
348336

349-
user.MustDo(
337+
unauthedClient.MustDo(
350338
t,
351339
"POST",
352-
append(getPathForUpdateDelayedEvents(), delayID),
353-
client.WithJSONBody(t, map[string]interface{}{
354-
"action": "restart",
355-
}),
340+
getPathForUpdateDelayedEvent(delayID, DelayedEventActionRestart),
341+
client.WithJSONBody(t, map[string]interface{}{}),
356342
)
357343

358344
time.Sleep(1 * time.Second)
@@ -489,10 +475,14 @@ func TestDelayedEvents(t *testing.T) {
489475
})
490476
}
491477

492-
func getPathForUpdateDelayedEvents() []string {
478+
func getPathForDelayedEvents() []string {
493479
return []string{"_matrix", "client", "unstable", "org.matrix.msc4140", "delayed_events"}
494480
}
495481

482+
func getPathForUpdateDelayedEvent(delayId string, action DelayedEventAction) []string {
483+
return append(getPathForDelayedEvents(), delayId, string(action))
484+
}
485+
496486
func getPathForSend(roomID string, eventType string, txnId string) []string {
497487
return []string{"_matrix", "client", "v3", "rooms", roomID, "send", eventType, txnId}
498488
}
@@ -509,7 +499,7 @@ func getDelayQueryParam(delayStr string) client.RequestOpt {
509499

510500
func getDelayedEvents(t *testing.T, user *client.CSAPI) *http.Response {
511501
t.Helper()
512-
return user.MustDo(t, "GET", getPathForUpdateDelayedEvents())
502+
return user.MustDo(t, "GET", getPathForDelayedEvents())
513503
}
514504

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

520510
// We need to retry this as replication can sometimes lag.
521-
user.MustDo(t, "GET", getPathForUpdateDelayedEvents(),
511+
user.MustDo(t, "GET", getPathForDelayedEvents(),
522512
client.WithRetryUntil(
523513
500*time.Millisecond,
524514
func(res *http.Response) bool {
@@ -548,10 +538,8 @@ func cleanupDelayedEvents(t *testing.T, user *client.CSAPI) {
548538
user.MustDo(
549539
t,
550540
"POST",
551-
append(getPathForUpdateDelayedEvents(), delayID),
552-
client.WithJSONBody(t, map[string]interface{}{
553-
"action": "cancel",
554-
}),
541+
getPathForUpdateDelayedEvent(delayID, DelayedEventActionCancel),
542+
client.WithJSONBody(t, map[string]interface{}{}),
555543
)
556544
}
557545

0 commit comments

Comments
 (0)