-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
102 lines (94 loc) · 3.28 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
package rest
import (
"reflect"
"strconv"
"testing"
"github.com/rs/xid"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
"github.com/surahman/mcq-platform/pkg/constants"
"github.com/surahman/mcq-platform/pkg/validator"
"gopkg.in/yaml.v3"
)
func TestRestConfigs_Load(t *testing.T) {
keyspaceGen := constants.GetHTTPRESTPrefix() + "_SERVER."
keyspaceAuth := constants.GetHTTPRESTPrefix() + "_AUTHORIZATION."
testCases := []struct {
name string
input string
expectErr require.ErrorAssertionFunc
expectLen int
}{
// ----- test cases start ----- //
{
"empty - etc dir",
restConfigTestData["empty"],
require.Error,
5,
}, {
"valid - etc dir",
restConfigTestData["valid"],
require.NoError,
0,
}, {
"out of range port - etc dir",
restConfigTestData["out of range port"],
require.Error,
1,
}, {
"no base path - etc dir",
restConfigTestData["no base path"],
require.Error,
1,
}, {
"no swagger path - etc dir",
restConfigTestData["no swagger path"],
require.Error,
1,
}, {
"no auth header - etc dir",
restConfigTestData["no auth header"],
require.Error,
1,
},
// ----- test cases end ----- //
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
// Configure mock filesystem.
fs := afero.NewMemMapFs()
require.NoError(t, fs.MkdirAll(constants.GetEtcDir(), 0644), "Failed to create in memory directory")
require.NoError(t, afero.WriteFile(fs, constants.GetEtcDir()+constants.GetHTTPRESTFileName(), []byte(testCase.input), 0644), "Failed to write in memory file")
// Load from mock filesystem.
actual := &config{}
err := actual.Load(fs)
testCase.expectErr(t, err)
if err != nil {
require.Equalf(t, testCase.expectLen, len(err.(*validator.ErrorValidation).Errors), "Expected errors count is incorrect: %v", err)
return
}
// Load expected struct.
expected := &config{}
require.NoError(t, yaml.Unmarshal([]byte(testCase.input), expected), "failed to unmarshal expected constants")
require.True(t, reflect.DeepEqual(expected, actual))
// Test configuring of environment variable.
basePath := xid.New().String()
swaggerPath := xid.New().String()
headerKey := xid.New().String()
portNumber := 1600
shutdownDelay := 36
t.Setenv(keyspaceGen+"BASE_PATH", basePath)
t.Setenv(keyspaceGen+"SWAGGER_PATH", swaggerPath)
t.Setenv(keyspaceGen+"PORT_NUMBER", strconv.Itoa(portNumber))
t.Setenv(keyspaceGen+"SHUTDOWN_DELAY", strconv.Itoa(shutdownDelay))
t.Setenv(keyspaceAuth+"HEADER_KEY", headerKey)
err = actual.Load(fs)
require.NoErrorf(t, err, "Failed to load constants file: %v", err)
require.Equal(t, basePath, actual.Server.BasePath, "Failed to load base path environment variable into configs")
require.Equal(t, swaggerPath, actual.Server.SwaggerPath, "Failed to load swagger path environment variable into configs")
require.Equal(t, portNumber, actual.Server.PortNumber, "Failed to load port environment variable into configs")
require.Equal(t, shutdownDelay, actual.Server.ShutdownDelay, "Failed to load shutdown delay environment variable into configs")
require.Equal(t, headerKey, actual.Authorization.HeaderKey, "Failed to load authorization header key environment variable into configs")
})
}
}