Skip to content

Commit 2ea38f3

Browse files
room creation cleanup (#277)
1 parent 788b35e commit 2ea38f3

File tree

1 file changed

+100
-55
lines changed

1 file changed

+100
-55
lines changed
Lines changed: 100 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package csapi_tests
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/tidwall/gjson"
@@ -11,153 +12,197 @@ import (
1112
"github.com/matrix-org/complement/internal/must"
1213
)
1314

15+
func doCreateRoom(t *testing.T, c *client.CSAPI, json map[string]interface{}, match match.HTTPResponse) {
16+
res := c.DoFunc(t, "POST", []string{"_matrix", "client", "r0", "createRoom"}, client.WithJSONBody(t, json))
17+
must.MatchResponse(t, res, match)
18+
}
19+
1420
func TestRoomCreate(t *testing.T) {
15-
deployment := Deploy(t, b.BlueprintAlice)
21+
deployment := Deploy(t, b.BlueprintOneToOneRoom)
1622
defer deployment.Destroy(t)
17-
authedClient := deployment.Client(t, "hs1", "@alice:hs1")
23+
24+
alice := deployment.Client(t, "hs1", "@alice:hs1")
25+
bob := deployment.Client(t, "hs1", "@bob:hs1")
26+
1827
t.Run("Parallel", func(t *testing.T) {
1928
// sytest: POST /createRoom makes a public room
2029
t.Run("POST /createRoom makes a public room", func(t *testing.T) {
2130
t.Parallel()
22-
reqBody := client.WithJSONBody(t, map[string]interface{}{
31+
roomAlias := "30-room-create-alias-random"
32+
33+
doCreateRoom(t, alice, map[string]interface{}{
2334
"visibility": "public",
24-
"room_alias_name": "30-room-create-alias-random",
25-
})
26-
res := authedClient.MustDoFunc(t, "POST", []string{"_matrix", "client", "r0", "createRoom"}, reqBody)
27-
must.MatchResponse(t, res, match.HTTPResponse{
35+
"room_alias_name": roomAlias,
36+
}, match.HTTPResponse{
37+
StatusCode: 200,
2838
JSON: []match.JSON{
29-
match.JSONKeyPresent("room_id"),
30-
match.JSONKeyPresent("room_alias"),
39+
match.JSONKeyEqual("room_alias", fmt.Sprintf("#%s:hs1", roomAlias)),
3140
match.JSONKeyTypeEqual("room_id", gjson.String),
3241
},
3342
})
3443
})
3544
// sytest: POST /createRoom makes a private room
3645
t.Run("POST /createRoom makes a private room", func(t *testing.T) {
3746
t.Parallel()
38-
reqBody := client.WithJSONBody(t, map[string]interface{}{
47+
48+
doCreateRoom(t, alice, map[string]interface{}{
3949
"visibility": "private",
40-
})
41-
res := authedClient.MustDoFunc(t, "POST", []string{"_matrix", "client", "r0", "createRoom"}, reqBody)
42-
must.MatchResponse(t, res, match.HTTPResponse{
50+
}, match.HTTPResponse{
51+
StatusCode: 200,
4352
JSON: []match.JSON{
44-
match.JSONKeyPresent("room_id"),
4553
match.JSONKeyTypeEqual("room_id", gjson.String),
4654
},
4755
})
4856
})
4957
// sytest: POST /createRoom makes a room with a topic
5058
t.Run("POST /createRoom makes a room with a topic", func(t *testing.T) {
5159
t.Parallel()
52-
roomID := authedClient.CreateRoom(t, map[string]interface{}{
60+
61+
roomID := alice.CreateRoom(t, map[string]interface{}{
5362
"topic": "Test Room",
5463
"preset": "public_chat",
5564
})
56-
res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.topic"})
65+
res := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.topic"})
5766
must.MatchResponse(t, res, match.HTTPResponse{
67+
StatusCode: 200,
5868
JSON: []match.JSON{
59-
match.JSONKeyPresent("topic"),
60-
match.JSONKeyTypeEqual("topic", gjson.String),
6169
match.JSONKeyEqual("topic", "Test Room"),
6270
},
6371
})
6472
})
6573
// sytest: POST /createRoom makes a room with a name
6674
t.Run("POST /createRoom makes a room with a name", func(t *testing.T) {
6775
t.Parallel()
68-
roomID := authedClient.CreateRoom(t, map[string]interface{}{
76+
roomID := alice.CreateRoom(t, map[string]interface{}{
6977
"name": "Test Room",
7078
"preset": "public_chat",
7179
})
72-
res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.name"})
80+
res := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.name"})
7381
must.MatchResponse(t, res, match.HTTPResponse{
82+
StatusCode: 200,
7483
JSON: []match.JSON{
75-
match.JSONKeyPresent("name"),
76-
match.JSONKeyTypeEqual("name", gjson.String),
7784
match.JSONKeyEqual("name", "Test Room"),
7885
},
7986
})
8087
})
8188
// sytest: POST /createRoom creates a room with the given version
8289
t.Run("POST /createRoom creates a room with the given version", func(t *testing.T) {
8390
t.Parallel()
84-
roomID := authedClient.CreateRoom(t, map[string]interface{}{
91+
roomID := alice.CreateRoom(t, map[string]interface{}{
8592
"room_version": "2",
8693
"preset": "public_chat",
8794
})
88-
res := authedClient.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.create"})
95+
res := alice.MustDoFunc(t, "GET", []string{"_matrix", "client", "r0", "rooms", roomID, "state", "m.room.create"})
8996
must.MatchResponse(t, res, match.HTTPResponse{
97+
StatusCode: 200,
9098
JSON: []match.JSON{
91-
match.JSONKeyPresent("room_version"),
92-
match.JSONKeyTypeEqual("room_version", gjson.String),
9399
match.JSONKeyEqual("room_version", "2"),
94100
},
95101
})
96102
})
97103
// sytest: POST /createRoom makes a private room with invites
98104
t.Run("POST /createRoom makes a private room with invites", func(t *testing.T) {
99105
t.Parallel()
100-
userInvite := deployment.RegisterUser(t, "hs1", "create_room", "superuser")
101-
reqBody := client.WithJSONBody(t, map[string]interface{}{
106+
107+
doCreateRoom(t, alice, map[string]interface{}{
102108
"visibility": "private",
103-
"invite": []string{userInvite.UserID},
104-
})
105-
res := authedClient.MustDoFunc(t, "POST", []string{"_matrix", "client", "r0", "createRoom"}, reqBody)
106-
must.MatchResponse(t, res, match.HTTPResponse{
109+
"invite": []string{bob.UserID},
110+
}, match.HTTPResponse{
111+
StatusCode: 200,
107112
JSON: []match.JSON{
108-
match.JSONKeyPresent("room_id"),
109113
match.JSONKeyTypeEqual("room_id", gjson.String),
110114
},
111115
})
112116
})
113117
// sytest: POST /createRoom rejects attempts to create rooms with numeric versions
114118
t.Run("POST /createRoom rejects attempts to create rooms with numeric versions", func(t *testing.T) {
115119
t.Parallel()
116-
reqBody := client.WithJSONBody(t, map[string]interface{}{
120+
121+
doCreateRoom(t, alice, map[string]interface{}{
117122
"visibility": "private",
118123
"room_version": 1,
119124
"preset": "public_chat",
120-
})
121-
res := authedClient.DoFunc(t, "POST", []string{"_matrix", "client", "r0", "createRoom"}, reqBody)
122-
must.MatchResponse(t, res, match.HTTPResponse{
125+
}, match.HTTPResponse{
123126
StatusCode: 400,
124127
JSON: []match.JSON{
125-
match.JSONKeyPresent("errcode"),
126128
match.JSONKeyEqual("errcode", "M_BAD_JSON"),
127129
},
128130
})
129131
})
130132
// sytest: POST /createRoom rejects attempts to create rooms with unknown versions
131133
t.Run("POST /createRoom rejects attempts to create rooms with unknown versions", func(t *testing.T) {
132134
t.Parallel()
133-
reqBody := client.WithJSONBody(t, map[string]interface{}{
135+
136+
doCreateRoom(t, alice, map[string]interface{}{
134137
"visibility": "private",
135138
"room_version": "ahfgwjyerhgiuveisbruvybseyrugvi",
136139
"preset": "public_chat",
137-
})
138-
res := authedClient.DoFunc(t, "POST", []string{"_matrix", "client", "r0", "createRoom"}, reqBody)
139-
must.MatchResponse(t, res, match.HTTPResponse{
140+
}, match.HTTPResponse{
140141
StatusCode: 400,
141142
JSON: []match.JSON{
142-
match.JSONKeyPresent("errcode"),
143143
match.JSONKeyEqual("errcode", "M_UNSUPPORTED_ROOM_VERSION"),
144144
},
145145
})
146146
})
147-
})
148-
}
147+
// sytest: Rooms can be created with an initial invite list (SYN-205)
148+
t.Run("Rooms can be created with an initial invite list (SYN-205)", func(t *testing.T) {
149+
roomID := alice.CreateRoom(t, map[string]interface{}{
150+
"invite": []string{bob.UserID},
151+
})
149152

150-
// sytest: Rooms can be created with an initial invite list (SYN-205)
151-
func TestRoomCreateWithInvites(t *testing.T) {
152-
deployment := Deploy(t, b.BlueprintOneToOneRoom)
153-
defer deployment.Destroy(t)
153+
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncInvitedTo(bob.UserID, roomID))
154+
})
154155

155-
alice := deployment.Client(t, "hs1", "@alice:hs1")
156-
bob := deployment.Client(t, "hs1", "@bob:hs1")
156+
// sytest: Can /sync newly created room
157+
t.Run("Can /sync newly created room", func(t *testing.T) {
158+
roomID := alice.CreateRoom(t, map[string]interface{}{})
157159

158-
roomID := alice.CreateRoom(t, map[string]interface{}{
159-
"invite": []string{bob.UserID},
160-
})
160+
// This will do the syncing for us
161+
alice.SendEventSynced(t, roomID, b.Event{
162+
Type: "m.room.test",
163+
Content: map[string]interface{}{},
164+
})
165+
})
161166

162-
bob.MustSyncUntil(t, client.SyncReq{}, client.SyncInvitedTo(bob.UserID, roomID))
167+
// sytest: POST /createRoom ignores attempts to set the room version via creation_content
168+
t.Run("POST /createRoom ignores attempts to set the room version via creation_content", func(t *testing.T) {
169+
roomID := alice.CreateRoom(t, map[string]interface{}{
170+
"creation_content": map[string]interface{}{
171+
"test": "azerty",
172+
"room_version": "test",
173+
},
174+
})
175+
176+
// Wait until user has joined the room
177+
alice.MustSyncUntil(t, client.SyncReq{}, client.SyncJoinedTo(alice.UserID, roomID))
178+
179+
// Get ordered timeline via a full sync
180+
res, _ := alice.MustSync(t, client.SyncReq{})
181+
182+
roomObj := res.Get("rooms.join." + client.GjsonEscape(roomID))
183+
184+
if !roomObj.Exists() {
185+
t.Fatalf("Room did not appear in sync")
186+
}
187+
188+
event0 := roomObj.Get("timeline.events.0")
189+
190+
if !event0.Exists() {
191+
t.Fatalf("First timeline event does not exist")
192+
}
193+
194+
if event0.Get("type").Str != "m.room.create" {
195+
t.Fatalf("First event was not m.room.create: %s", event0)
196+
}
197+
if !event0.Get("content.room_version").Exists() {
198+
t.Fatalf("Room creation event did not have room version: %s", event0)
199+
}
200+
if event0.Get("content.room_version").Str == "test" {
201+
t.Fatalf("Room creation event room_version was a bogus room version: %s", event0)
202+
}
203+
if event0.Get("content.test").Str != "azerty" {
204+
t.Fatalf("Room creation event content 'test' key did not have expected value of 'azerty': %s", event0)
205+
}
206+
})
207+
})
163208
}

0 commit comments

Comments
 (0)