-
Notifications
You must be signed in to change notification settings - Fork 371
Expand file tree
/
Copy pathpermissions_no_github_tool_test.go
More file actions
258 lines (224 loc) · 5.86 KB
/
permissions_no_github_tool_test.go
File metadata and controls
258 lines (224 loc) · 5.86 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
//go:build integration
package workflow
import (
"bytes"
"io"
"os"
"path/filepath"
"testing"
"github.com/github/gh-aw/pkg/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestPermissionsWithoutGitHubTool tests that when permissions are specified
// but tools.github is NOT configured, no warning is raised and the GitHub MCP
// server will handle permission issues
func TestPermissionsWithoutGitHubTool(t *testing.T) {
tests := []struct {
name string
content string
expectError bool
expectWarning bool
warningMessage string
}{
{
name: "permissions without github tool - no warning",
content: `---
on: push
permissions:
contents: read
issues: read
---
# Test Workflow
`,
expectError: false,
expectWarning: false,
},
{
name: "permissions with github tool - validates permissions",
content: `---
on: push
permissions:
contents: read
tools:
github:
toolsets: [repos, issues]
read-only: false
---
# Test Workflow
`,
expectError: false,
expectWarning: true,
warningMessage: "Missing required permissions for GitHub toolsets:",
},
{
name: "no permissions, no github tool - no warning",
content: `---
on: push
---
# Test Workflow
`,
expectError: false,
expectWarning: false,
},
{
name: "permissions with sufficient github tool config - no warning",
content: `---
on: push
permissions:
contents: write
issues: write
strict: false
features:
dangerous-permissions-write: true
tools:
github:
toolsets: [repos, issues]
---
# Test Workflow
`,
expectError: false,
expectWarning: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpDir := testutil.TempDir(t, "permissions-no-github-tool-test")
testFile := filepath.Join(tmpDir, "test-workflow.md")
err := os.WriteFile(testFile, []byte(tt.content), 0644)
require.NoError(t, err, "Failed to write test file")
// Capture stderr to check for warnings
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
compiler := NewCompiler()
compiler.SetStrictMode(false)
err = compiler.CompileWorkflow(testFile)
// Restore stderr
w.Close()
os.Stderr = oldStderr
var buf bytes.Buffer
io.Copy(&buf, r)
stderrOutput := buf.String()
// Check error expectation
if tt.expectError {
assert.Error(t, err, "Expected compilation to fail")
} else {
assert.NoError(t, err, "Expected compilation to succeed")
}
// Check warning expectation
if tt.expectWarning {
assert.Contains(t, stderrOutput, tt.warningMessage, "Expected warning message not found")
assert.Contains(t, stderrOutput, "warning:", "Expected 'warning:' prefix in output")
} else {
// For non-warning cases, we should not see permission-related warnings
if tt.warningMessage != "" {
assert.NotContains(t, stderrOutput, tt.warningMessage, "Unexpected warning in output")
}
assert.NotContains(t, stderrOutput, "Missing required permissions", "Unexpected permission warning")
}
})
}
}
// TestPermissionsWithoutGitHubToolStrictMode tests that in strict mode,
// permissions without github tool still doesn't raise validation errors
func TestPermissionsWithoutGitHubToolStrictMode(t *testing.T) {
tmpDir := testutil.TempDir(t, "permissions-no-github-tool-strict-test")
content := `---
on: push
strict: true
permissions:
contents: read
issues: read
---
# Test Workflow
`
testFile := filepath.Join(tmpDir, "test-workflow.md")
err := os.WriteFile(testFile, []byte(content), 0644)
require.NoError(t, err, "Failed to write test file")
// Capture stderr
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
compiler := NewCompiler()
compiler.SetStrictMode(true)
err = compiler.CompileWorkflow(testFile)
// Restore stderr
w.Close()
os.Stderr = oldStderr
var buf bytes.Buffer
io.Copy(&buf, r)
stderrOutput := buf.String()
// Should succeed without permission validation errors
assert.NoError(t, err, "Expected compilation to succeed in strict mode")
assert.NotContains(t, stderrOutput, "Missing required permissions", "Should not raise permission warnings")
}
// TestPermissionsWarningOnlyWithGitHubTool ensures that permission validation
// warnings are only raised when tools.github is explicitly configured
func TestPermissionsWarningOnlyWithGitHubTool(t *testing.T) {
tmpDir := testutil.TempDir(t, "permissions-warning-only-with-github-test")
tests := []struct {
name string
hasGitHubTool bool
expectWarning bool
}{
{
name: "no github tool - no warning",
hasGitHubTool: false,
expectWarning: false,
},
{
name: "with github tool - warning expected",
hasGitHubTool: true,
expectWarning: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var content string
if tt.hasGitHubTool {
content = `---
on: push
permissions:
contents: read
tools:
github:
toolsets: [repos, issues]
read-only: false
---
# Test Workflow
`
} else {
content = `---
on: push
permissions:
contents: read
---
# Test Workflow
`
}
testFile := filepath.Join(tmpDir, tt.name+"-workflow.md")
err := os.WriteFile(testFile, []byte(content), 0644)
require.NoError(t, err, "Failed to write test file")
// Capture stderr
oldStderr := os.Stderr
r, w, _ := os.Pipe()
os.Stderr = w
compiler := NewCompiler()
compiler.SetStrictMode(false)
err = compiler.CompileWorkflow(testFile)
// Restore stderr
w.Close()
os.Stderr = oldStderr
var buf bytes.Buffer
io.Copy(&buf, r)
stderrOutput := buf.String()
assert.NoError(t, err, "Expected compilation to succeed")
if tt.expectWarning {
assert.Contains(t, stderrOutput, "Missing required permissions", "Expected permission warning")
} else {
assert.NotContains(t, stderrOutput, "Missing required permissions", "Should not have permission warning")
}
})
}
}