Debian Codegraph @

pkg/endpoints/handlers/responsewriters/errors_test.go

raw
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 responsewriters
18
19 import (
20 "errors"
21 "net/http"
22 "net/http/httptest"
23 "net/url"
24 "testing"
25
26 "k8s.io/apimachinery/pkg/runtime"
27 "k8s.io/apimachinery/pkg/runtime/serializer"
28 "k8s.io/apiserver/pkg/authentication/user"
29 "k8s.io/apiserver/pkg/authorization/authorizer"
30 "k8s.io/apiserver/pkg/endpoints/request"
31 )
32
33 func TestErrors(t *testing.T) {
34 internalError := errors.New("ARGH")
35 fns := map[string]func(http.ResponseWriter, *http.Request){
36 "InternalError": func(w http.ResponseWriter, req *http.Request) {
37 InternalError(w, req, internalError)
38 },
39 }
40 cases := []struct {
41 fn string
42 uri string
43 expected string
44 }{
45 {"InternalError", "/get", "Internal Server Error: \"/get\": ARGH\n"},
46 {"InternalError", "/<script>", "Internal Server Error: \"/&lt;script&gt;\": ARGH\n"},
47 }
48 for _, test := range cases {
49 observer := httptest.NewRecorder()
50 fns[test.fn](observer, &http.Request{RequestURI: test.uri})
51 result := observer.Body.String()
52 if result != test.expected {
53 t.Errorf("%s(..., %q) != %q, got %q", test.fn, test.uri, test.expected, result)
54 }
55 }
56 }
57
58 func TestForbidden(t *testing.T) {
59 u := &user.DefaultInfo{Name: "NAME"}
60 cases := []struct {
61 expected string
62 attributes authorizer.Attributes
63 reason string
64 contentType string
65 }{
66 {`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"forbidden: User \"NAME\" cannot GET path \"/whatever\"","reason":"Forbidden","details":{},"code":403}
67 `, authorizer.AttributesRecord{User: u, Verb: "GET", Path: "/whatever"}, "", "application/json"},
68 {`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"forbidden: User \"NAME\" cannot GET path \"/\u0026lt;script\u0026gt;\"","reason":"Forbidden","details":{},"code":403}
69 `, authorizer.AttributesRecord{User: u, Verb: "GET", Path: "/<script>"}, "", "application/json"},
70 {`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"pod is forbidden: User \"NAME\" cannot get resource \"pod\" in API group \"\" at the cluster scope","reason":"Forbidden","details":{"kind":"pod"},"code":403}
71 `, authorizer.AttributesRecord{User: u, Verb: "get", Resource: "pod", ResourceRequest: true}, "", "application/json"},
72 {`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"pod \"mypod\" is forbidden: User \"NAME\" cannot get resource \"pod\" in API group \"\" at the cluster scope","reason":"Forbidden","details":{"name":"mypod","kind":"pod"},"code":403}
73 `, authorizer.AttributesRecord{User: u, Verb: "get", Resource: "pod", ResourceRequest: true, Name: "mypod"}, "", "application/json"},
74 {`{"kind":"Status","apiVersion":"v1","metadata":{},"status":"Failure","message":"pod.v2 is forbidden: User \"NAME\" cannot get resource \"pod/quota\" in API group \"v2\" in the namespace \"test\"","reason":"Forbidden","details":{"group":"v2","kind":"pod"},"code":403}
75 `, authorizer.AttributesRecord{User: u, Verb: "get", Namespace: "test", APIGroup: "v2", Resource: "pod", Subresource: "quota", ResourceRequest: true}, "", "application/json"},
76 }
77 for _, test := range cases {
78 observer := httptest.NewRecorder()
79 scheme := runtime.NewScheme()
80 negotiatedSerializer := serializer.NewCodecFactory(scheme).WithoutConversion()
81 Forbidden(request.NewDefaultContext(), test.attributes, observer, &http.Request{URL: &url.URL{Path: "/path"}}, test.reason, negotiatedSerializer)
82 result := observer.Body.String()
83 if result != test.expected {
84 t.Errorf("Forbidden response body(%#v...)\n expected: %v\ngot: %v", test.attributes, test.expected, result)
85 }
86 //nolint:staticcheck // SA1019 backwards compatibility
87 resultType := observer.HeaderMap.Get("Content-Type")
88 if resultType != test.contentType {
89 t.Errorf("Forbidden content type(%#v...) != %#v, got %#v", test.attributes, test.expected, result)
90 }
91 }
92 }