Skip to content

Canvix — Free & open-source graphic design tool. A community-built Canva alternative with a drag-and-drop editor, templates, shapes, text, images & one-click export. Works on desktop, tablet & mobile.

4.5 KB
Raw Blame
// Unit tests for the v0.9.2 AI-route abuse guard (strict same-origin)
// and rate-limit tightening — see src/lib/ai/provider.ts.
//
// Invariants:
//   1. POSTs without Origin/Referer (direct API bots) → 403, zero provider work
//   2. POSTs with a foreign Origin → 403
//   3. Same-origin POSTs (browser fetch from the app) → pass the guard
//      and complete the (mocked) provider path → 200
//
// Hermeticity: the SDK is mocked below, but withProvider() checks
// resolveProviderConfig() BEFORE touching the SDK — env vars
// (ZAI_BASE_URL/ZAI_API_KEY) → .z-ai-config file chain (cwd → ~ → /etc).
// Machines that carry a real config (e.g. a dev sandbox with
// /etc/.z-ai-config) used to answer 200 while clean CI runners
// answered 503 — caught by the first run of the v0.12 CI gate. The
// env pair is stubbed in beforeEach so every machine takes the same
// path; the mocked SDK supplies the response.

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'

// This sandbox (and dev machines) can carry a real /etc/.z-ai-config, which
// would make the provider path hit the live gateway from unit tests. Mock
// the SDK module itself so the route's full happy path runs hermetically.
//
// v0.16.2 (A2): the app constructs the SDK directly (new ZAI({ baseUrl,
// apiKey }) — see makeClient in provider.ts), because ZAI.create() only
// reads a .z-ai-config file and ignores env vars. The mock is therefore
// a CLASS now, mirroring the real constructor surface.
const generationsCreate = vi.fn(async () => ({ data: [{ base64: 'aGVsbG8=' }] }))
vi.mock('z-ai-web-dev-sdk', () => ({
  default: class MockZAI {
    images: { generations: { create: typeof generationsCreate } }
    constructor(_config: { baseUrl?: string; apiKey?: string }) {
      this.images = { generations: { create: generationsCreate } }
    }
  },
}))

// recordUsage is fire-and-forget against the DB — mock it so tests never
// open a real connection (which would hang the vitest worker).
const dbMock = vi.hoisted(() => ({
  aIUsage: { create: vi.fn().mockResolvedValue({}) },
}))
vi.mock('@/lib/db', () => ({ db: dbMock }))

import { NextRequest } from 'next/server'
import { POST } from './route'

function post(opts: { origin?: string; referer?: string } = {}) {
  const headers: Record<string, string> = {
    host: 'canvix.test',
    'content-type': 'application/json',
  }
  if (opts.origin) headers.origin = opts.origin
  if (opts.referer) headers.referer = opts.referer
  return POST(
    new NextRequest('http://canvix.test/api/ai/image', {
      method: 'POST',
      headers,
      body: JSON.stringify({ prompt: 'a poster' }),
    }),
  )
}

beforeEach(() => {
  // configured-server simulation — short-circuits the config-file chain
  // deterministically (see the hermeticity note above)
  vi.stubEnv('ZAI_BASE_URL', 'http://provider.test')
  vi.stubEnv('ZAI_API_KEY', 'test-key')
  vi.clearAllMocks()
  dbMock.aIUsage.create.mockResolvedValue({})
  generationsCreate.mockResolvedValue({ data: [{ base64: 'aGVsbG8=' }] })
})

afterEach(() => {
  vi.unstubAllEnvs()
})

describe('POST /api/ai/image — v0.9.2 abuse guard', () => {
  it('rejects Origin-less POSTs (direct API bots) before any provider work', async () => {
    const res = await post()
    expect(res.status).toBe(403)
    // guard fired before the rate limit even ran → no usage accounting
    expect(dbMock.aIUsage.create).not.toHaveBeenCalled()
  })

  it('rejects foreign origins', async () => {
    const res = await post({ origin: 'https://evil.example' })
    expect(res.status).toBe(403)
  })

  it('lets same-origin POSTs through the guard (full happy path)', async () => {
    const res = await post({ origin: 'http://canvix.test' })
    expect(res.status).toBe(200)
    const body = await res.json()
    expect(body.dataUrl).toBe('data:image/png;base64,aGVsbG8=')
    expect(generationsCreate).toHaveBeenCalledTimes(1)
  })

  it('accepts a same-origin Referer fallback', async () => {
    const res = await post({ referer: 'http://canvix.test/editor' })
    expect(res.status).not.toBe(403)
  })

  it('rejects when the Referer matches neither host nor x-forwarded-host', async () => {
    const res = await POST(
      new NextRequest('http://canvix.test/api/ai/image', {
        method: 'POST',
        headers: {
          host: 'canvix.test',
          'x-forwarded-host': 'canvix.real',
          referer: 'http://evil.example/editor',
          'content-type': 'application/json',
        },
        body: JSON.stringify({ prompt: 'x' }),
      }),
    )
    expect(res.status).toBe(403)
  })
})