Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/github-authentication/src/test/flows.test.ts
5221 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import * as assert from 'assert';
7
import { ExtensionHost, GitHubTarget, IFlowQuery, getFlows } from '../flows';
8
import { Config } from '../config';
9
import * as vscode from 'vscode';
10
11
const enum Flows {
12
UrlHandlerFlow = 'url handler',
13
LocalServerFlow = 'local server',
14
DeviceCodeFlow = 'device code',
15
PatFlow = 'personal access token'
16
}
17
18
suite('getFlows', () => {
19
let lastClientSecret: string | undefined = undefined;
20
suiteSetup(() => {
21
lastClientSecret = Config.gitHubClientSecret;
22
Config.gitHubClientSecret = 'asdf';
23
});
24
25
suiteTeardown(() => {
26
Config.gitHubClientSecret = lastClientSecret;
27
});
28
29
const testCases: Array<{ label: string; query: IFlowQuery; expectedFlows: Flows[] }> = [
30
{
31
label: 'VS Code Desktop. Local filesystem. GitHub.com',
32
query: {
33
extensionHost: ExtensionHost.Local,
34
isSupportedClient: true,
35
target: GitHubTarget.DotCom
36
},
37
expectedFlows: [
38
Flows.LocalServerFlow,
39
Flows.UrlHandlerFlow,
40
Flows.DeviceCodeFlow
41
]
42
},
43
{
44
label: 'VS Code Desktop. Local filesystem. GitHub Hosted Enterprise',
45
query: {
46
extensionHost: ExtensionHost.Local,
47
isSupportedClient: true,
48
target: GitHubTarget.HostedEnterprise
49
},
50
expectedFlows: [
51
Flows.LocalServerFlow,
52
Flows.UrlHandlerFlow,
53
Flows.DeviceCodeFlow,
54
Flows.PatFlow
55
]
56
},
57
{
58
label: 'VS Code Desktop. Local filesystem. GitHub Enterprise Server',
59
query: {
60
extensionHost: ExtensionHost.Local,
61
isSupportedClient: true,
62
target: GitHubTarget.Enterprise
63
},
64
expectedFlows: [
65
Flows.DeviceCodeFlow,
66
Flows.PatFlow
67
]
68
},
69
{
70
label: 'vscode.dev. serverful. GitHub.com',
71
query: {
72
extensionHost: ExtensionHost.Remote,
73
isSupportedClient: true,
74
target: GitHubTarget.DotCom
75
},
76
expectedFlows: [
77
Flows.UrlHandlerFlow,
78
Flows.DeviceCodeFlow
79
]
80
},
81
{
82
label: 'vscode.dev. serverful. GitHub Hosted Enterprise',
83
query: {
84
extensionHost: ExtensionHost.Remote,
85
isSupportedClient: true,
86
target: GitHubTarget.HostedEnterprise
87
},
88
expectedFlows: [
89
Flows.UrlHandlerFlow,
90
Flows.DeviceCodeFlow,
91
Flows.PatFlow
92
]
93
},
94
{
95
label: 'vscode.dev. serverful. GitHub Enterprise',
96
query: {
97
extensionHost: ExtensionHost.Remote,
98
isSupportedClient: true,
99
target: GitHubTarget.Enterprise
100
},
101
expectedFlows: [
102
Flows.DeviceCodeFlow,
103
Flows.PatFlow
104
]
105
},
106
{
107
label: 'vscode.dev. serverless. GitHub.com',
108
query: {
109
extensionHost: ExtensionHost.WebWorker,
110
isSupportedClient: true,
111
target: GitHubTarget.DotCom
112
},
113
expectedFlows: [
114
Flows.UrlHandlerFlow
115
]
116
},
117
{
118
label: 'vscode.dev. serverless. GitHub Hosted Enterprise',
119
query: {
120
extensionHost: ExtensionHost.WebWorker,
121
isSupportedClient: true,
122
target: GitHubTarget.HostedEnterprise
123
},
124
expectedFlows: [
125
Flows.UrlHandlerFlow,
126
Flows.PatFlow
127
]
128
},
129
{
130
label: 'vscode.dev. serverless. GitHub Enterprise Server',
131
query: {
132
extensionHost: ExtensionHost.WebWorker,
133
isSupportedClient: true,
134
target: GitHubTarget.Enterprise
135
},
136
expectedFlows: [
137
Flows.PatFlow
138
]
139
},
140
{
141
label: 'Code - OSS. Local filesystem. GitHub.com',
142
query: {
143
extensionHost: ExtensionHost.Local,
144
isSupportedClient: false,
145
target: GitHubTarget.DotCom
146
},
147
expectedFlows: [
148
Flows.LocalServerFlow,
149
Flows.DeviceCodeFlow,
150
Flows.PatFlow
151
]
152
},
153
{
154
label: 'Code - OSS. Local filesystem. GitHub Hosted Enterprise',
155
query: {
156
extensionHost: ExtensionHost.Local,
157
isSupportedClient: false,
158
target: GitHubTarget.HostedEnterprise
159
},
160
expectedFlows: [
161
Flows.LocalServerFlow,
162
Flows.DeviceCodeFlow,
163
Flows.PatFlow
164
]
165
},
166
{
167
label: 'Code - OSS. Local filesystem. GitHub Enterprise Server',
168
query: {
169
extensionHost: ExtensionHost.Local,
170
isSupportedClient: false,
171
target: GitHubTarget.Enterprise
172
},
173
expectedFlows: [
174
Flows.DeviceCodeFlow,
175
Flows.PatFlow
176
]
177
},
178
];
179
180
for (const testCase of testCases) {
181
test(`gives the correct flows - ${testCase.label}`, () => {
182
const flows = getFlows(testCase.query);
183
184
assert.strictEqual(
185
flows.length,
186
testCase.expectedFlows.length,
187
`Unexpected number of flows: ${flows.map(f => f.label).join(',')}`
188
);
189
190
for (let i = 0; i < flows.length; i++) {
191
const flow = flows[i];
192
193
assert.strictEqual(flow.label, testCase.expectedFlows[i]);
194
}
195
});
196
}
197
198
suite('preferDeviceCodeFlow configuration', () => {
199
let originalConfig: boolean | undefined;
200
201
suiteSetup(async () => {
202
const config = vscode.workspace.getConfiguration('github-authentication');
203
originalConfig = config.get<boolean>('preferDeviceCodeFlow');
204
});
205
206
suiteTeardown(async () => {
207
const config = vscode.workspace.getConfiguration('github-authentication');
208
await config.update('preferDeviceCodeFlow', originalConfig, vscode.ConfigurationTarget.Global);
209
});
210
211
test('returns device code flow first when preferDeviceCodeFlow is true - VS Code Desktop', async () => {
212
const config = vscode.workspace.getConfiguration('github-authentication');
213
await config.update('preferDeviceCodeFlow', true, vscode.ConfigurationTarget.Global);
214
215
const flows = getFlows({
216
extensionHost: ExtensionHost.Local,
217
isSupportedClient: true,
218
target: GitHubTarget.DotCom
219
});
220
221
// Should return device code flow first, then other flows
222
assert.strictEqual(flows.length, 3, `Expected 3 flows, got ${flows.length}: ${flows.map(f => f.label).join(',')}`);
223
assert.strictEqual(flows[0].label, Flows.DeviceCodeFlow);
224
// Other flows should still be available
225
assert.strictEqual(flows[1].label, Flows.LocalServerFlow);
226
assert.strictEqual(flows[2].label, Flows.UrlHandlerFlow);
227
});
228
229
test('returns device code flow first when preferDeviceCodeFlow is true - Remote', async () => {
230
const config = vscode.workspace.getConfiguration('github-authentication');
231
await config.update('preferDeviceCodeFlow', true, vscode.ConfigurationTarget.Global);
232
233
const flows = getFlows({
234
extensionHost: ExtensionHost.Remote,
235
isSupportedClient: true,
236
target: GitHubTarget.DotCom
237
});
238
239
// Should return device code flow first, then other flows
240
assert.strictEqual(flows.length, 2, `Expected 2 flows, got ${flows.length}: ${flows.map(f => f.label).join(',')}`);
241
assert.strictEqual(flows[0].label, Flows.DeviceCodeFlow);
242
assert.strictEqual(flows[1].label, Flows.UrlHandlerFlow);
243
});
244
245
test('returns normal flows when preferDeviceCodeFlow is true but device code flow is not supported - WebWorker', async () => {
246
const config = vscode.workspace.getConfiguration('github-authentication');
247
await config.update('preferDeviceCodeFlow', true, vscode.ConfigurationTarget.Global);
248
249
const flows = getFlows({
250
extensionHost: ExtensionHost.WebWorker,
251
isSupportedClient: true,
252
target: GitHubTarget.DotCom
253
});
254
255
// WebWorker doesn't support DeviceCodeFlow, so should return normal flows
256
// Based on the original logic, WebWorker + DotCom should return UrlHandlerFlow
257
assert.strictEqual(flows.length, 1, `Expected 1 flow for WebWorker configuration, got ${flows.length}: ${flows.map(f => f.label).join(',')}`);
258
assert.strictEqual(flows[0].label, Flows.UrlHandlerFlow);
259
});
260
});
261
});
262
263