| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { existsSync, mkdtempSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { promisify } |
| 6 | import type { INestApplication |
| 7 | import request from 'supertest |
| 8 | import { afterAll, beforeAll |
| 9 | |
| 10 | import { WalStoreService } |
| 11 | import { S3Service } |
| 12 | import { hasBackends, signUp |
| 13 | |
| 14 | describe.skipIf(!hasBackends |
| 15 | let app: |
| 16 | let origin: |
| 17 | let owner: |
| 18 | let stranger: |
| 19 | let work: |
| 20 | const username = |
| 21 | let repo: |
| 22 | |
| 23 | const api = |
| 24 | const at = |
| 25 | const git = |
| 26 | execFileSync('git', |
| 27 | // The server runs in this process, so anything that talks to it must not block the event loop. |
| 28 | const remote = |
| 29 | promisify(execFile)('git |
| 30 | cwd, |
| 31 | env: { ...process |
| 32 | }); |
| 33 | const remoteUrl = |
| 34 | `${origin.replace( |
| 35 | const update = |
| 36 | api().patch(at |
| 37 | const commit = |
| 38 | git( |
| 39 | work, |
| 40 | '-c', |
| 41 | 'user.name=E2E', |
| 42 | '-c', |
| 43 | 'user.email=e2e@example.com', |
| 44 | 'commit', |
| 45 | '-q', |
| 46 | '--allow-empty', |
| 47 | '-m', |
| 48 | message, |
| 49 | ); |
| 50 | const repositoryId = |
| 51 | (await api(). |
| 52 | .id as string |
| 53 | const storedKeys = |
| 54 | const s3 = |
| 55 | const listed = |
| 56 | Bucket: s3.bucket |
| 57 | Prefix: `repos/${id |
| 58 | }); |
| 59 | return (listed.Contents |
| 60 | }; |
| 61 | const cacheOf = |
| 62 | |
| 63 | beforeAll(async () |
| 64 | ({ app, origin |
| 65 | owner = await |
| 66 | stranger = await |
| 67 | |
| 68 | const created = |
| 69 | .post('/api/repositories') |
| 70 | .set('cookie', |
| 71 | .send({ name: |
| 72 | .expect(201); |
| 73 | repo = created. |
| 74 | |
| 75 | work = mkdtempSync( |
| 76 | git(work, ' |
| 77 | commit('first'); |
| 78 | git(work, ' |
| 79 | await remote(work |
| 80 | }, 120_000); |
| 81 | |
| 82 | afterAll(async () |
| 83 | await app?.close |
| 84 | if (work) rmSync |
| 85 | }); |
| 86 | |
| 87 | it('turns away everyone but the owner', |
| 88 | await update({ |
| 89 | await api().delete |
| 90 | }); |
| 91 | |
| 92 | it('refuses a default branch the repository does not have', |
| 93 | await update({ |
| 94 | }); |
| 95 | |
| 96 | it('serves and clones the chosen default branch', |
| 97 | await update({ |
| 98 | |
| 99 | const branches = |
| 100 | expect(branches.body |
| 101 | |
| 102 | const clone = |
| 103 | await remote(work |
| 104 | expect(git(clone |
| 105 | }); |
| 106 | |
| 107 | it('updates description and visibility without touching the slug', |
| 108 | await update({ |
| 109 | .expect(200) |
| 110 | .expect(({ body |
| 111 | |
| 112 | const { body |
| 113 | .get(at()) |
| 114 | .set('cookie', |
| 115 | .expect(200); |
| 116 | expect(body).toMatchObject |
| 117 | description: 'settled', |
| 118 | visibility: 'private', |
| 119 | }); |
| 120 | await api().get |
| 121 | }); |
| 122 | |
| 123 | it('keeps the slug when renamed to the same name', |
| 124 | await update({ |
| 125 | .expect(200) |
| 126 | .expect(({ body |
| 127 | |
| 128 | // A clash on creation gave this one a suffixed slug; saving it unchanged must not hand it another. |
| 129 | const { body: |
| 130 | .post('/api/repositories') |
| 131 | .set('cookie', |
| 132 | .send({ name: |
| 133 | .expect(201); |
| 134 | expect(twin.slug |
| 135 | await api() |
| 136 | .patch(at(twin |
| 137 | .set('cookie', |
| 138 | .send({ name: |
| 139 | .expect(200) |
| 140 | .expect(({ body |
| 141 | }); |
| 142 | |
| 143 | it('moves the repository to a new slug when renamed, and redirects the old one', |
| 144 | const { body |
| 145 | expect(body.slug |
| 146 | |
| 147 | const followed = |
| 148 | .get(at()) |
| 149 | .set('cookie', |
| 150 | .expect(200); |
| 151 | expect(followed.body |
| 152 | repo = body. |
| 153 | await api().get |
| 154 | }); |
| 155 | |
| 156 | it('deletes the repository and everything it stored', |
| 157 | const id = |
| 158 | expect((await storedKeys |
| 159 | expect(existsSync(cacheOf |
| 160 | |
| 161 | await api().delete |
| 162 | |
| 163 | await api().get |
| 164 | // Only the tombstone is left, and it holds no refs and no objects. |
| 165 | expect(await storedKeys |
| 166 | expect(existsSync(cacheOf |
| 167 | }); |
| 168 | |
| 169 | it('turns away a push once the delete has begun, and finishes on retry', |
| 170 | const created = |
| 171 | .post('/api/repositories') |
| 172 | .set('cookie', |
| 173 | .send({ name: |
| 174 | .expect(201); |
| 175 | repo = created. |
| 176 | await remote(work |
| 177 | const id = |
| 178 | const before = |
| 179 | |
| 180 | // A delete that failed after its first step. |
| 181 | await app.get |
| 182 | |
| 183 | commit('too late'); |
| 184 | await expect( |
| 185 | remote(work, ' |
| 186 | ).rejects.toThrow() |
| 187 | expect(await storedKeys |
| 188 | |
| 189 | await api().delete |
| 190 | expect(await storedKeys |
| 191 | }); |
| 192 | }); |