| 1 |
package types_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"os" |
| 6 |
"path" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/jkroepke/openvpn-auth-oauth2/v2/internal/config/types" |
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
) |
| 13 |
|
| 14 |
func TestSecretString(t *testing.T) { |
| 15 |
t.Parallel() |
| 16 |
|
| 17 |
secret := types.Secret("SECRET") |
| 18 |
|
| 19 |
assert.Equal(t, "SECRET", secret.String()) |
| 20 |
} |
| 21 |
|
| 22 |
func TestSecretMarshalText(t *testing.T) { |
| 23 |
t.Parallel() |
| 24 |
|
| 25 |
secret, err := types.Secret("SECRET").MarshalText() |
| 26 |
|
| 27 |
require.NoError(t, err) |
| 28 |
assert.Equal(t, []byte("SECRET"), secret) |
| 29 |
} |
| 30 |
|
| 31 |
func TestSecretUnmarshalText(t *testing.T) { |
| 32 |
t.Parallel() |
| 33 |
|
| 34 |
var secret types.Secret |
| 35 |
|
| 36 |
require.NoError(t, secret.UnmarshalText([]byte("SECRET"))) |
| 37 |
assert.Equal(t, types.Secret("SECRET"), secret) |
| 38 |
} |
| 39 |
|
| 40 |
func TestSecretUnmarshalTextFile(t *testing.T) { |
| 41 |
t.Parallel() |
| 42 |
|
| 43 |
var secret types.Secret |
| 44 |
|
| 45 |
filePath := path.Join(t.TempDir(), "test.file") |
| 46 |
|
| 47 |
require.NoError(t, os.WriteFile(filePath, []byte("SECRET"), 0o600)) |
| 48 |
require.NoError(t, secret.UnmarshalText([]byte("file://"+filePath))) |
| 49 |
assert.Equal(t, types.Secret("SECRET"), secret) |
| 50 |
} |
| 51 |
|
| 52 |
func TestSecretMarshalJSON(t *testing.T) { |
| 53 |
t.Parallel() |
| 54 |
|
| 55 |
tests := []struct { |
| 56 |
name string |
| 57 |
secret types.Secret |
| 58 |
expected string |
| 59 |
}{ |
| 60 |
{ |
| 61 |
name: "empty", |
| 62 |
expected: `""`, |
| 63 |
}, |
| 64 |
{ |
| 65 |
name: "redacted", |
| 66 |
secret: types.Secret("SECRET"), |
| 67 |
expected: `"***"`, |
| 68 |
}, |
| 69 |
} |
| 70 |
|
| 71 |
for _, testCase := range tests { |
| 72 |
t.Run(testCase.name, func(t *testing.T) { |
| 73 |
t.Parallel() |
| 74 |
|
| 75 |
body, err := json.Marshal(testCase.secret) |
| 76 |
|
| 77 |
require.NoError(t, err) |
| 78 |
assert.JSONEq(t, testCase.expected, string(body)) |
| 79 |
}) |
| 80 |
} |
| 81 |
} |