mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-17 02:53:11 -06:00
Add join room tests
This commit is contained in:
parent
8a5095c0b0
commit
ea64dc381e
|
|
@ -15,6 +15,8 @@
|
|||
package clientapi
|
||||
|
||||
import (
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
||||
appserviceAPI "github.com/matrix-org/dendrite/appservice/api"
|
||||
"github.com/matrix-org/dendrite/clientapi/api"
|
||||
"github.com/matrix-org/dendrite/clientapi/producers"
|
||||
|
|
@ -26,7 +28,6 @@ import (
|
|||
"github.com/matrix-org/dendrite/setup/base"
|
||||
"github.com/matrix-org/dendrite/setup/jetstream"
|
||||
userapi "github.com/matrix-org/dendrite/userapi/api"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
)
|
||||
|
||||
// AddPublicRoutes sets up and registers HTTP handlers for the ClientAPI component.
|
||||
|
|
|
|||
95
clientapi/routing/joinroom_test.go
Normal file
95
clientapi/routing/joinroom_test.go
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
package routing
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
||||
"github.com/matrix-org/dendrite/appservice"
|
||||
"github.com/matrix-org/dendrite/keyserver"
|
||||
"github.com/matrix-org/dendrite/roomserver"
|
||||
"github.com/matrix-org/dendrite/test"
|
||||
"github.com/matrix-org/dendrite/test/testrig"
|
||||
"github.com/matrix-org/dendrite/userapi"
|
||||
uapi "github.com/matrix-org/dendrite/userapi/api"
|
||||
)
|
||||
|
||||
func TestJoinRoomByIDOrAlias(t *testing.T) {
|
||||
alice := test.NewUser(t)
|
||||
bob := test.NewUser(t)
|
||||
charlie := test.NewUser(t, test.WithAccountType(uapi.AccountTypeGuest))
|
||||
|
||||
ctx := context.Background()
|
||||
test.WithAllDatabases(t, func(t *testing.T, dbType test.DBType) {
|
||||
base, baseClose := testrig.CreateBaseDendrite(t, dbType)
|
||||
defer baseClose()
|
||||
|
||||
rsAPI := roomserver.NewInternalAPI(base)
|
||||
keyAPI := keyserver.NewInternalAPI(base, &base.Cfg.KeyServer, nil)
|
||||
userAPI := userapi.NewInternalAPI(base, &base.Cfg.UserAPI, nil, keyAPI, rsAPI, nil)
|
||||
asAPI := appservice.NewInternalAPI(base, userAPI, rsAPI)
|
||||
rsAPI.SetFederationAPI(nil, nil) // creates the rs.Inputer etc
|
||||
|
||||
// Create the users in the userapi
|
||||
for _, u := range []*test.User{alice, bob, charlie} {
|
||||
localpart, serverName, _ := gomatrixserverlib.SplitID('@', u.ID)
|
||||
userRes := &uapi.PerformAccountCreationResponse{}
|
||||
if err := userAPI.PerformAccountCreation(ctx, &uapi.PerformAccountCreationRequest{
|
||||
AccountType: u.AccountType,
|
||||
Localpart: localpart,
|
||||
ServerName: serverName,
|
||||
Password: "someRandomPassword",
|
||||
}, userRes); err != nil {
|
||||
t.Errorf("failed to create account: %s", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
aliceDev := &uapi.Device{UserID: alice.ID}
|
||||
bobDev := &uapi.Device{UserID: bob.ID}
|
||||
charlieDev := &uapi.Device{UserID: charlie.ID, AccountType: uapi.AccountTypeGuest}
|
||||
|
||||
// create the room
|
||||
resp := createRoom(ctx, createRoomRequest{
|
||||
Name: "testing",
|
||||
IsDirect: true,
|
||||
Topic: "testing",
|
||||
Visibility: "public",
|
||||
Preset: presetPublicChat,
|
||||
RoomAliasName: "alias",
|
||||
Invite: []string{bob.ID},
|
||||
GuestCanJoin: false,
|
||||
}, aliceDev, &base.Cfg.ClientAPI, userAPI, rsAPI, asAPI, time.Now())
|
||||
crResp, ok := resp.JSON.(createRoomResponse)
|
||||
if !ok {
|
||||
t.Fatalf("response is not a createRoomResponse: %+v", resp)
|
||||
}
|
||||
|
||||
// Dummy request
|
||||
body := &bytes.Buffer{}
|
||||
req, err := http.NewRequest(http.MethodPost, "/?server_name=test", body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Bob can join the room as usual
|
||||
joinResp := JoinRoomByIDOrAlias(req, bobDev, rsAPI, userAPI, crResp.RoomAlias)
|
||||
if !joinResp.Is2xx() {
|
||||
t.Fatalf("expected join to succeed, but didn't: %+v", joinResp)
|
||||
}
|
||||
|
||||
// Charlie is a guest, and guests are prohibited to join the room
|
||||
joinResp = JoinRoomByIDOrAlias(req, charlieDev, rsAPI, userAPI, crResp.RoomID)
|
||||
if joinResp.Is2xx() {
|
||||
t.Fatalf("expected join to fail, but didn't: %+v", joinResp)
|
||||
}
|
||||
|
||||
if joinResp.Code != http.StatusForbidden {
|
||||
t.Fatalf("expected response code to be %d, got %d", http.StatusForbidden, joinResp.Code)
|
||||
}
|
||||
|
||||
})
|
||||
}
|
||||
Loading…
Reference in a new issue