test(supabase): avatar suite on the sandbox, per-run sandbox passwords
pnpm db:sandbox avatar-e2e runs tests/e2e/avatar-flow.e2e.ts with the real worker (default mode) on the throwaway stack; the suite now requires its database container by name, makes both accounts per run and expedites only its own purges. Throwaway sandbox accounts get random passwords per run.
This commit is contained in:
1 parent
96b19e5a87
commit
2a84e3702e
8 files changed
+139
-79
No files matched your search
@@ -1,14 +1,17 @@
|
||||
/**
|
||||
* Avatar flow against the real local stack and the worker container
|
||||
* Avatar flow against a real stack and the worker container
|
||||
* (docs/development/STORAGE.md §4, §3; STEP8_FILES_ANALYSIS IV.2 "integration"
|
||||
* and "real-stack e2e"). Run through `pnpm e2e:avatar` (scripts/avatar-e2e.sh),
|
||||
* which starts the worker with a 5 s sweep and a 0 s quarantine grace.
|
||||
* and "real-stack e2e"). Run through `pnpm db:sandbox avatar-e2e` (the throwaway
|
||||
* stack) or `pnpm e2e:avatar` (scripts/avatar-e2e.sh, the local DEV stack), both
|
||||
* starting the worker with a 5 s sweep and a 0 s quarantine grace.
|
||||
*
|
||||
* Environment: SUPABASE_URL, SUPABASE_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY
|
||||
* (the service key is used for inspection only: listing objects and shifting
|
||||
* the purge clock through psql in the database container).
|
||||
* the purge clock through psql) and AVATAR_E2E_DB_CONTAINER, the database
|
||||
* container of that same stack, named by the caller: no default, so a run never
|
||||
* reaches another stack's database. Both accounts are made per run.
|
||||
*/
|
||||
import { createHash } from 'node:crypto'
|
||||
import { createHash, randomBytes } from 'node:crypto'
|
||||
import { execFileSync } from 'node:child_process'
|
||||
import { deflateSync } from 'node:zlib'
|
||||
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
|
||||
@@ -26,9 +29,9 @@ import {
|
||||
const URL_ = process.env.SUPABASE_URL
|
||||
const ANON = process.env.SUPABASE_ANON_KEY
|
||||
const SERVICE = process.env.SUPABASE_SERVICE_ROLE_KEY
|
||||
const DB_CONTAINER = process.env.AVATAR_E2E_DB_CONTAINER ?? 'supabase_db_faunosteo'
|
||||
const PASSWORD = 'LocalDev-only-password-1'
|
||||
const BOB = 'bob@faunosteo.local'
|
||||
const DB_CONTAINER = process.env.AVATAR_E2E_DB_CONTAINER
|
||||
/** Per run, kept in memory only: both accounts are deleted after the suite. */
|
||||
const PASSWORD = randomBytes(18).toString('base64url')
|
||||
const WORKER_WAIT_MS = 60_000
|
||||
|
||||
type Client = SupabaseClient<Database>
|
||||
@@ -166,19 +169,24 @@ async function objectExists(service: Client, bucket: string, key: string): Promi
|
||||
|
||||
function psql(sql: string): string {
|
||||
return execFileSync('docker', [
|
||||
'exec', DB_CONTAINER, 'psql', '-U', 'postgres', '-d', 'postgres', '-Atc', sql,
|
||||
'exec', DB_CONTAINER ?? '', 'psql', '-U', 'postgres', '-d', 'postgres', '-Atc', sql,
|
||||
]).toString().trim()
|
||||
}
|
||||
|
||||
/** Moves every due date of the purge queue to now (the grace is 48 h / 1 h in production). */
|
||||
function expeditePurges(): void {
|
||||
psql('update private.object_purge_queue set not_before = now() where not_before > now()')
|
||||
function accountPattern(userId: string): string {
|
||||
if (!/^[0-9a-f-]{36}$/.test(userId)) throw new Error('userId is not a uuid')
|
||||
return `'%/${userId}/%'`
|
||||
}
|
||||
|
||||
/** Moves an account's due dates of the purge queue to now (the grace is 48 h / 1 h in production). */
|
||||
function expeditePurges(userId: string): void {
|
||||
psql(`update private.object_purge_queue set not_before = now()
|
||||
where not_before > now() and object_name like ${accountPattern(userId)}`)
|
||||
}
|
||||
|
||||
/** Purge entries still queued for an account (served entries are acked, hence deleted). */
|
||||
function queuedPurges(userId: string): number {
|
||||
if (!/^[0-9a-f-]{36}$/.test(userId)) throw new Error('userId is not a uuid')
|
||||
return Number(psql(`select count(*) from private.object_purge_queue where object_name like '%/${userId}/%'`))
|
||||
return Number(psql(`select count(*) from private.object_purge_queue where object_name like ${accountPattern(userId)}`))
|
||||
}
|
||||
|
||||
async function downloadFails(client: Client, key: string): Promise<boolean> {
|
||||
@@ -192,31 +200,41 @@ function sha256(bytes: Uint8Array): string {
|
||||
|
||||
// --- suite ------------------------------------------------------------------
|
||||
|
||||
const configured = Boolean(URL_ && ANON && SERVICE)
|
||||
const configured = Boolean(URL_ && ANON && SERVICE && DB_CONTAINER)
|
||||
|
||||
/** A confirmed throwaway account; resolves with its id. */
|
||||
async function newAccount(service: Client, tag: string, name: string): Promise<{ id: string; email: string }> {
|
||||
const email = `e2e-${tag}-${Date.now()}@faunosteo.local`
|
||||
const created = await service.auth.admin.createUser({
|
||||
email,
|
||||
password: PASSWORD,
|
||||
email_confirm: true,
|
||||
user_metadata: { display_name: name, locale: 'fr' },
|
||||
})
|
||||
if (created.error || !created.data.user) throw new Error(`createUser: ${created.error?.message}`)
|
||||
return { id: created.data.user.id, email }
|
||||
}
|
||||
|
||||
describe.skipIf(!configured)('avatar flow on the real stack', () => {
|
||||
const service = serviceClient()
|
||||
let alice: Client
|
||||
let aliceId: string
|
||||
let bob: Client
|
||||
let bobId: string
|
||||
|
||||
beforeAll(async () => {
|
||||
// A fresh account per run keeps the seeded accounts clean and the per-hour rate out of reach.
|
||||
const email = `e2e-avatar-${Date.now()}@faunosteo.local`
|
||||
const created = await service.auth.admin.createUser({
|
||||
email,
|
||||
password: PASSWORD,
|
||||
email_confirm: true,
|
||||
user_metadata: { display_name: 'E2E Avatar', locale: 'fr' },
|
||||
})
|
||||
if (created.error || !created.data.user) throw new Error(`createUser: ${created.error?.message}`)
|
||||
aliceId = created.data.user.id
|
||||
alice = await signedIn(email)
|
||||
bob = await signedIn(BOB)
|
||||
// Fresh accounts per run keep the seeded accounts out of the suite and the per-hour rate out of reach.
|
||||
const a = await newAccount(service, 'avatar', 'E2E Avatar')
|
||||
aliceId = a.id
|
||||
alice = await signedIn(a.email)
|
||||
const b = await newAccount(service, 'other', 'E2E Other')
|
||||
bobId = b.id
|
||||
bob = await signedIn(b.email)
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
if (aliceId) await service.auth.admin.deleteUser(aliceId)
|
||||
if (bobId) await service.auth.admin.deleteUser(bobId)
|
||||
})
|
||||
|
||||
it('cleans a valid upload, and only the owner can download the result', async () => {
|
||||
@@ -260,7 +278,7 @@ describe.skipIf(!configured)('avatar flow on the real stack', () => {
|
||||
expect(state.clean?.id).toBe(row.id)
|
||||
expect(await downloadFails(alice, previous!.object_key)).toBe(true)
|
||||
expect(await objectExists(service, 'avatars', previous!.object_key)).toBe(true)
|
||||
expeditePurges()
|
||||
expeditePurges(aliceId)
|
||||
await waitFor(
|
||||
async () => ((await objectExists(service, 'avatars', previous!.object_key)) ? null : true),
|
||||
30_000,
|
||||
@@ -316,7 +334,7 @@ describe.skipIf(!configured)('avatar flow on the real stack', () => {
|
||||
expect(await downloadFails(alice, row.object_key)).toBe(true)
|
||||
const state = await fetchOwnAvatarState(alice, aliceId)
|
||||
expect(state.clean).toBeNull()
|
||||
expeditePurges()
|
||||
expeditePurges(aliceId)
|
||||
await waitFor(
|
||||
async () => {
|
||||
const listed = await service.storage.from('avatars').list(`profiles/${aliceId}`, { limit: 100 })
|
||||
|
||||
Reference in new issue
Block a user