-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathPermissionTests.cs
More file actions
251 lines (209 loc) · 8.62 KB
/
PermissionTests.cs
File metadata and controls
251 lines (209 loc) · 8.62 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using GitHub.Copilot.SDK.Test.Harness;
using Xunit;
using Xunit.Abstractions;
namespace GitHub.Copilot.SDK.Test;
public class PermissionTests(E2ETestFixture fixture, ITestOutputHelper output) : E2ETestBase(fixture, "permissions", output)
{
[Fact]
public async Task Should_Invoke_Permission_Handler_For_Write_Operations()
{
var permissionRequests = new List<PermissionRequest>();
CopilotSession? session = null;
session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
permissionRequests.Add(request);
Assert.Equal(session!.SessionId, invocation.SessionId);
return Task.FromResult(new PermissionRequestResult { Kind = PermissionRequestResultKind.Approved });
}
});
await File.WriteAllTextAsync(Path.Combine(Ctx.WorkDir, "test.txt"), "original content");
await session.SendAsync(new MessageOptions
{
Prompt = "Edit test.txt and replace 'original' with 'modified'"
});
await TestHelper.GetFinalAssistantMessageAsync(session);
// Should have received at least one permission request
Assert.NotEmpty(permissionRequests);
// Should include write permission request
Assert.Contains(permissionRequests, r => r.Kind == "write");
}
[Fact]
public async Task Should_Deny_Permission_When_Handler_Returns_Denied()
{
var session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
return Task.FromResult(new PermissionRequestResult
{
Kind = PermissionRequestResultKind.DeniedInteractivelyByUser
});
}
});
var testFilePath = Path.Combine(Ctx.WorkDir, "protected.txt");
await File.WriteAllTextAsync(testFilePath, "protected content");
await session.SendAsync(new MessageOptions
{
Prompt = "Edit protected.txt and replace 'protected' with 'hacked'."
});
await TestHelper.GetFinalAssistantMessageAsync(session);
// Verify the file was NOT modified
var content = await File.ReadAllTextAsync(testFilePath);
Assert.Equal("protected content", content);
}
[Fact]
public async Task Should_Deny_Tool_Operations_When_Handler_Explicitly_Denies()
{
var session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (_, _) =>
Task.FromResult(new PermissionRequestResult { Kind = PermissionRequestResultKind.DeniedCouldNotRequestFromUser })
});
var permissionDenied = false;
session.On(evt =>
{
if (evt is ToolExecutionCompleteEvent toolEvt &&
!toolEvt.Data.Success &&
toolEvt.Data.Error?.Message.Contains("Permission denied") == true)
{
permissionDenied = true;
}
});
await session.SendAndWaitAsync(new MessageOptions
{
Prompt = "Run 'node --version'"
});
Assert.True(permissionDenied, "Expected a tool.execution_complete event with Permission denied result");
}
[Fact]
public async Task Should_Work_With_Approve_All_Permission_Handler()
{
var session = await CreateSessionAsync(new SessionConfig());
await session.SendAsync(new MessageOptions
{
Prompt = "What is 2+2?"
});
var message = await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.Contains("4", message?.Data.Content ?? string.Empty);
}
[Fact]
public async Task Should_Handle_Async_Permission_Handler()
{
var permissionRequestReceived = false;
var session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = async (request, invocation) =>
{
permissionRequestReceived = true;
// Simulate async permission check
await Task.Delay(10);
return new PermissionRequestResult { Kind = PermissionRequestResultKind.Approved };
}
});
await session.SendAsync(new MessageOptions
{
Prompt = "Run 'echo test' and tell me what happens"
});
await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.True(permissionRequestReceived, "Permission request should have been received");
}
[Fact]
public async Task Should_Resume_Session_With_Permission_Handler()
{
var permissionRequestReceived = false;
// Create session without permission handler
var session1 = await CreateSessionAsync();
var sessionId = session1.SessionId;
await session1.SendAndWaitAsync(new MessageOptions { Prompt = "What is 1+1?" });
// Resume with permission handler
var session2 = await ResumeSessionAsync(sessionId, new ResumeSessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
permissionRequestReceived = true;
return Task.FromResult(new PermissionRequestResult { Kind = PermissionRequestResultKind.Approved });
}
});
await session2.SendAndWaitAsync(new MessageOptions
{
Prompt = "Run 'echo resumed' for me"
});
Assert.True(permissionRequestReceived, "Permission request should have been received");
}
[Fact]
public async Task Should_Handle_Permission_Handler_Errors_Gracefully()
{
var session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
// Simulate an error in the handler
throw new InvalidOperationException("Handler error");
}
});
await session.SendAsync(new MessageOptions
{
Prompt = "Run 'echo test'. If you can't, say 'failed'."
});
var message = await TestHelper.GetFinalAssistantMessageAsync(session);
// Should handle the error and deny permission
Assert.Matches("fail|cannot|unable|permission", message?.Data.Content?.ToLowerInvariant() ?? string.Empty);
}
[Fact]
public async Task Should_Deny_Tool_Operations_When_Handler_Explicitly_Denies_After_Resume()
{
var session1 = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = PermissionHandler.ApproveAll
});
var sessionId = session1.SessionId;
await session1.SendAndWaitAsync(new MessageOptions { Prompt = "What is 1+1?" });
var session2 = await ResumeSessionAsync(sessionId, new ResumeSessionConfig
{
OnPermissionRequest = (_, _) =>
Task.FromResult(new PermissionRequestResult { Kind = PermissionRequestResultKind.DeniedCouldNotRequestFromUser })
});
var permissionDenied = false;
session2.On(evt =>
{
if (evt is ToolExecutionCompleteEvent toolEvt &&
!toolEvt.Data.Success &&
toolEvt.Data.Error?.Message.Contains("Permission denied") == true)
{
permissionDenied = true;
}
});
await session2.SendAndWaitAsync(new MessageOptions
{
Prompt = "Run 'node --version'"
});
Assert.True(permissionDenied, "Expected a tool.execution_complete event with Permission denied result");
}
[Fact]
public async Task Should_Receive_ToolCallId_In_Permission_Requests()
{
var receivedToolCallId = false;
var session = await CreateSessionAsync(new SessionConfig
{
OnPermissionRequest = (request, invocation) =>
{
if (request is PermissionRequestShell shell && !string.IsNullOrEmpty(shell.ToolCallId))
{
receivedToolCallId = true;
}
return Task.FromResult(new PermissionRequestResult { Kind = PermissionRequestResultKind.Approved });
}
});
await session.SendAsync(new MessageOptions
{
Prompt = "Run 'echo test'"
});
await TestHelper.GetFinalAssistantMessageAsync(session);
Assert.True(receivedToolCallId, "Should have received toolCallId in permission request");
}
}