Skip to content
Draft
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
2 changes: 2 additions & 0 deletions api/app/settings/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,8 @@
f"e2e_non_admin_user_with_a_role@{E2E_TEST_EMAIL_DOMAIN}"
)
E2E_SEPARATE_TEST_USER = f"e2e_separate_test_user@{E2E_TEST_EMAIL_DOMAIN}"
# New user for the onboarding E2E test that reloads before the organisation exists
E2E_ONBOARDING_RELOAD_USER = f"e2e_onboarding_reload_user@{E2E_TEST_EMAIL_DOMAIN}"
# User on a Free-plan organisation, used by the Billing tab E2E tests
# that need to exercise the "no active subscription" payment flow.
E2E_BILLING_USER = f"e2e_billing_user@{E2E_TEST_EMAIL_DOMAIN}"
Expand Down
1 change: 1 addition & 0 deletions api/e2etests/e2e_seed_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ def teardown() -> None:
)
delete_user_and_its_organisations(user_email=settings.E2E_SEPARATE_TEST_USER)
delete_user_and_its_organisations(user_email=settings.E2E_BILLING_USER)
delete_user_and_its_organisations(user_email=settings.E2E_ONBOARDING_RELOAD_USER)


def seed_data() -> None:
Expand Down
48 changes: 48 additions & 0 deletions frontend/common/utils/__tests__/onboardingEntry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
getStoredOnboardingTargetingKey,
getStoredOnboardingVariant,
persistOnboardingEntry,
resolveOnboardingVariant,
} from 'common/utils/onboardingEntry'

