Skip to content

Commit 00638e2

Browse files
authored
Fix TestCustomPermissionsOverrideDefaults to check agent job permissions only (#25039)
1 parent 5fe3836 commit 00638e2

1 file changed

Lines changed: 12 additions & 23 deletions

File tree

pkg/workflow/compiler_cache_test.go

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -433,33 +433,22 @@ This workflow has custom permissions that should override defaults.
433433
}
434434
}
435435

436-
// Verify that default permissions that are not overridden are NOT present
437-
// since custom permissions completely replace defaults
438-
lockContentStr := string(lockContent)
436+
// Verify that default permissions that are not overridden are NOT present in the agent job
437+
// since custom permissions completely replace defaults.
438+
// Note: we check the agent job's permissions map directly (not the full lock file) because
439+
// other jobs like the activation job legitimately include permissions like "actions: read".
439440
defaultOnlyPermissions := []string{
440-
"pull-requests: read",
441-
"discussions: read",
442-
"deployments: read",
443-
"actions: read",
444-
"checks: read",
445-
"statuses: read",
441+
"pull-requests",
442+
"discussions",
443+
"deployments",
444+
"actions",
445+
"checks",
446+
"statuses",
446447
}
447448

448449
for _, defaultPerm := range defaultOnlyPermissions {
449-
if strings.Contains(lockContentStr, defaultPerm) {
450-
// Find the line containing the unexpected permission for context
451-
lines := strings.Split(lockContentStr, "\n")
452-
var contextLines []string
453-
for i, line := range lines {
454-
if strings.Contains(line, defaultPerm) {
455-
start := max(0, i-3)
456-
end := min(len(lines), i+4)
457-
contextLines = append(contextLines, fmt.Sprintf("Lines %d-%d:", start+1, end))
458-
contextLines = append(contextLines, lines[start:end]...)
459-
break
460-
}
461-
}
462-
t.Errorf("Default permission '%s' should not be present when custom permissions are specified.\nContext:\n%s", defaultPerm, strings.Join(contextLines, "\n"))
450+
if val, exists := permissionsMap[defaultPerm]; exists {
451+
t.Errorf("Default permission '%s' should not be present in the agent job when custom permissions are specified, got: %v", defaultPerm, val)
463452
}
464453
}
465454
}

0 commit comments

Comments
 (0)