mirror of
https://github.com/nomadkaraoke/karaoke-gen.git
synced 2026-12-02 09:58:08 +09:00
@coderabbitai ignore ## Problem The white-label tenant portals (`vocalstar.nomadkaraoke.com`, `singa.nomadkaraoke.com`) looked "done" but **had never worked end-to-end**: 0 of the last 484 production jobs carried a `tenant_id`. No tenant job had ever run. **Root cause:** the frontend API client never sent the `X-Tenant-ID` header. Tenant portals call the shared `api.nomadkaraoke.com`, whose Host isn't the tenant subdomain, and the `?tenant=` query param is disabled in prod — so the backend could never detect the tenant. Every tenant sign-in and job submission silently fell back to consumer behavior (default theme, no tenant Dropbox, etc.). The `getTenantHeaders()` helper existed and was unit-tested, but was never wired into requests. ## Changes **Frontend** (`lib/api.ts`, `lib/tenant.ts`) - Attach `X-Tenant-ID` on all authenticated requests **and** the magic-link request, sourced from `window.__TENANT_CONFIG__` (edge-injected by the Cloudflare function; mirrored from the tenant store for dev/preview). Consumer build resolves no tenant → no header → **behavior unchanged**. **Backend** (`_apply_tenant_overrides` in `file_upload.py`) - Make tenant config **authoritative** over global distribution defaults. Prod sets `DEFAULT_DROPBOX_PATH` / `DEFAULT_GDRIVE_FOLDER_ID` / `DEFAULT_BRAND_PREFIX` globally; these seed `dist`, and the old "fill only when unset" overlay let them leak — so every tenant job would have landed in the **shared consumer Dropbox folder** and uploaded to the **global Google Drive**. Now Dropbox/GDrive routing is gated by the tenant feature flags and cleared when disabled. **Tenant configs** (GCS + setup scripts, Vocal Star + Singa) - Disable Google Drive (`gdrive_upload=false`, `gdrive_folder_id=null`), keep YouTube off, enable Dropbox to each tenant's own folder. B2B = locked theme, no YouTube/GDrive, Dropbox + portal download only. ## Why it's consumer-safe - Frontend: header only attached when a tenant is resolved (never on `gen.nomadkaraoke.com`). - Backend: `_apply_tenant_overrides` early-returns when `tenant_config is None` (all consumer jobs). ## Testing - New: frontend header-attachment tests (tenant build vs consumer); backend authoritative-override + feature-flag-gating regression guards (incl. the global-default-leak case). - Full suites green: **3466 backend**, **1013 frontend**. - Verified live tenant configs via API after update. Manual prod E2E (real track through each portal) + a daily per-tenant E2E workflow follow in a second PR, once this deploys and is verified in prod. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
97 lines
3.0 KiB
TypeScript
97 lines
3.0 KiB
TypeScript
/**
|
|
* @jest-environment jsdom
|
|
*
|
|
* Tests that the API client attaches the X-Tenant-ID header on white-label tenant
|
|
* portals (sourced from window.__TENANT_CONFIG__) and omits it on the consumer build.
|
|
*
|
|
* Regression guard for the root-cause bug: the frontend never sent X-Tenant-ID, so the
|
|
* shared api.nomadkaraoke.com backend could not detect the tenant and every tenant-portal
|
|
* job/sign-in silently fell back to consumer behavior (0 tenant jobs ever ran in prod).
|
|
*/
|
|
|
|
import { api, getTenantId } from '@/lib/api'
|
|
|
|
function setEdgeTenant(id: string | null) {
|
|
if (id) {
|
|
;(window as any).__TENANT_CONFIG__ = {
|
|
tenant: { id, name: id, subdomain: `${id}.nomadkaraoke.com` },
|
|
is_default: false,
|
|
}
|
|
} else {
|
|
delete (window as any).__TENANT_CONFIG__
|
|
}
|
|
}
|
|
|
|
describe('getTenantId', () => {
|
|
afterEach(() => setEdgeTenant(null))
|
|
|
|
it('returns null when no tenant config is present (consumer build)', () => {
|
|
setEdgeTenant(null)
|
|
expect(getTenantId()).toBeNull()
|
|
})
|
|
|
|
it('returns the tenant id from window.__TENANT_CONFIG__', () => {
|
|
setEdgeTenant('vocalstar')
|
|
expect(getTenantId()).toBe('vocalstar')
|
|
})
|
|
|
|
it('returns null when the injected config has no tenant (default/consumer domain)', () => {
|
|
;(window as any).__TENANT_CONFIG__ = { tenant: null, is_default: true }
|
|
expect(getTenantId()).toBeNull()
|
|
})
|
|
})
|
|
|
|
describe('X-Tenant-ID header attachment', () => {
|
|
const fetchMock = jest.fn()
|
|
|
|
beforeEach(() => {
|
|
fetchMock.mockReset()
|
|
;(global as any).fetch = fetchMock
|
|
fetchMock.mockResolvedValue({ ok: true, json: async () => ({}) })
|
|
})
|
|
|
|
afterEach(() => setEdgeTenant(null))
|
|
|
|
it('attaches X-Tenant-ID to authenticated requests (createJobWithUploadUrls) on a tenant portal', async () => {
|
|
setEdgeTenant('vocalstar')
|
|
window.localStorage.setItem('karaoke_access_token', 'tok-123')
|
|
|
|
fetchMock.mockResolvedValue({
|
|
ok: true,
|
|
json: async () => ({ job_id: 'j1', upload_urls: [] }),
|
|
})
|
|
|
|
await api.createJobWithUploadUrls('Adele', 'Hello', [], {
|
|
is_private: true,
|
|
existing_instrumental: true,
|
|
})
|
|
|
|
const [, init] = fetchMock.mock.calls[0]
|
|
expect(init.headers['X-Tenant-ID']).toBe('vocalstar')
|
|
expect(init.headers['Authorization']).toBe('Bearer tok-123')
|
|
|
|
window.localStorage.removeItem('karaoke_access_token')
|
|
})
|
|
|
|
it('attaches X-Tenant-ID to the magic-link request on a tenant portal', async () => {
|
|
setEdgeTenant('singa')
|
|
fetchMock.mockResolvedValue({ ok: true, json: async () => ({ success: true }) })
|
|
|
|
await api.sendMagicLink('user@singa.com')
|
|
|
|
const [url, init] = fetchMock.mock.calls[0]
|
|
expect(url).toMatch(/\/api\/users\/auth\/magic-link$/)
|
|
expect(init.headers['X-Tenant-ID']).toBe('singa')
|
|
})
|
|
|
|
it('does NOT attach X-Tenant-ID on the consumer build', async () => {
|
|
setEdgeTenant(null)
|
|
fetchMock.mockResolvedValue({ ok: true, json: async () => ({ success: true }) })
|
|
|
|
await api.sendMagicLink('user@example.com')
|
|
|
|
const [, init] = fetchMock.mock.calls[0]
|
|
expect(init.headers['X-Tenant-ID']).toBeUndefined()
|
|
})
|
|
})
|