122 lines
5.1 KiB
TypeScript
122 lines
5.1 KiB
TypeScript
/**
|
|
* The V1-2.6 drawing accessors through real supabase-js against the throwaway
|
|
* stack: register, list, upload, confirm, the confirmed hash registered again
|
|
* (nothing to upload), and a second editor meeting a pending row of the first
|
|
* (`upload.pending_other`, then `upload_missing` on its confirm). Bytes are
|
|
* unique per run, so it runs again on the same stack. Run through
|
|
* `pnpm db:sandbox test tests/sandbox/sheet-editor-assets.sandbox.ts`.
|
|
*/
|
|
import { randomUUID } from 'node:crypto'
|
|
import { createClient } from '@supabase/supabase-js'
|
|
import {
|
|
SheetFailure,
|
|
confirmSheetAssetUpload,
|
|
createOfficialSheet,
|
|
enrollTotp,
|
|
listSheetAssets,
|
|
registerSheetAsset,
|
|
uploadSheetDrawing,
|
|
verifyTotp,
|
|
type Database,
|
|
type FaunosteoClient,
|
|
} from '../../src'
|
|
import { PASSWORD, confirmedAccount, sql, stack, type Account } from './stack'
|
|
import { drawing, sha256, sheetDoc } from './sheet-fixtures'
|
|
import { totpCode } from './totp'
|
|
|
|
const BASE = 'http://sandbox.faunosteo.invalid'
|
|
|
|
/** The browser client of the app, its requests replayed on the sandbox ports. */
|
|
async function editorClient(account: Account): Promise<FaunosteoClient> {
|
|
const mapped: typeof fetch = (input, init) => {
|
|
const url = String(input instanceof Request ? input.url : input)
|
|
.replace(`${BASE}/auth/v1`, stack.authUrl)
|
|
.replace(`${BASE}/rest/v1`, stack.restUrl)
|
|
.replace(`${BASE}/storage/v1`, stack.storageUrl)
|
|
return fetch(url, init)
|
|
}
|
|
const client = createClient<Database>(BASE, stack.anonKey, {
|
|
auth: { persistSession: false, autoRefreshToken: false },
|
|
global: { fetch: mapped },
|
|
})
|
|
const { error } = await client.auth.signInWithPassword({ email: account.email, password: PASSWORD })
|
|
if (error) throw error
|
|
const enrolment = await enrollTotp(client, 'device')
|
|
await verifyTotp(client, enrolment.factorId, totpCode(enrolment.secret))
|
|
return client
|
|
}
|
|
|
|
const created: string[] = []
|
|
|
|
async function editor(tag: string): Promise<FaunosteoClient> {
|
|
const a = await confirmedAccount(tag)
|
|
created.push(a.id)
|
|
await sql(`select private.assign_platform_role(null, '${a.id}', 'admin')`)
|
|
await sql(`insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by)
|
|
values ('${a.id}', 'sheet_editor', 'sandbox', 'sandbox', '${a.id}')`)
|
|
return editorClient(a)
|
|
}
|
|
|
|
async function kindOf(promise: Promise<unknown>) {
|
|
try {
|
|
await promise
|
|
return 'no failure'
|
|
} catch (error) {
|
|
return error instanceof SheetFailure ? error.kind : `other: ${String(error)}`
|
|
}
|
|
}
|
|
|
|
afterAll(async () => {
|
|
if (created.length === 0) return
|
|
const ids = created.map((id) => `'${id}'`).join(', ')
|
|
await sql(`begin;
|
|
set local session_replication_role = replica;
|
|
delete from public.anatomical_catalog_grants where user_id in (${ids});
|
|
set local session_replication_role = origin;
|
|
delete from auth.users where id in (${ids});
|
|
commit;`)
|
|
})
|
|
|
|
describe('drawing accessors on real Storage and PostgREST', () => {
|
|
let first: FaunosteoClient
|
|
let second: FaunosteoClient
|
|
let sheetId: string
|
|
|
|
beforeAll(async () => {
|
|
first = await editor('assets-first')
|
|
second = await editor('assets-second')
|
|
sheetId = await createOfficialSheet(first, `sb-${randomUUID().slice(0, 12)}`, sheetDoc(null) as never)
|
|
})
|
|
|
|
const register = (client: FaunosteoClient, bytes: Buffer) =>
|
|
registerSheetAsset(client, { sheet_id: sheetId, sha256: sha256(bytes), byte_size: bytes.length, file_name: 'd.svg' })
|
|
|
|
it('registers, uploads, confirms, then finds the confirmed hash without uploading', async () => {
|
|
const bytes = drawing()
|
|
const id = await register(first, bytes)
|
|
expect(await listSheetAssets(first, sheetId)).toContainEqual({
|
|
id, sha256: sha256(bytes), byte_size: bytes.length, uploaded_at: null,
|
|
})
|
|
expect(await uploadSheetDrawing(first, sha256(bytes), new Uint8Array(bytes))).toBe('uploaded')
|
|
expect(await uploadSheetDrawing(first, sha256(bytes), new Uint8Array(bytes))).toBe('present')
|
|
expect(await confirmSheetAssetUpload(first, id)).toBe(id)
|
|
const row = (await listSheetAssets(first, sheetId)).find((r) => r.id === id)
|
|
expect(row?.uploaded_at).toEqual(expect.any(String))
|
|
|
|
expect(await register(first, bytes)).toBe(id)
|
|
expect((await listSheetAssets(first, sheetId)).find((r) => r.id === id)?.uploaded_at).toBe(row?.uploaded_at)
|
|
})
|
|
|
|
it('refuses a second editor on a pending row of the first, with its own statusCode', async () => {
|
|
const bytes = drawing()
|
|
const id = await register(first, bytes)
|
|
expect(await register(second, bytes)).toBe(id)
|
|
expect(await kindOf(uploadSheetDrawing(second, sha256(bytes), new Uint8Array(bytes))))
|
|
.toBe('upload.pending_other')
|
|
const refusal = await uploadSheetDrawing(second, sha256(bytes), new Uint8Array(bytes)).catch((e) => e)
|
|
const cause = refusal.cause as { status?: number; statusCode?: string }
|
|
expect({ status: cause.status, statusCode: cause.statusCode }).toEqual({ status: 400, statusCode: '403' })
|
|
expect(await kindOf(confirmSheetAssetUpload(second, id))).toBe('upload_missing')
|
|
})
|
|
})
|