This repository has been archived by the owner on Mar 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpermissions_test.go
110 lines (94 loc) · 2.7 KB
/
permissions_test.go
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
package dstate
import (
"testing"
"github.com/jonas747/discordgo"
)
func TestGuildPermissions(t *testing.T) {
gs := &GuildState{
ID: 1,
OwnerID: 0,
}
roles := []discordgo.Role{
{
Name: "Admin",
ID: 10,
Permissions: discordgo.PermissionAdministrator,
},
{
Name: "Mod",
ID: 11,
Permissions: discordgo.PermissionBanMembers | discordgo.PermissionManageNicknames | discordgo.PermissionManageMessages,
},
{
Name: "everyone",
ID: 1,
Permissions: discordgo.PermissionSendMessages,
},
}
perms := CalculatePermissions(gs, roles, nil, 100, []int64{10})
expectPerms(t, perms, AllPermissions)
perms = CalculatePermissions(gs, roles, nil, 100, []int64{})
expectPerms(t, perms, discordgo.PermissionSendMessages)
perms = CalculatePermissions(gs, roles, nil, 100, []int64{1111})
expectPerms(t, perms, discordgo.PermissionSendMessages)
perms = CalculatePermissions(gs, roles, nil, 100, []int64{11})
expectPerms(t, perms, discordgo.PermissionBanMembers|discordgo.PermissionManageNicknames|discordgo.PermissionManageMessages|discordgo.PermissionSendMessages)
}
func TestChannelPermissions(t *testing.T) {
gs := &GuildState{
ID: 1,
OwnerID: 0,
}
roles := []discordgo.Role{
{
Name: "Admin",
ID: 10,
Permissions: discordgo.PermissionAdministrator,
},
{
Name: "Mod",
ID: 11,
Permissions: discordgo.PermissionBanMembers | discordgo.PermissionManageNicknames | discordgo.PermissionManageMessages,
},
{
Name: "Muted",
ID: 12,
Permissions: 0,
},
{
Name: "everyone",
ID: 1,
Permissions: discordgo.PermissionSendMessages,
},
}
overwrites := []discordgo.PermissionOverwrite{
{
Type: "role",
ID: 12,
Deny: discordgo.PermissionSendMessages,
Allow: discordgo.PermissionAdministrator,
},
{
Type: "member",
ID: 100,
Allow: discordgo.PermissionEmbedLinks,
Deny: discordgo.PermissionAddReactions,
},
{
Type: "role",
ID: 1,
Allow: discordgo.PermissionSendMessages | discordgo.PermissionAddReactions,
},
}
perms := CalculatePermissions(gs, roles, overwrites, 101, []int64{12})
expectPerms(t, perms, discordgo.PermissionAddReactions)
perms = CalculatePermissions(gs, roles, overwrites, 100, []int64{12})
expectPerms(t, perms, discordgo.PermissionEmbedLinks)
perms = CalculatePermissions(gs, roles, overwrites, 100, []int64{})
expectPerms(t, perms, discordgo.PermissionEmbedLinks|discordgo.PermissionSendMessages)
}
func expectPerms(t *testing.T, actual int64, expected int64) {
if actual != expected {
t.Fatalf("incorrect perms, got: %d, expected: %d", actual, expected)
}
}