github.com/xmidt-org/webpa-common@v1.11.9/basculechecks/capabilitiesmap_test.go (about) 1 /** 2 * Copyright 2020 Comcast Cable Communications Management, LLC 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 18 package basculechecks 19 20 import ( 21 "net/url" 22 "testing" 23 24 "github.com/stretchr/testify/assert" 25 "github.com/stretchr/testify/require" 26 "github.com/xmidt-org/bascule" 27 ) 28 29 func TestCapabilitiesMapCheck(t *testing.T) { 30 goodDefault := ConstCheck("default checker") 31 checkersMap := map[string]CapabilityChecker{ 32 "a": ConstCheck("meh"), 33 "bcedef": ConstCheck("yay"), 34 "all": ConstCheck("good"), 35 "fallback": nil, 36 } 37 cm := CapabilitiesMap{ 38 Checkers: checkersMap, 39 DefaultChecker: goodDefault, 40 } 41 nilCM := CapabilitiesMap{} 42 goodCapabilities := []string{ 43 "test", 44 "", 45 "yay", 46 "...", 47 } 48 goodToken := bascule.NewToken("test", "princ", 49 bascule.NewAttributes(map[string]interface{}{CapabilityKey: goodCapabilities})) 50 defaultCapabilities := []string{ 51 "test", 52 "", 53 "default checker", 54 "...", 55 } 56 defaultToken := bascule.NewToken("test", "princ", 57 bascule.NewAttributes(map[string]interface{}{CapabilityKey: defaultCapabilities})) 58 badToken := bascule.NewToken("", "", nil) 59 tests := []struct { 60 description string 61 cm CapabilitiesMap 62 token bascule.Token 63 includeURL bool 64 endpoint string 65 expectedReason string 66 expectedErr error 67 }{ 68 { 69 description: "Success", 70 cm: cm, 71 token: goodToken, 72 includeURL: true, 73 endpoint: "bcedef", 74 }, 75 { 76 description: "Success Not in Map", 77 cm: cm, 78 token: defaultToken, 79 includeURL: true, 80 endpoint: "b", 81 }, 82 { 83 description: "Success Nil Map Value", 84 cm: cm, 85 token: defaultToken, 86 includeURL: true, 87 endpoint: "fallback", 88 }, 89 { 90 description: "No Match Error", 91 cm: cm, 92 token: goodToken, 93 includeURL: true, 94 endpoint: "b", 95 expectedReason: NoCapabilitiesMatch, 96 expectedErr: ErrNoValidCapabilityFound, 97 }, 98 { 99 description: "No Match with Default Checker Error", 100 cm: cm, 101 token: defaultToken, 102 includeURL: true, 103 endpoint: "bcedef", 104 expectedReason: NoCapabilitiesMatch, 105 expectedErr: ErrNoValidCapabilityFound, 106 }, 107 { 108 description: "No Match Nil Default Checker Error", 109 cm: nilCM, 110 token: defaultToken, 111 includeURL: true, 112 endpoint: "bcedef", 113 expectedReason: NoCapabilitiesMatch, 114 expectedErr: ErrNoValidCapabilityFound, 115 }, 116 { 117 description: "No Token Error", 118 cm: cm, 119 token: nil, 120 includeURL: true, 121 expectedReason: TokenMissingValues, 122 expectedErr: ErrNoToken, 123 }, 124 { 125 description: "No Request URL Error", 126 cm: cm, 127 token: goodToken, 128 includeURL: false, 129 expectedReason: TokenMissingValues, 130 expectedErr: ErrNoURL, 131 }, 132 { 133 description: "Empty Endpoint Error", 134 cm: cm, 135 token: goodToken, 136 includeURL: true, 137 endpoint: "", 138 expectedReason: EmptyParsedURL, 139 expectedErr: ErrEmptyEndpoint, 140 }, 141 { 142 description: "Get Capabilities Error", 143 cm: cm, 144 token: badToken, 145 includeURL: true, 146 endpoint: "b", 147 expectedReason: UndeterminedCapabilities, 148 expectedErr: ErrNilAttributes, 149 }, 150 } 151 for _, tc := range tests { 152 t.Run(tc.description, func(t *testing.T) { 153 assert := assert.New(t) 154 require := require.New(t) 155 auth := bascule.Authentication{ 156 Token: tc.token, 157 } 158 if tc.includeURL { 159 goodURL, err := url.Parse("/test") 160 require.Nil(err) 161 auth.Request = bascule.Request{ 162 URL: goodURL, 163 Method: "GET", 164 } 165 } 166 reason, err := tc.cm.Check(auth, ParsedValues{Endpoint: tc.endpoint}) 167 assert.Equal(tc.expectedReason, reason) 168 if err == nil || tc.expectedErr == nil { 169 assert.Equal(tc.expectedErr, err) 170 return 171 } 172 assert.Contains(err.Error(), tc.expectedErr.Error()) 173 }) 174 } 175 }