mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 10:33:11 -06:00
Add tests for voip and capabilities
This commit is contained in:
parent
28d3e296a8
commit
d4861fb19e
194
clientapi/clientapi_test.go
Normal file
194
clientapi/clientapi_test.go
Normal file
|
|
@ -0,0 +1,194 @@
|
|||
package clientapi
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal/caching"
|
||||
"github.com/matrix-org/dendrite/internal/httputil"
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/roomserver"
|
||||
"github.com/matrix-org/dendrite/roomserver/version"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/setup/jetstream"
|
||||
"github.com/matrix-org/dendrite/test"
|
||||
"github.com/matrix-org/dendrite/test/testrig"
|
||||
"github.com/matrix-org/dendrite/userapi"
|
||||
"github.com/matrix-org/gomatrix"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestCapabilities(t *testing.T) {
|
||||
alice := test.NewUser(t)
|
||||
ctx := context.Background()
|
||||
|
||||
// construct the expected result
|
||||
versionsMap := map[gomatrixserverlib.RoomVersion]string{}
|
||||
for v, desc := range version.SupportedRoomVersions() {
|
||||
if desc.Stable {
|
||||
versionsMap[v] = "stable"
|
||||
} else {
|
||||
versionsMap[v] = "unstable"
|
||||
}
|
||||
}
|
||||
|
||||
expectedMap := map[string]interface{}{
|
||||
"capabilities": map[string]interface{}{
|
||||
"m.change_password": map[string]bool{
|
||||
"enabled": true,
|
||||
},
|
||||
"m.room_versions": map[string]interface{}{
|
||||
"default": version.DefaultRoomVersion(),
|
||||
"available": versionsMap,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
expectedBuf := &bytes.Buffer{}
|
||||
err := json.NewEncoder(expectedBuf).Encode(expectedMap)
|
||||
assert.NoError(t, err)
|
||||
|
||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||
cfg, processCtx, close := testrig.CreateConfig(t, dbType)
|
||||
cfg.ClientAPI.RateLimiting.Enabled = false
|
||||
defer close()
|
||||
natsInstance := jetstream.NATSInstance{}
|
||||
|
||||
routers := httputil.NewRouters()
|
||||
cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
|
||||
|
||||
// Needed to create accounts
|
||||
rsAPI := roomserver.NewInternalAPI(processCtx, cfg, cm, &natsInstance, nil, caching.DisableMetrics)
|
||||
userAPI := userapi.NewInternalAPI(processCtx, cfg, cm, &natsInstance, rsAPI, nil)
|
||||
// We mostly need the rsAPI/userAPI for this test, so nil for other APIs etc.
|
||||
AddPublicRoutes(processCtx, routers, cfg, &natsInstance, nil, rsAPI, nil, nil, nil, userAPI, nil, nil, caching.DisableMetrics)
|
||||
|
||||
// Create the users in the userapi and login
|
||||
accessTokens := map[*test.User]string{
|
||||
alice: "",
|
||||
}
|
||||
createAccessTokens(t, accessTokens, userAPI, ctx, routers)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
request *http.Request
|
||||
}{
|
||||
{
|
||||
name: "can get capabilities",
|
||||
request: httptest.NewRequest(http.MethodGet, "/_matrix/client/v3/capabilities", strings.NewReader("")),
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
tc.request.Header.Set("Authorization", "Bearer "+accessTokens[alice])
|
||||
routers.Client.ServeHTTP(rec, tc.request)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
assert.ObjectsAreEqual(expectedBuf.Bytes(), rec.Body.Bytes())
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestTurnserver(t *testing.T) {
|
||||
alice := test.NewUser(t)
|
||||
ctx := context.Background()
|
||||
|
||||
cfg, processCtx, close := testrig.CreateConfig(t, test.DBTypeSQLite)
|
||||
cfg.ClientAPI.RateLimiting.Enabled = false
|
||||
defer close()
|
||||
natsInstance := jetstream.NATSInstance{}
|
||||
|
||||
routers := httputil.NewRouters()
|
||||
cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
|
||||
|
||||
// Needed to create accounts
|
||||
rsAPI := roomserver.NewInternalAPI(processCtx, cfg, cm, &natsInstance, nil, caching.DisableMetrics)
|
||||
userAPI := userapi.NewInternalAPI(processCtx, cfg, cm, &natsInstance, rsAPI, nil)
|
||||
//rsAPI.SetUserAPI(userAPI)
|
||||
// We mostly need the rsAPI/userAPI for this test, so nil for other APIs etc.
|
||||
AddPublicRoutes(processCtx, routers, cfg, &natsInstance, nil, rsAPI, nil, nil, nil, userAPI, nil, nil, caching.DisableMetrics)
|
||||
|
||||
// Create the users in the userapi and login
|
||||
accessTokens := map[*test.User]string{
|
||||
alice: "",
|
||||
}
|
||||
createAccessTokens(t, accessTokens, userAPI, ctx, routers)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
turnConfig config.TURN
|
||||
wantEmptyResponse bool
|
||||
}{
|
||||
{
|
||||
name: "no turn server configured",
|
||||
wantEmptyResponse: true,
|
||||
},
|
||||
{
|
||||
name: "servers configured but not userLifeTime",
|
||||
wantEmptyResponse: true,
|
||||
turnConfig: config.TURN{URIs: []string{""}},
|
||||
},
|
||||
{
|
||||
name: "missing sharedSecret/username/password",
|
||||
wantEmptyResponse: true,
|
||||
turnConfig: config.TURN{URIs: []string{""}, UserLifetime: "1m"},
|
||||
},
|
||||
{
|
||||
name: "with shared secret",
|
||||
turnConfig: config.TURN{URIs: []string{""}, UserLifetime: "1m", SharedSecret: "iAmSecret"},
|
||||
},
|
||||
{
|
||||
name: "with username/password secret",
|
||||
turnConfig: config.TURN{URIs: []string{""}, UserLifetime: "1m", Username: "username", Password: "iAmSecret"},
|
||||
},
|
||||
{
|
||||
name: "only username set",
|
||||
turnConfig: config.TURN{URIs: []string{""}, UserLifetime: "1m", Username: "username"},
|
||||
wantEmptyResponse: true,
|
||||
},
|
||||
{
|
||||
name: "only password set",
|
||||
turnConfig: config.TURN{URIs: []string{""}, UserLifetime: "1m", Username: "username"},
|
||||
wantEmptyResponse: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rec := httptest.NewRecorder()
|
||||
req := httptest.NewRequest(http.MethodGet, "/_matrix/client/v3/voip/turnServer", strings.NewReader(""))
|
||||
req.Header.Set("Authorization", "Bearer "+accessTokens[alice])
|
||||
cfg.ClientAPI.TURN = tc.turnConfig
|
||||
routers.Client.ServeHTTP(rec, req)
|
||||
assert.Equal(t, http.StatusOK, rec.Code)
|
||||
|
||||
if tc.wantEmptyResponse && rec.Body.String() != "{}" {
|
||||
t.Fatalf("expected an empty response, but got %s", rec.Body.String())
|
||||
}
|
||||
if !tc.wantEmptyResponse {
|
||||
assert.NotEqual(t, "{}", rec.Body.String())
|
||||
|
||||
resp := gomatrix.RespTurnServer{}
|
||||
err := json.NewDecoder(rec.Body).Decode(&resp)
|
||||
assert.NoError(t, err)
|
||||
|
||||
duration, _ := time.ParseDuration(tc.turnConfig.UserLifetime)
|
||||
assert.Equal(t, tc.turnConfig.URIs, resp.URIs)
|
||||
assert.Equal(t, int(duration.Seconds()), resp.TTL)
|
||||
if tc.turnConfig.Username != "" && tc.turnConfig.Password != "" {
|
||||
assert.Equal(t, tc.turnConfig.Username, resp.Username)
|
||||
assert.Equal(t, tc.turnConfig.Password, resp.Password)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -17,26 +17,21 @@ package routing
|
|||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||
roomserverAPI "github.com/matrix-org/dendrite/roomserver/api"
|
||||
|
||||
"github.com/matrix-org/dendrite/roomserver/version"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/matrix-org/util"
|
||||
)
|
||||
|
||||
// GetCapabilities returns information about the server's supported feature set
|
||||
// and other relevant capabilities to an authenticated user.
|
||||
func GetCapabilities(
|
||||
req *http.Request, rsAPI roomserverAPI.ClientRoomserverAPI,
|
||||
) util.JSONResponse {
|
||||
roomVersionsQueryReq := roomserverAPI.QueryRoomVersionCapabilitiesRequest{}
|
||||
roomVersionsQueryRes := roomserverAPI.QueryRoomVersionCapabilitiesResponse{}
|
||||
if err := rsAPI.QueryRoomVersionCapabilities(
|
||||
req.Context(),
|
||||
&roomVersionsQueryReq,
|
||||
&roomVersionsQueryRes,
|
||||
); err != nil {
|
||||
util.GetLogger(req.Context()).WithError(err).Error("queryAPI.QueryRoomVersionCapabilities failed")
|
||||
return jsonerror.InternalServerError()
|
||||
func GetCapabilities() util.JSONResponse {
|
||||
versionsMap := map[gomatrixserverlib.RoomVersion]string{}
|
||||
for v, desc := range version.SupportedRoomVersions() {
|
||||
if desc.Stable {
|
||||
versionsMap[v] = "stable"
|
||||
} else {
|
||||
versionsMap[v] = "unstable"
|
||||
}
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
|
|
@ -44,7 +39,10 @@ func GetCapabilities(
|
|||
"m.change_password": map[string]bool{
|
||||
"enabled": true,
|
||||
},
|
||||
"m.room_versions": roomVersionsQueryRes,
|
||||
"m.room_versions": map[string]interface{}{
|
||||
"default": version.DefaultRoomVersion(),
|
||||
"available": versionsMap,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1194,7 +1194,7 @@ func Setup(
|
|||
if r := rateLimits.Limit(req, device); r != nil {
|
||||
return *r
|
||||
}
|
||||
return GetCapabilities(req, rsAPI)
|
||||
return GetCapabilities()
|
||||
}, httputil.WithAllowGuests()),
|
||||
).Methods(http.MethodGet, http.MethodOptions)
|
||||
|
||||
|
|
|
|||
|
|
@ -144,7 +144,6 @@ type ClientRoomserverAPI interface {
|
|||
QueryKnownUsers(ctx context.Context, req *QueryKnownUsersRequest, res *QueryKnownUsersResponse) error
|
||||
QueryRoomVersionForRoom(ctx context.Context, req *QueryRoomVersionForRoomRequest, res *QueryRoomVersionForRoomResponse) error
|
||||
QueryPublishedRooms(ctx context.Context, req *QueryPublishedRoomsRequest, res *QueryPublishedRoomsResponse) error
|
||||
QueryRoomVersionCapabilities(ctx context.Context, req *QueryRoomVersionCapabilitiesRequest, res *QueryRoomVersionCapabilitiesResponse) error
|
||||
|
||||
GetRoomIDForAlias(ctx context.Context, req *GetRoomIDForAliasRequest, res *GetRoomIDForAliasResponse) error
|
||||
GetAliasesForRoomID(ctx context.Context, req *GetAliasesForRoomIDRequest, res *GetAliasesForRoomIDResponse) error
|
||||
|
|
|
|||
|
|
@ -240,15 +240,6 @@ type QueryStateAndAuthChainResponse struct {
|
|||
IsRejected bool `json:"is_rejected"`
|
||||
}
|
||||
|
||||
// QueryRoomVersionCapabilitiesRequest asks for the default room version
|
||||
type QueryRoomVersionCapabilitiesRequest struct{}
|
||||
|
||||
// QueryRoomVersionCapabilitiesResponse is a response to QueryRoomVersionCapabilitiesRequest
|
||||
type QueryRoomVersionCapabilitiesResponse struct {
|
||||
DefaultRoomVersion gomatrixserverlib.RoomVersion `json:"default"`
|
||||
AvailableRoomVersions map[gomatrixserverlib.RoomVersion]string `json:"available"`
|
||||
}
|
||||
|
||||
// QueryRoomVersionForRoomRequest asks for the room version for a given room.
|
||||
type QueryRoomVersionForRoomRequest struct {
|
||||
RoomID string `json:"room_id"`
|
||||
|
|
|
|||
|
|
@ -35,7 +35,6 @@ import (
|
|||
"github.com/matrix-org/dendrite/roomserver/state"
|
||||
"github.com/matrix-org/dendrite/roomserver/storage"
|
||||
"github.com/matrix-org/dendrite/roomserver/types"
|
||||
"github.com/matrix-org/dendrite/roomserver/version"
|
||||
)
|
||||
|
||||
type Queryer struct {
|
||||
|
|
@ -694,25 +693,7 @@ func GetAuthChain(
|
|||
return authEvents, nil
|
||||
}
|
||||
|
||||
// QueryRoomVersionCapabilities implements api.RoomserverInternalAPI
|
||||
func (r *Queryer) QueryRoomVersionCapabilities(
|
||||
ctx context.Context,
|
||||
request *api.QueryRoomVersionCapabilitiesRequest,
|
||||
response *api.QueryRoomVersionCapabilitiesResponse,
|
||||
) error {
|
||||
response.DefaultRoomVersion = version.DefaultRoomVersion()
|
||||
response.AvailableRoomVersions = make(map[gomatrixserverlib.RoomVersion]string)
|
||||
for v, desc := range version.SupportedRoomVersions() {
|
||||
if desc.Stable {
|
||||
response.AvailableRoomVersions[v] = "stable"
|
||||
} else {
|
||||
response.AvailableRoomVersions[v] = "unstable"
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryRoomVersionCapabilities implements api.RoomserverInternalAPI
|
||||
// QueryRoomVersionForRoom implements api.RoomserverInternalAPI
|
||||
func (r *Queryer) QueryRoomVersionForRoom(
|
||||
ctx context.Context,
|
||||
request *api.QueryRoomVersionForRoomRequest,
|
||||
|
|
|
|||
Loading…
Reference in a new issue