-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathkeychanges_test.go
More file actions
114 lines (104 loc) · 3.82 KB
/
keychanges_test.go
File metadata and controls
114 lines (104 loc) · 3.82 KB
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
package csapi_tests
import (
"fmt"
"net/http"
"net/url"
"testing"
"github.com/tidwall/gjson"
"github.com/matrix-org/complement"
"github.com/matrix-org/complement/client"
"github.com/matrix-org/complement/helpers"
"github.com/matrix-org/complement/match"
"github.com/matrix-org/complement/must"
"github.com/matrix-org/gomatrixserverlib/spec"
)
func TestKeyChangesLocal(t *testing.T) {
deployment := complement.Deploy(t, 1)
defer deployment.Destroy(t)
alice := deployment.Register(t, "hs1", helpers.RegistrationOpts{})
password := "$uperSecretPassword"
bob := deployment.Register(t, "hs1", helpers.RegistrationOpts{
LocalpartSuffix: "bob",
Password: password,
})
unauthedClient := deployment.UnauthenticatedClient(t, "hs1")
t.Run("New login should create a device_lists.changed entry", func(t *testing.T) {
bobDeviceKeys, bobOTKs := bob.MustGenerateOneTimeKeys(t, 1)
bob.MustUploadKeys(t, bobDeviceKeys, bobOTKs)
roomID := alice.MustCreateRoom(t, map[string]interface{}{
"preset": "public_chat",
"initial_state": []map[string]interface{}{
{
"type": "m.room.encryption",
"state_key": "",
"content": map[string]interface{}{
"algorithm": "m.megolm.v1.aes-sha2",
},
},
},
})
bob.MustJoinRoom(t, roomID, []spec.ServerName{})
nextBatch1 := alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(bob.UserID, roomID))
reqBody := client.WithJSONBody(t, map[string]interface{}{
"identifier": map[string]interface{}{
"type": "m.id.user",
"user": bob.UserID,
},
"type": "m.login.password",
"password": password,
})
// Create a new device by logging in
res := unauthedClient.MustDo(t, "POST", []string{"_matrix", "client", "v3", "login"}, reqBody)
loginResp := must.ParseJSON(t, res.Body)
unauthedClient.AccessToken = must.GetJSONFieldStr(t, loginResp, "access_token")
unauthedClient.DeviceID = must.GetJSONFieldStr(t, loginResp, "device_id")
unauthedClient.UserID = must.GetJSONFieldStr(t, loginResp, "user_id")
unauthedKeys, unauthedOTKs := unauthedClient.MustGenerateOneTimeKeys(t, 1)
unauthedClient.MustUploadKeys(t, unauthedKeys, unauthedOTKs)
// Alice should now see a device list changed entry for Bob
nextBatch := alice.MustSyncUntil(t, client.SyncReq{Since: nextBatch1}, func(userID string, syncResp gjson.Result) error {
deviceListsChanged := syncResp.Get("device_lists.changed")
if !deviceListsChanged.IsArray() {
return fmt.Errorf("no device_lists.changed entry found: %+v", syncResp.Raw)
}
for _, userID := range deviceListsChanged.Array() {
if userID.String() == bob.UserID {
return nil
}
}
return fmt.Errorf("no device_lists.changed entry found for %s", bob.UserID)
})
// Verify on /keys/changes that Bob has changes
queryParams := url.Values{}
queryParams.Set("from", nextBatch1)
queryParams.Set("to", nextBatch)
resp := alice.MustDo(t, "GET", []string{"_matrix", "client", "v3", "keys", "changes"}, client.WithQueries(queryParams))
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONKeyEqual("changed.0", bob.UserID), // there should only be one change, so access it directly
},
})
// Get Bobs keys, there should be two
queryKeys := client.WithJSONBody(t, map[string]interface{}{
"device_keys": map[string][]string{
bob.UserID: {},
},
})
resp = alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "keys", "query"}, queryKeys)
keyCount := 0
must.MatchResponse(t, resp, match.HTTPResponse{
StatusCode: http.StatusOK,
JSON: []match.JSON{
match.JSONMapEach("device_keys."+bob.UserID, func(k, v gjson.Result) error {
keyCount++
return nil
}),
},
})
wantKeyCount := 2
if keyCount != wantKeyCount {
t.Fatalf("unexpected key count: got %d, want %d", keyCount, wantKeyCount)
}
})
}