-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathauth_basic_test.go
110 lines (95 loc) · 4.17 KB
/
auth_basic_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 gimlet
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSimpleAuthenticator(t *testing.T) {
assert := assert.New(t)
assert.Implements((*Authenticator)(nil), &simpleAuthenticator{})
auth := NewSimpleAuthenticator([]User{}, map[string][]string{})
assert.NotNil(auth)
assert.NotNil(auth.(*simpleAuthenticator).groups)
assert.NotNil(auth.(*simpleAuthenticator).users)
assert.Len(auth.(*simpleAuthenticator).groups, 0)
assert.Len(auth.(*simpleAuthenticator).users, 0)
// constructor avoids nils
auth = NewSimpleAuthenticator(nil, nil)
assert.NotNil(auth)
assert.NotNil(auth.(*simpleAuthenticator).groups)
assert.NotNil(auth.(*simpleAuthenticator).users)
assert.Len(auth.(*simpleAuthenticator).groups, 0)
assert.Len(auth.(*simpleAuthenticator).users, 0)
// constructor avoids nils
opts, err := NewBasicUserOptions("id")
require.NoError(t, err)
usr := NewBasicUser(opts.Name("name").Email("email").Password("pass").Key("key"))
auth = NewSimpleAuthenticator([]User{usr}, nil)
assert.NotNil(auth)
assert.NotNil(auth.(*simpleAuthenticator).groups)
assert.NotNil(auth.(*simpleAuthenticator).users)
assert.Len(auth.(*simpleAuthenticator).groups, 0)
assert.Len(auth.(*simpleAuthenticator).users, 1)
// if a user exists then it should work
assert.True(auth.CheckAuthenticated(usr))
// a second user shouldn't validate
opts2, err := NewBasicUserOptions("id2")
require.NoError(t, err)
usr2 := NewBasicUser(opts2.Name("name").Email("email").Password("pass").Key("key"))
assert.False(auth.CheckAuthenticated(usr2))
opts3, err := NewBasicUserOptions("id3")
require.NoError(t, err)
usr3 := NewBasicUser(opts3.Name("name").Email("email").Password("pass").Key("key").Roles("admin"))
usr3broken := NewBasicUser(opts3.Key("yek"))
auth = NewSimpleAuthenticator([]User{usr3}, map[string][]string{
"none": {"_"},
"admin": {"id3"}})
assert.NotNil(auth)
assert.Len(auth.(*simpleAuthenticator).groups, 2)
assert.Len(auth.(*simpleAuthenticator).users, 1)
assert.False(auth.CheckGroupAccess(usr, "admin"))
assert.False(auth.CheckGroupAccess(usr3broken, "admin"))
assert.False(auth.CheckGroupAccess(usr3, "proj"))
assert.False(auth.CheckGroupAccess(usr3, "none"))
assert.True(auth.CheckGroupAccess(usr3, "admin"))
// check user-based role access
usr.AccessRoles = []string{"admin", "project", "one"}
assert.False(auth.CheckResourceAccess(usr, "admin")) // not currently authenticated
auth.(*simpleAuthenticator).users[usr.Username()] = usr
assert.True(auth.CheckResourceAccess(usr, "admin")) // now it's defined
assert.True(auth.CheckResourceAccess(usr, "project"))
assert.False(auth.CheckResourceAccess(usr, "two")) // but not for this role
}
func TestBasicAuthenticator(t *testing.T) {
assert := assert.New(t)
// constructor avoids nils
auth := NewBasicAuthenticator(nil, nil)
assert.NotNil(auth)
assert.NotNil(auth.(*basicAuthenticator).groups)
assert.NotNil(auth.(*basicAuthenticator).resources)
assert.Len(auth.(*basicAuthenticator).groups, 0)
assert.Len(auth.(*basicAuthenticator).resources, 0)
// authenticated users are all non-nil users that have
// usernames
assert.False(auth.CheckAuthenticated(nil))
assert.False(auth.CheckAuthenticated(&BasicUser{}))
opts, err := NewBasicUserOptions("id")
require.NoError(t, err)
usr := NewBasicUser(opts.Name("name").Email("email").Password("pass").Key("key"))
assert.True(auth.CheckAuthenticated(usr))
auth = NewBasicAuthenticator(map[string][]string{"one": {"id"}}, nil)
assert.False(auth.CheckGroupAccess(usr, "two"))
assert.False(auth.CheckGroupAccess(usr, ""))
assert.True(auth.CheckGroupAccess(usr, "one"))
assert.False(auth.CheckGroupAccess(nil, "one"))
assert.False(auth.CheckGroupAccess(nil, ""))
auth = NewBasicAuthenticator(nil, map[string][]string{"/one": {"id"}})
assert.False(auth.CheckResourceAccess(usr, "two"))
assert.False(auth.CheckResourceAccess(usr, ""))
assert.False(auth.CheckResourceAccess(usr, "one"))
assert.False(auth.CheckResourceAccess(usr, "/two"))
assert.False(auth.CheckResourceAccess(usr, "/"))
assert.True(auth.CheckResourceAccess(usr, "/one"))
assert.False(auth.CheckResourceAccess(nil, "/one"))
assert.False(auth.CheckResourceAccess(nil, ""))
}