This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
path_roles_test.go
148 lines (124 loc) · 3.92 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
package artifactory
import (
"context"
"testing"
"time"
"github.com/hashicorp/vault/sdk/logical"
"github.com/stretchr/testify/assert"
)
// When there are no roles, an error must 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.True(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{}{
"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.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) {
b, config := configuredBackend(t, map[string]interface{}{
"access_token": "test-access-token",
"url": "test-url",
})
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) {
b, config := configuredBackend(t, map[string]interface{}{
"access_token": "test-access-token",
"url": "test-url",
})
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) {
b, config := configuredBackend(t, map[string]interface{}{
"access_token": "ignored",
"url": "ignored",
})
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"])
}