This repository was archived by the owner on Mar 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathauthenticator_test.go
More file actions
111 lines (90 loc) · 3.46 KB
/
authenticator_test.go
File metadata and controls
111 lines (90 loc) · 3.46 KB
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
111
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package keystone
import (
"os"
"reflect"
"testing"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"k8s.io/apiserver/pkg/authentication/user"
)
func newKeystoneAuthenticator(authURL string, client *gophercloud.ServiceClient) KeystoneAuthenticator {
return KeystoneAuthenticator{
authURL: authURL,
client: client,
}
}
func TestAuthenticateToken(t *testing.T) {
var (
calledWithToken []string
resultUsers map[string]user.Info
)
authUrl := os.Getenv("OS_AUTH_URL")
provider, _ := openstack.NewClient(authUrl)
cli := &gophercloud.ServiceClient{
ProviderClient: provider,
Endpoint: authUrl,
}
a := newKeystoneAuthenticator(authUrl, cli)
calledWithToken, resultUsers = []string{}, nil
a.AuthenticateToken("bad1")
a.AuthenticateToken("bad2")
a.AuthenticateToken("bad3")
a.AuthenticateToken("bad1")
a.AuthenticateToken("bad2")
a.AuthenticateToken("bad3")
if !reflect.DeepEqual(calledWithToken, []string{"bad1", "bad2", "bad3", "bad1", "bad2", "bad3"}) {
t.Errorf("Expected failing calls to bypass cache, got %v", calledWithToken)
}
// reset calls, make the backend return success for three user tokens
calledWithToken = []string{}
resultUsers = map[string]user.Info{}
resultUsers["usertoken1"] = &user.DefaultInfo{Name: "user1"}
resultUsers["usertoken2"] = &user.DefaultInfo{Name: "user2"}
resultUsers["usertoken3"] = &user.DefaultInfo{Name: "user3"}
if user, ok, err := a.AuthenticateToken("usertoken1"); err != nil || !ok || user.GetName() != "user1" {
t.Errorf("Expected user1")
}
if user, ok, err := a.AuthenticateToken("usertoken2"); err != nil || !ok || user.GetName() != "user2" {
t.Errorf("Expected user2")
}
if user, ok, err := a.AuthenticateToken("usertoken3"); err != nil || !ok || user.GetName() != "user3" {
t.Errorf("Expected user3")
}
if !reflect.DeepEqual(calledWithToken, []string{"usertoken1", "usertoken2", "usertoken3"}) {
t.Errorf("Expected token calls, got %v", calledWithToken)
}
// reset calls, make the backend return failures
calledWithToken = []string{}
resultUsers = nil
// authenticate calls still succeed and backend is not hit
if user, ok, err := a.AuthenticateToken("usertoken1"); err != nil || !ok || user.GetName() != "user1" {
t.Errorf("Expected user1")
}
if user, ok, err := a.AuthenticateToken("usertoken2"); err != nil || !ok || user.GetName() != "user2" {
t.Errorf("Expected user2")
}
if user, ok, err := a.AuthenticateToken("usertoken3"); err != nil || !ok || user.GetName() != "user3" {
t.Errorf("Expected user3")
}
if !reflect.DeepEqual(calledWithToken, []string{}) {
t.Errorf("Expected no token calls, got %v", calledWithToken)
}
a.AuthenticateToken("usertoken1")
a.AuthenticateToken("usertoken2")
a.AuthenticateToken("usertoken3")
if !reflect.DeepEqual(calledWithToken, []string{"usertoken1", "usertoken2", "usertoken3"}) {
t.Errorf("Expected token calls, got %v", calledWithToken)
}
}