Skip to content

[SaaS #8] Test suite: update all tests for subdomain routing and new URL patterns #96

@macwilling

Description

@macwilling

Overview

Update the entire test suite to work with the new subdomain-based routing, NEXT_PUBLIC_BASE_DOMAIN env var, and restructured portal routes. This is a follow-up to issues #88#95 and must be done last.

Context

Multiple test files reference NEXT_PUBLIC_APP_URL, hardcoded /portal/{slug} paths, and assume path-based tenant resolution. All of these need updating.

Unit / Integration Tests (Vitest)

app/actions/portal.test.ts

  • Change process.env.NEXT_PUBLIC_APP_URL = "https://app.example.com"process.env.NEXT_PUBLIC_BASE_DOMAIN = "example.com"
  • Update expected magic link URL:
    • Old: https://app.example.com/portal/test-slug/auth-callback
    • New: https://test-slug.example.com/auth/callback
  • Update finalizePortalSessionAction tests: remove tenantSlug argument; mock headers() to return x-tenant-slug

app/auth/callback/route.test.ts

  • Add Host header to mock requests: request.headers.set("host", "test-slug.example.com")
  • Remove tests that rely on ?next=/portal/{slug} query param for tenant detection
  • Add test: callback on root domain (taskflow.com) creates new tenant and redirects to {slug}.taskflow.com/dashboard
  • Add test: callback on tenant subdomain (acme.taskflow.com) for portal client → redirects to acme.taskflow.com/portal
  • Update NEXT_PUBLIC_BASE_DOMAIN env var mock (remove ALLOW_REGISTRATION)

lib/url.test.ts (new)

Unit tests for the new lib/url.ts utility:

  • tenantUrl("acme", "/dashboard")https://acme.taskflow.com/dashboard
  • tenantUrl("acme")https://acme.taskflow.com
  • portalUrl("acme")https://acme.taskflow.com/portal
  • In local dev (base domain = localhost:3000): tenantUrl(...)http://localhost:3000/...

proxy.test.ts (new or existing)

Unit tests for getTenantSlugFromHost():

  • "acme.taskflow.com""acme"
  • "taskflow.com"null
  • "www.taskflow.com"null
  • "localhost:3000"null
  • "acme.lvh.me" (dev testing) → "acme" when BASE_DOMAIN=lvh.me

E2E Tests (Playwright)

playwright.config.ts

  • baseURL currently set to http://localhost:3000
  • For subdomain testing in CI/E2E: configure /etc/hosts or use lvh.me trick
    • lvh.me and *.lvh.me always resolve to 127.0.0.1 — no hosts file needed
    • Set NEXT_PUBLIC_BASE_DOMAIN=lvh.me in E2E test environment
    • baseURLhttp://lvh.me:3000 for the landing page; tenant URLs: http://{slug}.lvh.me:3000

tests/e2e/portal.spec.ts

  • Update all portal navigation to use subdomain URLs instead of path-based
  • Portal URL: http://${tenantSlug}.lvh.me:3000/portal
  • Portal login: http://${tenantSlug}.lvh.me:3000/portal/login
  • Remove tenantSlug from path segments in assertions

tests/e2e/auth.spec.ts (or equivalent)

  • Add registration flow test: visit http://lvh.me:3000/auth/register, complete form, assert redirect to http://{slug}.lvh.me:3000/dashboard
  • Test session cookie is valid on tenant subdomain after registration

tests/e2e/fixtures/global-setup.ts

  • Update storageState fixtures to use subdomain origins
  • Seed data setup: ensure test tenant slug is predictable (e.g. test-tenant)

Key Files

  • app/actions/portal.test.ts
  • app/auth/callback/route.test.ts
  • New: lib/url.test.ts
  • New: proxy.test.ts
  • playwright.config.ts
  • tests/e2e/portal.spec.ts
  • tests/e2e/fixtures/global-setup.ts

Testing Definition of Done

  • npm run test:unit passes with 0 failures
  • npm run test:e2e passes with subdomain-aware configuration
  • npm run build passes

Depends On

All of issues #88#95 must be merged before this test update is complete (though tests can be written in parallel).

Metadata

Metadata

Assignees

No one assigned

    Labels

    saas-migrationTrue multi-tenant SaaS migration

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions