Files
supabase/apps/studio/components/interfaces/Account/Preferences/AnalyticsSettings.test.tsx
Sean Oliver b23e9e8d94 fix(consent): clear tracking state and reload when consent is withdrawn (#53294)
## Problem

Opting out from the Privacy Settings modal (www, docs, kb) clears no
cookies, and GTM keeps running. The banner's Opt out misses `_ga_<id>`,
`_dcid`, and PostHog.

## Solution

- `revokeTrackingConsent()` runs from `denyAll`, and from
`updateServices` unless every service is accepted. It clears `_ga`,
`_ga_*`, `_gcl_*`, `FP*`, `_dcid`, `session_id`, `ph_*_posthog`,
PostHog's localStorage, stale consent copies.
- PostHog resets and opts out, with persistence off. The opt-out
survives sign-out. A saved accept opts back in.
- Every opt-out calls `POST /platform/telemetry/reset` (keepalive,
errors ignored) to clear the API's HttpOnly `session_id`,
`anonymous_id`, and `user_id`. Studio's own call is removed.
- To stop GTM, a saved opt-out reloads the page if tracking was on,
waiting at most 1s for the reset.

## Review instructions

1. `cd packages/common && npx vitest run consent-state.test.ts
posthog-client.test.ts`
2. On prod: accept, open Privacy Settings, turn Marketing off, Confirm.
The page reloads with cookies gone and no `collect` request.

Tested: `common` (237 pass), `AnalyticsSettings` tests, CI unit tests
and typecheck. No browser test (previews lack Usercentrics).

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] If I wrote a new docs topic or edited an existing topic, I used
the `/write-the-docs` or `/edit-the-docs` skill
2026-11-17 16:38:23 -08:00

90 lines
2.8 KiB
TypeScript

import { fireEvent, screen, waitFor } from '@testing-library/react'
import { consentState } from 'common'
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
import { AnalyticsSettings } from './AnalyticsSettings'
import { customRender } from '@/tests/lib/custom-render'
const makeUC = () => ({
acceptAllServices: vi.fn(() => Promise.resolve()),
denyAllServices: vi.fn(() => Promise.resolve()),
getCategoriesBaseInfo: vi.fn(() => []),
})
describe('AnalyticsSettings', () => {
const reload = vi.fn()
const originalLocation = window.location
let UC: ReturnType<typeof makeUC>
beforeEach(() => {
vi.clearAllMocks()
UC = makeUC()
consentState.UC = UC as never
consentState.categories = []
consentState.hasConsented = true
Object.defineProperty(window, 'location', {
value: { ...originalLocation, reload },
writable: true,
configurable: true,
})
})
afterEach(() => {
consentState.UC = null
consentState.categories = null
consentState.hasConsented = false
Object.defineProperty(window, 'location', {
value: originalLocation,
writable: true,
configurable: true,
})
})
test('reloads once the opt-out is saved', async () => {
const saved = Promise.withResolvers<void>()
UC.denyAllServices.mockReturnValueOnce(saved.promise)
customRender(<AnalyticsSettings />)
fireEvent.click(screen.getByRole('switch'))
expect(UC.denyAllServices).toHaveBeenCalledOnce()
expect(reload).not.toHaveBeenCalled()
saved.resolve()
await waitFor(() => expect(reload).toHaveBeenCalledOnce())
})
test('does not reload when the opt-out fails to save', async () => {
UC.denyAllServices.mockRejectedValueOnce(new Error('network'))
customRender(<AnalyticsSettings />)
fireEvent.click(screen.getByRole('switch'))
await waitFor(() => expect(UC.denyAllServices).toHaveBeenCalledOnce())
await new Promise((resolve) => setTimeout(resolve, 100))
expect(reload).not.toHaveBeenCalled()
})
test('turns the switch back off when the opt-in fails to save', async () => {
consentState.hasConsented = false
UC.acceptAllServices.mockRejectedValueOnce(new Error('network'))
customRender(<AnalyticsSettings />)
fireEvent.click(screen.getByRole('switch'))
await waitFor(() => expect(UC.acceptAllServices).toHaveBeenCalledOnce())
await waitFor(() => expect(screen.getByRole('switch')).not.toBeChecked())
})
test('does not reload when opting in', async () => {
consentState.hasConsented = false
customRender(<AnalyticsSettings />)
fireEvent.click(screen.getByRole('switch'))
await waitFor(() => expect(consentState.hasConsented).toBe(true))
expect(UC.acceptAllServices).toHaveBeenCalledOnce()
expect(reload).not.toHaveBeenCalled()
})
})