Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/microsoft-authentication/src/common/test/scopeData.test.ts
3323 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 { ScopeData } from '../scopeData';
8
import { Uri } from 'vscode';
9
10
suite('ScopeData', () => {
11
test('should include default scopes if not present', () => {
12
const scopeData = new ScopeData(['custom_scope']);
13
assert.deepStrictEqual(scopeData.allScopes, ['custom_scope']);
14
});
15
16
test('should not duplicate default scopes if already present', () => {
17
const scopeData = new ScopeData(['custom_scope', 'openid', 'email', 'profile', 'offline_access']);
18
assert.deepStrictEqual(scopeData.allScopes, ['custom_scope', 'email', 'offline_access', 'openid', 'profile']);
19
});
20
21
test('should sort the scopes alphabetically', () => {
22
const scopeData = new ScopeData(['custom_scope', 'profile', 'email', 'openid', 'offline_access']);
23
assert.deepStrictEqual(scopeData.allScopes, ['custom_scope', 'email', 'offline_access', 'openid', 'profile']);
24
});
25
26
test('should create a space-separated string of all scopes', () => {
27
const scopeData = new ScopeData(['custom_scope', 'openid', 'email', 'offline_access', 'profile']);
28
assert.strictEqual(scopeData.scopeStr, 'custom_scope email offline_access openid profile');
29
});
30
31
test('should add TACK ON scope if all scopes are OIDC scopes', () => {
32
const scopeData = new ScopeData(['openid', 'email', 'offline_access', 'profile']);
33
assert.deepStrictEqual(scopeData.scopesToSend, ['email', 'offline_access', 'openid', 'profile', 'User.Read']);
34
});
35
36
test('should filter out internal VS Code scopes for scopesToSend', () => {
37
const scopeData = new ScopeData(['custom_scope', 'VSCODE_CLIENT_ID:some_id']);
38
assert.deepStrictEqual(scopeData.scopesToSend, ['custom_scope']);
39
});
40
41
test('should use the default client ID if no VSCODE_CLIENT_ID scope is present', () => {
42
const scopeData = new ScopeData(['custom_scope']);
43
assert.strictEqual(scopeData.clientId, 'aebc6443-996d-45c2-90f0-388ff96faa56');
44
});
45
46
test('should use the VSCODE_CLIENT_ID scope if present', () => {
47
const scopeData = new ScopeData(['custom_scope', 'VSCODE_CLIENT_ID:some_id']);
48
assert.strictEqual(scopeData.clientId, 'some_id');
49
});
50
51
test('should use the default tenant ID if no VSCODE_TENANT scope is present', () => {
52
const scopeData = new ScopeData(['custom_scope']);
53
assert.strictEqual(scopeData.tenant, 'organizations');
54
});
55
56
test('should use the VSCODE_TENANT scope if present', () => {
57
const scopeData = new ScopeData(['custom_scope', 'VSCODE_TENANT:some_tenant']);
58
assert.strictEqual(scopeData.tenant, 'some_tenant');
59
});
60
61
test('should have tenantId be undefined if no VSCODE_TENANT scope is present', () => {
62
const scopeData = new ScopeData(['custom_scope']);
63
assert.strictEqual(scopeData.tenantId, undefined);
64
});
65
66
test('should have tenantId be undefined if typical tenant values are present', () => {
67
for (const element of ['common', 'organizations', 'consumers']) {
68
const scopeData = new ScopeData(['custom_scope', `VSCODE_TENANT:${element}`]);
69
assert.strictEqual(scopeData.tenantId, undefined);
70
}
71
});
72
73
test('should have tenantId be the value of VSCODE_TENANT scope if set to a specific value', () => {
74
const scopeData = new ScopeData(['custom_scope', 'VSCODE_TENANT:some_guid']);
75
assert.strictEqual(scopeData.tenantId, 'some_guid');
76
});
77
78
test('should not return claims', () => {
79
const scopeData = new ScopeData(['custom_scope']);
80
assert.strictEqual(scopeData.claims, undefined);
81
});
82
83
test('should return claims', () => {
84
const scopeData = new ScopeData(['custom_scope'], 'test');
85
assert.strictEqual(scopeData.claims, 'test');
86
});
87
88
test('should extract tenant from authorization server URL path', () => {
89
const authorizationServer = Uri.parse('https://login.microsoftonline.com/tenant123/oauth2/v2.0');
90
const scopeData = new ScopeData(['custom_scope'], undefined, authorizationServer);
91
assert.strictEqual(scopeData.tenant, 'tenant123');
92
});
93
94
test('should fallback to default tenant if authorization server URL has no path segments', () => {
95
const authorizationServer = Uri.parse('https://login.microsoftonline.com');
96
const scopeData = new ScopeData(['custom_scope'], undefined, authorizationServer);
97
assert.strictEqual(scopeData.tenant, 'organizations');
98
});
99
100
test('should prioritize authorization server URL over VSCODE_TENANT scope', () => {
101
const authorizationServer = Uri.parse('https://login.microsoftonline.com/url_tenant/oauth2/v2.0');
102
const scopeData = new ScopeData(['custom_scope', 'VSCODE_TENANT:scope_tenant'], undefined, authorizationServer);
103
assert.strictEqual(scopeData.tenant, 'url_tenant');
104
});
105
106
test('should extract tenant from v1.0 authorization server URL path', () => {
107
const authorizationServer = Uri.parse('https://login.microsoftonline.com/tenant123');
108
const scopeData = new ScopeData(['custom_scope'], undefined, authorizationServer);
109
assert.strictEqual(scopeData.tenant, 'tenant123');
110
});
111
});
112
113