2023-04-27 07:07:13 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2023-06-12 12:19:25 +01:00
|
|
|
"context"
|
2023-04-27 07:07:13 +01:00
|
|
|
"testing"
|
|
|
|
|
2023-06-14 15:23:46 +01:00
|
|
|
"github.com/matrix-org/dendrite/roomserver/api"
|
2023-04-27 07:07:13 +01:00
|
|
|
"github.com/matrix-org/dendrite/test"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib"
|
|
|
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
|
|
|
)
|
|
|
|
|
2023-06-14 15:23:46 +01:00
|
|
|
type FakeQuerier struct {
|
|
|
|
api.QuerySenderIDAPI
|
2023-06-12 12:19:25 +01:00
|
|
|
}
|
|
|
|
|
2023-06-14 15:23:46 +01:00
|
|
|
func (f *FakeQuerier) QueryUserIDForSender(ctx context.Context, roomID spec.RoomID, senderID spec.SenderID) (*spec.UserID, error) {
|
2023-06-12 12:19:25 +01:00
|
|
|
return spec.NewUserID(string(senderID), true)
|
|
|
|
}
|
|
|
|
|
2023-04-27 07:07:13 +01:00
|
|
|
func TestIsServerAllowed(t *testing.T) {
|
|
|
|
alice := test.NewUser(t)
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want bool
|
|
|
|
roomFunc func() *test.Room
|
|
|
|
serverName spec.ServerName
|
|
|
|
serverCurrentlyInRoom bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "no servername specified",
|
|
|
|
roomFunc: func() *test.Room { return test.NewRoom(t, alice) },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no authEvents specified",
|
|
|
|
serverName: "test",
|
|
|
|
roomFunc: func() *test.Room { return &test.Room{} },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "default denied",
|
|
|
|
serverName: "test2",
|
|
|
|
roomFunc: func() *test.Room { return test.NewRoom(t, alice) },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "world readable room",
|
|
|
|
serverName: "test",
|
|
|
|
roomFunc: func() *test.Room {
|
|
|
|
return test.NewRoom(t, alice, test.RoomHistoryVisibility(gomatrixserverlib.HistoryVisibilityWorldReadable))
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "allowed due to alice being joined",
|
|
|
|
serverName: "test",
|
|
|
|
roomFunc: func() *test.Room { return test.NewRoom(t, alice) },
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "allowed due to 'serverCurrentlyInRoom'",
|
|
|
|
serverName: "test2",
|
|
|
|
roomFunc: func() *test.Room { return test.NewRoom(t, alice) },
|
|
|
|
want: true,
|
|
|
|
serverCurrentlyInRoom: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "allowed due to pending invite",
|
|
|
|
serverName: "test2",
|
|
|
|
roomFunc: func() *test.Room {
|
|
|
|
bob := test.User{ID: "@bob:test2"}
|
|
|
|
r := test.NewRoom(t, alice, test.RoomHistoryVisibility(gomatrixserverlib.HistoryVisibilityInvited))
|
|
|
|
r.CreateAndInsert(t, alice, spec.MRoomMember, map[string]interface{}{
|
|
|
|
"membership": spec.Invite,
|
|
|
|
}, test.WithStateKey(bob.ID))
|
|
|
|
return r
|
|
|
|
},
|
|
|
|
want: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
if tt.roomFunc == nil {
|
|
|
|
t.Fatalf("missing roomFunc")
|
|
|
|
}
|
2023-05-02 15:03:16 +01:00
|
|
|
var authEvents []gomatrixserverlib.PDU
|
2023-04-27 07:07:13 +01:00
|
|
|
for _, ev := range tt.roomFunc().Events() {
|
2023-05-02 15:03:16 +01:00
|
|
|
authEvents = append(authEvents, ev.PDU)
|
2023-04-27 07:07:13 +01:00
|
|
|
}
|
|
|
|
|
2023-06-14 15:23:46 +01:00
|
|
|
if got := IsServerAllowed(context.Background(), &FakeQuerier{}, tt.serverName, tt.serverCurrentlyInRoom, authEvents); got != tt.want {
|
2023-04-27 07:07:13 +01:00
|
|
|
t.Errorf("IsServerAllowed() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|