| 1 |
/* |
| 2 |
Copyright 2014 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 request |
| 18 |
|
| 19 |
import ( |
| 20 |
"testing" |
| 21 |
|
| 22 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 |
"k8s.io/apiserver/pkg/authentication/user" |
| 24 |
) |
| 25 |
|
| 26 |
// TestNamespaceContext validates that a namespace can be get/set on a context object |
| 27 |
func TestNamespaceContext(t *testing.T) { |
| 28 |
ctx := NewDefaultContext() |
| 29 |
result, ok := NamespaceFrom(ctx) |
| 30 |
if !ok { |
| 31 |
t.Fatalf("Error getting namespace") |
| 32 |
} |
| 33 |
if metav1.NamespaceDefault != result { |
| 34 |
t.Fatalf("Expected: %s, Actual: %s", metav1.NamespaceDefault, result) |
| 35 |
} |
| 36 |
|
| 37 |
ctx = NewContext() |
| 38 |
_, ok = NamespaceFrom(ctx) |
| 39 |
if ok { |
| 40 |
t.Fatalf("Should not be ok because there is no namespace on the context") |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// TestUserContext validates that a userinfo can be get/set on a context object |
| 45 |
func TestUserContext(t *testing.T) { |
| 46 |
ctx := NewContext() |
| 47 |
_, ok := UserFrom(ctx) |
| 48 |
if ok { |
| 49 |
t.Fatalf("Should not be ok because there is no user.Info on the context") |
| 50 |
} |
| 51 |
ctx = WithUser( |
| 52 |
ctx, |
| 53 |
&user.DefaultInfo{ |
| 54 |
Name: "bob", |
| 55 |
UID: "123", |
| 56 |
Groups: []string{"group1"}, |
| 57 |
Extra: map[string][]string{"foo": {"bar"}}, |
| 58 |
}, |
| 59 |
) |
| 60 |
|
| 61 |
result, ok := UserFrom(ctx) |
| 62 |
if !ok { |
| 63 |
t.Fatalf("Error getting user info") |
| 64 |
} |
| 65 |
|
| 66 |
expectedName := "bob" |
| 67 |
if result.GetName() != expectedName { |
| 68 |
t.Fatalf("Get user name error, Expected: %s, Actual: %s", expectedName, result.GetName()) |
| 69 |
} |
| 70 |
|
| 71 |
expectedUID := "123" |
| 72 |
if result.GetUID() != expectedUID { |
| 73 |
t.Fatalf("Get UID error, Expected: %s, Actual: %s", expectedUID, result.GetName()) |
| 74 |
} |
| 75 |
|
| 76 |
expectedGroup := "group1" |
| 77 |
actualGroup := result.GetGroups() |
| 78 |
if len(actualGroup) != 1 { |
| 79 |
t.Fatalf("Get user group number error, Expected: 1, Actual: %d", len(actualGroup)) |
| 80 |
} else if actualGroup[0] != expectedGroup { |
| 81 |
t.Fatalf("Get user group error, Expected: %s, Actual: %s", expectedGroup, actualGroup[0]) |
| 82 |
} |
| 83 |
|
| 84 |
expectedExtraKey := "foo" |
| 85 |
expectedExtraValue := "bar" |
| 86 |
actualExtra := result.GetExtra() |
| 87 |
if len(actualExtra[expectedExtraKey]) != 1 { |
| 88 |
t.Fatalf("Get user extra map number error, Expected: 1, Actual: %d", len(actualExtra[expectedExtraKey])) |
| 89 |
} else if actualExtra[expectedExtraKey][0] != expectedExtraValue { |
| 90 |
t.Fatalf("Get user extra map value error, Expected: %s, Actual: %s", expectedExtraValue, actualExtra[expectedExtraKey]) |
| 91 |
} |
| 92 |
|
| 93 |
} |