forked from andersfylling/disgord
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permissions_test.go
72 lines (61 loc) · 1.65 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
package disgord
import (
"context"
"testing"
"github.com/andersfylling/disgord/json"
)
var fakePermissionsRole = &Role{ID: 10, Permissions: 2048}
type permissionTestingSession struct {
getFakeRole bool
}
type permissionTestingGuildBuilder struct {
guildQueryBuilderNop
p *permissionTestingSession
}
func (p permissionTestingGuildBuilder) WithContext(_ context.Context) GuildQueryBuilder {
return &p
}
func (p permissionTestingSession) Guild(_ Snowflake) GuildQueryBuilder {
return &permissionTestingGuildBuilder{p: &p}
}
func (p permissionTestingGuildBuilder) GetRoles(_ ...Flag) ([]*Role, error) {
if p.p.getFakeRole {
return []*Role{fakePermissionsRole}, nil
}
return []*Role{}, nil
}
func TestChannel_GetPermissions_Overwrite(t *testing.T) {
data := []byte(`{"permission_overwrites": [{"allow":"2048", "deny": "0", "id": "1", "type": 1}]}`)
var c Channel
if err := json.Unmarshal(data, &c); err != nil {
t.Fatal(err)
}
executeInternalUpdater(c)
p, err := c.GetPermissions(context.TODO(), &permissionTestingSession{}, &Member{UserID: 1, Roles: []Snowflake{}})
if err != nil {
t.Fatal(err)
}
if p != 2048 {
t.Fatal("permissions should be 2048, is", p)
}
}
func TestMember_GetPermissions(t *testing.T) {
m := &Member{UserID: 1, Roles: []Snowflake{}}
s := &permissionTestingSession{}
p, err := m.GetPermissions(context.TODO(), s)
if err != nil {
t.Fatal(err)
}
if p != 0 {
t.Fatal("permissions should be 0, is", p)
}
s.getFakeRole = true
m.Roles = append(m.Roles, fakePermissionsRole.ID)
p, err = m.GetPermissions(context.TODO(), s)
if err != nil {
t.Fatal(err)
}
if p != 2048 {
t.Fatal("permissions should be 2048, is", p)
}
}