Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/github-authentication/src/test/node/authServer.test.ts
3326 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 { LoopbackAuthServer } from '../../node/authServer';
8
import { env } from 'vscode';
9
10
suite('LoopbackAuthServer', () => {
11
let server: LoopbackAuthServer;
12
let port: number;
13
14
setup(async () => {
15
server = new LoopbackAuthServer(__dirname, 'http://localhost:8080', 'https://code.visualstudio.com');
16
port = await server.start();
17
});
18
19
teardown(async () => {
20
await server.stop();
21
});
22
23
test('should redirect to starting redirect on /signin', async () => {
24
const response = await fetch(`http://localhost:${port}/signin?nonce=${server.nonce}`, {
25
redirect: 'manual'
26
});
27
// Redirect
28
assert.strictEqual(response.status, 302);
29
30
// Check location
31
const location = response.headers.get('location');
32
assert.ok(location);
33
const locationUrl = new URL(location);
34
assert.strictEqual(locationUrl.origin, 'http://localhost:8080');
35
36
// Check state
37
const state = locationUrl.searchParams.get('state');
38
assert.ok(state);
39
const stateLocation = new URL(state);
40
assert.strictEqual(stateLocation.origin, `http://127.0.0.1:${port}`);
41
assert.strictEqual(stateLocation.pathname, '/callback');
42
assert.strictEqual(stateLocation.searchParams.get('nonce'), server.nonce);
43
});
44
45
test('should return 400 on /callback with missing parameters', async () => {
46
const response = await fetch(`http://localhost:${port}/callback`);
47
assert.strictEqual(response.status, 400);
48
});
49
50
test('should resolve with code and state on /callback with valid parameters', async () => {
51
server.state = 'valid-state';
52
const response = await fetch(
53
`http://localhost:${port}/callback?code=valid-code&state=${server.state}&nonce=${server.nonce}`,
54
{ redirect: 'manual' }
55
);
56
assert.strictEqual(response.status, 302);
57
assert.strictEqual(response.headers.get('location'), `/?redirect_uri=https%3A%2F%2Fcode.visualstudio.com&app_name=${encodeURIComponent(env.appName)}`);
58
await Promise.race([
59
server.waitForOAuthResponse().then(result => {
60
assert.strictEqual(result.code, 'valid-code');
61
assert.strictEqual(result.state, server.state);
62
}),
63
new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), 5000))
64
]);
65
});
66
});
67
68