mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-21 04:53:14 -06:00
Add initial tests for external relayapi interface
This commit is contained in:
parent
fc365a3e06
commit
21a63203df
|
|
@ -15,6 +15,7 @@
|
||||||
package internal
|
package internal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
fedAPI "github.com/matrix-org/dendrite/federationapi/api"
|
||||||
"github.com/matrix-org/dendrite/federationapi/producers"
|
"github.com/matrix-org/dendrite/federationapi/producers"
|
||||||
"github.com/matrix-org/dendrite/relayapi/storage"
|
"github.com/matrix-org/dendrite/relayapi/storage"
|
||||||
rsAPI "github.com/matrix-org/dendrite/roomserver/api"
|
rsAPI "github.com/matrix-org/dendrite/roomserver/api"
|
||||||
|
|
@ -23,7 +24,7 @@ import (
|
||||||
|
|
||||||
type RelayInternalAPI struct {
|
type RelayInternalAPI struct {
|
||||||
db storage.Database
|
db storage.Database
|
||||||
fedClient *gomatrixserverlib.FederationClient
|
fedClient fedAPI.FederationClient
|
||||||
rsAPI rsAPI.RoomserverInternalAPI
|
rsAPI rsAPI.RoomserverInternalAPI
|
||||||
keyRing *gomatrixserverlib.KeyRing
|
keyRing *gomatrixserverlib.KeyRing
|
||||||
producer *producers.SyncAPIProducer
|
producer *producers.SyncAPIProducer
|
||||||
|
|
@ -33,7 +34,7 @@ type RelayInternalAPI struct {
|
||||||
|
|
||||||
func NewRelayInternalAPI(
|
func NewRelayInternalAPI(
|
||||||
db storage.Database,
|
db storage.Database,
|
||||||
fedClient *gomatrixserverlib.FederationClient,
|
fedClient fedAPI.FederationClient,
|
||||||
rsAPI rsAPI.RoomserverInternalAPI,
|
rsAPI rsAPI.RoomserverInternalAPI,
|
||||||
keyRing *gomatrixserverlib.KeyRing,
|
keyRing *gomatrixserverlib.KeyRing,
|
||||||
producer *producers.SyncAPIProducer,
|
producer *producers.SyncAPIProducer,
|
||||||
|
|
|
||||||
68
relayapi/internal/perform_test.go
Normal file
68
relayapi/internal/perform_test.go
Normal file
|
|
@ -0,0 +1,68 @@
|
||||||
|
// 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"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
fedAPI "github.com/matrix-org/dendrite/federationapi/api"
|
||||||
|
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||||
|
"github.com/matrix-org/dendrite/relayapi/api"
|
||||||
|
"github.com/matrix-org/dendrite/relayapi/storage"
|
||||||
|
"github.com/matrix-org/dendrite/relayapi/storage/shared"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type testFedClient struct {
|
||||||
|
fedAPI.FederationClient
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *testFedClient) GetAsyncEvents(ctx context.Context, u gomatrixserverlib.UserID, prev gomatrixserverlib.RelayEntry, relayServer gomatrixserverlib.ServerName) (res gomatrixserverlib.RespGetAsyncEvents, err error) {
|
||||||
|
return gomatrixserverlib.RespGetAsyncEvents{
|
||||||
|
Txn: gomatrixserverlib.Transaction{},
|
||||||
|
EntryID: 0,
|
||||||
|
EntriesQueued: false,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPerformRelayServerSync(t *testing.T) {
|
||||||
|
testDB := storage.NewFakeRelayDatabase()
|
||||||
|
db := shared.Database{
|
||||||
|
Writer: sqlutil.NewDummyWriter(),
|
||||||
|
RelayQueue: testDB,
|
||||||
|
RelayQueueJSON: testDB,
|
||||||
|
}
|
||||||
|
|
||||||
|
userID, err := gomatrixserverlib.NewUserID("@local:domain", false)
|
||||||
|
assert.Nil(t, err, "Invalid userID")
|
||||||
|
|
||||||
|
fedClient := &testFedClient{}
|
||||||
|
relayAPI := NewRelayInternalAPI(
|
||||||
|
&db, fedClient, nil, nil, nil, false, "",
|
||||||
|
)
|
||||||
|
|
||||||
|
req := api.PerformRelayServerSyncRequest{
|
||||||
|
UserID: *userID,
|
||||||
|
RelayServer: gomatrixserverlib.ServerName("relay"),
|
||||||
|
}
|
||||||
|
res := api.PerformRelayServerSyncResponse{}
|
||||||
|
err = relayAPI.PerformRelayServerSync(context.Background(), &req, &res)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO : called twice when queued
|
||||||
|
// TODO : returns err if fed returns err
|
||||||
|
|
@ -20,7 +20,6 @@ import (
|
||||||
"github.com/matrix-org/dendrite/federationapi/producers"
|
"github.com/matrix-org/dendrite/federationapi/producers"
|
||||||
keyserverAPI "github.com/matrix-org/dendrite/keyserver/api"
|
keyserverAPI "github.com/matrix-org/dendrite/keyserver/api"
|
||||||
"github.com/matrix-org/dendrite/relayapi/api"
|
"github.com/matrix-org/dendrite/relayapi/api"
|
||||||
relayAPI "github.com/matrix-org/dendrite/relayapi/api"
|
|
||||||
"github.com/matrix-org/dendrite/relayapi/internal"
|
"github.com/matrix-org/dendrite/relayapi/internal"
|
||||||
"github.com/matrix-org/dendrite/relayapi/inthttp"
|
"github.com/matrix-org/dendrite/relayapi/inthttp"
|
||||||
"github.com/matrix-org/dendrite/relayapi/routing"
|
"github.com/matrix-org/dendrite/relayapi/routing"
|
||||||
|
|
@ -45,7 +44,7 @@ func AddPublicRoutes(
|
||||||
fedClient *gomatrixserverlib.FederationClient,
|
fedClient *gomatrixserverlib.FederationClient,
|
||||||
keyRing gomatrixserverlib.JSONVerifier,
|
keyRing gomatrixserverlib.JSONVerifier,
|
||||||
rsAPI rsAPI.FederationRoomserverAPI,
|
rsAPI rsAPI.FederationRoomserverAPI,
|
||||||
relayAPI relayAPI.RelayInternalAPI,
|
relayAPI api.RelayInternalAPI,
|
||||||
fedAPI federationAPI.FederationInternalAPI,
|
fedAPI federationAPI.FederationInternalAPI,
|
||||||
keyAPI keyserverAPI.FederationKeyAPI,
|
keyAPI keyserverAPI.FederationKeyAPI,
|
||||||
) {
|
) {
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/matrix-org/dendrite/relayapi"
|
"github.com/matrix-org/dendrite/relayapi"
|
||||||
"github.com/matrix-org/dendrite/setup/jetstream"
|
"github.com/matrix-org/dendrite/setup/config"
|
||||||
"github.com/matrix-org/dendrite/test"
|
"github.com/matrix-org/dendrite/test"
|
||||||
"github.com/matrix-org/dendrite/test/testrig"
|
"github.com/matrix-org/dendrite/test/testrig"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
|
@ -27,11 +27,7 @@ import (
|
||||||
func TestCreateNewRelayInternalAPI(t *testing.T) {
|
func TestCreateNewRelayInternalAPI(t *testing.T) {
|
||||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||||
base, close := testrig.CreateBaseDendrite(t, dbType)
|
base, close := testrig.CreateBaseDendrite(t, dbType)
|
||||||
base.Cfg.FederationAPI.PreferDirectFetch = true
|
|
||||||
base.Cfg.FederationAPI.KeyPerspectives = nil
|
|
||||||
defer close()
|
defer close()
|
||||||
jsctx, _ := base.NATS.Prepare(base.ProcessContext, &base.Cfg.Global.JetStream)
|
|
||||||
defer jetstream.DeleteAllStreams(jsctx, &base.Cfg.Global.JetStream)
|
|
||||||
|
|
||||||
relayAPI := relayapi.NewRelayInternalAPI(
|
relayAPI := relayapi.NewRelayInternalAPI(
|
||||||
base,
|
base,
|
||||||
|
|
@ -43,3 +39,70 @@ func TestCreateNewRelayInternalAPI(t *testing.T) {
|
||||||
assert.NotNil(t, relayAPI)
|
assert.NotNil(t, relayAPI)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateRelayInternalInvalidDatabasePanics(t *testing.T) {
|
||||||
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||||
|
base, close := testrig.CreateBaseDendrite(t, dbType)
|
||||||
|
if dbType == test.DBTypeSQLite {
|
||||||
|
base.Cfg.RelayAPI.Database.ConnectionString = "file:"
|
||||||
|
} else {
|
||||||
|
base.Cfg.RelayAPI.Database.ConnectionString = "test"
|
||||||
|
}
|
||||||
|
defer close()
|
||||||
|
|
||||||
|
assert.Panics(t, func() {
|
||||||
|
relayapi.NewRelayInternalAPI(
|
||||||
|
base,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateRelayInternalRoutes(t *testing.T) {
|
||||||
|
base, close := testrig.CreateBaseDendrite(t, test.DBTypeSQLite)
|
||||||
|
base.Cfg.RelayAPI.InternalAPI.Connect = config.HTTPAddress("http://localhost:8008")
|
||||||
|
defer close()
|
||||||
|
|
||||||
|
relayAPI := relayapi.NewRelayInternalAPI(
|
||||||
|
base,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
assert.NotNil(t, relayAPI)
|
||||||
|
|
||||||
|
relayapi.AddInternalRoutes(base.InternalAPIMux, &relayAPI)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateInvalidRelayPublicRoutesPanics(t *testing.T) {
|
||||||
|
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||||
|
base, close := testrig.CreateBaseDendrite(t, dbType)
|
||||||
|
defer close()
|
||||||
|
|
||||||
|
assert.Panics(t, func() {
|
||||||
|
relayapi.AddPublicRoutes(base, nil, nil, nil, nil, nil, nil, nil)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestCreateRelayPublicRoutes(t *testing.T) {
|
||||||
|
base, close := testrig.CreateBaseDendrite(t, test.DBTypeSQLite)
|
||||||
|
base.Cfg.RelayAPI.InternalAPI.Connect = config.HTTPAddress("http://localhost:8008")
|
||||||
|
defer close()
|
||||||
|
|
||||||
|
relayAPI := relayapi.NewRelayInternalAPI(
|
||||||
|
base,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
)
|
||||||
|
assert.NotNil(t, relayAPI)
|
||||||
|
|
||||||
|
relayapi.AddPublicRoutes(base, nil, nil, nil, nil, &relayAPI, nil, nil)
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue