forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolicy_set_parameter_test.go
290 lines (232 loc) · 8.96 KB
/
policy_set_parameter_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package tfe
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestPolicySetParametersList(t *testing.T) {
client := testClient(t)
ctx := context.Background()
orgTest, orgTestCleanup := createOrganization(t, client)
defer orgTestCleanup()
psTest, pTestCleanup := createPolicySet(t, client, orgTest, nil, nil)
defer pTestCleanup()
pTest1, pTestCleanup1 := createPolicySetParameter(t, client, psTest)
defer pTestCleanup1()
pTest2, pTestCleanup2 := createPolicySetParameter(t, client, psTest)
defer pTestCleanup2()
t.Run("without list options", func(t *testing.T) {
pl, err := client.PolicySetParameters.List(ctx, psTest.ID, PolicySetParameterListOptions{})
require.NoError(t, err)
assert.Contains(t, pl.Items, pTest1)
assert.Contains(t, pl.Items, pTest2)
t.Skip("paging not supported yet in API")
assert.Equal(t, 1, pl.CurrentPage)
assert.Equal(t, 2, pl.TotalCount)
})
t.Run("with list options", func(t *testing.T) {
t.Skip("paging not supported yet in API")
// Request a page number which is out of range. The result should
// be successful, but return no results if the paging options are
// properly passed along.
pl, err := client.PolicySetParameters.List(ctx, psTest.ID, PolicySetParameterListOptions{
ListOptions: ListOptions{
PageNumber: 999,
PageSize: 100,
},
})
require.NoError(t, err)
assert.Empty(t, pl.Items)
assert.Equal(t, 999, pl.CurrentPage)
assert.Equal(t, 2, pl.TotalCount)
})
t.Run("when policy set ID is invalid ID", func(t *testing.T) {
pl, err := client.PolicySetParameters.List(ctx, badIdentifier, PolicySetParameterListOptions{})
assert.Nil(t, pl)
assert.EqualError(t, err, "invalid value for policy set ID")
})
}
func TestPolicySetParametersCreate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
psTest, psTestCleanup := createPolicySet(t, client, nil, nil, nil)
defer psTestCleanup()
t.Run("with valid options", func(t *testing.T) {
options := PolicySetParameterCreateOptions{
Key: String(randomString(t)),
Value: String(randomString(t)),
Category: Category(CategoryPolicySet),
}
p, err := client.PolicySetParameters.Create(ctx, psTest.ID, options)
require.NoError(t, err)
assert.NotEmpty(t, p.ID)
assert.Equal(t, *options.Key, p.Key)
assert.Equal(t, *options.Value, p.Value)
assert.Equal(t, *options.Category, p.Category)
assert.Equal(t, psTest.ID, p.PolicySet.ID)
// The policy set isn't returned correcly by the API.
// assert.Equal(t, *options.PolicySet, v.PolicySet)
})
t.Run("when options has an empty string value", func(t *testing.T) {
options := PolicySetParameterCreateOptions{
Key: String(randomString(t)),
Value: String(""),
Category: Category(CategoryPolicySet),
}
p, err := client.PolicySetParameters.Create(ctx, psTest.ID, options)
require.NoError(t, err)
assert.NotEmpty(t, p.ID)
assert.Equal(t, *options.Key, p.Key)
assert.Equal(t, *options.Value, p.Value)
assert.Equal(t, *options.Category, p.Category)
})
t.Run("when options is missing value", func(t *testing.T) {
options := PolicySetParameterCreateOptions{
Key: String(randomString(t)),
Category: Category(CategoryPolicySet),
}
p, err := client.PolicySetParameters.Create(ctx, psTest.ID, options)
require.NoError(t, err)
assert.NotEmpty(t, p.ID)
assert.Equal(t, *options.Key, p.Key)
assert.Equal(t, "", p.Value)
assert.Equal(t, *options.Category, p.Category)
})
t.Run("when options is missing key", func(t *testing.T) {
options := PolicySetParameterCreateOptions{
Value: String(randomString(t)),
Category: Category(CategoryPolicySet),
}
_, err := client.PolicySetParameters.Create(ctx, psTest.ID, options)
assert.EqualError(t, err, "key is required")
})
t.Run("when options has an empty key", func(t *testing.T) {
options := PolicySetParameterCreateOptions{
Key: String(""),
Value: String(randomString(t)),
Category: Category(CategoryPolicySet),
}
_, err := client.PolicySetParameters.Create(ctx, psTest.ID, options)
assert.EqualError(t, err, "key is required")
})
t.Run("when options is missing category", func(t *testing.T) {
options := PolicySetParameterCreateOptions{
Key: String(randomString(t)),
Value: String(randomString(t)),
}
_, err := client.PolicySetParameters.Create(ctx, psTest.ID, options)
assert.EqualError(t, err, "category is required")
})
t.Run("when policy set ID is invalid", func(t *testing.T) {
options := PolicySetParameterCreateOptions{
Key: String(randomString(t)),
Value: String(randomString(t)),
Category: Category(CategoryPolicySet),
}
_, err := client.PolicySetParameters.Create(ctx, badIdentifier, options)
assert.EqualError(t, err, "invalid value for policy set ID")
})
}
func TestPolicySetParametersRead(t *testing.T) {
client := testClient(t)
ctx := context.Background()
pTest, pTestCleanup := createPolicySetParameter(t, client, nil)
defer pTestCleanup()
t.Run("when the parameter exists", func(t *testing.T) {
p, err := client.PolicySetParameters.Read(ctx, pTest.PolicySet.ID, pTest.ID)
require.NoError(t, err)
assert.Equal(t, pTest.ID, p.ID)
assert.Equal(t, pTest.Category, p.Category)
assert.Equal(t, pTest.Key, p.Key)
assert.Equal(t, pTest.Sensitive, p.Sensitive)
assert.Equal(t, pTest.Value, p.Value)
assert.Equal(t, pTest.PolicySet.ID, p.PolicySet.ID)
})
t.Run("when the parameter does not exist", func(t *testing.T) {
p, err := client.PolicySetParameters.Read(ctx, pTest.PolicySet.ID, "nonexisting")
assert.Nil(t, p)
assert.Equal(t, ErrResourceNotFound, err)
})
t.Run("without a valid policy set ID", func(t *testing.T) {
p, err := client.PolicySetParameters.Read(ctx, badIdentifier, pTest.ID)
assert.Nil(t, p)
assert.EqualError(t, err, "invalid value for policy set ID")
})
t.Run("without a valid parameter ID", func(t *testing.T) {
p, err := client.PolicySetParameters.Read(ctx, pTest.PolicySet.ID, badIdentifier)
assert.Nil(t, p)
assert.EqualError(t, err, "invalid value for parameter ID")
})
}
func TestPolicySetParametersUpdate(t *testing.T) {
client := testClient(t)
ctx := context.Background()
pTest, pTestCleanup := createPolicySetParameter(t, client, nil)
defer pTestCleanup()
t.Run("with valid options", func(t *testing.T) {
options := PolicySetParameterUpdateOptions{
Key: String("newname"),
Value: String("newvalue"),
}
p, err := client.PolicySetParameters.Update(ctx, pTest.PolicySet.ID, pTest.ID, options)
require.NoError(t, err)
assert.Equal(t, *options.Key, p.Key)
assert.Equal(t, *options.Value, p.Value)
})
t.Run("when updating a subset of values", func(t *testing.T) {
options := PolicySetParameterUpdateOptions{
Key: String("someothername"),
}
p, err := client.PolicySetParameters.Update(ctx, pTest.PolicySet.ID, pTest.ID, options)
require.NoError(t, err)
assert.Equal(t, *options.Key, p.Key)
})
t.Run("with sensitive set", func(t *testing.T) {
options := PolicySetParameterUpdateOptions{
Sensitive: Bool(true),
}
p, err := client.PolicySetParameters.Update(ctx, pTest.PolicySet.ID, pTest.ID, options)
require.NoError(t, err)
assert.Equal(t, *options.Sensitive, p.Sensitive)
assert.Empty(t, p.Value) // Because its now sensitive
})
t.Run("without any changes", func(t *testing.T) {
pTest, pTestCleanup := createPolicySetParameter(t, client, nil)
defer pTestCleanup()
p, err := client.PolicySetParameters.Update(ctx, pTest.PolicySet.ID, pTest.ID, PolicySetParameterUpdateOptions{})
require.NoError(t, err)
assert.Equal(t, pTest, p)
})
t.Run("with invalid parameter ID", func(t *testing.T) {
_, err := client.PolicySetParameters.Update(ctx, badIdentifier, pTest.ID, PolicySetParameterUpdateOptions{})
assert.EqualError(t, err, "invalid value for policy set ID")
})
t.Run("with invalid parameter ID", func(t *testing.T) {
_, err := client.PolicySetParameters.Update(ctx, pTest.PolicySet.ID, badIdentifier, PolicySetParameterUpdateOptions{})
assert.EqualError(t, err, "invalid value for parameter ID")
})
}
func TestParametersDelete(t *testing.T) {
client := testClient(t)
ctx := context.Background()
psTest, psTestCleanup := createPolicySet(t, client, nil, nil, nil)
defer psTestCleanup()
pTest, _ := createPolicySetParameter(t, client, psTest)
t.Run("with valid options", func(t *testing.T) {
err := client.PolicySetParameters.Delete(ctx, psTest.ID, pTest.ID)
assert.NoError(t, err)
})
t.Run("with non existing parameter ID", func(t *testing.T) {
err := client.PolicySetParameters.Delete(ctx, psTest.ID, "nonexisting")
assert.Equal(t, err, ErrResourceNotFound)
})
t.Run("with invalid policy set ID", func(t *testing.T) {
err := client.PolicySetParameters.Delete(ctx, badIdentifier, pTest.ID)
assert.EqualError(t, err, "invalid value for policy set ID")
})
t.Run("with invalid parameter ID", func(t *testing.T) {
err := client.PolicySetParameters.Delete(ctx, psTest.ID, badIdentifier)
assert.EqualError(t, err, "invalid value for parameter ID")
})
}