forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermissionHandlerTests.cs
More file actions
127 lines (104 loc) · 3.9 KB
/
Copy pathPermissionHandlerTests.cs
File metadata and controls
127 lines (104 loc) · 3.9 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using GitHub.Copilot.Rpc;
using Xunit;
namespace GitHub.Copilot.Test.Unit;
public class PermissionHandlerTests
{
private static readonly JsonSerializerOptions SerializerOptions = new()
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver(),
};
[Fact]
public void PermissionEventExposesManagedApprovalRequired()
{
const string json = """
{
"permissionRequest": {
"kind": "read",
"intention": "Read managed content",
"path": "/workspace/file.txt",
"managedApprovalRequired": true
},
"requestId": "permission-1"
}
""";
var data = JsonSerializer.Deserialize<PermissionRequestedData>(
json,
SerializerOptions);
Assert.NotNull(data);
var request = Assert.IsType<PermissionRequestRead>(data.PermissionRequest);
Assert.True(request.ManagedApprovalRequired);
PermissionRequest genericRequest = request;
Assert.True(genericRequest.ManagedApprovalRequired);
}
[Fact]
public async Task ApproveAllThrowsWhenManagedSettingsEnabled()
{
var request = new PermissionRequest
{
Kind = "read",
ManagedApprovalRequired = true,
};
await Assert.ThrowsAsync<InvalidOperationException>(() =>
PermissionHandler.ApproveAll(request, new PermissionInvocation
{
ManagedSettingsEnabled = true,
}));
}
[Fact]
public async Task ApproveAllApprovesOrdinaryRequest()
{
var request = new PermissionRequest { Kind = "read" };
var decision = await PermissionHandler.ApproveAll(request, new PermissionInvocation());
Assert.IsType<PermissionDecisionApproveOnce>(decision);
}
[Fact]
public async Task ApproveAllLeavesManagedRequestPendingWhenSessionFlagIsAbsent()
{
var request = new PermissionRequestRead
{
Intention = "Read managed content",
ManagedApprovalRequired = true,
Path = "/workspace/file.txt",
};
var decision = await PermissionHandler.ApproveAll(request, new PermissionInvocation());
Assert.IsType<PermissionDecisionNoResult>(decision);
}
[Fact]
public async Task ApproveAllLeavesManagedKnownVariantPendingThroughBaseType()
{
PermissionRequest request = new PermissionRequestRead
{
Intention = "Read managed content",
ManagedApprovalRequired = true,
Path = "/workspace/file.txt",
};
var decision = await PermissionHandler.ApproveAll(request, new PermissionInvocation());
Assert.IsType<PermissionDecisionNoResult>(decision);
}
[Fact]
public void DerivedManagedApprovalAccessorForwardsToBaseStorage()
{
var request = new PermissionRequestRead
{
Intention = "Read managed content",
ManagedApprovalRequired = true,
Path = "/workspace/file.txt",
};
PermissionRequest genericRequest = request;
Assert.True(genericRequest.ManagedApprovalRequired);
genericRequest.ManagedApprovalRequired = false;
Assert.False(request.ManagedApprovalRequired);
}
[Fact]
public async Task ApproveAllLeavesUnknownRequestPending()
{
var request = new PermissionRequest { Kind = "future-managed-kind" };
var decision = await PermissionHandler.ApproveAll(request, new PermissionInvocation());
Assert.IsType<PermissionDecisionNoResult>(decision);
}
}