Debian Codegraph @

pkg/authorization/path/path_test.go

raw
1 /*
2 Copyright 2018 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 path
18
19 import (
20 "context"
21 "testing"
22
23 "k8s.io/apiserver/pkg/authorization/authorizer"
24 )
25
26 func TestNewAuthorizer(t *testing.T) {
27 tests := []struct {
28 name string
29 excludedPaths []string
30 allowed, denied, noOpinion []string
31 wantErr bool
32 }{
33 {"inner star", []string{"/foo*bar"}, nil, nil, nil, true},
34 {"double star", []string{"/foo**"}, nil, nil, nil, true},
35 {"empty", nil, nil, nil, []string{"/"}, false},
36 {"slash", []string{"/"}, []string{"/"}, nil, []string{"/foo", "//"}, false},
37 {"foo", []string{"/foo"}, []string{"/foo", "foo"}, nil, []string{"/", "", "/bar", "/foo/", "/fooooo", "//foo"}, false},
38 {"foo slash", []string{"/foo/"}, []string{"/foo/"}, nil, []string{"/", "", "/bar", "/foo", "/fooooo"}, false},
39 {"foo slash star", []string{"/foo/*"}, []string{"/foo/", "/foo/bar/bla"}, nil, []string{"/", "", "/foo", "/bar", "/fooooo"}, false},
40 {"foo bar", []string{"/foo", "/bar"}, []string{"/foo", "/bar"}, nil, []string{"/", "", "/foo/", "/bar/", "/fooooo"}, false},
41 {"foo star", []string{"/foo*"}, []string{"/foo", "/foooo"}, nil, []string{"/", "", "/fo", "/bar"}, false},
42 {"star", []string{"/*"}, []string{"/", "", "/foo", "/foooo"}, nil, nil, false},
43 }
44 for _, tt := range tests {
45 t.Run(tt.name, func(t *testing.T) {
46 a, err := NewAuthorizer(tt.excludedPaths)
47 if err != nil && !tt.wantErr {
48 t.Fatalf("unexpected error: %v", err)
49 }
50 if err == nil && tt.wantErr {
51 t.Fatalf("expected error, didn't get any")
52 }
53 if err != nil {
54 return
55 }
56
57 for _, cases := range []struct {
58 paths []string
59 want authorizer.Decision
60 }{
61 {tt.allowed, authorizer.DecisionAllow},
62 {tt.denied, authorizer.DecisionDeny},
63 {tt.noOpinion, authorizer.DecisionNoOpinion},
64 } {
65 for _, pth := range cases.paths {
66 info := authorizer.AttributesRecord{
67 Path: pth,
68 }
69 if got, _, err := a.Authorize(context.Background(), info); err != nil {
70 t.Errorf("NewAuthorizer(%v).Authorize(%q) return unexpected error: %v", tt.excludedPaths, pth, err)
71 } else if got != cases.want {
72 t.Errorf("NewAuthorizer(%v).Authorize(%q) = %v, want %v", tt.excludedPaths, pth, got, cases.want)
73 }
74 }
75 }
76 })
77 }
78 }