Skip to content

Commit aef9aae

Browse files
committed
Tests generated by Copilot
1 parent 216e6df commit aef9aae

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

fclient/federationclient_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,3 +282,111 @@ func jsonify(x interface{}) string {
282282
b, _ := json.Marshal(x)
283283
return string(b)
284284
}
285+
286+
func TestQueryKeysReturnsDeviceKeys(t *testing.T) {
287+
serverName := spec.ServerName("local.server.name")
288+
targetServerName := spec.ServerName("target.server.name")
289+
keyID := gomatrixserverlib.KeyID("ed25519:auto")
290+
_, privateKey, _ := ed25519.GenerateKey(nil)
291+
292+
respQueryKeysJSON := []byte(`{
293+
"device_keys": {
294+
"@user:target.server.name": {
295+
"device1": {
296+
"algorithms": ["m.olm.curve25519-aes-sha2"],
297+
"keys": {
298+
"curve25519:device1": "key1",
299+
"ed25519:device1": "key2"
300+
}
301+
}
302+
}
303+
}
304+
}`)
305+
306+
fc := fclient.NewFederationClient(
307+
[]*fclient.SigningIdentity{
308+
{
309+
ServerName: serverName,
310+
KeyID: keyID,
311+
PrivateKey: privateKey,
312+
},
313+
},
314+
fclient.WithSkipVerify(true),
315+
fclient.WithTransport(
316+
&roundTripper{
317+
fn: func(req *http.Request) (*http.Response, error) {
318+
if strings.HasPrefix(req.URL.Path, "/_matrix/federation/v1/user/keys/query") {
319+
return &http.Response{
320+
StatusCode: 200,
321+
Body: io.NopCloser(bytes.NewReader(respQueryKeysJSON)),
322+
}, nil
323+
}
324+
return &http.Response{
325+
StatusCode: 404,
326+
Body: io.NopCloser(strings.NewReader("404 not found")),
327+
}, nil
328+
},
329+
},
330+
),
331+
)
332+
333+
keys := map[string][]string{
334+
"@user:target.server.name": {"device1"},
335+
}
336+
res, err := fc.QueryKeys(context.Background(), serverName, targetServerName, keys)
337+
if err != nil {
338+
t.Fatalf("QueryKeys returned an error: %s", err)
339+
}
340+
if len(res.DeviceKeys["@user:target.server.name"]["device1"].Keys) == 0 {
341+
t.Fatalf("QueryKeys response missing device keys")
342+
}
343+
}
344+
345+
func TestQueryKeysHandlesNilDeviceKeys(t *testing.T) {
346+
serverName := spec.ServerName("local.server.name")
347+
targetServerName := spec.ServerName("target.server.name")
348+
keyID := gomatrixserverlib.KeyID("ed25519:auto")
349+
_, privateKey, _ := ed25519.GenerateKey(nil)
350+
351+
respQueryKeysJSON := []byte(`{
352+
"device_keys": {}
353+
}`)
354+
355+
fc := fclient.NewFederationClient(
356+
[]*fclient.SigningIdentity{
357+
{
358+
ServerName: serverName,
359+
KeyID: keyID,
360+
PrivateKey: privateKey,
361+
},
362+
},
363+
fclient.WithSkipVerify(true),
364+
fclient.WithTransport(
365+
&roundTripper{
366+
fn: func(req *http.Request) (*http.Response, error) {
367+
if strings.HasPrefix(req.URL.Path, "/_matrix/federation/v1/user/keys/query") {
368+
return &http.Response{
369+
StatusCode: 200,
370+
Body: io.NopCloser(bytes.NewReader(respQueryKeysJSON)),
371+
}, nil
372+
}
373+
return &http.Response{
374+
StatusCode: 404,
375+
Body: io.NopCloser(strings.NewReader("404 not found")),
376+
}, nil
377+
},
378+
},
379+
),
380+
)
381+
382+
keys := map[string][]string{
383+
"@user:target.server.name": nil,
384+
}
385+
res, err := fc.QueryKeys(context.Background(), serverName, targetServerName, keys)
386+
if err != nil {
387+
t.Fatalf("QueryKeys returned an error: %s", err)
388+
}
389+
if len(res.DeviceKeys) != 0 {
390+
t.Fatalf("QueryKeys response should be empty for nil device keys")
391+
}
392+
}

0 commit comments

Comments
 (0)