github.com/greenpau/go-authcrunch@v1.1.4/pkg/authproxy/config_test.go (about) 1 // Copyright 2022 Paul Greenberg greenpau@outlook.com 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package authproxy 16 17 import ( 18 "fmt" 19 "github.com/greenpau/go-authcrunch/internal/tests" 20 "github.com/greenpau/go-authcrunch/pkg/errors" 21 "testing" 22 ) 23 24 func TestParseConfig(t *testing.T) { 25 var testcases = []struct { 26 name string 27 config []string 28 want map[string]interface{} 29 shouldErr bool 30 err error 31 }{ 32 { 33 name: "basic and api key auth with realms", 34 config: []string{ 35 "basic auth portal default realm foo", 36 "api key auth portal default realm bar", 37 }, 38 want: map[string]interface{}{ 39 "config": &Config{ 40 PortalName: "default", 41 BasicAuth: BasicAuthConfig{ 42 Enabled: true, 43 Realms: map[string]interface{}{ 44 "foo": true, 45 }, 46 }, 47 APIKeyAuth: APIKeyAuthConfig{ 48 Enabled: true, 49 Realms: map[string]interface{}{ 50 "bar": true, 51 }, 52 }, 53 }, 54 }, 55 }, 56 { 57 name: "basic and api key auth with default realm", 58 config: []string{ 59 "basic auth portal default", 60 "api key auth portal default", 61 }, 62 want: map[string]interface{}{ 63 "config": &Config{ 64 PortalName: "default", 65 BasicAuth: BasicAuthConfig{ 66 Enabled: true, 67 Realms: map[string]interface{}{ 68 "local": true, 69 }, 70 }, 71 APIKeyAuth: APIKeyAuthConfig{ 72 Enabled: true, 73 Realms: map[string]interface{}{ 74 "local": true, 75 }, 76 }, 77 }, 78 }, 79 }, 80 { 81 name: "basic and api key auth with foo realm in bar portal", 82 config: []string{ 83 "basic auth realm foo portal bar", 84 "api key auth realm foo portal bar", 85 }, 86 want: map[string]interface{}{ 87 "config": &Config{ 88 PortalName: "bar", 89 BasicAuth: BasicAuthConfig{ 90 Enabled: true, 91 Realms: map[string]interface{}{ 92 "foo": true, 93 }, 94 }, 95 APIKeyAuth: APIKeyAuthConfig{ 96 Enabled: true, 97 Realms: map[string]interface{}{ 98 "foo": true, 99 }, 100 }, 101 }, 102 }, 103 }, 104 { 105 name: "invalid config", 106 config: []string{ 107 "foo", 108 }, 109 shouldErr: true, 110 err: errors.ErrAuthProxyConfigInvalid.WithArgs("foo"), 111 }, 112 { 113 name: "empty config", 114 config: []string{}, 115 shouldErr: true, 116 err: errors.ErrAuthProxyConfigInvalid.WithArgs("empty config"), 117 }, 118 { 119 name: "malformed config with incomplete realm", 120 config: []string{"basic auth realm"}, 121 shouldErr: true, 122 err: errors.ErrAuthProxyConfigInvalid.WithArgs("basic auth realm"), 123 }, 124 { 125 name: "malformed config with unsupported keyword", 126 config: []string{"basic auth realm foo bar baz"}, 127 shouldErr: true, 128 err: errors.ErrAuthProxyConfigInvalid.WithArgs("basic auth realm foo bar baz"), 129 }, 130 { 131 name: "malformed config with bad encoding", 132 config: []string{`basic auth realm foo bar "baz`}, 133 shouldErr: true, 134 err: fmt.Errorf(`parse error on line 1, column 30: extraneous or missing " in quoted-field`), 135 }, 136 { 137 name: "malformed config with multiple portals", 138 config: []string{ 139 "basic auth realm local portal foo", 140 "api key auth realm local portal bar", 141 }, 142 shouldErr: true, 143 err: errors.ErrAuthProxyConfigInvalid.WithArgs("multiple portals"), 144 }, 145 } 146 for _, tc := range testcases { 147 t.Run(tc.name, func(t *testing.T) { 148 msgs := []string{fmt.Sprintf("test name: %s", tc.name)} 149 msgs = append(msgs, fmt.Sprintf("config: %v", tc.config)) 150 config, err := ParseConfig(tc.config) 151 if tests.EvalErrWithLog(t, err, nil, tc.shouldErr, tc.err, msgs) { 152 return 153 } 154 got := make(map[string]interface{}) 155 got["config"] = config 156 tests.EvalObjectsWithLog(t, "config", tc.want, got, msgs) 157 }) 158 } 159 }