aboutsummaryrefslogtreecommitdiff
path: root/roomserver/auth/auth_test.go
blob: 192d9e5da73a1c8660edb01b85ff0f6f1ab5b6e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package auth

import (
	"context"
	"testing"

	"github.com/matrix-org/dendrite/roomserver/storage"
	"github.com/matrix-org/dendrite/test"
	"github.com/matrix-org/gomatrixserverlib"
	"github.com/matrix-org/gomatrixserverlib/spec"
)

type FakeStorageDB struct {
	storage.RoomDatabase
}

func (f *FakeStorageDB) GetUserIDForSender(ctx context.Context, roomID string, senderID spec.SenderID) (*spec.UserID, error) {
	return spec.NewUserID(string(senderID), true)
}

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.PDU
			for _, ev := range tt.roomFunc().Events() {
				authEvents = append(authEvents, ev.PDU)
			}

			if got := IsServerAllowed(context.Background(), &FakeStorageDB{}, tt.serverName, tt.serverCurrentlyInRoom, authEvents); got != tt.want {
				t.Errorf("IsServerAllowed() = %v, want %v", got, tt.want)
			}
		})
	}
}