-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
146 lines (133 loc) · 4.89 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package auth
import (
"errors"
"reflect"
"strconv"
"testing"
"github.com/rs/xid"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"github.com/surahman/FTeX/pkg/constants"
"github.com/surahman/FTeX/pkg/validator"
"gopkg.in/yaml.v3"
)
func TestAuthConfigs_Load(t *testing.T) {
keyspaceJwt := constants.AuthPrefix() + "_JWT."
keyspaceGen := constants.AuthPrefix() + "_GENERAL."
testCases := []struct {
name string
input string
expectErr require.ErrorAssertionFunc
expectErrCnt int
}{
{
name: "empty - etc dir",
input: authConfigTestData["empty"],
expectErr: require.Error,
expectErrCnt: 6,
}, {
name: "valid - etc dir",
input: authConfigTestData["valid"],
expectErr: require.NoError,
expectErrCnt: 0,
}, {
name: "no issuer - etc dir",
input: authConfigTestData["no_issuer"],
expectErr: require.Error,
expectErrCnt: 1,
}, {
name: "bcrypt cost below 4 - etc dir",
input: authConfigTestData["bcrypt_cost_below_4"],
expectErr: require.Error,
expectErrCnt: 1,
}, {
name: "bcrypt cost above 31 - etc dir",
input: authConfigTestData["bcrypt_cost_above_31"],
expectErr: require.Error,
expectErrCnt: 1,
}, {
name: "jwt expiration below 10s - etc dir",
input: authConfigTestData["jwt_expiration_below_60s"],
expectErr: require.Error,
expectErrCnt: 1,
}, {
name: "jwt key below 8 - etc dir",
input: authConfigTestData["jwt_key_below_8"],
expectErr: require.Error,
expectErrCnt: 1,
}, {
name: "jwt key above 256 - etc dir",
input: authConfigTestData["jwt_key_above_256"],
expectErr: require.Error,
expectErrCnt: 1,
}, {
name: "low refresh threshold - etc dir",
input: authConfigTestData["low_refresh_threshold"],
expectErr: require.Error,
expectErrCnt: 1,
}, {
name: "refresh_threshold_gt_expiration - etc dir",
input: authConfigTestData["refresh_threshold_gt_expiration"],
expectErr: require.Error,
expectErrCnt: 2,
}, {
name: "crypto_key_too_short- etc dir",
input: authConfigTestData["crypto_key_too_short"],
expectErr: require.Error,
expectErrCnt: 1,
}, {
name: "crypto_key_too_long- etc dir",
input: authConfigTestData["crypto_key_too_long"],
expectErr: require.Error,
expectErrCnt: 1,
},
}
for _, testCase := range testCases {
test := testCase
t.Run(test.name, func(t *testing.T) {
// Configure mock filesystem.
fs := afero.NewMemMapFs()
require.NoError(t, fs.MkdirAll(constants.EtcDir(), 0644), "Failed to create in memory directory")
require.NoError(t, afero.WriteFile(fs, constants.EtcDir()+constants.AuthFileName(),
[]byte(test.input), 0644), "Failed to write in memory file")
// Load from mock filesystem.
actual := &config{}
err := actual.Load(fs)
test.expectErr(t, err)
validationError := &validator.ValidationError{}
if errors.As(err, &validationError) {
require.Lenf(t, validationError.Errors, test.expectErrCnt, "expected errors count is incorrect: %v", err)
return
}
// Load expected struct.
expected := &config{}
require.NoError(t, yaml.Unmarshal([]byte(test.input), expected), "failed to unmarshal expected constants")
require.True(t, reflect.DeepEqual(expected, actual))
// Test configuring of environment variable.
testKey := xid.New().String()
testExpDur := int64(999)
testRefThreshold := int64(555)
testBcryptCost := 16
testIssuer := "test issuer"
testCryptoSecret := "**crypto secret set in env var**"
t.Setenv(keyspaceJwt+"KEY", testKey)
t.Setenv(keyspaceJwt+"ISSUER", testIssuer)
t.Setenv(keyspaceJwt+"EXPIRATIONDURATION", strconv.FormatInt(testExpDur, 10))
t.Setenv(keyspaceJwt+"REFRESHTHRESHOLD", strconv.FormatInt(testRefThreshold, 10))
t.Setenv(keyspaceGen+"BCRYPTCOST", strconv.Itoa(testBcryptCost))
t.Setenv(keyspaceGen+"CRYPTOSECRET", testCryptoSecret)
err = actual.Load(fs)
require.NoErrorf(t, err, "Failed to load constants file: %v", err)
require.Equal(t, testKey, actual.JWTConfig.Key, "Failed to load key environment variable into configs")
require.Equal(t, testIssuer, actual.JWTConfig.Issuer, "Failed to load issuer environment variable into configs")
require.Equal(t, testExpDur, actual.JWTConfig.ExpirationDuration,
"Failed to load duration environment variable into configs")
require.Equal(t, testRefThreshold, actual.JWTConfig.RefreshThreshold,
"Failed to load refresh threshold environment variable into configs")
require.Equal(t, testBcryptCost, actual.General.BcryptCost,
"Failed to load bcrypt cost environment variable into configs")
require.Equal(t, testCryptoSecret, actual.General.CryptoSecret,
"Failed to load crypto secret environment variable into configs")
})
}
}