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.

12.6 KB
Raw Blame
import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'
import posthog from 'posthog-js'

import { authLogic } from 'scenes/authentication/auth/authLogic'
import { loginLogic } from 'scenes/authentication/auth/loginLogic'
import { login2FALogic } from 'scenes/authentication/login-2fa/login2FALogic'
import { passwordResetLogic } from 'scenes/authentication/password-reset/passwordResetLogic'
import { LISTENED_ACTIONS, loginTelemetryLogic } from 'scenes/authentication/shared/loginTelemetryLogic'

import { useMocks } from '~/mocks/jest'
import { initKeaTests } from '~/test/init'

jest.mock('posthog-js')

// isWebKitBrowser() reads navigator.vendor: "Apple Computer, Inc." on WebKit, "Google Inc." on Chromium.
const WEBKIT_VENDOR = 'Apple Computer, Inc.'
const originalVendor = window.navigator.vendor

function capturedProperties(event: string): Record<string, any> | undefined {
    return (posthog.capture as jest.Mock).mock.calls.find(([name]) => name === event)?.[1]
}

function captureCount(event: string): number {
    return (posthog.capture as jest.Mock).mock.calls.filter(([name]) => name === event).length
}

// The real action type behind each literal in LISTENED_ACTIONS, read off the logic that dispatches
// it. The Record type makes the compiler demand one entry per literal, the typed getter stops
// compiling when an action is renamed, and the imports above fail when a logic's module moves.
const LISTENED_ACTION_SOURCES: Record<keyof typeof LISTENED_ACTIONS, () => string> = {
    submitLoginRequest: () => loginLogic.actionTypes.submitLoginRequest,
    loginError: () => loginLogic.actionTypes.setGeneralError,
    codeVerificationFailure: () => loginLogic.actionTypes.submitCodeVerificationFailure,
    twoFactorError: () => login2FALogic.actionTypes.setGeneralError,
    resetRequested: () => passwordResetLogic.actionTypes.submitRequestPasswordResetSuccess,
    accountPathChosen: () => authLogic.actionTypes.chooseAccountPath,
    setPasswordLinkSent: () => loginLogic.actionTypes.sendSetPasswordLinkSuccess,
}

