-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathpermissions_validator_test.go
More file actions
386 lines (364 loc) · 10.8 KB
/
permissions_validator_test.go
File metadata and controls
386 lines (364 loc) · 10.8 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
//go:build !integration
package workflow
import (
"strings"
"testing"
)
func TestCollectRequiredPermissions(t *testing.T) {
tests := []struct {
name string
toolsets []string
readOnly bool
expected map[PermissionScope]PermissionLevel
}{
{
name: "Context toolset requires no permissions",
toolsets: []string{"context"},
readOnly: false,
expected: map[PermissionScope]PermissionLevel{},
},
{
name: "Repos toolset in read-write mode",
toolsets: []string{"repos"},
readOnly: false,
expected: map[PermissionScope]PermissionLevel{
PermissionContents: PermissionWrite,
},
},
{
name: "Repos toolset in read-only mode",
toolsets: []string{"repos"},
readOnly: true,
expected: map[PermissionScope]PermissionLevel{
PermissionContents: PermissionRead,
},
},
{
name: "Issues toolset in read-write mode",
toolsets: []string{"issues"},
readOnly: false,
expected: map[PermissionScope]PermissionLevel{
PermissionIssues: PermissionWrite,
},
},
{
name: "Multiple toolsets",
toolsets: []string{"repos", "issues", "pull_requests"},
readOnly: false,
expected: map[PermissionScope]PermissionLevel{
PermissionContents: PermissionWrite,
PermissionIssues: PermissionWrite,
PermissionPullRequests: PermissionWrite,
},
},
{
name: "Default toolsets in read-write mode",
toolsets: DefaultGitHubToolsets,
readOnly: false,
expected: map[PermissionScope]PermissionLevel{
PermissionContents: PermissionWrite,
PermissionIssues: PermissionWrite,
PermissionPullRequests: PermissionWrite,
},
},
{
name: "Actions toolset (read-only)",
toolsets: []string{"actions"},
readOnly: false,
expected: map[PermissionScope]PermissionLevel{
PermissionActions: PermissionRead,
},
},
{
name: "Code security toolset",
toolsets: []string{"code_security"},
readOnly: false,
expected: map[PermissionScope]PermissionLevel{
PermissionSecurityEvents: PermissionWrite,
},
},
{
name: "Discussions toolset",
toolsets: []string{"discussions"},
readOnly: false,
expected: map[PermissionScope]PermissionLevel{
PermissionDiscussions: PermissionWrite,
},
},
{
name: "Projects toolset (requires PAT - no permissions)",
toolsets: []string{"projects"},
readOnly: false,
expected: map[PermissionScope]PermissionLevel{
// No permissions required - projects require PAT, not GITHUB_TOKEN
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := collectRequiredPermissions(tt.toolsets, tt.readOnly)
if len(result) != len(tt.expected) {
t.Errorf("Expected %d permissions, got %d: %v", len(tt.expected), len(result), result)
return
}
for scope, expectedLevel := range tt.expected {
actualLevel, found := result[scope]
if !found {
t.Errorf("Expected permission %s not found in result", scope)
continue
}
if actualLevel != expectedLevel {
t.Errorf("Permission %s: expected level %s, got %s", scope, expectedLevel, actualLevel)
}
}
})
}
}
func TestValidatePermissions_MissingPermissions(t *testing.T) {
tests := []struct {
name string
permissions *Permissions
githubToolConfig *GitHubToolConfig
expectMissing map[PermissionScope]PermissionLevel
expectMissingCount int
expectHasIssues bool
}{
{
name: "No GitHub tool configured",
permissions: NewPermissions(),
githubToolConfig: nil,
expectMissing: map[PermissionScope]PermissionLevel{},
expectMissingCount: 0,
expectHasIssues: false,
},
{
name: "Default toolsets with no permissions",
permissions: NewPermissions(),
githubToolConfig: &GitHubToolConfig{
Toolset: GitHubToolsets{"default"},
},
expectMissingCount: 3, // contents, issues, pull-requests
expectHasIssues: true,
},
{
name: "Default toolsets with all required permissions",
permissions: NewPermissionsFromMap(map[PermissionScope]PermissionLevel{
PermissionContents: PermissionWrite,
PermissionIssues: PermissionWrite,
PermissionPullRequests: PermissionWrite,
}),
githubToolConfig: &GitHubToolConfig{
Toolset: GitHubToolsets{"default"},
ReadOnly: false,
},
expectMissingCount: 0,
expectHasIssues: false,
},
{
name: "Default toolsets with only read permissions (missing write)",
permissions: NewPermissionsFromMap(map[PermissionScope]PermissionLevel{
PermissionContents: PermissionRead,
PermissionIssues: PermissionRead,
PermissionPullRequests: PermissionRead,
}),
githubToolConfig: &GitHubToolConfig{
Toolset: GitHubToolsets{"default"},
ReadOnly: false, // Need write permissions
},
expectMissingCount: 3, // All need write
expectHasIssues: true,
},
{
name: "Read-only mode with read permissions",
permissions: NewPermissionsFromMap(map[PermissionScope]PermissionLevel{
PermissionContents: PermissionRead,
PermissionIssues: PermissionRead,
PermissionPullRequests: PermissionRead,
}),
githubToolConfig: &GitHubToolConfig{
Toolset: GitHubToolsets{"default"},
ReadOnly: true,
},
expectMissingCount: 0,
expectHasIssues: false,
},
{
name: "Specific toolsets with partial permissions",
permissions: NewPermissionsFromMap(map[PermissionScope]PermissionLevel{
PermissionContents: PermissionWrite,
}),
githubToolConfig: &GitHubToolConfig{
Toolset: GitHubToolsets{"repos", "issues"},
ReadOnly: false,
},
expectMissingCount: 1, // Missing issues: write
expectHasIssues: true,
},
{
name: "Actions toolset with read permission",
permissions: NewPermissionsFromMap(map[PermissionScope]PermissionLevel{
PermissionActions: PermissionRead,
}),
githubToolConfig: &GitHubToolConfig{
Toolset: GitHubToolsets{"actions"},
},
expectMissingCount: 0,
expectHasIssues: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ValidatePermissions(tt.permissions, tt.githubToolConfig)
if len(result.MissingPermissions) != tt.expectMissingCount {
t.Errorf("Expected %d missing permissions, got %d: %v",
tt.expectMissingCount, len(result.MissingPermissions), result.MissingPermissions)
}
if result.HasValidationIssues != tt.expectHasIssues {
t.Errorf("Expected HasValidationIssues=%v, got %v", tt.expectHasIssues, result.HasValidationIssues)
}
if tt.expectMissing != nil {
for scope, expectedLevel := range tt.expectMissing {
actualLevel, found := result.MissingPermissions[scope]
if !found {
t.Errorf("Expected missing permission %s not found", scope)
continue
}
if actualLevel != expectedLevel {
t.Errorf("Missing permission %s: expected level %s, got %s", scope, expectedLevel, actualLevel)
}
}
}
})
}
}
func TestFormatValidationMessage(t *testing.T) {
tests := []struct {
name string
result *PermissionsValidationResult
strict bool
expectContains []string
expectNotContains []string
}{
{
name: "No validation issues",
result: &PermissionsValidationResult{
HasValidationIssues: false,
},
strict: false,
expectContains: []string{},
},
{
name: "Missing permissions message",
result: &PermissionsValidationResult{
HasValidationIssues: true,
MissingPermissions: map[PermissionScope]PermissionLevel{
PermissionContents: PermissionWrite,
PermissionIssues: PermissionWrite,
},
MissingToolsetDetails: map[string][]PermissionScope{
"repos": {PermissionContents},
"issues": {PermissionIssues},
},
},
strict: false,
expectContains: []string{
"Missing required permissions for GitHub toolsets:",
"contents: write (required by repos)",
"issues: write (required by issues)",
"Option 1: Add missing permissions to your workflow frontmatter:",
"Option 2: Reduce the required toolsets in your workflow:",
},
expectNotContains: []string{
"ERROR:",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
message := FormatValidationMessage(tt.result, tt.strict)
if !tt.result.HasValidationIssues {
if message != "" {
t.Errorf("Expected empty message for no issues, got: %s", message)
}
return
}
for _, expected := range tt.expectContains {
if !strings.Contains(message, expected) {
t.Errorf("Expected message to contain %q, got:\n%s", expected, message)
}
}
for _, notExpected := range tt.expectNotContains {
if strings.Contains(message, notExpected) {
t.Errorf("Expected message NOT to contain %q, got:\n%s", notExpected, message)
}
}
})
}
}
func TestToolsetPermissionsMapping(t *testing.T) {
// Verify that all toolsets are properly defined
expectedToolsets := []string{
"context", "repos", "issues", "pull_requests", "actions",
"code_security", "dependabot", "discussions", "experiments",
"gists", "labels", "notifications", "orgs", "projects",
"secret_protection", "security_advisories", "stargazers",
"users", "search",
}
for _, toolset := range expectedToolsets {
if _, exists := toolsetPermissionsMap[toolset]; !exists {
t.Errorf("Toolset %q not defined in toolsetPermissionsMap", toolset)
}
}
// Verify that default toolsets are valid
for _, toolset := range DefaultGitHubToolsets {
if _, exists := toolsetPermissionsMap[toolset]; !exists {
t.Errorf("Default toolset %q not defined in toolsetPermissionsMap", toolset)
}
}
}
func TestValidatePermissions_ComplexScenarios(t *testing.T) {
tests := []struct {
name string
permissions *Permissions
githubToolConfig *GitHubToolConfig
expectMsg []string
}{
{
name: "Shorthand read-all with default toolsets",
permissions: NewPermissionsReadAll(),
githubToolConfig: &GitHubToolConfig{
Toolset: GitHubToolsets{"default"},
ReadOnly: false,
},
expectMsg: []string{
"Missing required permissions for GitHub toolsets:",
"contents: write",
"issues: write",
"pull-requests: write",
},
},
{
name: "All: read with discussions toolset",
permissions: NewPermissionsAllRead(),
githubToolConfig: &GitHubToolConfig{
Toolset: GitHubToolsets{"discussions"},
ReadOnly: false,
},
expectMsg: []string{
"Missing required permissions for GitHub toolsets:",
"discussions: write",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ValidatePermissions(tt.permissions, tt.githubToolConfig)
message := FormatValidationMessage(result, false)
for _, expected := range tt.expectMsg {
if !strings.Contains(message, expected) {
t.Errorf("Expected message to contain %q, got:\n%s", expected, message)
}
}
})
}
}