Files
accounted/tests/pg/dashboard-nav-flags-rpc.pg.test.ts
T
Jakob Wennberg 4ac7b45c8a perf(layout): dashboard layout in two waves, nav flags as one RPC, local JWT verification (#1946)
The dashboard layout runs on every hard load, hard refresh, company switch
and the 16 router.refresh() sites, and loading.tsx cannot paint until it
resolves. It cost ~20 network calls in 4 sequential waves: a third
getUser() round trip to Supabase Auth (after the proxy's and the route
guard's), the company resolution, then 16 reads including four limit-1
probes whose only job is to decide whether to render the Webshop and
Körjournal nav rows, and an entitlements read that itself ran two waves.

- lib/auth/claims.ts: claimsPinned/userFromClaims extracted from
  require-auth.ts (unchanged) so the dashboard request context shares the
  exact pinning + mapping. getDashboardAuthContext verifies the JWT locally
  and falls back to getUser() only when claims are missing, unpinned or
  unverifiable: the proxy already performed the per-request revocation
  check before the layout runs (same semantics approved for routes on
  2026-07-23).
- Wave 1 (user-keyed, parallel with the company resolution): team
  membership, profile, user preferences and the memberships join, which
  now also supplies the active company's row and role, so the separate
  companies and company_members reads are gone.
- Wave 2 (company-keyed): settings, agent profile, the switcher's settings
  names, entitlements in ONE wave (getCompanyEntitlements takes the
  team_id the join already carries and runs the grants read alongside
  config + subscription), and get_dashboard_nav_flags().
- supabase/migrations/20260826120000_get_dashboard_nav_flags.sql:
  SECURITY INVOKER, STABLE, EXECUTE for authenticated only; RLS applies
  inside. lib/dashboard/nav-flags.ts wraps it with the pre-RPC four-probe
  fallback on PGRST202/42883/42501 (self-hosted not yet migrated, deploy
  ordering) and degrades to hidden rows on any other error.
- tests/pg/dashboard-nav-flags-rpc.pg.test.ts (6): fresh company, active vs
  pending WooCommerce, active Shopify, mileage trips, RLS for a member of
  another company, EXECUTE grants. Unit tests for the wrapper (RPC row,
  single-object payload, each fallback code, other errors) and for the
  entitlements teamId option.

~20 calls / 4 waves -> ~12 calls / 2 waves, 0 auth network calls.

Co-authored-by: Jakob Wennberg <311770904+jakobwennberg-oss@users.noreply.github.com>
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-08-26 15:14:49 +02:00

97 lines
4.5 KiB
TypeScript

import { describe, it, expect } from 'vitest'
import { getPool, withUserContext } from './setup'
import { insertAuthUser, insertCompany, insertCompanyMember, seedCompany } from './fixtures'
// Validates migration 20260826120000_get_dashboard_nav_flags:
// 1. has_webshop flips on an ACTIVE WooCommerce or Shopify connection
// (a pending/revoked one does not count) and has_mileage_trips on any
// mileage_trips row.
// 2. SECURITY INVOKER: a member of ANOTHER company sees (false, false)
// for this company, RLS applies inside the function.
// 3. EXECUTE is granted to authenticated only, not anon.
//
// webshop_orders is the third has_webshop source; its row shape carries many
// NOT NULL platform columns and the existing connection paths already prove
// the OR, so it is covered by the EXISTS shape, not a fixture here.
const FLAGS = `SELECT has_webshop, has_mileage_trips FROM public.get_dashboard_nav_flags($1::uuid)`
type Row = { has_webshop: boolean; has_mileage_trips: boolean }
async function flagsAs(userId: string, companyId: string): Promise<Row> {
return withUserContext(userId, async (client) => {
const res = await client.query<Row>(FLAGS, [companyId])
expect(res.rows).toHaveLength(1)
return res.rows[0]
})
}
describe('get_dashboard_nav_flags()', () => {
it('is (false, false) for a fresh company', async () => {
const { userId, companyId } = await seedCompany()
expect(await flagsAs(userId, companyId)).toEqual({ has_webshop: false, has_mileage_trips: false })
})
it('flips has_webshop on an active WooCommerce connection, not on a pending one', async () => {
const { userId, companyId } = await seedCompany()
await getPool().query(
`INSERT INTO public.woocommerce_connections (company_id, user_id, store_url, status)
VALUES ($1, $2, $3, 'pending')`,
// Unique per run: the active-connection unique index is on the store URL.
[companyId, userId, `https://nav-flags-${companyId.slice(0, 8)}.example.se`],
)
expect((await flagsAs(userId, companyId)).has_webshop).toBe(false)
await getPool().query(
`UPDATE public.woocommerce_connections SET status = 'active' WHERE company_id = $1`,
[companyId],
)
expect((await flagsAs(userId, companyId)).has_webshop).toBe(true)
})
it('flips has_webshop on an active Shopify connection', async () => {
const { userId, companyId } = await seedCompany()
await getPool().query(
`INSERT INTO public.shopify_connections (company_id, user_id, shop_domain, status)
VALUES ($1, $2, $3, 'active')`,
// Unique per run: shopify_connections_shop_active_uniq is on the domain.
[companyId, userId, `nav-flags-${companyId.slice(0, 8)}.myshopify.com`],
)
expect((await flagsAs(userId, companyId)).has_webshop).toBe(true)
})
it('flips has_mileage_trips on any mileage trip', async () => {
const { userId, companyId } = await seedCompany()
await getPool().query(
`INSERT INTO public.mileage_trips (company_id, user_id, trip_date, distance_km, from_location, to_location, purpose)
VALUES ($1, $2, '2026-08-01', 12.5, 'Kontoret', 'Kund AB', 'Kundbesök')`,
[companyId, userId],
)
expect(await flagsAs(userId, companyId)).toEqual({ has_webshop: false, has_mileage_trips: true })
})
it('applies RLS: a member of another company sees (false, false) for this one', async () => {
const { userId, companyId } = await seedCompany()
await getPool().query(
`INSERT INTO public.mileage_trips (company_id, user_id, trip_date, distance_km, from_location, to_location, purpose)
VALUES ($1, $2, '2026-08-01', 3, 'A', 'B', 'Test')`,
[companyId, userId],
)
const outsider = await insertAuthUser()
const otherCompany = await insertCompany({ createdBy: outsider })
await insertCompanyMember({ companyId: otherCompany, userId: outsider })
expect(await flagsAs(userId, companyId)).toEqual({ has_webshop: false, has_mileage_trips: true })
expect(await flagsAs(outsider, companyId)).toEqual({ has_webshop: false, has_mileage_trips: false })
})
it('grants EXECUTE to authenticated but not anon', async () => {
const res = await getPool().query<{ anon_can: boolean; authenticated_can: boolean }>(
`SELECT
has_function_privilege('anon', 'public.get_dashboard_nav_flags(uuid)', 'EXECUTE') AS anon_can,
has_function_privilege('authenticated', 'public.get_dashboard_nav_flags(uuid)', 'EXECUTE') AS authenticated_can`,
)
expect(res.rows[0].anon_can).toBe(false)
expect(res.rows[0].authenticated_can).toBe(true)
})
})