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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
// @ts-check
const DiscordTypes = require("discord-api-types/v10")
const tryToCatch = require("try-to-catch")
const {router, test} = require("../../../test/web")
const {MatrixServerError} = require("../../matrix/mreq")
const {_getPosition} = require("./guild")
let nonce
test("web guild: access denied when not logged in", async t => {
const html = await router.test("get", "/guild?guild_id=112760669178241024", {
sessionData: {
},
})
t.has(html, "You need to log in to manage your servers.")
})
test("web guild: asks to select guild if not selected", async t => {
const html = await router.test("get", "/guild", {
sessionData: {
userID: "1",
managedGuilds: []
},
})
t.has(html, "Select a server from the top right corner to continue.")
})
test("web guild: access denied when guild id messed up", async t => {
const html = await router.test("get", "/guild?guild_id=1", {
sessionData: {
userID: "1",
managedGuilds: []
},
})
t.has(html, "the selected server doesn't exist")
})
test("web qr: access denied when guild id messed up", async t => {
const html = await router.test("get", "/qr?guild_id=1", {
sessionData: {
userID: "1",
managedGuilds: []
},
})
t.has(html, "the selected server doesn't exist")
})
test("web invite: access denied with invalid nonce", async t => {
const html = await router.test("get", "/invite?nonce=1")
t.match(html, /This QR code has expired./)
})
test("web guild: can view unbridged guild", async t => {
const html = await router.test("get", "/guild?guild_id=66192955777486848", {
sessionData: {
managedGuilds: ["66192955777486848"]
}
})
t.has(html, `<h1 class="s-page-title--header">Function & Arg</h1>`)
})
test("web guild: unbridged self-service guild prompts log in to matrix", async t => {
const html = await router.test("get", "/guild?guild_id=665289423482519565", {
sessionData: {
managedGuilds: ["665289423482519565"]
}
})
t.has(html, `You picked self-service mode`)
t.has(html, `You need to log in with Matrix first`)
})
test("web guild: unbridged self-service guild asks to be invited", async t => {
const html = await router.test("get", "/guild?guild_id=665289423482519565", {
sessionData: {
mxid: "@user:example.org",
managedGuilds: ["665289423482519565"]
}
})
t.has(html, `On Matrix, invite <`)
})
test("web guild: unbridged self-service guild shows available spaces", async t => {
const html = await router.test("get", "/guild?guild_id=665289423482519565", {
sessionData: {
mxid: "@cadence:cadence.moe",
managedGuilds: ["665289423482519565"]
}
})
t.has(html, `<strong>Data Horde</strong>`)
t.has(html, `<li>here is the space topic</li>`)
t.has(html, `<img class="s-avatar--image" src="https://bridge.example.org/download/matrix/cadence.moe/TLqQOsTSrZkVKwBSWYTZNTrw" alt="">`)
t.notMatch(html, /<strong>some room<\/strong>/)
t.notMatch(html, /<strong>somebody else's space<\/strong>/)
})
test("web guild: can view bridged guild when logged in with discord", async t => {
const html = await router.test("get", "/guild?guild_id=112760669178241024", {
sessionData: {
managedGuilds: ["112760669178241024"]
},
api: {
async getFullHierarchy(roomID) {
return []
}
}
})
t.has(html, `<h1 class="s-page-title--header">Psychonauts 3</h1>`)
})
test("web guild: can view bridged guild when logged in with matrix", async t => {
const html = await router.test("get", "/guild?guild_id=112760669178241024", {
sessionData: {
mxid: "@cadence:cadence.moe"
},
api: {
async getFullHierarchy(roomID) {
return []
}
}
})
t.has(html, `<h1 class="s-page-title--header">Psychonauts 3</h1>`)
})
test("web qr: generates nonce", async t => {
const html = await router.test("get", "/qr?guild_id=112760669178241024", {
sessionData: {
managedGuilds: ["112760669178241024"]
}
})
nonce = html.match(/data-nonce="([a-f0-9-]+)"/)?.[1]
t.ok(nonce)
})
test("web invite: page loads with valid nonce", async t => {
const html = await router.test("get", `/invite?nonce=${nonce}`)
t.has(html, "Invite a Matrix user")
})
test("api invite: access denied with nothing", async t => {
const [error] = await tryToCatch(() =>
router.test("post", `/api/invite`, {
body: {
mxid: "@cadence:cadence.moe",
permissions: "moderator"
}
})
)
t.equal(error.message, "Missing guild ID")
})
test("api invite: access denied when not in guild", async t => {
const [error] = await tryToCatch(() =>
router.test("post", `/api/invite`, {
body: {
mxid: "@cadence:cadence.moe",
permissions: "moderator",
guild_id: "112760669178241024"
}
})
)
t.equal(error.message, "Forbidden")
})
test("api invite: can invite with valid nonce", async t => {
let called = 0
const [error] = await tryToCatch(() =>
router.test("post", `/api/invite`, {
body: {
mxid: "@cadence:cadence.moe",
permissions: "moderator",
nonce
},
api: {
async getStateEvent(roomID, type, key) {
called++
if (type === "m.room.member" && key === "@cadence:cadence.moe") {
throw new Error("event not found")
} else if (type === "m.room.power_levels" && key === "") {
return {}
}
/* c8 ignore next */
t.fail(`unexpected getStateEvent call. roomID: ${roomID}, type: ${type}, key: ${key}`)
},
async getStateEventOuter(roomID, type, key) {
called++
return {
type: "m.room.create",
state_key: "",
sender: "@_ooye_bot:cadence.moe",
event_id: "$create",
origin_server_ts: 0,
room_id: roomID,
content: {
room_version: "11"
}
}
},
async inviteToRoom(roomID, mxidToInvite, mxid) {
called++
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
},
async *generateFullHierarchy(spaceID) {
called++
yield {
room_id: "!hierarchy",
children_state: [],
guest_can_join: false,
num_joined_members: 2,
}
},
async sendState(roomID, type, key, content) {
called++
t.ok(["!hierarchy", "!jjmvBegULiLucuWEHU:cadence.moe"].includes(roomID), `expected room ID to be in hierarchy, but was ${roomID}`)
t.equal(type, "m.room.power_levels")
t.equal(key, "")
t.deepEqual(content, {
users: {"@cadence:cadence.moe": 50}
})
return "$updated"
}
}
})
)
t.notOk(error)
/*
1. get membership
2. invite to room
set power:
3. generate hierarchy
4-5. calculate powers
6. send state
7-8. calculate powers
9. send state
*/
t.equal(called, 9) // get membership +
})
test("api invite: access denied when nonce has been used", async t => {
const [error] = await tryToCatch(() =>
router.test("post", `/api/invite`, {
body: {
mxid: "@cadence:cadence.moe",
permissions: "moderator",
nonce
}
})
)
t.equal(error.message, "Nonce expired")
})
test("api invite: can invite to a moderated guild", async t => {
let called = 0
const [error] = await tryToCatch(() =>
router.test("post", `/api/invite`, {
body: {
mxid: "@cadence:cadence.moe",
permissions: "admin",
guild_id: "112760669178241024"
},
sessionData: {
managedGuilds: ["112760669178241024"]
},
api: {
async getStateEvent(roomID, type, key) {
called++
if (type === "m.room.member" && key === "@cadence:cadence.moe") {
return {membership: "leave"}
} else if (type === "m.room.power_levels" && key === "") {
return {}
}
/* c8 ignore next */
t.fail(`unexpected getStateEvent call. roomID: ${roomID}, type: ${type}, key: ${key}`)
},
async getStateEventOuter(roomID, type, key) {
called++
return {
type: "m.room.create",
state_key: "",
sender: "@_ooye_bot:cadence.moe",
event_id: "$create",
origin_server_ts: 0,
room_id: roomID,
content: {
room_version: "11"
}
}
},
async inviteToRoom(roomID, mxidToInvite, mxid) {
called++
t.equal(roomID, "!jjmvBegULiLucuWEHU:cadence.moe")
},
async *generateFullHierarchy(spaceID) {
called++
yield {
room_id: "!hierarchy",
children_state: [],
guest_can_join: false,
num_joined_members: 2,
}
yield {
room_id: spaceID,
children_state: [],
guest_can_join: false,
num_joined_members: 2,
room_type: "m.space"
}
},
async sendState(roomID, type, key, content) {
called++
t.ok(["!hierarchy", "!jjmvBegULiLucuWEHU:cadence.moe"].includes(roomID), `expected room ID to be in hierarchy, but was ${roomID}`)
t.equal(type, "m.room.power_levels")
t.equal(key, "")
t.deepEqual(content, {
users: {"@cadence:cadence.moe": 100}
})
return "$updated"
}
}
})
)
t.notOk(error)
t.equal(called, 9)
})
test("api invite: does not reinvite joined users", async t => {
let called = 0
const [error] = await tryToCatch(() =>
router.test("post", `/api/invite`, {
body: {
mxid: "@cadence:cadence.moe",
permissions: "default",
guild_id: "112760669178241024"
},
sessionData: {
managedGuilds: ["112760669178241024"]
},
api: {
async getStateEvent(roomID, type, key) {
called++
return {membership: "join"}
}
}
})
)
t.notOk(error)
t.equal(called, 1)
})
test("position sorting: sorts like discord does", t => {
const channelsList = [{
type: DiscordTypes.ChannelType.GuildText,
id: "first",
position: 0
}, {
type: DiscordTypes.ChannelType.PublicThread,
id: "thread",
parent_id: "first",
}, {
type: DiscordTypes.ChannelType.GuildText,
id: "second",
position: 1
}, {
type: DiscordTypes.ChannelType.GuildVoice,
id: "voice",
position: 0
}, {
type: DiscordTypes.ChannelType.GuildCategory,
id: "category",
position: 0
}, {
type: DiscordTypes.ChannelType.GuildText,
id: "category-first",
parent_id: "category",
position: 0
}, {
type: DiscordTypes.ChannelType.GuildText,
id: "category-second",
parent_id: "category",
position: 1
}, {
type: DiscordTypes.ChannelType.PublicThread,
id: "category-second-thread",
parent_id: "category-second",
}].reverse()
const channels = new Map(channelsList.map(c => [c.id, c]))
const sortedChannelIDs = [...channels.values()].sort((a, b) => _getPosition(a, channels) - _getPosition(b, channels)).map(c => c.id)
t.deepEqual(sortedChannelIDs, ["first", "thread", "second", "voice", "category", "category-first", "category-second", "category-second-thread"])
})
|