Files
civitai__civitai/tests/preview-apps-external-submit.spec.ts
T
Zachary Lowden 7c8814d2ca feat(app-blocks): W13 P3a — off-site listing submission backend (dark) (#2950)
* feat(app-blocks): W13 P3a off-site listing submission backend (dark)

Adds the native external-link off-site app submission flow behind the
`app-blocks-author` flag (mods + app-dev-testers). Design B1 (locked):
submit creates, in one transaction, a DRAFT AppListing(kind='offsite',
status='draft') + a pending AppListingPublishRequest(kind='offsite',
appListingId=<draft id>), so the author can reuse the P1 asset CRUD to
attach icon/cover/screenshots before a mod approves. The read path hides
non-approved rows, so a draft never surfaces in the store.

- New offsite-listing.schema.ts: submitExternalListingSchema
  (name/externalUrl/slug/tagline?/description?/category?/contentRating
  default 'g'/changelog?), reusing validateExternalUrl +
  assertNoOnPlatformSurface from external-app.schema (https-only, external
  vs on-platform mutual exclusivity).
- New offsite-listing.service.ts: submitExternalListing (owner-bound,
  slug-collision pre-check + P2002-race branch, cross-kind block-id check),
  withdrawExternalRequest (IDOR + TOCTOU status-guarded updateMany,
  terminal: deletes the draft listing to release the slug),
  listMySubmissions + read-only mod queue lists (pending/approved/rejected).
- Wire procs on the appListings router: submit/withdraw/listMySubmissions
  as appDeveloperProcedure; the queue lists as moderatorProcedure.
- Widen the P1 asset-CRUD flag gate mod->author (enforceAppBlocksAuthorFlag
  via isAppBlocksAuthorEnabled); the service-layer owner check still bounds
  each caller to their own listing. backfillAssets stays moderatorProcedure.
- Comment-only Prisma update on AppListingPublishRequest.appListingId (B1
  sets it at submit) — no schema/DDL change, no migration.

Tests: offsite-listing.schema (18), offsite-listing.service (17), router
authz matrix (22) — all green. e2e spec authored (submit -> mod queue ->
withdraw, self-cleaning; runs in Tekton pr-smoke-test, not locally).

Dark: no UI. PR-b (approve/reject + assertListingAssetsComplete), PR-c
(UI), and PR-d (#2821 retirement) follow as separate PRs off main.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* harden(app-blocks/offsite): rate-limit + pending cap + block_id primary re-check

Fold the pre-deploy hardening items from the PR #2950 audit into the P3a
off-site submission backend (dark behind app-blocks-author; reachable by
non-mod dev-testers via tRPC once deployed):

- submitExternalListing: add rateLimit (10/hour) middleware, mirroring the
  public read procs' rateLimit idiom — throttles draft-spam / slug-squat.
- Per-user OUTSTANDING pending-submission cap (MAX_PENDING_OFFSITE_SUBMISSIONS
  = 10) in the service — bounds standing orphan-draft accrual (drafts have no
  TTL, only clear on withdraw/reject); at/over cap -> TOO_MANY_REQUESTS.
- Cross-kind AppBlock.block_id collision: re-check from the PRIMARY (dbWrite)
  inside the create tx to close the replica-lag window the constraint-less
  pre-check leaves open (AppListing.slug is P2002-backstopped; block_id is not).
- Re-assert author-declared contentRating against OFFSITE_CONTENT_RATINGS in
  the service (defense-in-depth, matching the URL/surface/category re-checks;
  keeps the 'g' default).
- offsite-listing.schema: z.ZodIssueCode.custom -> 'custom' (Zod v4 idiom).

Tests: +5 service cases (pending cap at/under, block_id primary-recheck path,
contentRating re-assert). 61 offsite unit tests green (schema+service+router).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* chore(w13-p3a): re-trigger Tekton preview build

* chore(ci): re-trigger preview build (Tekton transient issue resolved)

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* test(app-blocks/offsite): run P3a submit e2e as mod, not the synthetic tester

The preview 'tester' fixture (id 2000000002) is in the preview-ACCESS
allowlist but NOT the app-blocks-author cohort, so submitExternalListing
(appDeveloperProcedure) 403s it by design. Mods are authors via the
app-blocks-author mod floor, so run the whole submit->queue->withdraw leg
as mod, matching every sibling apps smoke spec (publish/install/marketplace/page).
The author-gate rejection is covered by the unit router-authz tests.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 14:46:23 -05:00

177 lines
7.3 KiB
TypeScript

import { expect, test } from '@playwright/test';
import type { APIRequestContext } from '@playwright/test';
import { storageStatePath } from './preview-fixtures';
import { trpcMutation, trpcQuery } from './preview-trpc';
/**
* Preview-e2e: App Blocks W13 P3a — OFF-SITE (external-link) submission backend.
*
* The dark author-submit → mod-review-queue leg (design B1), run as `mod`:
* - `appListings.submitExternalListing` creates a DRAFT AppListing + a pending
* AppListingPublishRequest (kind='offsite') for an https target.
* - `appListings.listPendingRequests` shows the row.
* - `appListings.withdrawExternalRequest` is terminal — the draft listing is
* deleted and the row leaves the pending queue.
*
* approve/reject + the store render + the Visit-anchor invariant land in PR-b/PR-c
* (with the approve service + UI), so they are NOT exercised here — this PR ships
* the submission backend only.
*
* ROLE — why `mod`, not `tester`: `submitExternalListing`/`withdrawExternalRequest`
* are `appDeveloperProcedure` (`app-blocks-author`) and `listPendingRequests` is
* `moderatorProcedure`. The `mod` fixture satisfies BOTH (mods are authors via the
* app-blocks-author mod floor), so — like every sibling apps smoke spec
* (preview-apps-publish/-install/-marketplace/-page) — the whole leg runs as mod.
* The synthetic preview `tester` fixture is in the preview-ACCESS allowlist but NOT
* the `app-blocks-author` cohort (that's the real dev-tester user ids), so it 403s
* this proc BY DESIGN; the author-gate rejection is covered by the unit router-authz
* tests, not here.
*
* GATES (Tekton `pr-smoke-test` is authoritative — do NOT run browser-mode locally
* on NixOS): the `mod` fixture passes app-blocks-author (floor) + moderatorProcedure.
*
* SAFE + SELF-CLEANING (the dev DB is shared across concurrent previews):
* - The slug is per-preview (`ci-smoke-ext-<host-label>`), so two previews never
* collide on `AppListing.slug @unique`. A same-preview re-run pre-withdraws any
* leftover pending row before submitting.
* - No asset upload / approve → NO Image rows, NO Tekton build, NO CF DNS.
* - We withdraw in `finally` (deletes the draft listing + releases the slug) so a
* mid-test failure leaves no draft/pending row and re-runs don't collide.
*/
const AUTHOR_ROLE = 'mod' as const;
const PREVIEW_URL = process.env.PREVIEW_URL ?? '';
// Per-preview slug so concurrent previews don't collide on AppListing.slug @unique.
function previewSlug(): string {
let label = 'local';
try {
label = new URL(PREVIEW_URL).hostname.split('.')[0] || 'local';
} catch {
/* fall through to default */
}
const sanitized = label.toLowerCase().replace(/[^a-z0-9-]/g, '-');
const slug = `ci-smoke-ext-${sanitized}`.slice(0, 40).replace(/-+$/, '');
return /[a-z0-9]$/.test(slug) ? slug : `${slug}0`;
}
const SLUG = previewSlug();
const EXTERNAL_URL = 'https://example.com/ci-smoke-external-app';
type SubmitResult = { listingId: string; publishRequestId: string; slug: string };
type PendingItem = {
id: string;
slug: string;
appListingId: string | null;
appListing: { externalUrl: string | null } | null;
};
type PendingList = { items: PendingItem[]; nextCursor: string | null };
const submitInput = {
slug: SLUG,
name: 'CI Smoke — external app (P3a)',
externalUrl: EXTERNAL_URL,
tagline: 'a pure external-link app',
category: 'utility',
contentRating: 'g',
changelog: 'ci-smoke submit',
};
/** Page the oldest-first pending queue to find our row by slug (it's the newest). */
async function findPendingBySlug(
request: APIRequestContext,
slug: string
): Promise<PendingItem | null> {
let cursor: string | null = null;
for (let page = 0; page < 25; page++) {
const input: { limit: number; cursor?: string } = { limit: 100 };
if (cursor) input.cursor = cursor;
const list = await trpcQuery<PendingList>(request, 'appListings.listPendingRequests', input);
const hit = list.items.find((i) => i.slug === slug);
if (hit) return hit;
if (!list.nextCursor) break;
cursor = list.nextCursor;
}
return null;
}
/** Best-effort: withdraw any leftover pending row for this slug (self-clean). */
async function withdrawPendingForSlug(
authorRequest: APIRequestContext,
modRequest: APIRequestContext,
slug: string
): Promise<void> {
const row = await findPendingBySlug(modRequest, slug).catch(() => null);
if (row?.id) {
await trpcMutation(authorRequest, 'appListings.withdrawExternalRequest', {
publishRequestId: row.id,
}).catch(() => {});
}
}
test.describe('App Blocks P3a: off-site submit → mod queue → withdraw (mod, self-cleaning)', () => {
test.use({ storageState: storageStatePath(AUTHOR_ROLE) });
test('mod submits an external listing → appears in the pending queue → withdraw removes it', async ({
page,
playwright,
baseURL,
}) => {
// Warm page.request against the preview origin (carries the mod auth cookie;
// the trpc helpers stamp Origin/Referer for the CSRF gate).
await page.goto('/', { waitUntil: 'domcontentloaded' });
const authorRequest = page.request;
// A second mod context, to read the review queue independently of the submitter.
const modRequest = await playwright.request.newContext({
baseURL: baseURL ?? PREVIEW_URL,
storageState: storageStatePath('mod'),
});
let publishRequestId: string | null = null;
try {
// Pre-clean any leftover pending row for this preview's slug (prior crashed run).
await withdrawPendingForSlug(authorRequest, modRequest, SLUG);
// SUBMIT as mod (author via the app-blocks-author mod floor) — creates a draft AppListing + pending request.
const result = await trpcMutation<SubmitResult>(
authorRequest,
'appListings.submitExternalListing',
submitInput
);
publishRequestId = result.publishRequestId;
expect(typeof result.publishRequestId, 'submit returns a publishRequestId').toBe('string');
expect(result.slug, 'slug echoes the submission').toBe(SLUG);
// The row shows up in the MOD pending queue (kind='offsite' rows only).
const item = await findPendingBySlug(modRequest, SLUG);
expect(item, 'the submitted request appears in the mod pending queue').not.toBeNull();
expect(item!.id, 'queue row id matches the submit result').toBe(publishRequestId);
expect(
item!.appListing?.externalUrl,
'the draft listing carries the submitted https URL'
).toBe(EXTERNAL_URL);
// WITHDRAW as mod — terminal; deletes the draft listing + releases the slug.
await trpcMutation(authorRequest, 'appListings.withdrawExternalRequest', {
publishRequestId,
});
publishRequestId = null; // withdrawn — nothing left to clean in finally
// Gone from the pending queue.
const afterWithdraw = await findPendingBySlug(modRequest, SLUG);
expect(afterWithdraw, 'the withdrawn request no longer appears in the queue').toBeNull();
} finally {
// SELF-CLEAN: withdraw so no draft/pending row lingers and re-runs don't collide.
if (publishRequestId) {
await trpcMutation(authorRequest, 'appListings.withdrawExternalRequest', {
publishRequestId,
}).catch(() => {});
} else {
await withdrawPendingForSlug(authorRequest, modRequest, SLUG);
}
await modRequest.dispose();
}
});
});