Skip to content

Commit

Permalink
Add UserShouldUseEnterprise field
Browse files Browse the repository at this point in the history
small fixes
  • Loading branch information
julialeex committed Nov 23, 2024
1 parent 9954d17 commit 4885529
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/shared/src/sourcegraph-api/clientConfig.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const CLIENT_CONFIG_FIXTURE: CodyClientConfig = {
attributionEnabled: false,
smartContextWindowEnabled: true,
modelsAPIEnabled: false,
userShouldUseEnterprise: false,
}

describe('ClientConfigSingleton', () => {
Expand Down
10 changes: 10 additions & 0 deletions lib/shared/src/sourcegraph-api/clientConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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 => {
Expand Down Expand Up @@ -198,6 +207,7 @@ export class ClientConfigSingleton {

// Things that did not exist before logically default to disabled.
modelsAPIEnabled: false,
userShouldUseEnterprise: false,
}
}

Expand Down
2 changes: 1 addition & 1 deletion vscode/src/auth/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ export async function showSignOutMenu(): Promise<void> {
/**
* Log user out of the selected endpoint (remove token from secret).
*/
async function signOut(endpoint: string): Promise<void> {
export async function signOut(endpoint: string): Promise<void> {
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
Expand Down
5 changes: 4 additions & 1 deletion vscode/src/chat/chat-view/ChatController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
63 changes: 55 additions & 8 deletions vscode/test/e2e/auth.test.ts
Original file line number Diff line number Diff line change
@@ -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<ExpectedV2Events>({
// 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()
Expand All @@ -35,7 +35,6 @@ test.extend<ExpectedV2Events>({
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)
Expand All @@ -51,3 +50,51 @@ test.extend<ExpectedV2Events>({
// 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<DotcomUrlOverride>({
dotcomUrl: SERVER_URL,
})
.extend<ExpectedV2Events>({
// 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()
}
)
1 change: 1 addition & 0 deletions vscode/test/fixtures/mock-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}),
);
});
Expand Down

0 comments on commit 4885529

Please sign in to comment.