forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_rules_test.go
More file actions
475 lines (381 loc) · 13.5 KB
/
util_rules_test.go
File metadata and controls
475 lines (381 loc) · 13.5 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
package github
import (
"testing"
"github.com/google/go-github/v81/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func TestExpandRulesBasicRules(t *testing.T) {
// Test expanding basic boolean rules with RepositoryRulesetRules
rulesMap := map[string]any{
"creation": true,
"deletion": true,
"required_linear_history": true,
"required_signatures": false,
"non_fast_forward": true,
}
input := []any{rulesMap}
result := expandRules(input, false)
if result == nil {
t.Fatal("Expected result to not be nil")
return
}
// Check boolean rules - they use EmptyRuleParameters and are nil when false
if result.Creation == nil {
t.Error("Expected Creation rule to be set")
}
if result.Deletion == nil {
t.Error("Expected Deletion rule to be set")
}
if result.RequiredLinearHistory == nil {
t.Error("Expected RequiredLinearHistory rule to be set")
}
if result.RequiredSignatures != nil {
t.Error("Expected RequiredSignatures rule to be nil (false)")
}
if result.NonFastForward == nil {
t.Error("Expected NonFastForward rule to be set")
}
}
func TestFlattenRulesBasicRules(t *testing.T) {
// Test flattening basic boolean rules with RepositoryRulesetRules
rules := &github.RepositoryRulesetRules{
Creation: &github.EmptyRuleParameters{},
Deletion: &github.EmptyRuleParameters{},
RequiredLinearHistory: &github.EmptyRuleParameters{},
RequiredSignatures: nil, // false means nil
NonFastForward: &github.EmptyRuleParameters{},
}
result := flattenRules(rules, false)
if len(result) != 1 {
t.Fatalf("Expected 1 element in result, got %d", len(result))
}
rulesMap := result[0].(map[string]any)
// Should contain the rules
if !rulesMap["creation"].(bool) {
t.Error("Expected creation rule to be true")
}
if !rulesMap["deletion"].(bool) {
t.Error("Expected deletion rule to be true")
}
if !rulesMap["required_linear_history"].(bool) {
t.Error("Expected required_linear_history rule to be true")
}
if rulesMap["required_signatures"].(bool) {
t.Error("Expected required_signatures rule to be false")
}
if !rulesMap["non_fast_forward"].(bool) {
t.Error("Expected non_fast_forward rule to be true")
}
}
func TestExpandRulesMaxFilePathLength(t *testing.T) {
// Test that max_file_path_length rule is properly expanded
maxPathLength := 512
rulesMap := map[string]any{
"max_file_path_length": []any{
map[string]any{
"max_file_path_length": maxPathLength,
},
},
}
input := []any{rulesMap}
result := expandRules(input, false)
if result == nil {
t.Fatal("Expected result to not be nil")
return
}
if result.MaxFilePathLength == nil {
t.Fatal("Expected MaxFilePathLength rule to be set")
return
}
if result.MaxFilePathLength.MaxFilePathLength != maxPathLength {
t.Errorf("Expected MaxFilePathLength to be %d, got %d", maxPathLength, result.MaxFilePathLength.MaxFilePathLength)
}
}
func TestFlattenRulesMaxFilePathLength(t *testing.T) {
// Test that max_file_path_length rule is properly flattened
maxPathLength := 256
rules := &github.RepositoryRulesetRules{
MaxFilePathLength: &github.MaxFilePathLengthRuleParameters{
MaxFilePathLength: maxPathLength,
},
}
result := flattenRules(rules, false)
if len(result) != 1 {
t.Fatalf("Expected 1 element in result, got %d", len(result))
}
rulesMap := result[0].(map[string]any)
maxFilePathLengthRules := rulesMap["max_file_path_length"].([]map[string]any)
if len(maxFilePathLengthRules) != 1 {
t.Fatalf("Expected 1 max_file_path_length rule, got %d", len(maxFilePathLengthRules))
}
if maxFilePathLengthRules[0]["max_file_path_length"] != maxPathLength {
t.Errorf("Expected max_file_path_length to be %d, got %v", maxPathLength, maxFilePathLengthRules[0]["max_file_path_length"])
}
}
func TestRoundTripMaxFilePathLength(t *testing.T) {
// Test that max_file_path_length rule survives expand -> flatten round trip
maxPathLength := 1024
// Start with terraform configuration
rulesMap := map[string]any{
"max_file_path_length": []any{
map[string]any{
"max_file_path_length": maxPathLength,
},
},
}
input := []any{rulesMap}
// Expand to GitHub API format
expandedRules := expandRules(input, false)
if expandedRules == nil {
t.Fatal("Expected expandedRules to not be nil")
}
// Flatten back to terraform format
flattenedResult := flattenRules(expandedRules, false)
if len(flattenedResult) != 1 {
t.Fatalf("Expected 1 flattened result, got %d", len(flattenedResult))
}
flattenedRulesMap := flattenedResult[0].(map[string]any)
maxFilePathLengthRules := flattenedRulesMap["max_file_path_length"].([]map[string]any)
if len(maxFilePathLengthRules) != 1 {
t.Fatalf("Expected 1 max_file_path_length rule after round trip, got %d", len(maxFilePathLengthRules))
}
if maxFilePathLengthRules[0]["max_file_path_length"] != maxPathLength {
t.Errorf("Expected max_file_path_length to be %d after round trip, got %v", maxPathLength, maxFilePathLengthRules[0]["max_file_path_length"])
}
}
func TestExpandRulesMaxFileSize(t *testing.T) {
// Test that max_file_size rule is properly expanded
maxFileSize := int64(1048576) // 1MB
rulesMap := map[string]any{
"max_file_size": []any{
map[string]any{
"max_file_size": float64(maxFileSize),
},
},
}
input := []any{rulesMap}
result := expandRules(input, false)
if result == nil {
t.Fatal("Expected result to not be nil")
return
}
if result.MaxFileSize == nil {
t.Fatal("Expected MaxFileSize rule to be set")
return
}
if result.MaxFileSize.MaxFileSize != maxFileSize {
t.Errorf("Expected MaxFileSize to be %d, got %d", maxFileSize, result.MaxFileSize.MaxFileSize)
}
}
func TestFlattenRulesMaxFileSize(t *testing.T) {
// Test that max_file_size rule is properly flattened
maxFileSize := int64(5242880) // 5MB
rules := &github.RepositoryRulesetRules{
MaxFileSize: &github.MaxFileSizeRuleParameters{
MaxFileSize: maxFileSize,
},
}
result := flattenRules(rules, false)
if len(result) != 1 {
t.Fatalf("Expected 1 element in result, got %d", len(result))
}
rulesMap := result[0].(map[string]any)
maxFileSizeRules := rulesMap["max_file_size"].([]map[string]any)
if len(maxFileSizeRules) != 1 {
t.Fatalf("Expected 1 max_file_size rule, got %d", len(maxFileSizeRules))
}
if maxFileSizeRules[0]["max_file_size"] != maxFileSize {
t.Errorf("Expected max_file_size to be %d, got %v", maxFileSize, maxFileSizeRules[0]["max_file_size"])
}
}
func TestExpandRulesFileExtensionRestriction(t *testing.T) {
// Test that file_extension_restriction rule is properly expanded
restrictedExtensions := []string{".exe", ".bat", ".com"}
rulesMap := map[string]any{
"file_extension_restriction": []any{
map[string]any{
"restricted_file_extensions": schema.NewSet(schema.HashString, []any{".exe", ".bat", ".com"}),
},
},
}
input := []any{rulesMap}
result := expandRules(input, false)
if result == nil {
t.Fatal("Expected result to not be nil")
return
}
if result.FileExtensionRestriction == nil {
t.Fatal("Expected FileExtensionRestriction rule to be set")
return
}
if len(result.FileExtensionRestriction.RestrictedFileExtensions) != len(restrictedExtensions) {
t.Errorf("Expected %d restricted extensions, got %d", len(restrictedExtensions), len(result.FileExtensionRestriction.RestrictedFileExtensions))
}
resultExtensions := make(map[string]bool)
for _, ext := range result.FileExtensionRestriction.RestrictedFileExtensions {
resultExtensions[ext] = true
}
for _, expectedExt := range restrictedExtensions {
if !resultExtensions[expectedExt] {
t.Errorf("Expected extension %s not found in result", expectedExt)
}
}
}
func TestFlattenRulesFileExtensionRestriction(t *testing.T) {
// Test that file_extension_restriction rule is properly flattened
restrictedExtensions := []string{".exe", ".bat", ".com"}
rules := &github.RepositoryRulesetRules{
FileExtensionRestriction: &github.FileExtensionRestrictionRuleParameters{
RestrictedFileExtensions: restrictedExtensions,
},
}
result := flattenRules(rules, false)
if len(result) != 1 {
t.Fatalf("Expected 1 element in result, got %d", len(result))
}
rulesMap := result[0].(map[string]any)
fileExtensionRules := rulesMap["file_extension_restriction"].([]map[string]any)
if len(fileExtensionRules) != 1 {
t.Fatalf("Expected 1 file_extension_restriction rule, got %d", len(fileExtensionRules))
}
actualExtensions := fileExtensionRules[0]["restricted_file_extensions"].([]string)
if len(actualExtensions) != len(restrictedExtensions) {
t.Errorf("Expected %d restricted extensions, got %d", len(restrictedExtensions), len(actualExtensions))
}
for i, ext := range restrictedExtensions {
if actualExtensions[i] != ext {
t.Errorf("Expected extension %s at index %d, got %s", ext, i, actualExtensions[i])
}
}
}
func TestCompletePushRulesetSupport(t *testing.T) {
// Test that all push-specific rules are supported together
rulesMap := map[string]any{
"file_path_restriction": []any{
map[string]any{
"restricted_file_paths": []any{"secrets/", "*.key", "private/"},
},
},
"max_file_size": []any{
map[string]any{
"max_file_size": 5, // 5MB
},
},
"max_file_path_length": []any{
map[string]any{
"max_file_path_length": 300,
},
},
"file_extension_restriction": []any{
map[string]any{
"restricted_file_extensions": schema.NewSet(schema.HashString, []any{".exe", ".bat", ".sh"}),
},
},
}
input := []any{rulesMap}
// Expand to GitHub API format
expandedRules := expandRules(input, false)
if expandedRules == nil {
t.Fatal("Expected expandedRules to not be nil")
return
}
// Count how many rules we have
ruleCount := 0
if expandedRules.FilePathRestriction != nil {
ruleCount++
}
if expandedRules.MaxFileSize != nil {
ruleCount++
}
if expandedRules.MaxFilePathLength != nil {
ruleCount++
}
if expandedRules.FileExtensionRestriction != nil {
ruleCount++
}
if ruleCount != 4 {
t.Fatalf("Expected 4 expanded rules for complete push ruleset, got %d", ruleCount)
}
// Flatten back to terraform format
flattenedResult := flattenRules(expandedRules, false)
if len(flattenedResult) != 1 {
t.Fatalf("Expected 1 flattened result, got %d", len(flattenedResult))
}
flattenedRulesMap := flattenedResult[0].(map[string]any)
// Verify file_path_restriction
filePathRules := flattenedRulesMap["file_path_restriction"].([]map[string]any)
if len(filePathRules) != 1 {
t.Fatalf("Expected 1 file_path_restriction rule, got %d", len(filePathRules))
}
restrictedPaths := filePathRules[0]["restricted_file_paths"].([]string)
if len(restrictedPaths) != 3 {
t.Errorf("Expected 3 restricted file paths, got %d", len(restrictedPaths))
}
// Verify max_file_size
maxFileSizeRules := flattenedRulesMap["max_file_size"].([]map[string]any)
if len(maxFileSizeRules) != 1 {
t.Fatalf("Expected 1 max_file_size rule, got %d", len(maxFileSizeRules))
}
if maxFileSizeRules[0]["max_file_size"] != int64(5) {
t.Errorf("Expected max_file_size to be 5, got %v", maxFileSizeRules[0]["max_file_size"])
}
// Verify max_file_path_length
maxFilePathLengthRules := flattenedRulesMap["max_file_path_length"].([]map[string]any)
if len(maxFilePathLengthRules) != 1 {
t.Fatalf("Expected 1 max_file_path_length rule, got %d", len(maxFilePathLengthRules))
}
if maxFilePathLengthRules[0]["max_file_path_length"] != 300 {
t.Errorf("Expected max_file_path_length to be 300, got %v", maxFilePathLengthRules[0]["max_file_path_length"])
}
// Verify file_extension_restriction
fileExtRules := flattenedRulesMap["file_extension_restriction"].([]map[string]any)
if len(fileExtRules) != 1 {
t.Fatalf("Expected 1 file_extension_restriction rule, got %d", len(fileExtRules))
}
restrictedExts := fileExtRules[0]["restricted_file_extensions"].([]string)
if len(restrictedExts) != 3 {
t.Errorf("Expected 3 restricted file extensions, got %d", len(restrictedExts))
}
}
func TestCopilotCodeReviewRoundTrip(t *testing.T) {
// Test that copilot_code_review rule survives expand -> flatten round trip
rulesMap := map[string]any{
"copilot_code_review": []any{
map[string]any{
"review_on_push": true,
"review_draft_pull_requests": false,
},
},
}
input := []any{rulesMap}
// Expand to GitHub API format
expandedRules := expandRules(input, false)
if expandedRules == nil {
t.Fatal("Expected expandedRules to not be nil")
}
if expandedRules.CopilotCodeReview == nil {
t.Fatal("Expected CopilotCodeReview rule to be set")
}
if expandedRules.CopilotCodeReview.ReviewOnPush != true {
t.Errorf("Expected ReviewOnPush to be true, got %v", expandedRules.CopilotCodeReview.ReviewOnPush)
}
if expandedRules.CopilotCodeReview.ReviewDraftPullRequests != false {
t.Errorf("Expected ReviewDraftPullRequests to be false, got %v", expandedRules.CopilotCodeReview.ReviewDraftPullRequests)
}
// Flatten back to terraform format
flattenedResult := flattenRules(expandedRules, false)
if len(flattenedResult) != 1 {
t.Fatalf("Expected 1 flattened result, got %d", len(flattenedResult))
}
flattenedRulesMap := flattenedResult[0].(map[string]any)
copilotRules := flattenedRulesMap["copilot_code_review"].([]map[string]any)
if len(copilotRules) != 1 {
t.Fatalf("Expected 1 copilot_code_review rule after round trip, got %d", len(copilotRules))
}
if copilotRules[0]["review_on_push"] != true {
t.Errorf("Expected review_on_push to be true, got %v", copilotRules[0]["review_on_push"])
}
if copilotRules[0]["review_draft_pull_requests"] != false {
t.Errorf("Expected review_draft_pull_requests to be false, got %v", copilotRules[0]["review_draft_pull_requests"])
}
}