jest.mock('@flagsmith/flagsmith', () => ({
Expand Down Expand Up @@ -96,3 +97,50 @@ describe('persistOnboardingEntry', () => {
expect(getStoredOnboardingTargetingKey()).toBeNull()
})
})

describe('resolveOnboardingVariant', () => {
beforeEach(() => {
storage.clear()
})

it.each`
stored | decision | expected
${'single_page'} | ${null} | ${'single_page'}
${'single_page'} | ${{ targetingKey: 'anon-1', variant: 'control' }} | ${'single_page'}
${'control'} | ${{ targetingKey: 'anon-1', variant: 'single_page' }} | ${'control'}
${null} | ${{ targetingKey: 'anon-1', variant: 'single_page' }} | ${'single_page'}
${null} | ${null} | ${'control'}
`(
'stored $stored with decision $decision routes as $expected',
({ decision, expected, stored }) => {
expect(resolveOnboardingVariant(stored, decision)).toBe(expected)
},
)

it('keeps the stored variant instead of persisting a new decision', () => {
// Given
storage.set('onboarding_variant', 'single_page')

// When
resolveOnboardingVariant('single_page', {
targetingKey: 'anon-2',
variant: 'control',
})

// Then
expect(getStoredOnboardingVariant()).toBe('single_page')
expect(getStoredOnboardingTargetingKey()).toBeNull()
})

it('persists a new decision when nothing is stored', () => {
// When
resolveOnboardingVariant(null, {
targetingKey: 'anon-3',
variant: 'single_page',
})

// Then
expect(getStoredOnboardingVariant()).toBe('single_page')
expect(getStoredOnboardingTargetingKey()).toBe('anon-3')
})
})
9 changes: 9 additions & 0 deletions frontend/common/utils/onboardingEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export function persistOnboardingEntry(
return variant
}

// A variant stored earlier in the session wins over a new decision. A reload
// during onboarding logs in again without isGettingStarted, and must not send
// a single_page user to /create.
export const resolveOnboardingVariant = (
stored: OnboardingVariant | null,
decision: OnboardingEntryDecision | null,
): OnboardingVariant =>
stored ?? (decision ? persistOnboardingEntry(decision) : 'control')

export const getStoredOnboardingVariant = (): OnboardingVariant | null => {
const variant = storageGet(VARIANT_STORAGE_KEY)
return variant === 'single_page' || variant === 'control' ? variant : null
Expand Down
1 change: 1 addition & 0 deletions frontend/e2e/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const E2E_EMAIL_DOMAIN = 'flagsmithe2etestdomain.io'
export const E2E_SIGN_UP_USER = `e2e_signup_user@${E2E_EMAIL_DOMAIN}`
export const E2E_ONBOARDING_RELOAD_USER = `e2e_onboarding_reload_user@${E2E_EMAIL_DOMAIN}`
export const E2E_USER = `e2e_user@${E2E_EMAIL_DOMAIN}`
export const E2E_NON_ADMIN_USER = `e2e_user@${E2E_EMAIL_DOMAIN}`
//e2e_non_admin_user_with_project_permissions@flagsmithe2etestdomain.io
Expand Down
65 changes: 64 additions & 1 deletion frontend/e2e/tests/onboarding-tests.pw.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { test, expect } from '../test-setup';
import { byId, createHelpers, getFlagsmith, log, visualSnapshot } from '../helpers';
import { E2E_SIGN_UP_USER, PASSWORD } from '../config';
import {
E2E_ONBOARDING_RELOAD_USER,
E2E_SIGN_UP_USER,
PASSWORD,
} from '../config';

// The single-page onboarding flow (onboarding_quickstart_flow), at
// /getting-started. Runs only when the flag is on (the legacy signup test runs
Expand Down Expand Up @@ -182,4 +186,63 @@ test.describe('Onboarding', () => {
await page.getByRole('button', { name: /Experiment/ }).click();
await expect(page).toHaveURL(/\/experiments$/);
});

// Pins the variant instead of reading the real flag, so this runs whatever
// the experiment is set to. A reload before the organisation exists used to
// send the user to /create, and onboarding then made a second organisation.
test('A reload before the organisation exists keeps a new user on the onboarding flow @oss', async ({
page,
}) => {
const { addErrorLogging, click, setText, waitForElementVisible } =
createHelpers(page);
await addErrorLogging();

// Flagsmith-on-Flagsmith requests carry the environment key header; the
// app's own API calls don't.
await page.route('**/identities/', async (route) => {
if (!route.request().headers()['x-environment-key']) {
return route.fallback();
}
const response = await route.fetch();
const json = await response.json();
json.flags = (json.flags ?? []).map((flag) =>
flag.feature?.name === 'onboarding_quickstart_flow'
? { ...flag, enabled: true, variant: 'single_page' }
: flag,
);
await route.fulfill({ json, response });
});

// Fail the first organisation create, so the reload below happens while
// the user still has no organisation.
const createOrganisation = '**/api/v1/organisations/';
await page.route(createOrganisation, (route) =>
route.request().method() === 'POST' ? route.abort() : route.fallback(),
);

log('Sign up');
await page.goto('/');
await click(byId('jsSignup'));
await waitForElementVisible(byId('firstName'));
await setText(byId('firstName'), 'Bullet');
await setText(byId('lastName'), 'Train');
await setText(byId('email'), E2E_ONBOARDING_RELOAD_USER);
await setText(byId('password'), PASSWORD);
await click(byId('signup-btn'));
await page.waitForURL((url) => url.pathname === '/getting-started', {
timeout: 30000,
});

log('Reload before the organisation exists');
await page.unroute(createOrganisation);
await page.reload();

await expect(
page.getByRole('heading', { name: /Welcome/ }),
).toBeVisible({ timeout: 30000 });
expect(new URL(page.url()).pathname).toBe('/getting-started');
await expect(
page.getByRole('heading', { name: 'Create your organisation' }),
).not.toBeVisible();
});
});
12 changes: 9 additions & 3 deletions frontend/web/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import ProjectStore from 'common/stores/project-store'
import {
decideOnboardingEntry,
getStoredOnboardingVariant,
persistOnboardingEntry,
resolveOnboardingVariant,
} from 'common/utils/onboardingEntry'
import { AUTHORISE_PATH } from 'common/utils/pendingAuthorisation'
import { Provider } from 'react-redux'
Expand Down Expand Up @@ -163,8 +163,9 @@ const App = class extends Component {
// targeting key, so bucketing never diverges from this decision.
// Capped at 2s: a degraded flags API falls back to the legacy page
// instead of blocking the redirect.
const storedVariant = getStoredOnboardingVariant()
Promise.race([
AccountStore.getUser()?.isGettingStarted
!storedVariant && AccountStore.getUser()?.isGettingStarted
? decideOnboardingEntry(AccountStore.getUser()?.email).catch(
() => null,
)
Expand All @@ -173,7 +174,12 @@ const App = class extends Component {
]).then((decision) => {
// Only an accepted decision is persisted: a decision losing the
// race must not store its assignment after routing has happened.
const variant = decision ? persistOnboardingEntry(decision) : 'control'
const variant = resolveOnboardingVariant(storedVariant, decision)
// Label both arms here, at the decision. On /getting-started, control
// was only labelled after /create, so its drop-offs were never counted.
if (storedVariant || decision) {
API.trackTraits({ onboarding_variant: variant })
}
// Restore the logged-in identity for the rest of the app.
Promise.resolve(API.flagsmithIdentify()).catch(() => {})
if (variant === 'single_page') {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { FC, useEffect } from 'react'
import React, { FC } from 'react'
import ConfigProvider from 'common/providers/ConfigProvider'
import { getStoredOnboardingVariant } from 'common/utils/onboardingEntry'
import API from 'project/api'
import GettingStartedPage from 'components/pages/GettingStartedPage'
import OnboardingFlow from './OnboardingFlow'

Expand All @@ -11,11 +10,6 @@ const GettingStartedGate: FC = () => {
// legacy page.
const variant = getStoredOnboardingVariant()

useEffect(() => {
if (!variant) return
API.trackTraits({ onboarding_variant: variant })
}, [variant])

return variant === 'single_page' ? <OnboardingFlow /> : <GettingStartedPage />
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export type OnboardingResources = {
*/
export const useEnsureOnboardingResources = (): OnboardingResources => {
const { data: profile } = useGetProfileQuery({})
const { data: organisations, isLoading: orgsLoading } =
// isFetching, not isLoading: after /create invalidates the list, the cached
// one is stale and would read as "no organisation".
const { data: organisations, isFetching: orgsFetching } =
useGetOrganisationsQuery({})
const defaults = useSmartDefaults(
profile?.email ?? '',
Expand All @@ -55,7 +57,7 @@ export const useEnsureOnboardingResources = (): OnboardingResources => {
useEffect(() => {
// Run once, and only after the profile + organisations have loaded so the
// reuse-vs-create decision is made against real data.
if (ranRef.current || !profile || orgsLoading) {
if (ranRef.current || !profile || orgsFetching) {
return
}
ranRef.current = true
Expand Down Expand Up @@ -83,7 +85,7 @@ export const useEnsureOnboardingResources = (): OnboardingResources => {
setError(e)
setStatus('error')
})
}, [profile, orgsLoading, organisations, selectedOrganisation, defaults])
}, [profile, orgsFetching, organisations, selectedOrganisation, defaults])

return {
caseSensitive,
Expand Down
Loading