mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-16 18:43:10 -06:00
Add auth test
This commit is contained in:
parent
c8dae9cfab
commit
2fcb6dfbec
|
|
@ -26,6 +26,10 @@ func IsServerAllowed(
|
||||||
serverCurrentlyInRoom bool,
|
serverCurrentlyInRoom bool,
|
||||||
authEvents []*gomatrixserverlib.Event,
|
authEvents []*gomatrixserverlib.Event,
|
||||||
) bool {
|
) bool {
|
||||||
|
// In practice should not happen, but avoids unneeded CPU cycles
|
||||||
|
if serverName == "" || len(authEvents) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
historyVisibility := HistoryVisibilityForRoom(authEvents)
|
historyVisibility := HistoryVisibilityForRoom(authEvents)
|
||||||
|
|
||||||
// 1. If the history_visibility was set to world_readable, allow.
|
// 1. If the history_visibility was set to world_readable, allow.
|
||||||
|
|
|
||||||
85
roomserver/auth/auth_test.go
Normal file
85
roomserver/auth/auth_test.go
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/matrix-org/dendrite/test"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib/spec"
|
||||||
|
)
|
||||||
|
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
var authEvents []*gomatrixserverlib.Event
|
||||||
|
for _, ev := range tt.roomFunc().Events() {
|
||||||
|
authEvents = append(authEvents, ev.Event)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got := IsServerAllowed(tt.serverName, tt.serverCurrentlyInRoom, authEvents); got != tt.want {
|
||||||
|
t.Errorf("IsServerAllowed() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue