| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { randomBytes } |
| 4 | import { tmpdir } |
| 5 | import path from 'node:path |
| 6 | import { promisify } |
| 7 | import type { INestApplication |
| 8 | import request from 'supertest |
| 9 | import { afterAll, beforeAll |
| 10 | |
| 11 | import { type Database, |
| 12 | |
| 13 | import { DATABASE } |
| 14 | import { hasBackends, signUp |
| 15 | |
| 16 | describe.skipIf(!hasBackends |
| 17 | let app: |
| 18 | let origin: |
| 19 | let owner: |
| 20 | let forker: |
| 21 | let work: |
| 22 | const stamp = |
| 23 | const ownerName = |
| 24 | const forkerName = |
| 25 | let repo: |
| 26 | |
| 27 | const api = |
| 28 | const remote = |
| 29 | `${origin.replace( |
| 30 | const git = |
| 31 | execFileSync( |
| 32 | 'git', |
| 33 | ['-c', 'user.name=E2E' |
| 34 | { cwd: work, |
| 35 | ).trim(); |
| 36 | // The server runs in this process, so anything that talks to it must not block the event loop. |
| 37 | const run = |
| 38 | promisify(execFile)('git |
| 39 | cwd: work, |
| 40 | env: { ...process |
| 41 | }); |
| 42 | const usage = |
| 43 | api() |
| 44 | .get(`/api/storage/${account |
| 45 | .set('cookie', |
| 46 | .expect(200) |
| 47 | .then((response) |
| 48 | const commitRandom = |
| 49 | // Random bytes do not compress, so the pack is about as large as the file. |
| 50 | writeFileSync(path.join |
| 51 | git('add', |
| 52 | git('commit', |
| 53 | }; |
| 54 | |
| 55 | beforeAll(async () |
| 56 | ({ app, origin |
| 57 | STORAGE_QUOTA_BYTES: '64kb', |
| 58 | FORK_STORAGE_QUOTA_BYTES: '', |
| 59 | })); |
| 60 | owner = await |
| 61 | forker = await |
| 62 | |
| 63 | repo = ( |
| 64 | await api() |
| 65 | .post('/api/repositories') |
| 66 | .set('cookie', |
| 67 | .send({ name: |
| 68 | .expect(201) |
| 69 | ).body.slug; |
| 70 | work = mkdtempSync( |
| 71 | git('init', |
| 72 | commitRandom('small.bin', |
| 73 | await run('push |
| 74 | }, 120_000); |
| 75 | |
| 76 | afterAll(async () |
| 77 | await app?.close |
| 78 | if (work) rmSync |
| 79 | }); |
| 80 | |
| 81 | it('bills a push against the repository quota', |
| 82 | const { usedBytes, |
| 83 | expect(usedBytes).toBeGreaterThan |
| 84 | expect(quotaBytes).toBe |
| 85 | }); |
| 86 | |
| 87 | it('refuses a push that would take the account past its quota, and bills nothing for it', |
| 88 | const before = |
| 89 | commitRandom('large.bin', |
| 90 | |
| 91 | await expect( |
| 92 | run('push', |
| 93 | ).rejects.toThrow( |
| 94 | /\[remote rejected\] main -> main \( |
| 95 | ); |
| 96 | git('reset', |
| 97 | expect((await usage |
| 98 | before.usedBytes, |
| 99 | ); |
| 100 | }); |
| 101 | |
| 102 | it('lets an account past its quota still delete a branch', |
| 103 | const target = |
| 104 | await run('push |
| 105 | await app |
| 106 | .get<Database |
| 107 | .insert(schema.storageLimit |
| 108 | .values({ userId: |
| 109 | |
| 110 | await run('push |
| 111 | }); |
| 112 | |
| 113 | it('bills a fork against the fork quota, which an account override can set', |
| 114 | await app |
| 115 | .get<Database |
| 116 | .insert(schema.storageLimit |
| 117 | .values({ userId: |
| 118 | |
| 119 | const { body |
| 120 | .post(`/api/repositories/${ownerName |
| 121 | .set('cookie', |
| 122 | .send({ name: |
| 123 | .expect(413); |
| 124 | expect(body.message |
| 125 | |
| 126 | await app |
| 127 | .get<Database |
| 128 | .update(schema.storageLimit |
| 129 | .set({ forkBytes: |
| 130 | await api() |
| 131 | .post(`/api/repositories/${ownerName |
| 132 | .set('cookie', |
| 133 | .send({ name: |
| 134 | .expect(201); |
| 135 | |
| 136 | const { usedBytes, |
| 137 | expect(usedBytes).toBe |
| 138 | expect(fork.usedBytes |
| 139 | expect(fork.quotaBytes |
| 140 | }); |
| 141 | }); |