| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package group |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"encoding/json" |
| 22 |
"reflect" |
| 23 |
"testing" |
| 24 |
|
| 25 |
"k8s.io/apiserver/pkg/authentication/authenticator" |
| 26 |
"k8s.io/apiserver/pkg/authentication/user" |
| 27 |
) |
| 28 |
|
| 29 |
func TestTokenGroupAdder(t *testing.T) { |
| 30 |
capacity := make([]string, 0, 1024) |
| 31 |
response := &authenticator.Response{User: &user.DefaultInfo{Name: "user", Groups: append(capacity, "original")}} |
| 32 |
orig := toJson(response) |
| 33 |
|
| 34 |
adder := authenticator.Token( |
| 35 |
NewTokenGroupAdder( |
| 36 |
authenticator.TokenFunc(func(ctx context.Context, token string) (*authenticator.Response, bool, error) { |
| 37 |
return response, true, nil |
| 38 |
}), |
| 39 |
[]string{"added"}, |
| 40 |
), |
| 41 |
) |
| 42 |
|
| 43 |
r, _, _ := adder.AuthenticateToken(context.Background(), "") |
| 44 |
if !reflect.DeepEqual(r.User.GetGroups(), []string{"original", "added"}) { |
| 45 |
t.Errorf("Expected original,added groups, got %#v", r.User.GetGroups()) |
| 46 |
} |
| 47 |
|
| 48 |
if got := toJson(response); got != orig { |
| 49 |
t.Errorf("Expected response from delegate to be unmodified: orig=%v got=%v", orig, got) |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
func toJson(x interface{}) string { |
| 54 |
b, _ := json.Marshal(x) |
| 55 |
return string(b) |
| 56 |
} |