aboutsummaryrefslogtreecommitdiff
path: root/roomserver/storage/tables/interface_test.go
blob: 8727e2436384905c0b45481294e9eef936c12534 (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
package tables

import (
	"testing"

	"github.com/matrix-org/dendrite/roomserver/types"
	"github.com/matrix-org/dendrite/test"
	"github.com/matrix-org/gomatrixserverlib/spec"
	"github.com/stretchr/testify/assert"
)

func TestExtractContentValue(t *testing.T) {
	alice := test.NewUser(t)
	room := test.NewRoom(t, alice)

	tests := []struct {
		name  string
		event *types.HeaderedEvent
		want  string
	}{
		{
			name:  "returns creator ID for create events",
			event: room.Events()[0],
			want:  alice.ID,
		},
		{
			name:  "returns the alias for canonical alias events",
			event: room.CreateEvent(t, alice, spec.MRoomCanonicalAlias, map[string]string{"alias": "#test:test"}),
			want:  "#test:test",
		},
		{
			name:  "returns the history_visibility for history visibility events",
			event: room.CreateEvent(t, alice, spec.MRoomHistoryVisibility, map[string]string{"history_visibility": "shared"}),
			want:  "shared",
		},
		{
			name:  "returns the join rules for join_rules events",
			event: room.CreateEvent(t, alice, spec.MRoomJoinRules, map[string]string{"join_rule": "public"}),
			want:  "public",
		},
		{
			name:  "returns the membership for room_member events",
			event: room.CreateEvent(t, alice, spec.MRoomMember, map[string]string{"membership": "join"}, test.WithStateKey(alice.ID)),
			want:  "join",
		},
		{
			name:  "returns the room name for room_name events",
			event: room.CreateEvent(t, alice, spec.MRoomName, map[string]string{"name": "testing"}, test.WithStateKey(alice.ID)),
			want:  "testing",
		},
		{
			name:  "returns the room avatar for avatar events",
			event: room.CreateEvent(t, alice, spec.MRoomAvatar, map[string]string{"url": "mxc://testing"}, test.WithStateKey(alice.ID)),
			want:  "mxc://testing",
		},
		{
			name:  "returns the room topic for topic events",
			event: room.CreateEvent(t, alice, spec.MRoomTopic, map[string]string{"topic": "testing"}, test.WithStateKey(alice.ID)),
			want:  "testing",
		},
		{
			name:  "returns guest_access for guest access events",
			event: room.CreateEvent(t, alice, "m.room.guest_access", map[string]string{"guest_access": "forbidden"}, test.WithStateKey(alice.ID)),
			want:  "forbidden",
		},
		{
			name:  "returns empty string if key can't be found or unknown event",
			event: room.CreateEvent(t, alice, "idontexist", nil),
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			assert.Equalf(t, tt.want, ExtractContentValue(tt.event), "ExtractContentValue(%v)", tt.event)
		})
	}
}