describe('loginTelemetryLogic', () => {
    let logic: ReturnType<typeof loginTelemetryLogic.build>
    let login: ReturnType<typeof loginLogic.build>
    let precheckHandler: jest.Mock

    beforeEach(() => {
        // Skip the passkey auto-trigger, which is irrelevant to what these cases assert
        Object.defineProperty(window.navigator, 'vendor', { value: WEBKIT_VENDOR, configurable: true })
        precheckHandler = jest.fn(() => [200, { saml_available: false }])
        useMocks({
            get: {
                '/api/users/@me/': () => [200, {}],
                '/api/login/2fa/passkey/methods/': () => [200, { has_totp: true, has_passkeys: false }],
            },
            post: {
                '/api/login/precheck': precheckHandler,
                '/api/login': () => [401, { code: 'invalid_credentials', detail: 'Invalid email or password.' }],
                '/api/reset/': () => [200, { success: true }],
                '/api/login/code-based-verification': () => [
                    429,
                    { code: 'throttled', detail: 'Request was throttled.' },
                ],
            },
        })
        initKeaTests()
        router.actions.push('/login')
        login = loginLogic()
        login.mount()
        logic = loginTelemetryLogic()
        logic.mount()
    })

    afterEach(() => {
        logic.unmount()
        login.unmount()
        Object.defineProperty(window.navigator, 'vendor', { value: originalVendor, configurable: true })
        jest.clearAllMocks()
    })

    it('reports an attempt and its failure for a rejected password', async () => {
        login.actions.setLoginValues({ email: '[email protected]', password: 'wrong-password' })
        login.actions.submitLogin()
        await expectLogic(login).toDispatchActions(['setGeneralError'])

        expect(capturedProperties('login attempted')).toMatchObject({ method: 'password' })
        expect(capturedProperties('login failed')).toMatchObject({
            step: 'login',
            error_code: 'invalid_credentials',
        })
    })

    // A throttle and an expired code are different failures. Reporting the rejected response, not the
    // banner text, is what keeps them apart.
    it('reports the reason the verification code was rejected', async () => {
        login.actions.setCodeVerificationValue('code', '123456')
        login.actions.submitCodeVerification()
        await expectLogic(login).toDispatchActions(['submitCodeVerificationFailure']).toFinishAllListeners()

        expect(capturedProperties('login failed')).toMatchObject({ step: 'code', error_code: 'throttled' })
    })

    // Editing the code clears the manual error, which must not read as a second failure.
    it('does not report the clearing of a code error', () => {
        login.actions.setCodeVerificationManualErrors({})

        expect(captureCount('login failed')).toBe(0)
    })

    it('reports the step that rejected a second factor', async () => {
        const twoFA = login2FALogic()
        twoFA.mount()
        twoFA.actions.setGeneralError('2fa_invalid', 'Invalid token')
        await expectLogic(twoFA).toFinishAllListeners()
        twoFA.unmount()

        expect(capturedProperties('login failed')).toMatchObject({ step: '2fa', error_code: '2fa_invalid' })
    })

    it('reports a requested reset email', async () => {
        const reset = passwordResetLogic()
        reset.mount()
        reset.actions.setRequestPasswordResetValue('email', '[email protected]')
        reset.actions.submitRequestPasswordReset()
        await expectLogic(reset).toDispatchActions(['submitRequestPasswordResetSuccess']).toFinishAllListeners()
        reset.unmount()

        expect(captureCount('password reset requested')).toBe(1)
    })

    it('reports the no-method banner once per email on the login step, and the email it leads to', async () => {
        precheckHandler.mockImplementation(() => [
            200,
            { saml_available: false, password_login_available: false, social_providers: [] },
        ])
        const auth = authLogic()
        auth.mount()
        login.actions.setLoginValues({ email: '[email protected]' })
        login.actions.precheck({ email: '[email protected]' })
        await expectLogic(login).toDispatchActions(['precheckSuccess']).toFinishAllListeners()

        expect(captureCount('login no method shown')).toBe(0)

        auth.actions.continueWithEmail()
        await expectLogic(auth).toDispatchActions(['chooseAccountPath']).toFinishAllListeners()
        auth.actions.backToEmail()
        auth.actions.continueWithEmail()
        await expectLogic(auth).toDispatchActions(['chooseAccountPath']).toFinishAllListeners()
        auth.unmount()

        login.actions.sendSetPasswordLink('[email protected]')
        await expectLogic(login).toDispatchActions(['sendSetPasswordLinkSuccess']).toFinishAllListeners()

        expect(captureCount('login no method shown')).toBe(1)
        expect(capturedProperties('password reset requested')).toMatchObject({ source: 'login_no_method' })
    })

    it('does not report a set-password email that the server refused', async () => {
        useMocks({ post: { '/api/reset/': () => [429, { detail: 'Request was throttled.' }] } })

        login.actions.sendSetPasswordLink('[email protected]')
        await expectLogic(login).toDispatchActions(['sendSetPasswordLinkFailure']).toFinishAllListeners()

        expect(captureCount('password reset requested')).toBe(0)
    })

    // The server redirects a failed SSO sign-in back to /login?error_code=..., and kea-router replays
    // that URL while loginLogic mounts, before this logic exists. That is the case where the person
    // is most stuck, so it must not fall through the gap.
    it('reports an error that loginLogic already held when it mounted', () => {
        logic.unmount()
        login.unmount()
        router.actions.push('/login?error_code=improperly_configured_sso&error_detail=Check+your+SSO+setup')
        login = loginLogic()
        login.mount()
        logic = loginTelemetryLogic()
        logic.mount()

        expect(capturedProperties('login failed')).toMatchObject({
            step: 'login',
            error_code: 'improperly_configured_sso',
        })
        expect(captureCount('login failed')).toBe(1)
    })

    // The passkey prompt opens on its own after the precheck, so its failures are not attempts the
    // person made. A funnel that counts driven attempts has to be able to drop them.
    it('gives a passkey failure its own step', () => {
        login.actions.setGeneralError('passkey_error', 'Passkey login failed')

        expect(capturedProperties('login failed')).toMatchObject({ step: 'passkey' })
    })

    // An error raised while the auth scenes are gone — a failed passkey re-authentication inside the
    // app — must not land in the login funnel.
    it('reports nothing once the auth scene unmounts', () => {
        logic.unmount()
        login.actions.setGeneralError('passkey_error', 'Passkey login failed')
        logic.mount()

        expect(captureCount('login failed')).toBe(0)
    })

    // The listeners are keyed on strings, so nothing else ties a literal to the logic that dispatches
    // the action. A literal that drifts from the real type stops its listener without any error.
    it.each(Object.keys(LISTENED_ACTIONS) as (keyof typeof LISTENED_ACTIONS)[])(
        'listens for the real action type of %s',
        (key) => {
            const twoFA = login2FALogic()
            const reset = passwordResetLogic()
            const auth = authLogic()
            twoFA.mount()
            reset.mount()
            auth.mount()

            expect(LISTENED_ACTIONS[key]).toBe(LISTENED_ACTION_SOURCES[key]())

            twoFA.unmount()
            reset.unmount()
            auth.unmount()
        }
    )

    // Hearing another logic's actions must not hold that logic mounted. A held logic keeps its state
    // between visits, so a failed second factor would show its old error on the next attempt.
    it('mounts no other logic', () => {
        logic.unmount()
        login.unmount()
        logic = loginTelemetryLogic()
        logic.mount()

        expect(loginLogic.findMounted()).toBeNull()
        expect(login2FALogic.findMounted()).toBeNull()
        expect(passwordResetLogic.findMounted()).toBeNull()
        expect(authLogic.findMounted()).toBeNull()

        // afterEach unmounts both logics, so restore the one the test took down
        login = loginLogic()
        login.mount()
    })

    // precheck_failed must distinguish "the precheck ran and failed" from "no current precheck
    // exists" — a pending or stale-email result would otherwise be misattributed to this attempt.
    it.each([
        [
            'no precheck has run',
            async (): Promise<void> => {
                login.actions.setLoginValues({ email: '[email protected]', password: 'wrong' })
            },
            undefined,
        ],
        [
            'the precheck completed for a different email',
            async (): Promise<void> => {
                login.actions.precheck({ email: '[email protected]' })
                await expectLogic(login).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
                login.actions.setLoginValues({ email: '[email protected]', password: 'wrong' })
            },
            undefined,
        ],
        [
            'the precheck failed for the current email',
            async (): Promise<void> => {
                precheckHandler.mockImplementationOnce(() => [429, { detail: 'Request was throttled.' }])
                login.actions.setLoginValues({ email: '[email protected]', password: 'wrong' })
                login.actions.precheck({ email: '[email protected]' })
                await expectLogic(login).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
            },
            true,
        ],
        [
            'the precheck succeeded for the current email',
            async (): Promise<void> => {
                login.actions.setLoginValues({ email: '[email protected]', password: 'wrong' })
                login.actions.precheck({ email: '[email protected]' })
                await expectLogic(login).toDispatchActions(['precheckSuccess']).toFinishAllListeners()
            },
            false,
        ],
    ])('reports precheck_failed only for a current precheck: %s', async (_name, setup, expected) => {
        await setup()
        login.actions.setGeneralError('invalid_credentials', 'Invalid email or password.')
        await expectLogic(login).toDispatchActions(['setGeneralError']).toFinishAllListeners()

        expect(capturedProperties('login failed')?.precheck_failed).toBe(expected)
    })
})