Skip to content

Commit

Permalink
Fix flakey test due to incorrectly running in parallel (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston authored Aug 18, 2023
1 parent b986a30 commit 93ccbc8
Showing 1 changed file with 61 additions and 66 deletions.
127 changes: 61 additions & 66 deletions tests/federation_upload_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,77 +48,72 @@ func TestFederationKeyUploadQuery(t *testing.T) {
},
})

t.Run("Parallel", func(t *testing.T) {
// sytest: Can claim remote one time key using POST
t.Run("Can claim remote one time key using POST", func(t *testing.T) {
t.Parallel()
// check keys on remote server
reqBody = client.WithJSONBody(t, map[string]interface{}{
"one_time_keys": map[string]interface{}{
alice.UserID: map[string]string{
alice.DeviceID: "signed_curve25519",
},
// sytest: Can claim remote one time key using POST
t.Run("Can claim remote one time key using POST", func(t *testing.T) {
// check keys on remote server
reqBody = client.WithJSONBody(t, map[string]interface{}{
"one_time_keys": map[string]interface{}{
alice.UserID: map[string]string{
alice.DeviceID: "signed_curve25519",
},
})
resp = bob.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "keys", "claim"}, reqBody)
otksField := "one_time_keys." + client.GjsonEscape(alice.UserID) + "." + client.GjsonEscape(alice.DeviceID)
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONKeyTypeEqual(otksField, gjson.JSON),
match.JSONKeyEqual(otksField, oneTimeKeys),
},
})

// there should be no OTK left now
resp = bob.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "keys", "claim"}, reqBody)
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONKeyMissing("one_time_keys." + client.GjsonEscape(alice.UserID)),
},
})
},
})
resp = bob.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "keys", "claim"}, reqBody)
otksField := "one_time_keys." + client.GjsonEscape(alice.UserID) + "." + client.GjsonEscape(alice.DeviceID)
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONKeyTypeEqual(otksField, gjson.JSON),
match.JSONKeyEqual(otksField, oneTimeKeys),
},
})

// there should be no OTK left now
resp = bob.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "keys", "claim"}, reqBody)
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONKeyMissing("one_time_keys." + client.GjsonEscape(alice.UserID)),
},
})
})

// sytest: Can query remote device keys using POST
t.Run("Can query remote device keys using POST", func(t *testing.T) {
t.Parallel()

displayName := "My new displayname"
body := client.WithJSONBody(t, map[string]interface{}{
"display_name": displayName,
})
alice.MustDoFunc(t, http.MethodPut, []string{"_matrix", "client", "v3", "devices", alice.DeviceID}, body)
// wait for bob to receive the displayname change
bob.MustSyncUntil(t, client.SyncReq{}, func(clientUserID string, topLevelSyncJSON gjson.Result) error {
devicesChanged := topLevelSyncJSON.Get("device_lists.changed")
if devicesChanged.Exists() {
for _, userID := range devicesChanged.Array() {
if userID.Str == alice.UserID {
return nil
}
// sytest: Can query remote device keys using POST
t.Run("Can query remote device keys using POST", func(t *testing.T) {
displayName := "My new displayname"
body := client.WithJSONBody(t, map[string]interface{}{
"display_name": displayName,
})
alice.MustDoFunc(t, http.MethodPut, []string{"_matrix", "client", "v3", "devices", alice.DeviceID}, body)
// wait for bob to receive the displayname change
bob.MustSyncUntil(t, client.SyncReq{}, func(clientUserID string, topLevelSyncJSON gjson.Result) error {
devicesChanged := topLevelSyncJSON.Get("device_lists.changed")
if devicesChanged.Exists() {
for _, userID := range devicesChanged.Array() {
if userID.Str == alice.UserID {
return nil
}
}
return fmt.Errorf("no device_lists found")
})
reqBody = client.WithJSONBody(t, map[string]interface{}{
"device_keys": map[string]interface{}{
alice.UserID: []string{},
},
})
resp = bob.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "keys", "query"}, reqBody)
deviceKeysField := "device_keys." + client.GjsonEscape(alice.UserID) + "." + client.GjsonEscape(alice.DeviceID)

must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONKeyTypeEqual(deviceKeysField, gjson.JSON),
match.JSONKeyEqual(deviceKeysField+".algorithms", deviceKeys["algorithms"]),
match.JSONKeyEqual(deviceKeysField+".keys", deviceKeys["keys"]),
match.JSONKeyEqual(deviceKeysField+".signatures", deviceKeys["signatures"]),
match.JSONKeyEqual(deviceKeysField+".unsigned.device_display_name", displayName),
},
})
}
return fmt.Errorf("no device_lists found")
})
reqBody = client.WithJSONBody(t, map[string]interface{}{
"device_keys": map[string]interface{}{
alice.UserID: []string{},
},
})
resp = bob.MustDoFunc(t, "POST", []string{"_matrix", "client", "v3", "keys", "query"}, reqBody)
deviceKeysField := "device_keys." + client.GjsonEscape(alice.UserID) + "." + client.GjsonEscape(alice.DeviceID)

must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONKeyTypeEqual(deviceKeysField, gjson.JSON),
match.JSONKeyEqual(deviceKeysField+".algorithms", deviceKeys["algorithms"]),
match.JSONKeyEqual(deviceKeysField+".keys", deviceKeys["keys"]),
match.JSONKeyEqual(deviceKeysField+".signatures", deviceKeys["signatures"]),
match.JSONKeyEqual(deviceKeysField+".unsigned.device_display_name", displayName),
},
})
})
}
Expand Down

0 comments on commit 93ccbc8

Please sign in to comment.