mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 23:49:03 +00:00
For a user with MFA enabled, Studio requests PostHog flags once with the pre-MFA token, the API refuses it, and nothing requests them again after the challenge is verified. The flags stay missing until the user opens an organization or project, or reloads, so pages reached straight after MFA (`/organizations`, `/new`, `/account`) get no flag values. I made `FeatureFlagProvider` refetch when the auth client reports a verified MFA challenge. Verification changes none of the existing fetch inputs (user, organization, project), which is why the refused first response stuck. **Note:** The refetch runs on any verified MFA challenge (sign-in, enrolling a factor, identity re-verification), so users who never verify a challenge make the same flag requests as before. I did not key it on the access token, because that would refetch on every token refresh. ## To test Tested on the PR preview, with a temporary TOTP factor on a staging account: - [x] Sign in with an MFA-enabled account, with the network log open, landing on `/organizations`. Expect a `GET /platform/telemetry/feature-flags` returning 200 with the account's flags after `POST /auth/v1/factors/:id/verify`, before opening any organization or project. Observed: flags 403 before the verify, verify 200, then one flags 200 with no organization or project param. - [x] Repeat with `returnTo=/new`. Expect the same 200 after the verify request. Observed: landed on `/new`, same order (403, verify 200, flags 200). - [x] On the landing page after MFA, open the dev toolbar's Flags tab, which reads the provider's PostHog flags. Expect the account's flags and no `message` entry. Observed: the account's flags listed, no `message` entry, and no flag-lookup console errors. - [x] Sign in with an account without MFA. Expect no verify request, so no refetch from this change. Observed: no `factors/:id/verify` request; every flags request returned 200. - [x] Enroll a TOTP factor in Account > Security. Expect one flags request after the enrollment verify (the case the Note describes). Observed: verify 200, then flags 200. ## Linear - fixes GROWTH-1507
132 lines
4.4 KiB
TypeScript
132 lines
4.4 KiB
TypeScript
import { act, render, waitFor } from '@testing-library/react'
|
|
import {
|
|
AuthContext,
|
|
FeatureFlagProvider,
|
|
gotrueClient,
|
|
useFeatureFlags,
|
|
type CallFeatureFlagsResponse,
|
|
type FeatureFlagContextType,
|
|
} from 'common'
|
|
import { HttpResponse } from 'msw'
|
|
import type { ComponentProps } from 'react'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
|
|
import { API_URL } from '@/lib/constants'
|
|
import { createMockAuthContext, createMockSession } from '@/tests/lib/auth-helpers'
|
|
import { addAPIMock, type APIErrorBody } from '@/tests/lib/msw'
|
|
|
|
type AuthContextValue = ComponentProps<typeof AuthContext.Provider>['value']
|
|
type ConfigCatFlagsFetcher = NonNullable<
|
|
ComponentProps<typeof FeatureFlagProvider>['getConfigCatFlags']
|
|
>
|
|
|
|
const LOGGED_OUT: AuthContextValue = {
|
|
session: null,
|
|
error: null,
|
|
isLoading: false,
|
|
refreshSession: async () => null,
|
|
}
|
|
|
|
const ANOTHER_USER = createMockAuthContext({
|
|
session: createMockSession({ user: { ...createMockSession().user, id: 'another-user' } }),
|
|
})
|
|
|
|
let flags: FeatureFlagContextType
|
|
const FlagsProbe = () => {
|
|
flags = useFeatureFlags()
|
|
return null
|
|
}
|
|
|
|
const renderProvider = (auth: AuthContextValue, getConfigCatFlags: ConfigCatFlagsFetcher) => {
|
|
const tree = (authContext: AuthContextValue) => (
|
|
<AuthContext.Provider value={authContext}>
|
|
<FeatureFlagProvider enabled={{ cc: true, ph: false }} getConfigCatFlags={getConfigCatFlags}>
|
|
<FlagsProbe />
|
|
</FeatureFlagProvider>
|
|
</AuthContext.Provider>
|
|
)
|
|
const { rerender } = render(tree(auth))
|
|
return { changeAuth: (authContext: AuthContextValue) => rerender(tree(authContext)) }
|
|
}
|
|
|
|
const flagsLoaded = () => waitFor(() => expect(flags.hasLoaded).toBe(true))
|
|
|
|
// The first fetch lands at once, the refetch only when `landRefetch` is called
|
|
const createFetcherWithHeldRefetch = () => {
|
|
let landRefetch = () => {}
|
|
const refetch = new Promise<[]>((resolve) => {
|
|
landRefetch = () => resolve([])
|
|
})
|
|
const fetcher = vi.fn<ConfigCatFlagsFetcher>().mockResolvedValueOnce([]).mockReturnValue(refetch)
|
|
return { fetcher, landRefetch: () => landRefetch() }
|
|
}
|
|
|
|
describe('FeatureFlagProvider hasFlagsForSignedInUser', () => {
|
|
it('is true for flags fetched for the signed-in user', async () => {
|
|
renderProvider(createMockAuthContext(), async () => [])
|
|
await flagsLoaded()
|
|
|
|
expect(flags.hasFlagsForSignedInUser).toBe(true)
|
|
})
|
|
|
|
it('is false for a logged-out visitor', async () => {
|
|
renderProvider(LOGGED_OUT, async () => [])
|
|
await flagsLoaded()
|
|
|
|
expect(flags.hasFlagsForSignedInUser).toBe(false)
|
|
})
|
|
|
|
it.each<[string, AuthContextValue]>([
|
|
['flags fetched before sign-in', LOGGED_OUT],
|
|
['flags fetched for another user', ANOTHER_USER],
|
|
])('is false for %s until the refetch lands', async (_, authAtFirstFetch) => {
|
|
const { fetcher, landRefetch } = createFetcherWithHeldRefetch()
|
|
const { changeAuth } = renderProvider(authAtFirstFetch, fetcher)
|
|
await flagsLoaded()
|
|
|
|
changeAuth(createMockAuthContext())
|
|
expect(flags.hasFlagsForSignedInUser).toBe(false)
|
|
|
|
landRefetch()
|
|
await waitFor(() => expect(flags.hasFlagsForSignedInUser).toBe(true))
|
|
})
|
|
})
|
|
|
|
describe('FeatureFlagProvider PostHog flags for a user with MFA enabled', () => {
|
|
it('loads the flags refused to the pre-MFA token once the challenge is verified', async () => {
|
|
const onAuthStateChange = vi.spyOn(gotrueClient, 'onAuthStateChange')
|
|
const session = createMockSession()
|
|
let isMfaVerified = false
|
|
addAPIMock({
|
|
method: 'get',
|
|
path: '/platform/telemetry/feature-flags',
|
|
response: () =>
|
|
isMfaVerified
|
|
? HttpResponse.json<CallFeatureFlagsResponse>({ someExperiment: 'variant' })
|
|
: HttpResponse.json<APIErrorBody>(
|
|
{ message: 'Insufficient AAL: MFA required' },
|
|
{ status: 403 }
|
|
),
|
|
})
|
|
|
|
render(
|
|
<AuthContext.Provider value={createMockAuthContext({ session })}>
|
|
<FeatureFlagProvider API_URL={API_URL} enabled={{ cc: false, ph: true }}>
|
|
<FlagsProbe />
|
|
</FeatureFlagProvider>
|
|
</AuthContext.Provider>
|
|
)
|
|
await flagsLoaded()
|
|
expect(flags.posthog).not.toHaveProperty('someExperiment')
|
|
|
|
isMfaVerified = true
|
|
act(() => {
|
|
onAuthStateChange.mock.calls.forEach(([notifySubscriber]) =>
|
|
notifySubscriber('MFA_CHALLENGE_VERIFIED', session)
|
|
)
|
|
})
|
|
|
|
await waitFor(() => expect(flags.posthog).toEqual({ someExperiment: 'variant' }))
|
|
})
|
|
})
|