forked from pote/philote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess_key_test.go
61 lines (48 loc) · 1.43 KB
/
access_key_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
package main
import (
"testing"
"github.com/dgrijalva/jwt-go"
)
func TestNewAccessKey(t *testing.T) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"read": []string{"test-channel"},
"write": []string{"test-channel"},
})
tokenString, err := token.SignedString(Config.jwtSecret)
if err != nil {
t.Fatal(err)
}
ak, err := NewAccessKey(tokenString)
if err != nil {
t.Fatal(err)
}
if len(ak.Read) < 1 || ak.Read[0] != "test-channel" {
t.Error("Access Key does not have proper read permissions")
}
if len(ak.Write) < 1 || ak.Write[0] != "test-channel" {
t.Error("Access Key does not have proper write permissions")
}
}
func TestMultiChannelAccessKey(t *testing.T) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"read": []string{"test-channel", "read-channel"},
"write": []string{"test-channel", "write-channel"},
})
tokenString, err := token.SignedString(Config.jwtSecret)
if err != nil {
t.Fatal(err)
}
ak, err := NewAccessKey(tokenString)
if err != nil {
t.Fatal(err)
}
if !ak.CanRead("test-channel") || !ak.CanRead("read-channel") {
t.Error("Access Key does not have proper read permissions")
}
if !ak.CanWrite("test-channel") || !ak.CanWrite("write-channel") {
t.Error("Access Key does not have proper write permissions")
}
if ak.CanRead("random-channel-name") || ak.CanWrite("random-channel-name") {
t.Error("channel has permissions it shouldn't")
}
}