-
Notifications
You must be signed in to change notification settings - Fork 1
/
path_role_test.go
111 lines (95 loc) · 2.9 KB
/
path_role_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
package exoscale
import (
"context"
"strings"
"testing"
"github.com/hashicorp/vault/sdk/logical"
)
var (
testRoleName = "read-only"
testRole = backendRole{Validator: defaultRoleValidator}
)
func (ts *backendTestSuite) TestPathRoleWrite() {
tests := []struct {
name string
resCheckFunc func(*backendTestSuite, *logical.Response, error)
reqData map[string]interface{}
wantErr bool
}{
{
name: "fail_bad_validator",
resCheckFunc: func(ts *backendTestSuite, res *logical.Response, err error) {
ts.Require().True(strings.Contains(res.Error().Error(), "invalid field value: validator"))
},
reqData: map[string]interface{}{
roleKeyValidator: "lolnope",
},
},
{
name: "ok",
resCheckFunc: func(ts *backendTestSuite, res *logical.Response, _ error) {
var actual backendRole
entry, err := ts.storage.Get(context.Background(), roleStoragePathPrefix+testRoleName)
ts.Require().NoError(err)
ts.Require().NoError(entry.DecodeJSON(&actual))
ts.Require().Equal(testRole, actual)
},
reqData: map[string]interface{}{
roleKeyValidator: testRole.Validator,
},
},
}
for _, tt := range tests {
ts.T().Run(tt.name, func(t *testing.T) {
res, err := ts.backend.HandleRequest(context.Background(), &logical.Request{
Storage: ts.storage,
Operation: logical.CreateOperation,
Path: roleStoragePathPrefix + testRoleName,
Data: tt.reqData,
})
if err != nil != tt.wantErr {
t.Fatalf("error = %v, wantErr %v", err, tt.wantErr)
return
}
tt.resCheckFunc(ts, res, err)
})
}
}
func (ts *backendTestSuite) TestPathRoleRead() {
ts.storeEntry(roleStoragePathPrefix+testRoleName, testRole)
res, err := ts.backend.HandleRequest(context.Background(), &logical.Request{
Storage: ts.storage,
Operation: logical.ReadOperation,
Path: roleStoragePathPrefix + testRoleName,
})
if err != nil {
ts.FailNow("request failed", err)
}
ts.Require().Equal(testRole.Validator, res.Data[roleKeyValidator].(string))
}
func (ts *backendTestSuite) TestPathRoleList() {
ts.storeEntry(roleStoragePathPrefix+testRoleName, testRole)
res, err := ts.backend.HandleRequest(context.Background(), &logical.Request{
Storage: ts.storage,
Operation: logical.ListOperation,
Path: roleStoragePathPrefix,
})
if err != nil {
ts.FailNow("request failed", err)
}
ts.Require().Equal(logical.ListResponse([]string{testRoleName}), res)
}
func (ts *backendTestSuite) TestPathRoleDelete() {
ts.storeEntry(roleStoragePathPrefix+testRoleName, testRole)
_, err := ts.backend.HandleRequest(context.Background(), &logical.Request{
Storage: ts.storage,
Operation: logical.DeleteOperation,
Path: roleStoragePathPrefix + testRoleName,
})
if err != nil {
ts.FailNow("request failed", err)
}
res, err := ts.storage.Get(context.Background(), roleStoragePathPrefix+testRoleName)
ts.Require().NoError(err)
ts.Require().Nil(res)
}