Path: blob/main/extensions/microsoft-authentication/src/common/test/scopeData.test.ts
3323 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import * as assert from 'assert';6import { ScopeData } from '../scopeData';7import { Uri } from 'vscode';89suite('ScopeData', () => {10test('should include default scopes if not present', () => {11const scopeData = new ScopeData(['custom_scope']);12assert.deepStrictEqual(scopeData.allScopes, ['custom_scope']);13});1415test('should not duplicate default scopes if already present', () => {16const scopeData = new ScopeData(['custom_scope', 'openid', 'email', 'profile', 'offline_access']);17assert.deepStrictEqual(scopeData.allScopes, ['custom_scope', 'email', 'offline_access', 'openid', 'profile']);18});1920test('should sort the scopes alphabetically', () => {21const scopeData = new ScopeData(['custom_scope', 'profile', 'email', 'openid', 'offline_access']);22assert.deepStrictEqual(scopeData.allScopes, ['custom_scope', 'email', 'offline_access', 'openid', 'profile']);23});2425test('should create a space-separated string of all scopes', () => {26const scopeData = new ScopeData(['custom_scope', 'openid', 'email', 'offline_access', 'profile']);27assert.strictEqual(scopeData.scopeStr, 'custom_scope email offline_access openid profile');28});2930test('should add TACK ON scope if all scopes are OIDC scopes', () => {31const scopeData = new ScopeData(['openid', 'email', 'offline_access', 'profile']);32assert.deepStrictEqual(scopeData.scopesToSend, ['email', 'offline_access', 'openid', 'profile', 'User.Read']);33});3435test('should filter out internal VS Code scopes for scopesToSend', () => {36const scopeData = new ScopeData(['custom_scope', 'VSCODE_CLIENT_ID:some_id']);37assert.deepStrictEqual(scopeData.scopesToSend, ['custom_scope']);38});3940test('should use the default client ID if no VSCODE_CLIENT_ID scope is present', () => {41const scopeData = new ScopeData(['custom_scope']);42assert.strictEqual(scopeData.clientId, 'aebc6443-996d-45c2-90f0-388ff96faa56');43});4445test('should use the VSCODE_CLIENT_ID scope if present', () => {46const scopeData = new ScopeData(['custom_scope', 'VSCODE_CLIENT_ID:some_id']);47assert.strictEqual(scopeData.clientId, 'some_id');48});4950test('should use the default tenant ID if no VSCODE_TENANT scope is present', () => {51const scopeData = new ScopeData(['custom_scope']);52assert.strictEqual(scopeData.tenant, 'organizations');53});5455test('should use the VSCODE_TENANT scope if present', () => {56const scopeData = new ScopeData(['custom_scope', 'VSCODE_TENANT:some_tenant']);57assert.strictEqual(scopeData.tenant, 'some_tenant');58});5960test('should have tenantId be undefined if no VSCODE_TENANT scope is present', () => {61const scopeData = new ScopeData(['custom_scope']);62assert.strictEqual(scopeData.tenantId, undefined);63});6465test('should have tenantId be undefined if typical tenant values are present', () => {66for (const element of ['common', 'organizations', 'consumers']) {67const scopeData = new ScopeData(['custom_scope', `VSCODE_TENANT:${element}`]);68assert.strictEqual(scopeData.tenantId, undefined);69}70});7172test('should have tenantId be the value of VSCODE_TENANT scope if set to a specific value', () => {73const scopeData = new ScopeData(['custom_scope', 'VSCODE_TENANT:some_guid']);74assert.strictEqual(scopeData.tenantId, 'some_guid');75});7677test('should not return claims', () => {78const scopeData = new ScopeData(['custom_scope']);79assert.strictEqual(scopeData.claims, undefined);80});8182test('should return claims', () => {83const scopeData = new ScopeData(['custom_scope'], 'test');84assert.strictEqual(scopeData.claims, 'test');85});8687test('should extract tenant from authorization server URL path', () => {88const authorizationServer = Uri.parse('https://login.microsoftonline.com/tenant123/oauth2/v2.0');89const scopeData = new ScopeData(['custom_scope'], undefined, authorizationServer);90assert.strictEqual(scopeData.tenant, 'tenant123');91});9293test('should fallback to default tenant if authorization server URL has no path segments', () => {94const authorizationServer = Uri.parse('https://login.microsoftonline.com');95const scopeData = new ScopeData(['custom_scope'], undefined, authorizationServer);96assert.strictEqual(scopeData.tenant, 'organizations');97});9899test('should prioritize authorization server URL over VSCODE_TENANT scope', () => {100const authorizationServer = Uri.parse('https://login.microsoftonline.com/url_tenant/oauth2/v2.0');101const scopeData = new ScopeData(['custom_scope', 'VSCODE_TENANT:scope_tenant'], undefined, authorizationServer);102assert.strictEqual(scopeData.tenant, 'url_tenant');103});104105test('should extract tenant from v1.0 authorization server URL path', () => {106const authorizationServer = Uri.parse('https://login.microsoftonline.com/tenant123');107const scopeData = new ScopeData(['custom_scope'], undefined, authorizationServer);108assert.strictEqual(scopeData.tenant, 'tenant123');109});110});111112113