-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
entry_config_update_form_field_data_test.go
99 lines (93 loc) · 2.78 KB
/
entry_config_update_form_field_data_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
package gitlab_test
import (
"testing"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault/sdk/framework"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gitlab "github.com/ilijamt/vault-plugin-secrets-gitlab"
)
func TestEntryConfigUpdateFromFieldData(t *testing.T) {
t.Run("nil data", func(t *testing.T) {
e := new(gitlab.EntryConfig)
_, err := e.UpdateFromFieldData(nil)
require.ErrorIs(t, err, gitlab.ErrNilValue)
})
var tests = []struct {
name string
raw map[string]interface{}
expectedConfig *gitlab.EntryConfig
warnings []string
err bool
errMap map[string]int
}{
{
name: "no data should fail",
raw: map[string]interface{}{},
err: true,
warnings: []string{"auto_rotate_token not specified setting to 24h0m0s"},
errMap: map[string]int{
gitlab.ErrFieldRequired.Error(): 3,
},
},
{
name: "empty token and invalid type",
raw: map[string]interface{}{
"base_url": "https://gitlab.com",
"type": "type",
},
expectedConfig: &gitlab.EntryConfig{AutoRotateBefore: gitlab.DefaultAutoRotateBeforeMinTTL, BaseURL: "https://gitlab.com"},
warnings: []string{"auto_rotate_token not specified setting to 24h0m0s"},
err: true,
errMap: map[string]int{
gitlab.ErrFieldRequired.Error(): 1,
gitlab.ErrUnknownType.Error(): 1,
},
},
{
name: "unconvertible data type",
expectedConfig: &gitlab.EntryConfig{},
raw: map[string]interface{}{
"token": struct{}{},
},
err: true,
errMap: map[string]int{},
},
{
name: "valid config",
expectedConfig: &gitlab.EntryConfig{
Token: "token",
Type: gitlab.TypeSelfManaged,
AutoRotateToken: false,
AutoRotateBefore: gitlab.DefaultAutoRotateBeforeMinTTL,
BaseURL: "https://gitlab.com",
},
warnings: []string{"auto_rotate_token not specified setting to 24h0m0s"},
raw: map[string]interface{}{
"token": "token",
"type": gitlab.TypeSelfManaged.String(),
"base_url": "https://gitlab.com",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
e := new(gitlab.EntryConfig)
assert.Empty(t, e)
warnings, err := e.UpdateFromFieldData(&framework.FieldData{Raw: test.raw, Schema: gitlab.FieldSchemaConfig})
assert.Equal(t, test.warnings, warnings)
if test.expectedConfig == nil {
test.expectedConfig = &gitlab.EntryConfig{AutoRotateBefore: gitlab.DefaultAutoRotateBeforeMinTTL}
}
assert.EqualValues(t, test.expectedConfig, e)
if test.err {
assert.Error(t, err)
if len(test.errMap) > 0 {
assert.Equal(t, countErrByName(err.(*multierror.Error)), test.errMap)
}
} else {
assert.NoError(t, err)
}
})
}
}