Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions browser_tests/fixtures/data/sso.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type {
ErrorResponse,
SsoDiscoverResponse,
WebSessionResponse
} from '@comfyorg/ingest-types'

import { WEB_SESSION } from '@e2e/fixtures/data/webSession'

export const SSO_EMAIL = 'ada@acme.example'

export const SSO_DISCOVERED: SsoDiscoverResponse = {
sso: true,
organization_name: 'Acme'
}

export const NOT_SSO: SsoDiscoverResponse = { sso: false }

export const SSO_DISCOVER_DOWN: ErrorResponse = {
code: 'internal_error',
message: 'discover is down'
}

export const SSO_REQUIRED: ErrorResponse = {
code: 'sso_required',
message: 'This account signs in with SSO'
}

export const SSO_WEB_SESSION: WebSessionResponse = {
...WEB_SESSION,
user: { ...WEB_SESSION.user, sign_in_provider: 'saml.workos' }
}
12 changes: 10 additions & 2 deletions browser_tests/fixtures/webSessionFixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,16 @@ interface TokenMint {
}

interface WebSessionFixtures {
/** Flags merged into both `/api/features` answers, over the backend's. */
serverFeatures: RemoteConfig
tokenMints: TokenMint[]
workspaceReads: Request[]
firebaseRequests: string[]
credentialedFeatureReads: Request[]
}

