From 488552941fec91658f154f496e0ca2a30d9ad155 Mon Sep 17 00:00:00 2001 From: julialeex Date: Fri, 22 Nov 2024 16:40:16 -0800 Subject: [PATCH] Add UserShouldUseEnterprise field small fixes --- .../src/sourcegraph-api/clientConfig.test.ts | 1 + .../src/sourcegraph-api/clientConfig.ts | 10 +++ vscode/src/auth/auth.ts | 2 +- vscode/src/chat/chat-view/ChatController.ts | 5 +- vscode/test/e2e/auth.test.ts | 63 ++++++++++++++++--- vscode/test/fixtures/mock-server.ts | 1 + 6 files changed, 72 insertions(+), 10 deletions(-) diff --git a/lib/shared/src/sourcegraph-api/clientConfig.test.ts b/lib/shared/src/sourcegraph-api/clientConfig.test.ts index 03ce98ea6a9c..63cec126c236 100644 --- a/lib/shared/src/sourcegraph-api/clientConfig.test.ts +++ b/lib/shared/src/sourcegraph-api/clientConfig.test.ts @@ -15,6 +15,7 @@ const CLIENT_CONFIG_FIXTURE: CodyClientConfig = { attributionEnabled: false, smartContextWindowEnabled: true, modelsAPIEnabled: false, + userShouldUseEnterprise: false, } describe('ClientConfigSingleton', () => { diff --git a/lib/shared/src/sourcegraph-api/clientConfig.ts b/lib/shared/src/sourcegraph-api/clientConfig.ts index 0fed4076461f..43c96f90ce0f 100644 --- a/lib/shared/src/sourcegraph-api/clientConfig.ts +++ b/lib/shared/src/sourcegraph-api/clientConfig.ts @@ -46,6 +46,9 @@ export interface CodyClientConfig { // Whether the new Sourcegraph backend LLM models API endpoint should be used to query which // models are available. modelsAPIEnabled: boolean + + // Whether the user should sign in to an enterprise instance. + userShouldUseEnterprise: boolean } /** @@ -165,12 +168,18 @@ export class ClientConfigSingleton { if (isError(clientConfig)) { throw clientConfig } + // TODO: delete this once backend API is implemented + // For testing purpose, ensure userShouldUseEnterprise is always true + clientConfig.userShouldUseEnterprise = true return clientConfig }) }) .then(clientConfig => { signal?.throwIfAborted() logDebug('ClientConfigSingleton', 'refreshed', JSON.stringify(clientConfig)) + // TODO: delete this once backend API is implemented + // For testing purpose, ensure userShouldUseEnterprise is always true + clientConfig.userShouldUseEnterprise = true return clientConfig }) .catch(e => { @@ -198,6 +207,7 @@ export class ClientConfigSingleton { // Things that did not exist before logically default to disabled. modelsAPIEnabled: false, + userShouldUseEnterprise: false, } } diff --git a/vscode/src/auth/auth.ts b/vscode/src/auth/auth.ts index 584baec4e809..4693ba2f66e1 100644 --- a/vscode/src/auth/auth.ts +++ b/vscode/src/auth/auth.ts @@ -370,7 +370,7 @@ export async function showSignOutMenu(): Promise { /** * Log user out of the selected endpoint (remove token from secret). */ -async function signOut(endpoint: string): Promise { +export async function signOut(endpoint: string): Promise { const token = await secretStorage.getToken(endpoint) const tokenSource = await secretStorage.getTokenSource(endpoint) // Delete the access token from the Sourcegraph instance on signout if it was created diff --git a/vscode/src/chat/chat-view/ChatController.ts b/vscode/src/chat/chat-view/ChatController.ts index af1ce59a3049..522dd17187cf 100644 --- a/vscode/src/chat/chat-view/ChatController.ts +++ b/vscode/src/chat/chat-view/ChatController.ts @@ -85,7 +85,7 @@ import type { TelemetryEventParameters } from '@sourcegraph/telemetry' import { Subject, map } from 'observable-fns' import type { URI } from 'vscode-uri' import { View } from '../../../webviews/tabs/types' -import { redirectToEndpointLogin, showSignInMenu, showSignOutMenu } from '../../auth/auth' +import { redirectToEndpointLogin, showSignInMenu, showSignOutMenu, signOut } from '../../auth/auth' import { closeAuthProgressIndicator, startAuthProgressIndicator, @@ -553,6 +553,9 @@ export class ChatController implements vscode.Disposable, vscode.WebviewViewProv let clientConfig: CodyClientConfig | undefined try { clientConfig = await ClientConfigSingleton.getInstance().getConfig(abortController.signal) + if (clientConfig?.userShouldUseEnterprise && isDotCom(authStatus.endpoint)) { + signOut(authStatus.endpoint) + } if (abortController.signal.aborted) { return } diff --git a/vscode/test/e2e/auth.test.ts b/vscode/test/e2e/auth.test.ts index bea6dbc20ab6..1eb7627be316 100644 --- a/vscode/test/e2e/auth.test.ts +++ b/vscode/test/e2e/auth.test.ts @@ -1,24 +1,24 @@ import { expect } from '@playwright/test' import { SERVER_URL, VALID_TOKEN } from '../fixtures/mock-server' - -import { focusSidebar, getChatSidebarPanel } from './common' -import { type ExpectedV2Events, signOut, test } from './helpers' - +import { focusSidebar } from './common' +import { getChatSidebarPanel } from './common' +import { type DotcomUrlOverride, type ExpectedV2Events, signOut, test } from './helpers' test.extend({ // list of V2 telemetry events we expect this test to log, add to this list as needed expectedV2Events: [ 'cody.extension:installed', 'cody.auth.login:clicked', 'cody.auth.signin.menu:clicked', - 'cody.auth.login:firstEver', 'cody.auth.signin.token:clicked', 'cody.auth:connected', - 'cody.auth.logout:clicked', + 'cody.signInNotification:shown', + 'cody.auth:failed', + 'cody.auth.login:firstEver', 'cody.auth:disconnected', 'cody.interactiveTutorial:attemptingStart', 'cody.experiment.interactiveTutorial:enrolled', ], -})('requires a valid auth token and allows logouts', async ({ page, sidebar }) => { +})('requires a valid auth token and allows logouts', async ({ page, sidebar, nap }) => { await expect(page.getByText('Authentication failed.')).not.toBeVisible() await sidebar?.getByRole('button', { name: 'Sign In to Your Enterprise Instance' }).click() await page.getByRole('option', { name: 'Sign In with URL and Access Token' }).click() @@ -35,7 +35,6 @@ test.extend({ await page.getByRole('combobox', { name: 'input' }).press('Enter') await page.getByRole('combobox', { name: 'input' }).fill(VALID_TOKEN) await page.getByRole('combobox', { name: 'input' }).press('Enter') - // Sign out. await signOut(page) await focusSidebar(page) @@ -51,3 +50,51 @@ test.extend({ // Expect status bar to show the sign in button. await expect(page.getByRole('button', { name: 'cody-logo-heavy Sign In, Sign' })).toBeVisible() }) + +// When an enterprise customer is logged in to a dotcom url, they should be logged out immediately +test + .extend({ + dotcomUrl: SERVER_URL, + }) + .extend({ + // list of V2 telemetry events we expect this test to log, add to this list as needed + expectedV2Events: [ + 'cody.extension:installed', + 'cody.auth.login:clicked', + 'cody.auth.signin.menu:clicked', + 'cody.auth.signin.token:clicked', + 'cody.signInNotification:shown', + 'cody.auth:disconnected', + 'cody.auth:disconnected', + 'cody.auth:disconnected', + ], + })( + 'test enterprise customers should get logged out for dotcomUrl', + async ({ page, sidebar, nap }) => { + await sidebar?.getByRole('button', { name: 'Sign In to Your Enterprise Instance' }).click() + await page.getByRole('option', { name: 'Sign In with URL and Access Token' }).click() + await page.getByRole('combobox', { name: 'input' }).fill(SERVER_URL) + await page.getByRole('combobox', { name: 'input' }).press('Enter') + await page.getByRole('combobox', { name: 'input' }).fill(VALID_TOKEN) + await page.getByRole('combobox', { name: 'input' }).press('Enter') + + await expect( + page + .getByLabel(/^Signed in to localhost/, { exact: false }) + .locator('div') + .nth(2) + ).toBeVisible() + + await focusSidebar(page) + + // Makes sure the sign in page is loaded in the sidebar view with Cody: Chat as the heading + // instead of the chat panel. + const sidebarFrame = getChatSidebarPanel(page) + await expect( + sidebarFrame.getByRole('button', { name: 'Sign In to Your Enterprise Instance' }) + ).toBeVisible() + await expect(page.getByRole('heading', { name: 'Cody: Chat' })).toBeVisible() + // Expect status bar to show the sign in button. + await expect(page.getByRole('button', { name: 'cody-logo-heavy Sign In, Sign' })).toBeVisible() + } +) diff --git a/vscode/test/fixtures/mock-server.ts b/vscode/test/fixtures/mock-server.ts index abaaabebe333..cb064ad05638 100644 --- a/vscode/test/fixtures/mock-server.ts +++ b/vscode/test/fixtures/mock-server.ts @@ -370,6 +370,7 @@ export class MockServer { attributionEnabled: attribution, // When server-sent LLMs have been set, we enable the models api modelsAPIEnabled: !!controller.availableLLMs, + userShouldUseEnterprise: true, }), ); });