-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
authorization_test.go
108 lines (101 loc) · 3.66 KB
/
authorization_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
package g8
import "testing"
func TestAuthorizationService_IsAuthorized(t *testing.T) {
authorizationService := NewAuthorizationService().WithToken("token")
if !authorizationService.IsAuthorized("token", nil) {
t.Error("should've returned true")
}
if authorizationService.IsAuthorized("bad-token", nil) {
t.Error("should've returned false")
}
if authorizationService.IsAuthorized("token", []string{"admin"}) {
t.Error("should've returned false")
}
if authorizationService.IsAuthorized("", nil) {
t.Error("should've returned false")
}
}
func TestAuthorizationService_IsAuthorizedWithPermissions(t *testing.T) {
authorizationService := NewAuthorizationService().WithClient(NewClient("token").WithPermissions([]string{"a", "b"}))
if !authorizationService.IsAuthorized("token", nil) {
t.Error("should've returned true")
}
if !authorizationService.IsAuthorized("token", []string{"a"}) {
t.Error("should've returned true")
}
if !authorizationService.IsAuthorized("token", []string{"b"}) {
t.Error("should've returned true")
}
if !authorizationService.IsAuthorized("token", []string{"a", "b"}) {
t.Error("should've returned true")
}
if authorizationService.IsAuthorized("token", []string{"c"}) {
t.Error("should've returned false")
}
if authorizationService.IsAuthorized("token", []string{"a", "c"}) {
t.Error("should've returned false")
}
if authorizationService.IsAuthorized("bad-token", nil) {
t.Error("should've returned false")
}
if authorizationService.IsAuthorized("bad-token", []string{"a"}) {
t.Error("should've returned false")
}
if authorizationService.IsAuthorized("", []string{"a"}) {
t.Error("should've returned false")
}
}
func TestAuthorizationService_WithToken(t *testing.T) {
authorizationService := NewAuthorizationService().WithToken("token")
if !authorizationService.IsAuthorized("token", nil) {
t.Error("should've returned true")
}
if authorizationService.IsAuthorized("bad-token", nil) {
t.Error("should've returned false")
}
if authorizationService.IsAuthorized("token", []string{"admin"}) {
t.Error("should've returned false")
}
}
func TestAuthorizationService_WithTokens(t *testing.T) {
authorizationService := NewAuthorizationService().WithTokens([]string{"1", "2"})
if !authorizationService.IsAuthorized("1", nil) {
t.Error("should've returned true")
}
if !authorizationService.IsAuthorized("2", nil) {
t.Error("should've returned true")
}
if authorizationService.IsAuthorized("3", nil) {
t.Error("should've returned false")
}
}
func TestAuthorizationService_WithClient(t *testing.T) {
authorizationService := NewAuthorizationService().WithClient(NewClient("token").WithPermissions([]string{"a", "b"}))
if !authorizationService.IsAuthorized("token", []string{"a", "b"}) {
t.Error("should've returned true")
}
if !authorizationService.IsAuthorized("token", []string{"a"}) {
t.Error("should've returned true")
}
if !authorizationService.IsAuthorized("token", []string{"b"}) {
t.Error("should've returned true")
}
if authorizationService.IsAuthorized("token", []string{"c"}) {
t.Error("should've returned false")
}
}
func TestAuthorizationService_WithClients(t *testing.T) {
authorizationService := NewAuthorizationService().WithClients([]*Client{NewClient("1").WithPermission("a"), NewClient("2").WithPermission("b")})
if !authorizationService.IsAuthorized("1", []string{"a"}) {
t.Error("should've returned true")
}
if !authorizationService.IsAuthorized("2", []string{"b"}) {
t.Error("should've returned true")
}
if authorizationService.IsAuthorized("1", []string{"b"}) {
t.Error("should've returned false")
}
if authorizationService.IsAuthorized("2", []string{"a"}) {
t.Error("should've returned false")
}
}