also looking at this
fix(tests): a fresh database has no user 1 and no hook secret
#4
2 files
+24
-3
Review threads live on the whole diff, not on one commit, so none are shown here - a thread's line means something in the branch's final form, and painting it into an intermediate step would put it on code it is not about.
| @@ -18,6 +18,10 @@ let available = false | ||
| 18 | 18 | let db: any = null |
| 19 | 19 | let sweepPolymorphic: (id: number) => Promise<{ ok: boolean, removed: Array<{ table: string, rows: number }>, error?: string }> |
| 20 | 20 | const made: number[] = [] |
| 21 | // The user everything here is owned by. Created per run rather than assumed: | |
| 22 | // `reactions.user_id` and `notification_subscriptions.user_id` are real foreign | |
| 23 | // keys, and a freshly migrated database has no user 1 to point them at. | |
| 24 | let userId = 0 | |
| 21 | 25 | |
| 22 | 26 | beforeAll(async () => { |
| 23 | 27 | try { |
| @@ -27,6 +31,12 @@ beforeAll(async () => { | ||
| 27 | 31 | db = (globalThis as any).db |
| 28 | 32 | await db.selectFrom('repositories').select(['id']).limit(1).execute() |
| 29 | 33 | |
| 34 | const handle = `del${Math.floor(Math.random() * 1e9)}` | |
| 35 | const user = await db.insertInto('users') | |
| 36 | .values({ name: 'Delete Tester', email: `${handle}@example.com`, handle, password: 'x' }) | |
| 37 | .returning(['id']).executeTakeFirst() | |
| 38 | userId = Number(user.id) | |
| 39 | ||
| 30 | 40 | ;({ sweepPolymorphic } = await import('../../app/Actions/Repo/polymorphic')) |
| 31 | 41 | available = true |
| 32 | 42 | } |
| @@ -41,6 +51,9 @@ afterAll(async () => { | ||
| 41 | 51 | |
| 42 | 52 | for (const id of made) |
| 43 | 53 | await db.deleteFrom('repositories').where('id', '=', id).execute() |
| 54 | ||
| 55 | if (userId) | |
| 56 | await db.deleteFrom('users').where('id', '=', userId).execute() | |
| 44 | 57 | }) |
| 45 | 58 | |
| 46 | 59 | /** A repository with one of everything that hangs off it, polymorphic or not. */ |
| @@ -50,7 +63,7 @@ async function seedRepository(): Promise<{ id: number, issueId: number, commentI | ||
| 50 | 63 | const repository = await db.insertInto('repositories') |
| 51 | 64 | .values({ |
| 52 | 65 | owner_type: 'user', |
| 53 | owner_id: 1, | |
| 66 | owner_id: userId, | |
| 54 | 67 | name, |
| 55 | 68 | description: '', |
| 56 | 69 | visibility: 'public', |
| @@ -73,11 +86,11 @@ async function seedRepository(): Promise<{ id: number, issueId: number, commentI | ||
| 73 | 86 | const commentId = Number(comment.id) |
| 74 | 87 | |
| 75 | 88 | await db.insertInto('reactions') |
| 76 | .values({ subject_type: 'issue_comment', subject_id: commentId, content: '+1', user_id: 1 }).execute() | |
| 89 | .values({ subject_type: 'issue_comment', subject_id: commentId, content: '+1', user_id: userId }).execute() | |
| 77 | 90 | await db.insertInto('timeline_entries') |
| 78 | 91 | .values({ subject_type: 'issue', subject_id: issueId, kind: 'closed' }).execute() |
| 79 | 92 | await db.insertInto('notification_subscriptions') |
| 80 | .values({ subject_type: 'repository', subject_id: id, user_id: 1, reason: 'watching' }).execute() | |
| 93 | .values({ subject_type: 'repository', subject_id: id, user_id: userId, reason: 'watching' }).execute() | |
| 81 | 94 | await db.insertInto('repository_labels') |
| 82 | 95 | .values({ repository_id: id, name: 'bug', color: '#f00', description: '', is_default: true }).execute() |
| 83 | 96 | |
| @@ -10,6 +10,14 @@ | ||
| 10 | 10 | if (!Bun.env.STRIPE_SECRET_KEY) |
| 11 | 11 | Bun.env.STRIPE_SECRET_KEY = 'sk_test_fake_key_for_testing' |
| 12 | 12 | |
| 13 | // A run-local hook secret, when the checkout has not configured one. An empty | |
| 14 | // secret deliberately disables the post-receive endpoint (a default secret is | |
| 15 | // a published secret), which on a fresh `.env` makes git-http's push-pipeline | |
| 16 | // test fail for configuration rather than for code. Random per run, so it is | |
| 17 | // never a value anything can come to depend on. | |
| 18 | if (!Bun.env.GIT_HOOK_SECRET || Bun.env.GIT_HOOK_SECRET.trim().length < 16) | |
| 19 | Bun.env.GIT_HOOK_SECRET = Buffer.from(crypto.getRandomValues(new Uint8Array(24))).toString('hex') | |
| 20 | ||
| 13 | 21 | import { applyRuntimeDirectoryEnv } from '@stacksjs/path' |
| 14 | 22 | import { setupTestEnvironment } from '@stacksjs/testing' |
| 15 | 23 | |