-
Notifications
You must be signed in to change notification settings - Fork 965
Expand file tree
/
Copy pathresource_github_organization_custom_properties_test.go
More file actions
350 lines (314 loc) · 10.1 KB
/
resource_github_organization_custom_properties_test.go
File metadata and controls
350 lines (314 loc) · 10.1 KB
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package github
import (
"fmt"
"regexp"
"testing"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)
func TestAccGithubOrganizationCustomPropertiesValidation(t *testing.T) {
t.Run("rejects invalid values_editable_by value", func(t *testing.T) {
config := `
resource "github_organization_custom_properties" "test" {
property_name = "TestInvalidValuesEditableBy"
value_type = "string"
required = false
description = "Test invalid values_editable_by"
values_editable_by = "invalid_value"
}`
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("invalid_value"),
},
},
})
})
t.Run("rejects allowed_values for string type", func(t *testing.T) {
config := `
resource "github_organization_custom_properties" "test" {
property_name = "TestInvalidAllowedValues"
value_type = "string"
allowed_values = ["a", "b"]
}`
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("allowed_values can only be set for single_select or multi_select"),
},
},
})
})
t.Run("requires allowed_values for single_select type", func(t *testing.T) {
config := `
resource "github_organization_custom_properties" "test" {
property_name = "TestMissingAllowedValues"
value_type = "single_select"
}`
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
ExpectError: regexp.MustCompile("allowed_values is required for single_select"),
},
},
})
})
}
func TestAccGithubOrganizationCustomProperties(t *testing.T) {
t.Run("creates custom property without error", func(t *testing.T) {
config := `
resource "github_organization_custom_properties" "test" {
allowed_values = [ "Test" ]
description = "Test Description"
default_value = "Test"
property_name = "Test"
required = true
value_type = "single_select"
}`
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_organization_custom_properties.test",
"property_name", "Test",
),
)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
})
t.Run("create custom property and update them", func(t *testing.T) {
configBefore := `
resource "github_organization_custom_properties" "test" {
allowed_values = ["one"]
description = "Test Description"
property_name = "Test"
value_type = "single_select"
}`
configAfter := `
resource "github_organization_custom_properties" "test" {
allowed_values = ["one", "two"]
description = "Test Description 2"
property_name = "Test"
value_type = "single_select"
}`
const resourceName = "github_organization_custom_properties.test"
checkBefore := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "allowed_values.#", "1"),
)
checkAfter := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "allowed_values.#", "2"),
)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: configBefore,
Check: checkBefore,
},
{
Config: configAfter,
Check: checkAfter,
},
},
})
})
t.Run("imports organization custom property without error", func(t *testing.T) {
description := "Test Description Import"
propertyName := "Test"
valueType := "string"
config := fmt.Sprintf(`
resource "github_organization_custom_properties" "test" {
description = "%s"
property_name = "%s"
value_type = "%s"
}`, description, propertyName, valueType)
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_organization_custom_properties.test",
"description", description,
),
)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
{
ResourceName: "github_organization_custom_properties.test",
ImportState: true,
ImportStateVerify: true,
},
},
})
})
t.Run("creates custom property with values_editable_by without error", func(t *testing.T) {
config := `
resource "github_organization_custom_properties" "test" {
property_name = "TestValuesEditableBy"
value_type = "string"
required = false
description = "Test property for values_editable_by"
values_editable_by = "org_and_repo_actors"
}`
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_organization_custom_properties.test",
"property_name", "TestValuesEditableBy",
),
resource.TestCheckResourceAttr(
"github_organization_custom_properties.test",
"values_editable_by", "org_and_repo_actors",
),
)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
})
t.Run("backward compatibility - property without values_editable_by defaults correctly", func(t *testing.T) {
config := `
resource "github_organization_custom_properties" "test" {
property_name = "TestBackwardCompat"
value_type = "string"
required = false
description = "Test property without values_editable_by"
}`
check := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"github_organization_custom_properties.test",
"property_name", "TestBackwardCompat",
),
// When not specified, API returns "org_actors" as the default
resource.TestCheckResourceAttr(
"github_organization_custom_properties.test",
"values_editable_by", "org_actors",
),
)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: config,
Check: check,
},
},
})
})
t.Run("update values_editable_by from org_actors to org_and_repo_actors", func(t *testing.T) {
configBefore := `
resource "github_organization_custom_properties" "test" {
property_name = "TestUpdateValuesEditableBy"
value_type = "string"
required = false
description = "Test updating values_editable_by"
values_editable_by = "org_actors"
}`
configAfter := `
resource "github_organization_custom_properties" "test" {
property_name = "TestUpdateValuesEditableBy"
value_type = "string"
required = false
description = "Test updating values_editable_by"
values_editable_by = "org_and_repo_actors"
}`
const resourceName = "github_organization_custom_properties.test"
checkBefore := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "values_editable_by", "org_actors"),
)
checkAfter := resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "values_editable_by", "org_and_repo_actors"),
)
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: configBefore,
Check: checkBefore,
},
{
Config: configAfter,
Check: checkAfter,
},
},
})
})
t.Run("imports existing property with values_editable_by set via UI", func(t *testing.T) {
// This test simulates a scenario where values_editable_by was set to
// org_and_repo_actors in the GitHub UI before Terraform support was added.
// The resource config intentionally omits values_editable_by to verify
// Terraform can read and maintain the existing value from the API.
configWithoutField := `
resource "github_organization_custom_properties" "test" {
property_name = "TestImportWithUISet"
value_type = "string"
required = false
description = "Test property set via UI"
}`
// After import, we explicitly set the value in config to match what's in the API
configWithField := `
resource "github_organization_custom_properties" "test" {
property_name = "TestImportWithUISet"
value_type = "string"
required = false
description = "Test property set via UI"
values_editable_by = "org_and_repo_actors"
}`
const resourceName = "github_organization_custom_properties.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { skipUnlessHasOrgs(t) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
// First, create a property with values_editable_by set
Config: configWithField,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "values_editable_by", "org_and_repo_actors"),
),
},
{
// Simulate the scenario: config doesn't have values_editable_by
// (as it would have been before Terraform support was added)
// Terraform should read the existing value from the API
Config: configWithoutField,
Check: resource.ComposeTestCheckFunc(
// Terraform should still see the value from the API
resource.TestCheckResourceAttr(resourceName, "values_editable_by", "org_and_repo_actors"),
),
},
{
// Now add it back to the config - should be no changes needed
Config: configWithField,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "values_editable_by", "org_and_repo_actors"),
),
},
},
})
})
}