Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

7.3 KB
Raw Blame
import { randomString } from '@playwright-utils'
import type { Page } from '@playwright/test'

import { expect, test } from '../utils/workspace-test-base'

const VALID_PASSWORD = 'hedgE-hog-123%'

const startSignupFlow = async (page: Page, email: string, password: string): Promise<void> => {
    await page.locator('[data-attr=signup-email]').fill(email)
    await expect(page.locator('[data-attr=signup-email]')).toHaveValue(email)
    await page.locator('[data-attr=signup-start]').click()

    await expect(page.locator('[data-attr=signup-auth-continue]')).toBeVisible()
    await page.locator('[data-attr=password]').fill(password)
    await expect(page.locator('[data-attr=password]')).toHaveValue(password)
    await page.locator('[data-attr=signup-auth-continue]').click()

    await expect(page.locator('[data-attr=signup-name]')).toBeVisible()
}

const submitEmailAndExpectLoginStep = async (page: Page, email: string): Promise<void> => {
    await page.locator('[data-attr=signup-email]').fill(email)
    await page.locator('[data-attr=signup-start]').click()

    await expect(page.locator('[data-attr=password-login]')).toBeVisible()
    await expect(page.locator('[data-attr=signup-auth-continue]')).not.toBeVisible()
}

test.describe('Signup', () => {
    test.beforeEach(async ({ page }) => {
        await page.route('**/flags/*', async (route) => {
            const response = {
                config: {
                    enable_collect_everything: true,
                },
                featureFlags: {},
                isAuthenticated: false,
            }
            await route.fulfill({ json: response })
        })
        // Log out via cookies, not the account-menu UI, which flakes on slow/torn-down workers.
        await page.context().clearCookies()
        await page.goto('/signup')
    })

    test('Can create user account with first name, last name and organization name', async ({ page }) => {
        let signupRequestBody: string | null = null

        await page.route('/api/signup/', async (route) => {
            signupRequestBody = route.request().postData()
            await route.continue()
        })
        await page.goto('/signup?next=/custom_path')

        const email = `${randomString('new_user')}@posthog.com`
        await startSignupFlow(page, email, VALID_PASSWORD)
        await page.locator('[data-attr=signup-name]').fill('Alice Bob')
        await expect(page.locator('[data-attr=signup-name]')).toHaveValue('Alice Bob')
        await page.locator('[data-attr=signup-organization-name]').fill('Hogflix SpinOff')
        await expect(page.locator('[data-attr=signup-organization-name]')).toHaveValue('Hogflix SpinOff')
        await page.locator('[data-attr=signup-role-at-organization]').click()
        await page.locator('.Popover li').filter({ hasText: 'Engineering' }).click()
        await expect(page.locator('[data-attr=signup-role-at-organization]')).toContainText('Engineering')
        const signupPromise = page.waitForResponse('/api/signup/')
        await page.locator('[data-attr=signup-submit]').click()
        await signupPromise

        const parsedBody = JSON.parse(signupRequestBody!)
        expect(parsedBody.first_name).toEqual('Alice')
        expect(parsedBody.last_name).toEqual('Bob')
        expect(parsedBody.organization_name).toEqual('Hogflix SpinOff')

        await expect(page).toHaveURL(/\/verify_email\/[a-zA-Z0-9_.-]*\?next=(\/|%2F)custom_path/)
    })

    test('Can submit the signup form multiple times if there is a generic email set', async ({ page }) => {
        let signupRequestBody: string | null = null
        const email = `${randomString('new_user-generic_error_test')}@posthog.com`

        await page.route('/api/signup/', async (route) => {
            signupRequestBody = route.request().postData()
            await route.continue()
        })

        // Create initial account
        await startSignupFlow(page, email, VALID_PASSWORD)
        await page.locator('[data-attr=signup-name]').fill('Alice Bob')
        await expect(page.locator('[data-attr=signup-name]')).toHaveValue('Alice Bob')
        await page.locator('[data-attr=signup-role-at-organization]').click()
        await page.locator('.Popover li').filter({ hasText: 'Engineering' }).click()
        await expect(page.locator('[data-attr=signup-role-at-organization]')).toContainText('Engineering')

        // Wait for the signup request to complete
        const signupPromise = page.waitForResponse('/api/signup/')
        await page.locator('[data-attr=signup-submit]').click()
        await signupPromise

        const parsedBody = JSON.parse(signupRequestBody!)
        expect(parsedBody.first_name).toEqual('Alice')
        expect(parsedBody.last_name).toEqual('Bob')

        // Wait for the first signup navigation to complete before navigating away,
        // otherwise page.goto('/signup') races with the in-progress navigation to
        // /verify_email/ and causes net::ERR_ABORTED
        await expect(page).toHaveURL(/\/verify_email\/[a-zA-Z0-9_.-]*/)
        await page.goto('/signup')

        // The same email now has an account, so it goes to the login step instead of signup
        await submitEmailAndExpectLoginStep(page, email)

        // Typing a new email closes the login step's password field and retries the signup
        const newEmail = `${randomString('new_user')}@posthog.com`
        await startSignupFlow(page, newEmail, VALID_PASSWORD)
        await page.locator('[data-attr=signup-name]').fill('Alice Bob')
        await expect(page.locator('[data-attr=signup-name]')).toHaveValue('Alice Bob')
        await page.locator('[data-attr=signup-name]').fill('Alice Bob')
        await expect(page.locator('[data-attr=signup-name]')).toHaveValue('Alice Bob')
        await page.locator('[data-attr=signup-role-at-organization]').click()
        await page.locator('.Popover li').filter({ hasText: 'Engineering' }).click()
        await expect(page.locator('[data-attr=signup-role-at-organization]')).toContainText('Engineering')
        const retrySignupPromise = page.waitForResponse('/api/signup/')
        await page.locator('[data-attr=signup-submit]').click()
        await retrySignupPromise

        await expect(page).toHaveURL(/\/verify_email\/[a-zA-Z0-9_.-]*/)
    })

    test('Can fill out all the fields on social login', async ({ page }) => {
        await page.context().clearCookies()
        await page.goto('/')
        await expect(page).toHaveURL(/.*\/login/)
        await page.goto('/organization/confirm-creation?organization_name=&first_name=Test&email=test%40posthog.com')

        await expect(page.locator('[name=email]')).toHaveValue('[email protected]')
        await expect(page.locator('[name=first_name]')).toHaveValue('Test')
        await page.locator('[name=organization_name]').fill('Hogflix SpinOff')
        await expect(page.locator('[name=organization_name]')).toHaveValue('Hogflix SpinOff')
        await page.locator('[data-attr=signup-role-at-organization]').click()
        await page.locator('.Popover li').filter({ hasText: 'Engineering' }).click()
        await expect(page.locator('[data-attr=signup-role-at-organization]')).toContainText('Engineering')
        await page.locator('[type=submit]').click()
        await expect(page.locator('.Toastify [data-attr="error-toast"]')).toContainText(
            'Inactive social login session.'
        )
    })
})