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.

10.6 KB
Raw Blame
import { MOCK_DEFAULT_USER } from 'lib/api.mock'

import { router } from 'kea-router'
import { expectLogic } from 'kea-test-utils'

import { lemonToast } from '@posthog/lemon-ui'

import { dayjs } from 'lib/dayjs'
import { apiStatusLogic } from 'lib/logic/apiStatusLogic'
import { ERROR_MESSAGES } from 'scenes/authentication/shared/loginErrorMessages'
import { userLogic } from 'scenes/userLogic'

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

import { timeSensitiveAuthenticationLogic } from './timeSensitiveAuthenticationLogic'

jest.mock('lib/api')
jest.mock('posthog-js')

describe('timeSensitiveAuthenticationLogic', () => {
    let logic: ReturnType<typeof timeSensitiveAuthenticationLogic.build>

    beforeEach(() => {
        initKeaTests()
        jest.spyOn(lemonToast, 'error').mockImplementation(() => null as any)
        logic = timeSensitiveAuthenticationLogic()
        logic.mount()
    })

    afterEach(() => {
        logic.unmount()
        jest.restoreAllMocks()
    })

    describe('initial state', () => {
        it('should have correct default values', async () => {
            await expectLogic(logic).toMatchValues({
                dismissedReauthentication: false,
                twoFactorRequired: false,
                precheckResponse: null,
            })
        })
    })

    describe('reauthentication form', () => {
        it('should validate password is required', async () => {
            await expectLogic(logic, () => {
                logic.actions.setReauthenticationValues({ password: '', token: undefined })
                logic.actions.submitReauthentication()
            }).toMatchValues({
                reauthenticationValidationErrors: {
                    password: 'Please enter your password to continue',
                },
            })
        })

        it('should validate 2FA token is required when 2FA is required', async () => {
            await expectLogic(logic, () => {
                logic.actions.setRequiresTwoFactor(true)
                logic.actions.setReauthenticationValues({ password: 'test', token: undefined })
                logic.actions.submitReauthentication()
            }).toMatchValues({
                reauthenticationValidationErrors: {
                    token: 'Please enter your 2FA code',
                },
            })
        })

        it('should clear the gate even when the sensitive action throws', async () => {
            userLogic.actions.loadUserSuccess(MOCK_DEFAULT_USER)
            const onSuccess = jest.fn(() => {
                throw new Error('sensitive action failed')
            })
            apiStatusLogic.actions.setTimeSensitiveAuthenticationRequired([onSuccess, jest.fn()])

            logic.actions.setReauthenticationValues({ password: 'test', token: undefined })
            logic.actions.submitReauthentication()
            await expectLogic(logic).toFinishAllListeners()

            expect(onSuccess).toHaveBeenCalledTimes(1)
            await expectLogic(logic).toMatchValues({ showAuthenticationModal: false })
        })

        it('should handle successful reauthentication', async () => {
            userLogic.actions.loadUserSuccess(MOCK_DEFAULT_USER)

            await expectLogic(logic, () => {
                logic.actions.setReauthenticationValues({ password: 'test', token: undefined })
                logic.actions.submitReauthentication()
            })
                .toDispatchActions(['submitReauthentication'])
                .toMatchValues({
                    reauthenticationValidationErrors: {},
                })
        })
    })

    describe('session expiry', () => {
        it('should show authentication modal when session is expired', async () => {
            const mockUser = {
                ...MOCK_DEFAULT_USER,
                sensitive_session_expires_at: dayjs().subtract(1, 'hour').toISOString(),
            }
            userLogic.actions.loadUserSuccess(mockUser)

            await expectLogic(logic, () => {
                logic.actions.checkReauthentication()
            }).toMatchValues({
                showAuthenticationModal: true,
            })
        })

        it('should show authentication modal when session is about to expire', async () => {
            const mockUser = {
                ...MOCK_DEFAULT_USER,
                sensitive_session_expires_at: dayjs().add(4, 'minutes').toISOString(),
            }
            userLogic.actions.loadUserSuccess(mockUser)

            await expectLogic(logic, () => {
                logic.actions.checkReauthentication()
            }).toMatchValues({
                showAuthenticationModal: true,
            })
        })

        it('should not show authentication modal when session is not about to expire', async () => {
            const mockUser = {
                ...MOCK_DEFAULT_USER,
                sensitive_session_expires_at: dayjs().add(6, 'minutes').toISOString(),
            }
            userLogic.actions.loadUserSuccess(mockUser)

            await expectLogic(logic, () => {
                logic.actions.checkReauthentication()
            }).toMatchValues({
                showAuthenticationModal: false,
            })
        })
    })

    describe('modal interactions', () => {
        it('should handle modal dismissal', async () => {
            await expectLogic(logic, () => {
                logic.actions.setDismissedReauthentication(true)
            }).toMatchValues({
                dismissedReauthentication: true,
            })
        })

        it('should show modal when required', async () => {
            await expectLogic(logic, () => {
                apiStatusLogic.actions.setTimeSensitiveAuthenticationRequired(true)
            }).toMatchValues({
                showAuthenticationModal: true,
            })
        })

        it('should settle a pending checkReauthentication when the modal is dismissed', async () => {
            userLogic.actions.loadUserSuccess({
                ...MOCK_DEFAULT_USER,
                sensitive_session_expires_at: dayjs().add(4, 'minutes').toISOString(),
            })

            const pending = logic.asyncActions.checkReauthentication()
            expect(logic.values.showAuthenticationModal).toBe(true) // guard: the check is actually pending on the modal
            logic.actions.setDismissedReauthentication(true)

            // Rejects (TypeError on undefined.message in kea's listener wrapper) if dismissal
            // rejects the stored callback pair instead of resolving it
            await pending

            await expectLogic(logic).toMatchValues({ showAuthenticationModal: false })
        })

        it('should email the signed-in user a link to set a password', async () => {
            userLogic.actions.loadUserSuccess({ ...MOCK_DEFAULT_USER, has_password: false })
            const resetRequests: unknown[] = []
            useMocks({
                post: {
                    '/api/reset/': async ({ request }) => {
                        resetRequests.push(await request.json())
                        return [204]
                    },
                },
            })

            await expectLogic(logic, () => {
                logic.actions.sendSetPasswordLink()
            })
                .toDispatchActions(['sendSetPasswordLinkSuccess'])
                .toMatchValues({ setPasswordLinkSentTo: MOCK_DEFAULT_USER.email })

            expect(resetRequests).toEqual([{ email: MOCK_DEFAULT_USER.email }])
        })
    })

    describe('SSO re-authentication in a popup', () => {
        const startPopupAttempt = (): string => {
            const open = jest.spyOn(window, 'open').mockReturnValue({} as Window)
            logic.actions.beginSsoReauthentication('google-oauth2')
            const next = new URL(open.mock.calls[0][0] as string, location.origin).searchParams.get('next')!
            return new URL(next, location.origin).searchParams.get('attempt')!
        }

        it.each([
            ['success', 'own', null, true, false],
            ['failure', 'own', 'reauth_user_mismatch', false, true],
            ['a message from another attempt', 'other', null, false, false],
        ])(
            'on %s, settles the waiting write only when it succeeded',
            async (_, source, errorCode, settled, toasted) => {
                const onSuccess = jest.fn()
                apiStatusLogic.actions.setTimeSensitiveAuthenticationRequired([onSuccess, jest.fn()])
                const attempt = startPopupAttempt()

                const popup = new BroadcastChannel('posthog-sso-reauth')
                const acknowledgment = new Promise((resolve) => (popup.onmessage = (event) => resolve(event.data)))
                popup.postMessage({
                    type: 'sso_reauth_complete',
                    attempt: source === 'own' ? attempt : 'someone-else',
                    error_code: errorCode,
                })
                await expectLogic(logic).toDispatchActions(['ssoReauthenticationFinished'])

                expect(onSuccess).toHaveBeenCalledTimes(settled ? 1 : 0)
                expect(logic.values.showAuthenticationModal).toBe(!settled)
                expect(lemonToast.error).toHaveBeenCalledTimes(toasted ? 1 : 0)
                if (source === 'own') {
                    // The popup waits for this before it closes, so the result is never dropped
                    expect(await acknowledgment).toEqual({ type: 'sso_reauth_received', attempt })
                }
                popup.close()
            }
        )
        it('keeps SAML on the full-page redirect', () => {
            const open = jest.spyOn(window, 'open')
            // jsdom reports the page navigation as not implemented
            jest.spyOn(console, 'error').mockImplementation(() => {})

            logic.actions.beginSsoReauthentication('saml')

            expect(open).not.toHaveBeenCalled()
        })
    })

    describe('failed SSO re-authentication', () => {
        it('should report the error the backend sent back and drop it from the URL', async () => {
            router.actions.push('/settings/user', { error_code: 'reauth_user_mismatch' })

            logic.actions.showSsoReauthenticationError()

            expect(lemonToast.error).toHaveBeenCalledWith(ERROR_MESSAGES.reauth_user_mismatch)
            expect(router.values.searchParams).toEqual({})
        })

        it('should leave unrelated query params alone', async () => {
            router.actions.push('/settings/user', { tab: 'danger-zone' })

            logic.actions.showSsoReauthenticationError()

            expect(lemonToast.error).not.toHaveBeenCalled()
            expect(router.values.searchParams).toEqual({ tab: 'danger-zone' })
        })
    })
})