mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-24 18:49:00 +01:00
Add some integration tests for github-auth (#195729)
This commit is contained in:
committed by
GitHub
parent
9abd7cbbc7
commit
482d5ba393
196
extensions/github-authentication/src/test/flows.test.ts
Normal file
196
extensions/github-authentication/src/test/flows.test.ts
Normal file
@@ -0,0 +1,196 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as assert from 'assert';
|
||||
import { ExtensionHost, GitHubTarget, IFlowQuery, getFlows } from '../flows';
|
||||
import { Config } from '../config';
|
||||
|
||||
const enum Flows {
|
||||
UrlHandlerFlow = 'url handler',
|
||||
LocalServerFlow = 'local server',
|
||||
DeviceCodeFlow = 'device code',
|
||||
PatFlow = 'personal access token'
|
||||
}
|
||||
|
||||
suite('getFlows', () => {
|
||||
let lastClientSecret: string | undefined = undefined;
|
||||
suiteSetup(() => {
|
||||
lastClientSecret = Config.gitHubClientSecret;
|
||||
Config.gitHubClientSecret = 'asdf';
|
||||
});
|
||||
|
||||
suiteTeardown(() => {
|
||||
Config.gitHubClientSecret = lastClientSecret;
|
||||
});
|
||||
|
||||
const testCases: Array<{ label: string; query: IFlowQuery; expectedFlows: Flows[] }> = [
|
||||
{
|
||||
label: 'VS Code Desktop. Local filesystem. GitHub.com',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Local,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.DotCom
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.UrlHandlerFlow,
|
||||
Flows.LocalServerFlow,
|
||||
Flows.DeviceCodeFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'VS Code Desktop. Local filesystem. GitHub Hosted Enterprise',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Local,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.HostedEnterprise
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.UrlHandlerFlow,
|
||||
Flows.LocalServerFlow,
|
||||
Flows.DeviceCodeFlow,
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'VS Code Desktop. Local filesystem. GitHub Enterprise Server',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Local,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.Enterprise
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.DeviceCodeFlow,
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'vscode.dev. serverful. GitHub.com',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Remote,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.DotCom
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.UrlHandlerFlow,
|
||||
Flows.DeviceCodeFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'vscode.dev. serverful. GitHub Hosted Enterprise',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Remote,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.HostedEnterprise
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.UrlHandlerFlow,
|
||||
Flows.DeviceCodeFlow,
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'vscode.dev. serverful. GitHub Enterprise',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Remote,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.Enterprise
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.DeviceCodeFlow,
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'vscode.dev. serverless. GitHub.com',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.WebWorker,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.DotCom
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.UrlHandlerFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'vscode.dev. serverless. GitHub Hosted Enterprise',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.WebWorker,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.HostedEnterprise
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.UrlHandlerFlow,
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'vscode.dev. serverless. GitHub Enterprise Server',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.WebWorker,
|
||||
isSupportedClient: true,
|
||||
target: GitHubTarget.Enterprise
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'Code - OSS. Local filesystem. GitHub.com',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Local,
|
||||
isSupportedClient: false,
|
||||
target: GitHubTarget.DotCom
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.LocalServerFlow,
|
||||
Flows.DeviceCodeFlow,
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'Code - OSS. Local filesystem. GitHub Hosted Enterprise',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Local,
|
||||
isSupportedClient: false,
|
||||
target: GitHubTarget.HostedEnterprise
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.LocalServerFlow,
|
||||
Flows.DeviceCodeFlow,
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
{
|
||||
label: 'Code - OSS. Local filesystem. GitHub Enterprise Server',
|
||||
query: {
|
||||
extensionHost: ExtensionHost.Local,
|
||||
isSupportedClient: false,
|
||||
target: GitHubTarget.Enterprise
|
||||
},
|
||||
expectedFlows: [
|
||||
Flows.DeviceCodeFlow,
|
||||
Flows.PatFlow
|
||||
]
|
||||
},
|
||||
];
|
||||
|
||||
for (const testCase of testCases) {
|
||||
test(`gives the correct flows - ${testCase.label}`, () => {
|
||||
const flows = getFlows(testCase.query);
|
||||
|
||||
assert.strictEqual(
|
||||
flows.length,
|
||||
testCase.expectedFlows.length,
|
||||
`Unexpected number of flows: ${flows.map(f => f.label).join(',')}`
|
||||
);
|
||||
|
||||
for (let i = 0; i < flows.length; i++) {
|
||||
const flow = flows[i];
|
||||
|
||||
assert.strictEqual(flow.label, testCase.expectedFlows[i]);
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,65 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
import * as assert from 'assert';
|
||||
import { LoopbackAuthServer } from '../../node/authServer';
|
||||
|
||||
suite('LoopbackAuthServer', () => {
|
||||
let server: LoopbackAuthServer;
|
||||
let port: number;
|
||||
|
||||
setup(async () => {
|
||||
server = new LoopbackAuthServer(__dirname, 'http://localhost:8080');
|
||||
port = await server.start();
|
||||
});
|
||||
|
||||
teardown(async () => {
|
||||
await server.stop();
|
||||
});
|
||||
|
||||
test('should redirect to starting redirect on /signin', async () => {
|
||||
const response = await fetch(`http://localhost:${port}/signin?nonce=${server.nonce}`, {
|
||||
redirect: 'manual'
|
||||
});
|
||||
// Redirect
|
||||
assert.strictEqual(response.status, 302);
|
||||
|
||||
// Check location
|
||||
const location = response.headers.get('location');
|
||||
assert.ok(location);
|
||||
const locationUrl = new URL(location);
|
||||
assert.strictEqual(locationUrl.origin, 'http://localhost:8080');
|
||||
|
||||
// Check state
|
||||
const state = locationUrl.searchParams.get('state');
|
||||
assert.ok(state);
|
||||
const stateLocation = new URL(state);
|
||||
assert.strictEqual(stateLocation.origin, `http://127.0.0.1:${port}`);
|
||||
assert.strictEqual(stateLocation.pathname, '/callback');
|
||||
assert.strictEqual(stateLocation.searchParams.get('nonce'), server.nonce);
|
||||
});
|
||||
|
||||
test('should return 400 on /callback with missing parameters', async () => {
|
||||
const response = await fetch(`http://localhost:${port}/callback`);
|
||||
assert.strictEqual(response.status, 400);
|
||||
});
|
||||
|
||||
test('should resolve with code and state on /callback with valid parameters', async () => {
|
||||
server.state = 'valid-state';
|
||||
const response = await fetch(
|
||||
`http://localhost:${port}/callback?code=valid-code&state=${server.state}&nonce=${server.nonce}`,
|
||||
{ redirect: 'manual' }
|
||||
);
|
||||
assert.strictEqual(response.status, 302);
|
||||
assert.strictEqual(response.headers.get('location'), '/');
|
||||
await Promise.race([
|
||||
server.waitForOAuthResponse().then(result => {
|
||||
assert.strictEqual(result.code, 'valid-code');
|
||||
assert.strictEqual(result.state, server.state);
|
||||
}),
|
||||
new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 5000))
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user