export const webSessionTest = comfyPageFixture.extend<WebSessionFixtures>({
serverFeatures: [{}, { option: true }],
tokenMints: async ({ context }, use) => {
const mints: TokenMint[] = []
context.on('request', (request) => {
Expand Down Expand Up @@ -97,6 +100,7 @@ export const webSessionTest = comfyPageFixture.extend<WebSessionFixtures>({
{
page,
context,
serverFeatures,
tokenMints,
workspaceReads,
firebaseRequests,
Expand All @@ -120,8 +124,12 @@ export const webSessionTest = comfyPageFixture.extend<WebSessionFixtures>({
await route.fulfill(
jsonRoute(
credentialed
? { ...backendFeatures, ...WEB_SESSION_FEATURES }
: { ...backendFeatures, ...WEB_SESSION_ANONYMOUS_FEATURES }
? { ...backendFeatures, ...WEB_SESSION_FEATURES, ...serverFeatures }
: {
...backendFeatures,
...WEB_SESSION_ANONYMOUS_FEATURES,
...serverFeatures
}
)
)
})
Expand Down
51 changes: 51 additions & 0 deletions browser_tests/tests/cloudSsoSessionBoot.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { expect } from '@playwright/test'

import { SSO_WEB_SESSION } from '@e2e/fixtures/data/sso'
import { webSessionTest } from '@e2e/fixtures/webSessionFixture'
import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute'

/** The session of a person who signed in through their organization's SSO. */
const test = webSessionTest.extend({
page: async ({ page }, use) => {
await page.route('**/api/auth/session', async (route) => {
if (route.request().method() !== 'GET') return route.fallback()
await route.fulfill(jsonRoute(SSO_WEB_SESSION))
})
await use(page)
}
})

test.describe(
'Cloud boot on an SSO session',
{ tag: ['@cloud', '@ui'] },
() => {
test.describe.configure({ timeout: 60_000 })

test.use({
firebaseLogin: false,
serverFeatures: { sso_enabled: true }
})

test('a session-only SSO user boots into the app as the session user, with no Firebase traffic', async ({
comfyPage,
firebaseRequests
}) => {
const page = comfyPage.page
await comfyPage.waitForAppReady()
expect(page.url()).not.toContain('/cloud/login')

await comfyPage.toast.closeToasts()
await page.keyboard.press('Escape')
await page.getByRole('button', { name: 'Current user' }).click()
await expect(page.getByText(SSO_WEB_SESSION.user.email)).toBeVisible()

expect(
await page.evaluate(() =>
localStorage.getItem('Comfy.WebSession.SsoHint')
),
'the SSO session leaves its email for a later SSO re-entry'
).toBe(JSON.stringify({ email: SSO_WEB_SESSION.user.email }))
expect(firebaseRequests).toEqual([])
})
}
)
255 changes: 255 additions & 0 deletions browser_tests/tests/cloudSsoSignIn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
import { expect } from '@playwright/test'
import type { Page, Request } from '@playwright/test'

import type { ErrorResponse, SsoDiscoverResponse } from '@comfyorg/ingest-types'

import enMessages from '@/locales/en/main.json' with { type: 'json' }
import type { RemoteConfig } from '@/platform/remoteConfig/types'
import type { operations } from '@/types/comfyRegistryTypes'

import {
NOT_SSO,
SSO_DISCOVERED,
SSO_DISCOVER_DOWN,
SSO_EMAIL,
SSO_REQUIRED
} from '@e2e/fixtures/data/sso'
import { comfyPageFixture } from '@e2e/fixtures/ComfyPage'
import {
CLOUD_SELF_EMAIL,
CloudAuthHelper
} from '@e2e/fixtures/helpers/CloudAuthHelper'
import {
mockCloudBoot,
preselectCloudUser
} from '@e2e/fixtures/utils/cloudBootMocks'
import { jsonRoute } from '@e2e/fixtures/utils/jsonRoute'

const APP_URL = process.env.PLAYWRIGHT_TEST_URL || 'http://localhost:8188'
const APP_ROOT = new RegExp(
`^${APP_URL.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}/?(\\?.*)?$`
)
const SSO_COPY = enMessages.auth.sso
type CreateCustomerResponse =
operations['createCustomer']['responses']['201']['content']['application/json']

function isPath(request: Request, pathname: string) {
return new URL(request.url()).pathname === pathname
}

/**
* The cloud login page's SSO entry, with `sso_enabled` patched into the
* backend's own `/api/features` answer and ingest's SSO endpoints mocked at
* the network layer. Ingest's `/api/auth/sso/start` answers with a redirect
* to WorkOS; here it is fulfilled in place so the navigation is observed and
* never followed.
*/
const test = comfyPageFixture.extend<{
ssoEnabled: boolean
cloudAuth: CloudAuthHelper
discoverRequests: Request[]
ssoStarts: Request[]
}>({
ssoEnabled: [true, { option: true }],
discoverRequests: async ({ context }, use) => {
const requests: Request[] = []
context.on('request', (request) => {
if (isPath(request, '/api/auth/sso/discover')) requests.push(request)
})
await use(requests)
},
ssoStarts: async ({ context }, use) => {
const requests: Request[] = []
context.on('request', (request) => {
if (isPath(request, '/api/auth/sso/start')) requests.push(request)
})
await use(requests)
},
page: async ({ page, ssoEnabled, discoverRequests, ssoStarts }, use) => {
void discoverRequests
void ssoStarts
await mockCloudBoot(page, { features: {} })
await page.route('**/api/features', async (route) => {
const backendFeatures: RemoteConfig = await (await route.fetch()).json()
await route.fulfill(
jsonRoute({ ...backendFeatures, sso_enabled: ssoEnabled })
)
})
await preselectCloudUser(page)
await page.route('**/customers', (route) => {
if (route.request().method() !== 'POST') return route.fallback()
return route.fulfill({
status: 201,
json: { id: 'test-customer-e2e' } satisfies CreateCustomerResponse
})
})
await page.route('**/api/auth/sso/start**', (route) =>
route.fulfill({ status: 200, contentType: 'text/html', body: '' })
)
await use(page)
},
cloudAuth: async ({ page }, use) => {
await use(new CloudAuthHelper(page))
}
})

async function answerDiscover(
page: Page,
status: number,
body: SsoDiscoverResponse | ErrorResponse
) {
await page.route('**/api/auth/sso/discover', (route) =>
route.fulfill({ status, json: body })
)
}

async function openLogin(page: Page) {
await page.goto(APP_URL)
await expect(
page.getByRole('heading', { name: 'Log in to your account' })
).toBeVisible()
}

async function signInWithEmail(page: Page) {
await openLogin(page)
await page.getByRole('button', { name: 'Use email instead' }).click()
await page.locator('#cloud-sign-in-email').fill(CLOUD_SELF_EMAIL)
await page
.locator('#cloud-sign-in-password')
.fill('correct-horse-battery-staple')
await page.locator('#cloud-sign-in-password').blur()
await page.getByRole('button', { name: 'Sign in' }).click()
}

async function continueWithSso(page: Page, email: string) {
await openLogin(page)
await page.getByRole('button', { name: SSO_COPY.continueWithSso }).click()
await page.locator('#cloud-sso-email').fill(email)
await page.getByRole('button', { name: SSO_COPY.submit, exact: true }).click()
}

test.describe('Cloud login SSO entry', { tag: ['@cloud', '@ui'] }, () => {
test.describe('sso_enabled off', () => {
test.use({ ssoEnabled: false })

test('offers no SSO entry and signs an email in through Firebase without asking discover', async ({
page,
cloudAuth,
discoverRequests
}) => {
await answerDiscover(page, 200, SSO_DISCOVERED)
await cloudAuth.mockLiveEmailSignIn()

await openLogin(page)
await expect(
page.getByRole('button', { name: SSO_COPY.continueWithSso })
).toHaveCount(0)
await signInWithEmail(page)

await expect(page).toHaveURL(APP_ROOT, { timeout: 10_000 })
expect(discoverRequests).toEqual([])
})
})

test.describe('sso_enabled on', () => {
test('an SSO email navigates to ingest SSO start with its email and return path', async ({
page,
discoverRequests,
ssoStarts
}) => {
await answerDiscover(page, 200, SSO_DISCOVERED)
const start = page.waitForRequest((request) =>
isPath(request, '/api/auth/sso/start')
)

await continueWithSso(page, SSO_EMAIL)

const url = new URL((await start).url())
expect({
origin: url.origin,
email: url.searchParams.get('email'),
returnTo: url.searchParams.get('return_to'),
navigation: (await start).isNavigationRequest()
}).toEqual({
origin: new URL(APP_URL).origin,
email: SSO_EMAIL,
returnTo: '/cloud/user-check',
navigation: true
})
expect(discoverRequests.map((r) => r.postDataJSON())).toEqual([
{ email: SSO_EMAIL }
])
expect(ssoStarts).toHaveLength(1)
})

test('an email that does not use SSO stays on the login page with the not-SSO message', async ({
page,
ssoStarts
}) => {
await answerDiscover(page, 200, NOT_SSO)

await continueWithSso(page, CLOUD_SELF_EMAIL)

await expect(page.getByText(SSO_COPY.notSso)).toBeVisible()
await expect(page).toHaveURL(/\/cloud\/login/)
expect(ssoStarts).toEqual([])
})

test('a discover outage still signs a password login in through Firebase', async ({
page,
cloudAuth,
discoverRequests,
ssoStarts
}) => {
await answerDiscover(page, 500, SSO_DISCOVER_DOWN)
await cloudAuth.mockLiveEmailSignIn()
const firebaseSignIn = page.waitForRequest((request) =>
request.url().includes('accounts:signInWithPassword')
)

await signInWithEmail(page)

await firebaseSignIn
await expect(page).toHaveURL(APP_ROOT, { timeout: 10_000 })
expect(discoverRequests).toHaveLength(1)
expect(ssoStarts).toEqual([])
})

test('an sso_error ingest sends back shows its message on the login page', async ({
page
}) => {
await page.goto(`${APP_URL}/?sso_error=SSO_ORG_DISABLED`)

await expect(page).toHaveURL(
/\/cloud\/login\?.*sso_error=SSO_ORG_DISABLED/
)
await expect(page.getByText(SSO_COPY.errors.orgDisabled)).toBeVisible()
})

test('a Firebase sign-in ingest refuses with sso_required opens the SSO dialog', async ({
page,
cloudAuth
}) => {
await answerDiscover(page, 200, NOT_SSO)
await cloudAuth.mockLiveEmailSignIn()
await page.route('**/api/auth/session', (route) =>
route.request().method() === 'POST'
? route.fulfill({ status: 403, json: SSO_REQUIRED })
: route.fallback()
)

await signInWithEmail(page)

const dialog = page.getByRole('dialog')
await expect(
dialog.getByRole('heading', { name: SSO_COPY.required.title })
).toBeVisible()
await expect(
dialog.getByText(
SSO_COPY.required.bodyWithEmail.replace('{email}', CLOUD_SELF_EMAIL)
)
).toBeVisible()
await expect(page).toHaveURL(/\/cloud\/login/)
})
})
})
Loading