summary refs log tree commit diff
path: root/src/web/routes/guild-settings.test.js
blob: fccc2660ad433c7296b18f396c14aa211b6aa8e3 (plain) (blame)
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
// @ts-check

const tryToCatch = require("try-to-catch")
const {router, test} = require("../../../test/web")
const {select} = require("../../passthrough")
const {MatrixServerError} = require("../../matrix/mreq")

test("web autocreate: checks permissions", async t => {
	const [error] = await tryToCatch(() => router.test("post", "/api/autocreate", {
		body: {
			guild_id: "66192955777486848"
		}
	}))
	t.equal(error.data, "Can't change settings for a guild you don't have Manage Server permissions in")
})


test("web autocreate: turns off autocreate and does htmx page refresh when guild not linked", async t => {
	const event = {}
	await router.test("post", "/api/autocreate", {
		sessionData: {
			managedGuilds: ["66192955777486848"]
		},
		body: {
			guild_id: "66192955777486848",
			// autocreate is false
		},
		headers: {
			"hx-request": "true"
		},
		event
	})
	t.equal(event.node.res.getHeader("hx-refresh"), "true")
	t.equal(select("guild_active", "autocreate", {guild_id: "66192955777486848"}).pluck().get(), 0)
})

test("web autocreate: turns on autocreate and issues 302 when not using htmx", async t => {
	const event = {}
	await router.test("post", "/api/autocreate", {
		sessionData: {
			managedGuilds: ["66192955777486848"]
		},
		body: {
			guild_id: "66192955777486848",
			autocreate: "yes"
		},
		event
	})
	t.equal(event.node.res.getHeader("location"), "")
	t.equal(select("guild_active", "autocreate", {guild_id: "66192955777486848"}).pluck().get(), 1)
})

test("web privacy level: checks permissions", async t => {
	const [error] = await tryToCatch(() => router.test("post", "/api/privacy-level", {
		body: {
			guild_id: "112760669178241024",
			privacy_level: "directory"
		}
	}))
	t.equal(error.data, "Can't change settings for a guild you don't have Manage Server permissions in")
})

test("web privacy level: updates privacy level", async t => {
	let called = 0
	await router.test("post", "/api/privacy-level", {
		sessionData: {
			managedGuilds: ["112760669178241024"]
		},
		body: {
			guild_id: "112760669178241024",
			privacy_level: "directory"
		},
		createSpace: {
			async syncSpaceFully(guildID) {
				called++
				t.equal(guildID, "112760669178241024")
				return ""
			}
		}
	})
	t.equal(called, 1)
	t.equal(select("guild_space", "privacy_level", {guild_id: "112760669178241024"}).pluck().get(), 2) // directory = 2
})

test("web presence: updates presence", async t => {
	await router.test("post", "/api/presence", {
		sessionData: {
			managedGuilds: ["112760669178241024"]
		},
		body: {
			guild_id: "112760669178241024"
			// presence is on by default - turn it off
		}
	})
	t.equal(select("guild_space", "presence", {guild_id: "112760669178241024"}).pluck().get(), 0)
})