| 1 |
package config_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"testing" |
| 6 |
|
| 7 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config" |
| 8 |
"github.com/stretchr/testify/assert" |
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
"golang.org/x/oauth2" |
| 11 |
) |
| 12 |
|
| 13 |
func TestOpenVPNConfigStrategyUnmarshalText(t *testing.T) { |
| 14 |
t.Parallel() |
| 15 |
|
| 16 |
var strategy config.OpenVPNConfigStrategy |
| 17 |
|
| 18 |
require.NoError(t, strategy.UnmarshalText([]byte(""))) |
| 19 |
assert.Equal(t, config.OpenVPNConfigStrategyMerge, strategy) |
| 20 |
|
| 21 |
require.NoError(t, strategy.UnmarshalText([]byte("merge"))) |
| 22 |
assert.Equal(t, config.OpenVPNConfigStrategyMerge, strategy) |
| 23 |
|
| 24 |
require.NoError(t, strategy.UnmarshalText([]byte("user-selector"))) |
| 25 |
assert.Equal(t, config.OpenVPNConfigStrategyUserSelector, strategy) |
| 26 |
|
| 27 |
require.Error(t, strategy.UnmarshalText([]byte("selector"))) |
| 28 |
require.Error(t, strategy.UnmarshalText([]byte("single"))) |
| 29 |
} |
| 30 |
|
| 31 |
func TestOpenVPNConfigStrategyMarshalText(t *testing.T) { |
| 32 |
t.Parallel() |
| 33 |
|
| 34 |
strategy, err := config.OpenVPNConfigStrategyMerge.MarshalText() |
| 35 |
|
| 36 |
require.NoError(t, err) |
| 37 |
assert.Equal(t, []byte("merge"), strategy) |
| 38 |
|
| 39 |
strategy, err = config.OpenVPNConfigStrategyUserSelector.MarshalText() |
| 40 |
|
| 41 |
require.NoError(t, err) |
| 42 |
assert.Equal(t, []byte("user-selector"), strategy) |
| 43 |
|
| 44 |
_, err = config.OpenVPNConfigStrategy(-1).MarshalText() |
| 45 |
|
| 46 |
require.Error(t, err) |
| 47 |
} |
| 48 |
|
| 49 |
func TestOpenVPNConfigStrategyString(t *testing.T) { |
| 50 |
t.Parallel() |
| 51 |
|
| 52 |
assert.Equal(t, "merge", config.OpenVPNConfigStrategyMerge.String()) |
| 53 |
assert.Equal(t, "user-selector", config.OpenVPNConfigStrategyUserSelector.String()) |
| 54 |
|
| 55 |
assert.Panics(t, func() { _ = config.OpenVPNConfigStrategy(-1).String() }, "The code did not panic") |
| 56 |
} |
| 57 |
|
| 58 |
func TestOAuth2AuthStyleUnmarshalText(t *testing.T) { |
| 59 |
t.Parallel() |
| 60 |
|
| 61 |
for _, tc := range []struct { |
| 62 |
name string |
| 63 |
value string |
| 64 |
expected config.OAuth2AuthStyle |
| 65 |
wantError bool |
| 66 |
}{ |
| 67 |
{name: "auto", value: "auto", expected: config.OAuth2AuthStyle(oauth2.AuthStyleAutoDetect)}, |
| 68 |
{name: "params", value: "params", expected: config.OAuth2AuthStyle(oauth2.AuthStyleInParams)}, |
| 69 |
{name: "header", value: "header", expected: config.OAuth2AuthStyle(oauth2.AuthStyleInHeader)}, |
| 70 |
{name: "unknown", value: "unknown", wantError: true}, |
| 71 |
{name: "legacy auto", value: "AuthStyleAutoDetect", wantError: true}, |
| 72 |
{name: "legacy params", value: "AuthStyleInParams", wantError: true}, |
| 73 |
{name: "legacy header", value: "AuthStyleInHeader", wantError: true}, |
| 74 |
} { |
| 75 |
t.Run(tc.name, func(t *testing.T) { |
| 76 |
t.Parallel() |
| 77 |
|
| 78 |
var actual config.OAuth2AuthStyle |
| 79 |
|
| 80 |
err := actual.UnmarshalText([]byte(tc.value)) |
| 81 |
|
| 82 |
if tc.wantError { |
| 83 |
require.Error(t, err) |
| 84 |
|
| 85 |
return |
| 86 |
} |
| 87 |
|
| 88 |
require.NoError(t, err) |
| 89 |
assert.Equal(t, tc.expected, actual) |
| 90 |
}) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
func TestOAuth2AuthStyleMarshalText(t *testing.T) { |
| 95 |
t.Parallel() |
| 96 |
|
| 97 |
oAuth2AuthStyle, err := config.OAuth2AuthStyle(oauth2.AuthStyleInHeader).MarshalText() |
| 98 |
|
| 99 |
require.NoError(t, err) |
| 100 |
assert.Equal(t, []byte("header"), oAuth2AuthStyle) |
| 101 |
|
| 102 |
oAuth2AuthStyle, err = config.OAuth2AuthStyle(oauth2.AuthStyleInParams).MarshalText() |
| 103 |
|
| 104 |
require.NoError(t, err) |
| 105 |
assert.Equal(t, []byte("params"), oAuth2AuthStyle) |
| 106 |
|
| 107 |
oAuth2AuthStyle, err = config.OAuth2AuthStyle(oauth2.AuthStyleAutoDetect).MarshalText() |
| 108 |
|
| 109 |
require.NoError(t, err) |
| 110 |
assert.Equal(t, []byte("auto"), oAuth2AuthStyle) |
| 111 |
|
| 112 |
_, err = config.OAuth2AuthStyle(-1).MarshalText() |
| 113 |
|
| 114 |
require.Error(t, err) |
| 115 |
} |
| 116 |
|
| 117 |
func TestOAuth2AuthStyleString(t *testing.T) { |
| 118 |
t.Parallel() |
| 119 |
|
| 120 |
assert.Equal(t, "header", config.OAuth2AuthStyle(oauth2.AuthStyleInHeader).String()) |
| 121 |
assert.Equal(t, "params", config.OAuth2AuthStyle(oauth2.AuthStyleInParams).String()) |
| 122 |
assert.Equal(t, "auto", config.OAuth2AuthStyle(oauth2.AuthStyleAutoDetect).String()) |
| 123 |
|
| 124 |
assert.Panics(t, func() { _ = config.OAuth2AuthStyle(-1).String() }, "The code did not panic") |
| 125 |
} |
| 126 |
|
| 127 |
func TestOAuth2AuthStyleGetAuthStyle(t *testing.T) { |
| 128 |
t.Parallel() |
| 129 |
|
| 130 |
oAuth2AuthStyle := config.OAuth2AuthStyle(oauth2.AuthStyleInHeader).AuthStyle() |
| 131 |
assert.Equal(t, oauth2.AuthStyleInHeader, oAuth2AuthStyle) |
| 132 |
|
| 133 |
oAuth2AuthStyle = config.OAuth2AuthStyle(oauth2.AuthStyleInParams).AuthStyle() |
| 134 |
assert.Equal(t, oauth2.AuthStyleInParams, oAuth2AuthStyle) |
| 135 |
|
| 136 |
oAuth2AuthStyle = config.OAuth2AuthStyle(oauth2.AuthStyleAutoDetect).AuthStyle() |
| 137 |
assert.Equal(t, oauth2.AuthStyleAutoDetect, oAuth2AuthStyle) |
| 138 |
} |
| 139 |
|
| 140 |
func TestOAuth2RefreshNonceUnmarshalText(t *testing.T) { |
| 141 |
t.Parallel() |
| 142 |
|
| 143 |
var refreshNonce config.OAuth2RefreshNonce |
| 144 |
|
| 145 |
require.NoError(t, refreshNonce.UnmarshalText([]byte("auto"))) |
| 146 |
assert.Equal(t, config.OAuth2RefreshNonceAuto, refreshNonce) |
| 147 |
|
| 148 |
require.NoError(t, refreshNonce.UnmarshalText([]byte("empty"))) |
| 149 |
assert.Equal(t, config.OAuth2RefreshNonceEmpty, refreshNonce) |
| 150 |
|
| 151 |
require.NoError(t, refreshNonce.UnmarshalText([]byte("equal"))) |
| 152 |
assert.Equal(t, config.OAuth2RefreshNonceEqual, refreshNonce) |
| 153 |
|
| 154 |
require.Error(t, refreshNonce.UnmarshalText([]byte("unknown"))) |
| 155 |
} |
| 156 |
|
| 157 |
func TestOAuth2RefreshNonceMarshalText(t *testing.T) { |
| 158 |
t.Parallel() |
| 159 |
|
| 160 |
refreshNonce, err := config.OAuth2RefreshNonceAuto.MarshalText() |
| 161 |
|
| 162 |
require.NoError(t, err) |
| 163 |
assert.Equal(t, []byte("auto"), refreshNonce) |
| 164 |
|
| 165 |
refreshNonce, err = config.OAuth2RefreshNonceEmpty.MarshalText() |
| 166 |
|
| 167 |
require.NoError(t, err) |
| 168 |
assert.Equal(t, []byte("empty"), refreshNonce) |
| 169 |
|
| 170 |
refreshNonce, err = config.OAuth2RefreshNonceEqual.MarshalText() |
| 171 |
|
| 172 |
require.NoError(t, err) |
| 173 |
assert.Equal(t, []byte("equal"), refreshNonce) |
| 174 |
|
| 175 |
_, err = config.OAuth2RefreshNonce(-1).MarshalText() |
| 176 |
|
| 177 |
require.Error(t, err) |
| 178 |
} |
| 179 |
|
| 180 |
func TestOAuth2RefreshNonceString(t *testing.T) { |
| 181 |
t.Parallel() |
| 182 |
|
| 183 |
assert.Equal(t, "auto", config.OAuth2RefreshNonceAuto.String()) |
| 184 |
assert.Equal(t, "empty", config.OAuth2RefreshNonceEmpty.String()) |
| 185 |
assert.Equal(t, "equal", config.OAuth2RefreshNonceEqual.String()) |
| 186 |
|
| 187 |
assert.Panics(t, func() { _ = config.OAuth2RefreshNonce(-1).String() }, "The code did not panic") |
| 188 |
} |
| 189 |
|
| 190 |
func TestConfigString(t *testing.T) { |
| 191 |
t.Parallel() |
| 192 |
|
| 193 |
conf := config.Defaults |
| 194 |
conf.HTTP.Secret = "HTTP_SECRET" |
| 195 |
conf.OpenVPN.Password = "OPENVPN_SECRET" |
| 196 |
conf.OAuth2.Client.Secret = "OAUTH_SECRET" |
| 197 |
|
| 198 |
body := conf.String() |
| 199 |
|
| 200 |
require.JSONEq(t, `"***"`, extractJSONValue(t, body, "http", "secret")) |
| 201 |
require.JSONEq(t, `"***"`, extractJSONValue(t, body, "openvpn", "password")) |
| 202 |
require.JSONEq(t, `"***"`, extractJSONValue(t, body, "oauth2", "client", "secret")) |
| 203 |
require.NotContains(t, body, "HTTP_SECRET") |
| 204 |
require.NotContains(t, body, "OPENVPN_SECRET") |
| 205 |
require.NotContains(t, body, "OAUTH_SECRET") |
| 206 |
} |
| 207 |
|
| 208 |
func TestHTTPMarshalJSON(t *testing.T) { |
| 209 |
t.Parallel() |
| 210 |
|
| 211 |
httpConfig := config.Defaults.HTTP |
| 212 |
httpConfig.Secret = "HTTP_SECRET" |
| 213 |
|
| 214 |
body, err := json.Marshal(httpConfig) |
| 215 |
require.NoError(t, err) |
| 216 |
|
| 217 |
require.JSONEq(t, `"***"`, extractJSONValue(t, string(body), "secret")) |
| 218 |
require.NotContains(t, string(body), "HTTP_SECRET") |
| 219 |
} |
| 220 |
|
| 221 |
func extractJSONValue(t *testing.T, body string, path ...string) string { |
| 222 |
t.Helper() |
| 223 |
|
| 224 |
var value any |
| 225 |
require.NoError(t, json.Unmarshal([]byte(body), &value)) |
| 226 |
|
| 227 |
for _, key := range path { |
| 228 |
object, ok := value.(map[string]any) |
| 229 |
require.True(t, ok) |
| 230 |
|
| 231 |
value = object[key] |
| 232 |
} |
| 233 |
|
| 234 |
encodedValue, err := json.Marshal(value) |
| 235 |
require.NoError(t, err) |
| 236 |
|
| 237 |
return string(encodedValue) |
| 238 |
} |