forked from jfrog/vault-plugin-secrets-artifactory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_roles_test.go
179 lines (147 loc) · 4.68 KB
/
path_roles_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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package artifactory
import (
"context"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
)
func TestAcceptanceBackend_PathRole(t *testing.T) {
if !runAcceptanceTests {
t.SkipNow()
}
accTestEnv, err := newAcceptanceTestEnv()
if err != nil {
t.Fatal(err)
}
t.Run("configure backend", accTestEnv.UpdatePathConfig)
t.Run("create", accTestEnv.CreatePathRole)
t.Run("read", accTestEnv.ReadPathRole)
t.Run("delete", accTestEnv.DeletePathRole)
t.Run("cleanup backend", accTestEnv.DeletePathConfig)
}
// When there are no roles, no error should be returned.
func TestBackend_PathRoleList_Empty(t *testing.T) {
b, config := makeBackend(t)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "roles",
Storage: config.StorageView,
})
assert.NotNil(t, resp)
assert.NoError(t, err)
assert.Empty(t, resp.Warnings)
assert.False(t, resp.IsError())
}
// The backend must be configured before it will accept roles
func TestBackend_PathRoleList_CannotAddRoleWhenNotConfigured(t *testing.T) {
b, config := makeBackend(t)
roleData := map[string]interface{}{
"username": "test-username",
"scope": "test-scope",
}
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "roles/test-role",
Storage: config.StorageView,
Data: roleData,
})
assert.NotNil(t, resp)
assert.NoError(t, err)
assert.Empty(t, resp.Warnings)
assert.True(t, resp.IsError())
}
// A configured backend must accept a new role.
func TestBackend_PathRoleList_AddRole(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockArtifactoryUsageVersionRequests("")
b, config := configuredBackend(t, map[string]interface{}{
"access_token": "test-access-token",
"url": "http://myserver.com:80",
})
roleData := map[string]interface{}{
"role": "test-role",
"username": "test-username",
"scope": "test-scope",
}
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "roles/test-role",
Storage: config.StorageView,
Data: roleData,
})
assert.Nil(t, resp)
assert.NoError(t, err)
}
// Listing roles must return the name of the role.
func TestBackend_PathRoleListReturnsRole(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockArtifactoryUsageVersionRequests("")
b, config := configuredBackend(t, map[string]interface{}{
"access_token": "test-access-token",
"url": "http://myserver.com:80",
})
roleData := map[string]interface{}{
"role": "test-role",
"username": "test-username",
"scope": "test-scope",
}
_, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "roles/test-role",
Storage: config.StorageView,
Data: roleData,
})
assert.NoError(t, err)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ListOperation,
Path: "roles",
Storage: config.StorageView,
})
assert.NotNil(t, resp)
assert.NoError(t, err)
assert.False(t, resp.IsError())
assert.Len(t, resp.Data, 1)
assert.Len(t, resp.Data["keys"], 1)
assert.EqualValues(t, "test-role", resp.Data["keys"].([]string)[0])
}
// Simple test that enforces what goes in is what comes out.
func TestBackend_PathRoleWriteThenRead(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
mockArtifactoryUsageVersionRequests("")
b, config := configuredBackend(t, map[string]interface{}{
"access_token": "test-access-token",
"url": "http://myserver.com:80",
})
roleData := map[string]interface{}{
"role": "test-role",
"username": "test-username",
"scope": "test-scope",
"audience": "test-audience",
"default_ttl": 30 * time.Minute,
"max_ttl": 45 * time.Minute,
}
_, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.UpdateOperation,
Path: "roles/test-role",
Storage: config.StorageView,
Data: roleData,
})
assert.NoError(t, err)
resp, err := b.HandleRequest(context.Background(), &logical.Request{
Operation: logical.ReadOperation,
Path: "roles/test-role",
Storage: config.StorageView,
})
assert.NotNil(t, resp)
assert.NoError(t, err)
assert.EqualValues(t, "test-username", resp.Data["username"])
assert.EqualValues(t, "test-scope", resp.Data["scope"])
assert.EqualValues(t, "test-audience", resp.Data["audience"])
assert.EqualValues(t, 30*time.Minute.Seconds(), resp.Data["default_ttl"])
assert.EqualValues(t, 45*time.Minute.Seconds(), resp.Data["max_ttl"])
}