mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-21 13:03:09 -06:00
Add test for federation keys endpoint
This commit is contained in:
parent
be43dfe5e0
commit
90690b96ff
197
federationapi/internal/federationclient_test.go
Normal file
197
federationapi/internal/federationclient_test.go
Normal file
|
|
@ -0,0 +1,197 @@
|
||||||
|
// Copyright 2022 The Matrix.org Foundation C.I.C.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package internal
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/federationapi/queue"
|
||||||
|
"github.com/matrix-org/dendrite/federationapi/statistics"
|
||||||
|
"github.com/matrix-org/dendrite/federationapi/storage"
|
||||||
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
|
"github.com/matrix-org/dendrite/setup/process"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t *testFedClient) QueryKeys(ctx context.Context, origin, s gomatrixserverlib.ServerName, keys map[string][]string) (gomatrixserverlib.RespQueryKeys, error) {
|
||||||
|
t.queryKeysCalled = true
|
||||||
|
if t.shouldFail {
|
||||||
|
return gomatrixserverlib.RespQueryKeys{}, fmt.Errorf("Failure")
|
||||||
|
}
|
||||||
|
return gomatrixserverlib.RespQueryKeys{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *testFedClient) ClaimKeys(ctx context.Context, origin, s gomatrixserverlib.ServerName, oneTimeKeys map[string]map[string]string) (gomatrixserverlib.RespClaimKeys, error) {
|
||||||
|
t.claimKeysCalled = true
|
||||||
|
if t.shouldFail {
|
||||||
|
return gomatrixserverlib.RespClaimKeys{}, fmt.Errorf("Failure")
|
||||||
|
}
|
||||||
|
return gomatrixserverlib.RespClaimKeys{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFederationClientQueryKeys(t *testing.T) {
|
||||||
|
testDB := storage.NewFakeFederationDatabase()
|
||||||
|
|
||||||
|
cfg := config.FederationAPI{
|
||||||
|
Matrix: &config.Global{
|
||||||
|
SigningIdentity: gomatrixserverlib.SigningIdentity{
|
||||||
|
ServerName: "server",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fedClient := &testFedClient{}
|
||||||
|
stats := statistics.NewStatistics(testDB, 8, 3)
|
||||||
|
queues := queue.NewOutgoingQueues(
|
||||||
|
testDB, process.NewProcessContext(),
|
||||||
|
false,
|
||||||
|
cfg.Matrix.ServerName, fedClient, nil, &stats,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
fedapi := FederationInternalAPI{
|
||||||
|
db: testDB,
|
||||||
|
cfg: &cfg,
|
||||||
|
statistics: &stats,
|
||||||
|
federation: fedClient,
|
||||||
|
queues: queues,
|
||||||
|
}
|
||||||
|
_, err := fedapi.QueryKeys(context.Background(), "origin", "server", nil)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, fedClient.queryKeysCalled)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFederationClientQueryKeysBlacklisted(t *testing.T) {
|
||||||
|
testDB := storage.NewFakeFederationDatabase()
|
||||||
|
testDB.AddServerToBlacklist("server")
|
||||||
|
|
||||||
|
cfg := config.FederationAPI{
|
||||||
|
Matrix: &config.Global{
|
||||||
|
SigningIdentity: gomatrixserverlib.SigningIdentity{
|
||||||
|
ServerName: "server",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fedClient := &testFedClient{}
|
||||||
|
stats := statistics.NewStatistics(testDB, 8, 3)
|
||||||
|
queues := queue.NewOutgoingQueues(
|
||||||
|
testDB, process.NewProcessContext(),
|
||||||
|
false,
|
||||||
|
cfg.Matrix.ServerName, fedClient, nil, &stats,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
fedapi := FederationInternalAPI{
|
||||||
|
db: testDB,
|
||||||
|
cfg: &cfg,
|
||||||
|
statistics: &stats,
|
||||||
|
federation: fedClient,
|
||||||
|
queues: queues,
|
||||||
|
}
|
||||||
|
_, err := fedapi.QueryKeys(context.Background(), "origin", "server", nil)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
assert.False(t, fedClient.queryKeysCalled)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFederationClientQueryKeysFailure(t *testing.T) {
|
||||||
|
testDB := storage.NewFakeFederationDatabase()
|
||||||
|
|
||||||
|
cfg := config.FederationAPI{
|
||||||
|
Matrix: &config.Global{
|
||||||
|
SigningIdentity: gomatrixserverlib.SigningIdentity{
|
||||||
|
ServerName: "server",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fedClient := &testFedClient{shouldFail: true}
|
||||||
|
stats := statistics.NewStatistics(testDB, 8, 3)
|
||||||
|
queues := queue.NewOutgoingQueues(
|
||||||
|
testDB, process.NewProcessContext(),
|
||||||
|
false,
|
||||||
|
cfg.Matrix.ServerName, fedClient, nil, &stats,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
fedapi := FederationInternalAPI{
|
||||||
|
db: testDB,
|
||||||
|
cfg: &cfg,
|
||||||
|
statistics: &stats,
|
||||||
|
federation: fedClient,
|
||||||
|
queues: queues,
|
||||||
|
}
|
||||||
|
_, err := fedapi.QueryKeys(context.Background(), "origin", "server", nil)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
assert.True(t, fedClient.queryKeysCalled)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFederationClientClaimKeys(t *testing.T) {
|
||||||
|
testDB := storage.NewFakeFederationDatabase()
|
||||||
|
|
||||||
|
cfg := config.FederationAPI{
|
||||||
|
Matrix: &config.Global{
|
||||||
|
SigningIdentity: gomatrixserverlib.SigningIdentity{
|
||||||
|
ServerName: "server",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fedClient := &testFedClient{}
|
||||||
|
stats := statistics.NewStatistics(testDB, 8, 3)
|
||||||
|
queues := queue.NewOutgoingQueues(
|
||||||
|
testDB, process.NewProcessContext(),
|
||||||
|
false,
|
||||||
|
cfg.Matrix.ServerName, fedClient, nil, &stats,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
fedapi := FederationInternalAPI{
|
||||||
|
db: testDB,
|
||||||
|
cfg: &cfg,
|
||||||
|
statistics: &stats,
|
||||||
|
federation: fedClient,
|
||||||
|
queues: queues,
|
||||||
|
}
|
||||||
|
_, err := fedapi.ClaimKeys(context.Background(), "origin", "server", nil)
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.True(t, fedClient.claimKeysCalled)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFederationClientClaimKeysBlacklisted(t *testing.T) {
|
||||||
|
testDB := storage.NewFakeFederationDatabase()
|
||||||
|
testDB.AddServerToBlacklist("server")
|
||||||
|
|
||||||
|
cfg := config.FederationAPI{
|
||||||
|
Matrix: &config.Global{
|
||||||
|
SigningIdentity: gomatrixserverlib.SigningIdentity{
|
||||||
|
ServerName: "server",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fedClient := &testFedClient{}
|
||||||
|
stats := statistics.NewStatistics(testDB, 8, 3)
|
||||||
|
queues := queue.NewOutgoingQueues(
|
||||||
|
testDB, process.NewProcessContext(),
|
||||||
|
false,
|
||||||
|
cfg.Matrix.ServerName, fedClient, nil, &stats,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
fedapi := FederationInternalAPI{
|
||||||
|
db: testDB,
|
||||||
|
cfg: &cfg,
|
||||||
|
statistics: &stats,
|
||||||
|
federation: fedClient,
|
||||||
|
queues: queues,
|
||||||
|
}
|
||||||
|
_, err := fedapi.ClaimKeys(context.Background(), "origin", "server", nil)
|
||||||
|
assert.NotNil(t, err)
|
||||||
|
assert.False(t, fedClient.claimKeysCalled)
|
||||||
|
}
|
||||||
|
|
@ -30,6 +30,9 @@ import (
|
||||||
|
|
||||||
type testFedClient struct {
|
type testFedClient struct {
|
||||||
api.FederationClient
|
api.FederationClient
|
||||||
|
queryKeysCalled bool
|
||||||
|
claimKeysCalled bool
|
||||||
|
shouldFail bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestPerformWakeupServers(t *testing.T) {
|
func TestPerformWakeupServers(t *testing.T) {
|
||||||
|
|
|
||||||
|
|
@ -92,9 +92,9 @@ func TestHandleQueryDirectory(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error: %s", err.Error())
|
t.Fatalf("Error: %s", err.Error())
|
||||||
}
|
}
|
||||||
vars := map[string]string{"room_alias": "#room:server"}
|
// vars := map[string]string{"room_alias": "#room:server"}
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
httpReq = mux.SetURLVars(httpReq, vars)
|
// httpReq = mux.SetURLVars(httpReq, vars)
|
||||||
handler(w, httpReq)
|
handler(w, httpReq)
|
||||||
|
|
||||||
res := w.Result()
|
res := w.Result()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue