import { Kysely, sql } from 'kysely' import { describe, expect, it, vi } from 'vitest' import { f, resource } from '@protobase/schema' import { createAuth } from '../src/better-auth/create-auth' import { addMember, createOrganization } from '../src/better-auth/organizations/host' import { createUser, setUserBanned } from '../src/better-auth/users' import { betterAuthOwnerRoles } from '../src/webhooks/table/owner-roles' import { moduleDifference } from '../src/schema-modules/module-difference' import { webhooksSchemaSql } from '../src/schema-modules/feature-schemas-sql' import { webhookDeliveryLogModule } from '../src/webhooks/table/delivery-log-module' import { webhooksSchemaModule } from '../src/webhooks/table/webhooks-schema-module' import { webhooksTargets } from '../src/webhooks/table/webhooks-targets' import { tableSubscriptions } from '../src/webhooks/table/table-subscriptions' import { subscriptionUrlCheck, subscriptionWriteCheck } from '../src/webhooks/table/validate-hook' import { checkWebhookUrl } from '../src/webhooks/table/address-check' import { createAdmin } from '../src/create-admin' import { configExports } from '../src/resource-source' import { webhooksModule } from '../src/webhooks/table/webhooks-module' import type { WriteEvent } from '../src/types' import { openSnapshotDb, settled, type SnapshotDb } from '../../../test-support/sql-snapshot' import { as, testAuthenticator } from '../../../test-support/server' // Real scrypt hashing runs on this thread, CPU work a busy machine stretches past 5 s. vi.setConfig({ testTimeout: 30_000 }) const orders = resource('orders') .table('orders') .fields({ id: f.integer(), total: f.decimal({ precision: 12, scale: 2 }), iban: f.text().optional().sensitive() }) .primaryKey((r) => r.id) .events((e) => e.crud()) .toModel() describe('the webhooks schema', () => { const targets = webhooksTargets({ deliveryLog: true }) it('is read once by the baseline at its last step, its functions at their version, and the delivery log as a module of its own', async () => { const pg = await openSnapshotDb('jobs') const db = new Kysely({ dialect: pg.dialect }) expect(await webhooksSchemaModule.read(db, targets.webhooks)).toBeUndefined() await pg.exec(webhooksSchemaSql()) expect(await webhooksSchemaModule.read(db, targets.webhooks)).toEqual({ lastStep: 1, replaceables: { functions: 2 }, names: { schema: 'webhooks', changes: 'changes' } }) expect(await webhookDeliveryLogModule.read(db, targets.webhook_delivery_log!)).toBeUndefined() const log = moduleDifference(webhookDeliveryLogModule, targets.webhook_delivery_log!, undefined).sql! expect(log).toContain('create table if not exists "webhooks"."delivery_log"') expect(log).not.toContain('create table if not exists "webhooks"."subscriptions"') await pg.exec(log) expect(await webhookDeliveryLogModule.read(db, targets.webhook_delivery_log!)).toEqual({ lastStep: 1, replaceables: {}, names: { schema: 'webhooks' } }) await db.destroy() }) // The webhooks schema as version 1 wrote it, with only the tenant's index, and one index an app added by hand. const earlierSchema = ` drop index webhooks.subscriptions_owner, webhooks.subscriptions_created_at, webhooks.subscriptions_name, webhooks.subscriptions_enabled, webhooks.subscriptions_last_success_at, webhooks.subscriptions_name_search, webhooks.subscriptions_url_search; comment on function webhooks.subscription_commands() is 'Protobase webhooks 1'; comment on function webhooks.deliveries_resend() is 'Protobase webhooks 1'; create index subscriptions_name on webhooks.subscriptions (name); ` const listReads = ['order_by=created_at desc', 'order_by=name asc', 'order_by=name desc', 'order_by=last_success_at asc', 'order_by=last_success_at desc', 'filter=enabled = true', 'filter=search("x")'] it("serves every read of the subscriptions list on its step, and refuses to read a schema of version 1 without their indexes, which no step records", async () => { const pg = await openSnapshotDb('jobs') const db = new Kysely({ dialect: pg.dialect }) const { resources, views } = configExports(webhooksModule()) const statuses = async () => { const admin = createAdmin({ resources, views, db, authenticate: testAuthenticator }) const answers = [] for (const read of listReads) answers.push((await admin.request(`/api/v1/webhook_subscriptions?${encodeURI(read)}`, { headers: as(1) })).status) return answers } await pg.exec(webhooksSchemaSql()) expect(await statuses()).toEqual([200, 200, 200, 200, 200, 200, 200]) await pg.exec(earlierSchema) await expect(webhooksSchemaModule.read(db, targets.webhooks)).rejects.toThrow(/^The subscriptions table in schema "webhooks" lacks indexes no step of this version of Protobase records$/) await db.destroy() }) it('writes the functions of a schema at version 1 with its indexes again, which the code before still runs on', async () => { const pg = await openSnapshotDb('jobs') const db = new Kysely({ dialect: pg.dialect }) await pg.exec(webhooksSchemaSql()) await pg.exec(`comment on function webhooks.subscription_commands() is 'Protobase webhooks 1'`) const found = await webhooksSchemaModule.read(db, targets.webhooks) expect(found!.replaceables).toEqual({ functions: 1 }) const difference = moduleDifference(webhooksSchemaModule, targets.webhooks, found) expect(difference.sql).toMatch(/^-- The subscriptions' commands and the deliveries view\.\ncreate or replace function "webhooks"\."subscription_commands"\(\)/) expect(difference.breaks).toEqual([]) await pg.exec(difference.sql!) expect((await webhooksSchemaModule.read(db, targets.webhooks))!.replaceables).toEqual({ functions: 2 }) await db.destroy() }) it('keeps the outcome of each try on the row, disables a subscription after three days of failures, and logs every try', async () => { const pg = await openSnapshotDb('jobs') const db = new Kysely({ dialect: pg.dialect }) await pg.exec(webhooksSchemaSql({ deliveryLog: true })) // A secret of its own rather than the random default, so the recording stays the same. const { rows } = await sql<{ id: string }>`insert into webhooks.subscriptions (name, events, url, tenant_id, owner_id, secret) values ('a', 'orders.created', 'https://x.test', '1', 'u1', 'whsec_dGVzdA==') returning id::text`.execute(db) const id = rows[0]!.id const table = tableSubscriptions({ schema: 'webhooks', ownerRoles: async () => ['sales'], deliveryLog: true }) expect((await table.load(db))[0]).toMatchObject({ name: `ui:${id}`, options: { events: ['orders.created'], tenant: '1', as: { roles: ['sales'] }, url: 'https://x.test' } }) const event = { id: 'evt_1_created', type: 'orders.created', timestamp: '2026-10-11T00:00:00Z', data: {} as never } await table.record(db, id, event, 1, { ok: false, error: '503', status: 503, final: false }, 12) await sql`update webhooks.subscriptions set failing_since = now() - interval '4 days'`.execute(db) await table.record(db, id, event, 2, { ok: false, error: '503', status: 503, final: false }, 15) expect((await sql`select enabled, disabled_reason from webhooks.subscriptions`.execute(db)).rows).toEqual([{ enabled: false, disabled_reason: 'No delivery succeeded for 3 days' }]) expect(await table.load(db)).toEqual([]) await sql`update webhooks.subscriptions set enabled = true`.execute(db) expect((await sql`select failing_since, disabled_reason from webhooks.subscriptions`.execute(db)).rows).toEqual([{ failing_since: null, disabled_reason: null }]) await table.record(db, id, event, 3, { ok: true, status: 200 }, 9) const log = (await sql`select status, attempt, status_code, tenant_id from webhooks.delivery_log order by id`.execute(db)).rows expect(log).toEqual([ { status: 'failed', attempt: 1, status_code: 503, tenant_id: '1' }, { status: 'failed', attempt: 2, status_code: 503, tenant_id: '1' }, { status: 'delivered', attempt: 3, status_code: 200, tenant_id: '1' }, ]) await db.destroy() }) }) describe('subscriptionWriteCheck', () => { const write = (after: Record) => ({ operation: 'create', resource: { name: 'webhook_subscriptions' }, user: { id: 'u1', roles: [] }, after }) as unknown as WriteEvent const check = subscriptionWriteCheck([orders]) it('passes a subscription to declared events at a public address', async () => { await check(write({ events: 'orders.created orders.*', url: 'https://93.184.216.34/hook', filter: 'total > 10', headers: { 'x-key': 'k' } }), undefined as never) }) it('refuses unknown events, a filter on a sensitive field and headers that are not text', async () => { await expect(check(write({ events: 'orders.shipped', url: 'https://93.184.216.34/hook' }), undefined as never)).rejects.toThrow('"orders.shipped" matches no declared event') await expect(check(write({ events: 'orders.created', url: 'https://93.184.216.34/hook', filter: 'iban:*' }), undefined as never)).rejects.toThrow('when uses sensitive field "iban"') await expect(check(write({ events: 'orders.created', url: 'https://93.184.216.34/hook', headers: { n: 1 } }), undefined as never)).rejects.toThrow('headers must be an object of text values') await check({ ...write({}), resource: { name: 'orders' } } as unknown as WriteEvent, undefined as never) }) }) describe('subscriptionUrlCheck', () => { const check = subscriptionUrlCheck(checkWebhookUrl) it('refuses a private address before the write, and leaves other bodies and resources alone', async () => { await expect(check({ resource: 'webhook_subscriptions', operation: 'create', body: { url: 'http://169.254.169.254/latest' } })).rejects.toThrow( 'The webhook subscription cannot be saved: 169.254.169.254 is a private address', ) await check({ resource: 'webhook_subscriptions', operation: 'update', body: { url: 'https://93.184.216.34/hook' } }) await check({ resource: 'webhook_subscriptions', operation: 'update', body: { name: 'renamed' } }) await check({ resource: 'webhook_subscriptions', operation: 'create', body: { url: 'not a url' } }) await check({ resource: 'orders', operation: 'create', body: { url: 'http://127.0.0.1/' } }) }) }) describe('betterAuthOwnerRoles', () => { it('reads the role someone acts with now, of their global ones and app roles in the organization they are a member of', async () => { const store = await (await openSnapshotDb('auth')).clone() // Each instance's schema check starts once its context is made; let it finish before the next queries. const make = async () => { const auth = createAuth({ database: { dialect: store.dialect, type: 'postgres' }, baseURL: 'http://localhost', secret: 'test-secret-test-secret-test-secret-1234', mailer: false, roles: { sales: 'Sales', support: 'Support' }, organizations: { create: 'everyone' }, }) await auth.$context await settled() return auth } const auth = await make() const password = 'correct horse battery' const root = await createUser(auth, { email: 'root@example.com', password }) const bo = await createUser(auth, { email: 'bo@example.com', password }) const ana = await createUser(auth, { email: 'ana@example.com', password, role: 'support' }) const { organization } = await createOrganization(auth, { name: 'Acme', slug: 'acme', owner: 'root@example.com' }) await addMember(auth, { organization: 'acme', email: 'bo@example.com', appRoles: ['sales'] }) const roles = betterAuthOwnerRoles(auth) expect(await roles({ id: bo.id, tenant: organization.id })).toEqual(['sales']) expect(await roles({ id: bo.id, tenant: 'elsewhere' })).toEqual([]) expect(await roles({ id: ana.id, tenant: organization.id })).toEqual(['support']) expect((await roles({ id: root.id, tenant: organization.id })).length).toBeGreaterThan(0) await setUserBanned(auth, { email: 'bo@example.com', banned: true }) expect(await roles({ id: bo.id, tenant: organization.id })).toEqual([]) expect(await roles({ id: 'gone' })).toEqual([]) }) })