| 1 | import { execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { createDatabase, type |
| 6 | import { inArray } |
| 7 | import { migrate } |
| 8 | import { |
| 9 | afterAll, |
| 10 | afterEach, |
| 11 | beforeAll, |
| 12 | beforeEach, |
| 13 | describe, |
| 14 | expect, |
| 15 | it, |
| 16 | } from 'vitest'; |
| 17 | |
| 18 | import { Readable } |
| 19 | import { buffer } |
| 20 | import { ConfigService } |
| 21 | import { InMemoryWalStore } |
| 22 | import { InMemoryS3 } |
| 23 | import { StorageQuotaExceededError } |
| 24 | import type { S3Service |
| 25 | import { StorageQuotaService } |
| 26 | import { StorageService } |
| 27 | import { UserNotFoundError } |
| 28 | import { bufferBody } |
| 29 | import { ZERO_OID } |
| 30 | import { BranchesService } |
| 31 | import { RepositoryMaterializerService } |
| 32 | import { RepositoryAccessService } |
| 33 | import type { RepositoryStorageService |
| 34 | import { PushTransactionService } |
| 35 | import { UsersService } |
| 36 | import { listTags } |
| 37 | import type { WalStoreService |
| 38 | import { InvalidCursorError } |
| 39 | import { ReleaseAssetsService } |
| 40 | import { |
| 41 | ContentLengthRequiredError, |
| 42 | InvalidAssetNameError, |
| 43 | ReleaseAssetExistsError, |
| 44 | ReleaseAssetNotFoundError, |
| 45 | ReleaseAssetTooLargeError, |
| 46 | UploadNotOctetStreamError, |
| 47 | InvalidTagNameError, |
| 48 | ReleaseAlreadyExistsError, |
| 49 | ReleaseNotFoundError, |
| 50 | TagTargetNotFoundError, |
| 51 | } from './releases.errors.js'; |
| 52 | import { ReleasesService } |
| 53 | |
| 54 | const CONNECTION = process |
| 55 | const MIGRATIONS = path |
| 56 | import.meta.dirname, |
| 57 | '../../../../../packages/db/drizzle', |
| 58 | ); |
| 59 | |
| 60 | const OWNER = ' |
| 61 | const READER = ' |
| 62 | |
| 63 | function git(cwd: |
| 64 | const env = |
| 65 | delete env.GIT_DIR; |
| 66 | return execFileSync('git |
| 67 | } |
| 68 | |
| 69 | // Needs a throwaway Postgres; the suite is skipped without one. |
| 70 | describe.skipIf(!CONNECTION |
| 71 | let db: |
| 72 | let pool: |
| 73 | let root: |
| 74 | let source: |
| 75 | let cache: |
| 76 | let releases: |
| 77 | let assets: |
| 78 | let s3: |
| 79 | let quota: |
| 80 | let pushes: |
| 81 | let repository: |
| 82 | let first: |
| 83 | let second: |
| 84 | |
| 85 | const owner = |
| 86 | const reader = |
| 87 | const anonymous = |
| 88 | |
| 89 | /** Rebuilds the quota and asset services as an instance configured with `env` would have them. */ |
| 90 | function limits(env |
| 91 | quota = new |
| 92 | assets = new |
| 93 | db, |
| 94 | new RepositoryAccessService(db |
| 95 | s3 as unknown |
| 96 | quota, |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | function upload( |
| 101 | releaseId: string, |
| 102 | name: string, |
| 103 | bytes: Buffer |
| 104 | as: { |
| 105 | ) { |
| 106 | return assets.upload |
| 107 | ...owner, |
| 108 | ...as, |
| 109 | releaseId, |
| 110 | name, |
| 111 | type: 'application/zip', |
| 112 | bodyType: 'application/octet-stream', |
| 113 | contentLength: String(bytes |
| 114 | body: Readable.from |
| 115 | }); |
| 116 | } |
| 117 | |
| 118 | /** Commits in the source repository and pushes the commit through the log. */ |
| 119 | async function commitAndLog |
| 120 | writeFileSync(path.join |
| 121 | git(source, ' |
| 122 | git(source, ' |
| 123 | const after = |
| 124 | |
| 125 | await pushes.commitPush |
| 126 | repoId: repository.id |
| 127 | transitions: [ |
| 128 | { |
| 129 | ref: 'refs/heads/main', |
| 130 | oldOid: before ? |
| 131 | newOid: Buffer.from |
| 132 | }, |
| 133 | ], |
| 134 | body: bufferBody( |
| 135 | execFileSync('git', |
| 136 | cwd: source, |
| 137 | input: before ? |
| 138 | }), |
| 139 | ), |
| 140 | packOffset: 0, |
| 141 | }); |
| 142 | return after; |
| 143 | } |
| 144 | |
| 145 | beforeAll(async () |
| 146 | ({ db, pool |
| 147 | await migrate(db |
| 148 | }); |
| 149 | |
| 150 | beforeEach(async () |
| 151 | root = mkdtempSync( |
| 152 | source = path. |
| 153 | cache = path. |
| 154 | execFileSync('git', |
| 155 | git(source, ' |
| 156 | git(source, ' |
| 157 | execFileSync('git', |
| 158 | |
| 159 | const store = |
| 160 | pushes = new |
| 161 | s3 = new |
| 162 | limits({}); |
| 163 | releases = new |
| 164 | db, |
| 165 | new RepositoryAccessService(db |
| 166 | { getRepoPath: async |
| 167 | new RepositoryMaterializerService(store |
| 168 | new BranchesService(), |
| 169 | pushes, |
| 170 | new UsersService(db |
| 171 | assets, |
| 172 | ); |
| 173 | |
| 174 | await db |
| 175 | .delete(schema.user |
| 176 | .where(inArray(schema |
| 177 | await db.insert |
| 178 | { |
| 179 | id: OWNER, |
| 180 | name: 'Owner', |
| 181 | email: 'release-owner@example.com', |
| 182 | username: 'release-owner', |
| 183 | }, |
| 184 | { |
| 185 | id: READER, |
| 186 | name: 'Reader', |
| 187 | email: 'release-reader@example.com', |
| 188 | username: 'release-reader', |
| 189 | }, |
| 190 | ]); |
| 191 | [repository] = await |
| 192 | .insert(schema.repository |
| 193 | .values({ |
| 194 | name: 'app', |
| 195 | slug: 'app', |
| 196 | ownerId: OWNER, |
| 197 | visibility: 'public', |
| 198 | }) |
| 199 | .returning(); |
| 200 | |
| 201 | first = await |
| 202 | second = await |
| 203 | }); |
| 204 | |
| 205 | afterEach(() => |
| 206 | |
| 207 | afterAll(async () |
| 208 | await db |
| 209 | .delete(schema.user |
| 210 | .where(inArray(schema |
| 211 | await pool?.end |
| 212 | }); |
| 213 | |
| 214 | it('creates a missing tag at the default branch, or at a named target', |
| 215 | const onDefault = |
| 216 | ...owner, |
| 217 | body: { tagName: ' |
| 218 | }); |
| 219 | const onFirst = |
| 220 | ...owner, |
| 221 | body: { tagName: ' |
| 222 | }); |
| 223 | |
| 224 | expect(onDefault).toMatchObject |
| 225 | tagName: 'v2.0', |
| 226 | name: 'Two', |
| 227 | commitSha: second, |
| 228 | authorUsername: 'release-owner', |
| 229 | isDraft: false, |
| 230 | viewerCanEdit: true, |
| 231 | }); |
| 232 | expect(onDefault.publishedAt |
| 233 | expect(onFirst).toMatchObject |
| 234 | |
| 235 | // created annotated, so the tag is dated now rather than by its commit |
| 236 | const created = |
| 237 | expect(created.map |
| 238 | expect.arrayContaining([ |
| 239 | ['v2.0', 'Two' |
| 240 | ['release/v1.0', 'release/v1.0' |
| 241 | ]), |
| 242 | ); |
| 243 | for (const tag |
| 244 | expect(Date.now |
| 245 | } |
| 246 | await expect( |
| 247 | releases.getReleaseByTag({ |
| 248 | ).resolves.toMatchObject( |
| 249 | }); |
| 250 | |
| 251 | it('attaches to an existing tag and ignores the target', |
| 252 | await releases.createRelease |
| 253 | ...owner, |
| 254 | body: { tagName: ' |
| 255 | }); |
| 256 | await releases.deleteRelease |
| 257 | ...owner, |
| 258 | id: (await releases |
| 259 | }); |
| 260 | |
| 261 | const again = |
| 262 | ...owner, |
| 263 | body: { tagName: ' |
| 264 | }); |
| 265 | expect(again.commitSha |
| 266 | }); |
| 267 | |
| 268 | it('refuses a bad tag name, an unknown target, a duplicate and a reader', |
| 269 | await expect( |
| 270 | releases.createRelease({ |
| 271 | ).rejects.toBeInstanceOf( |
| 272 | await expect( |
| 273 | releases.createRelease({ |
| 274 | ...owner, |
| 275 | body: { tagName: ' |
| 276 | }), |
| 277 | ).rejects.toBeInstanceOf( |
| 278 | |
| 279 | await releases.createRelease |
| 280 | await expect( |
| 281 | releases.createRelease({ |
| 282 | ).rejects.toBeInstanceOf( |
| 283 | |
| 284 | await expect( |
| 285 | releases.createRelease({ |
| 286 | ).rejects.toThrow() |
| 287 | }); |
| 288 | |
| 289 | it('hides drafts from readers and stamps publishedAt once, on first publish', |
| 290 | const draft = |
| 291 | ...owner, |
| 292 | body: { tagName: ' |
| 293 | }); |
| 294 | expect(draft.publishedAt |
| 295 | |
| 296 | await expect( |
| 297 | releases.listReleases({ |
| 298 | ).resolves.toMatchObject( |
| 299 | await expect( |
| 300 | releases.getReleaseByTag({ |
| 301 | ).rejects.toBeInstanceOf( |
| 302 | |
| 303 | const published = |
| 304 | ...owner, |
| 305 | id: draft.id |
| 306 | body: { isDraft: false |
| 307 | }); |
| 308 | expect(published).toMatchObject |
| 309 | expect(published.publishedAt |
| 310 | |
| 311 | const redrafted = |
| 312 | ...owner, |
| 313 | id: draft.id |
| 314 | body: { isDraft: true |
| 315 | }); |
| 316 | expect(redrafted).toMatchObject |
| 317 | isDraft: true, |
| 318 | name: null, |
| 319 | publishedAt: published.publishedAt |
| 320 | }); |
| 321 | }); |
| 322 | |
| 323 | it('marks the newest published non-prerelease as latest', |
| 324 | await expect(releases |
| 325 | ReleaseNotFoundError, |
| 326 | ); |
| 327 | |
| 328 | await releases.createRelease |
| 329 | await releases.createRelease |
| 330 | ...owner, |
| 331 | body: { tagName: ' |
| 332 | }); |
| 333 | await releases.createRelease |
| 334 | ...owner, |
| 335 | body: { tagName: ' |
| 336 | }); |
| 337 | |
| 338 | await expect(releases |
| 339 | tagName: 'v1', |
| 340 | isLatest: true, |
| 341 | }); |
| 342 | const { releases: |
| 343 | ...owner, |
| 344 | query: {}, |
| 345 | }); |
| 346 | expect( |
| 347 | listed.map((release |
| 348 | ).toEqual([ |
| 349 | ['v3', false], |
| 350 | ['v2-rc', false], |
| 351 | ['v1', true], |
| 352 | ]); |
| 353 | }); |
| 354 | |
| 355 | it('pages newest first and rejects a malformed cursor', |
| 356 | for (const tagName |
| 357 | await releases.createRelease |
| 358 | } |
| 359 | |
| 360 | const page = |
| 361 | expect(page.releases |
| 362 | |
| 363 | const next = |
| 364 | ...owner, |
| 365 | query: { limit: 2 |
| 366 | }); |
| 367 | expect(next).toMatchObject |
| 368 | releases: [{ tagName: |
| 369 | nextCursor: null, |
| 370 | }); |
| 371 | |
| 372 | await expect( |
| 373 | releases.listReleases({ |
| 374 | ).rejects.toBeInstanceOf( |
| 375 | }); |
| 376 | |
| 377 | it('keeps the tag when a release is deleted, and reports a deleted tag as no commit', |
| 378 | const release = |
| 379 | ...owner, |
| 380 | body: { tagName: ' |
| 381 | }); |
| 382 | await releases.deleteRelease |
| 383 | await expect( |
| 384 | releases.deleteRelease({ |
| 385 | ).rejects.toBeInstanceOf( |
| 386 | |
| 387 | const again = |
| 388 | ...owner, |
| 389 | body: { tagName: ' |
| 390 | }); |
| 391 | await pushes.commitPush |
| 392 | repoId: repository.id |
| 393 | transitions: [ |
| 394 | { |
| 395 | ref: 'refs/tags/v1', |
| 396 | oldOid: Buffer.from |
| 397 | newOid: ZERO_OID, |
| 398 | }, |
| 399 | ], |
| 400 | body: bufferBody(Buffer |
| 401 | packOffset: 0, |
| 402 | }); |
| 403 | |
| 404 | await expect( |
| 405 | releases.getReleaseByTag({ |
| 406 | ).resolves.toMatchObject( |
| 407 | await expect( |
| 408 | releases.updateRelease({ |
| 409 | ).rejects.toBeInstanceOf( |
| 410 | }); |
| 411 | |
| 412 | describe('assets', |
| 413 | let release: |
| 414 | const zip = |
| 415 | |
| 416 | beforeEach(async () |
| 417 | release = await |
| 418 | ...owner, |
| 419 | body: { tagName: ' |
| 420 | }); |
| 421 | }); |
| 422 | |
| 423 | it('stores, lists, serves and counts an asset, then deletes it', |
| 424 | const asset = |
| 425 | expect(asset).toMatchObject |
| 426 | name: 'app.zip', |
| 427 | contentType: 'application/zip', |
| 428 | size: zip.length, |
| 429 | downloadCount: 0, |
| 430 | }); |
| 431 | |
| 432 | await expect( |
| 433 | releases.getReleaseByTag({ |
| 434 | ).resolves.toMatchObject( |
| 435 | |
| 436 | const download = |
| 437 | ...anonymous, |
| 438 | tagName: 'v1', |
| 439 | name: 'app.zip', |
| 440 | }); |
| 441 | await expect(buffer |
| 442 | await expect( |
| 443 | releases.getReleaseByTag({ |
| 444 | ).resolves.toMatchObject( |
| 445 | |
| 446 | await assets.delete |
| 447 | expect(s3.objects |
| 448 | await expect( |
| 449 | assets.download({ |
| 450 | ).rejects.toBeInstanceOf( |
| 451 | await expect( |
| 452 | assets.delete({ |
| 453 | ).rejects.toBeInstanceOf( |
| 454 | }); |
| 455 | |
| 456 | it('refuses a path for a name, a missing length, an oversized file, a duplicate and a reader', |
| 457 | await expect( |
| 458 | upload(release.id |
| 459 | ).rejects.toBeInstanceOf( |
| 460 | await expect( |
| 461 | assets.upload({ |
| 462 | ...owner, |
| 463 | releaseId: release.id |
| 464 | name: 'app.zip', |
| 465 | type: undefined, |
| 466 | bodyType: 'application/octet-stream', |
| 467 | contentLength: undefined, |
| 468 | body: Readable.from |
| 469 | }), |
| 470 | ).rejects.toBeInstanceOf( |
| 471 | |
| 472 | // a JSON or form body has already been parsed away by the time it could be streamed |
| 473 | for (const bodyType |
| 474 | await expect( |
| 475 | assets.upload({ |
| 476 | ...owner, |
| 477 | releaseId: release.id |
| 478 | name: 'app.zip', |
| 479 | type: 'application/json', |
| 480 | bodyType, |
| 481 | contentLength: String(zip |
| 482 | body: Readable.from |
| 483 | }), |
| 484 | ).rejects.toBeInstanceOf( |
| 485 | } |
| 486 | |
| 487 | limits({ RELEASE_ASSET_MAX_BYTES: |
| 488 | await expect(upload |
| 489 | ReleaseAssetTooLargeError, |
| 490 | ); |
| 491 | |
| 492 | limits({}); |
| 493 | await upload(release |
| 494 | await expect(upload |
| 495 | ReleaseAssetExistsError, |
| 496 | ); |
| 497 | await expect( |
| 498 | upload(release.id |
| 499 | ).rejects.toThrow() |
| 500 | await expect( |
| 501 | upload('release_missing', |
| 502 | ).rejects.toBeInstanceOf( |
| 503 | }); |
| 504 | |
| 505 | it('drops the reservation when the upload fails, so the name and the space are free again', |
| 506 | s3.failNextPut = |
| 507 | await expect(upload |
| 508 | 'connection reset', |
| 509 | ); |
| 510 | await expect(quota |
| 511 | await expect(upload |
| 512 | name: 'app.zip', |
| 513 | }); |
| 514 | }); |
| 515 | |
| 516 | it('holds an account to its quota, counting live reservations but not lapsed ones', |
| 517 | limits({ STORAGE_QUOTA_BYTES: |
| 518 | await upload(release |
| 519 | |
| 520 | // an upload in flight elsewhere holds its space |
| 521 | const [inFlight] |
| 522 | .insert(schema.releaseAsset |
| 523 | .values({ |
| 524 | releaseId: release.id |
| 525 | repositoryId: repository.id |
| 526 | name: 'b.zip', |
| 527 | contentType: 'application/zip', |
| 528 | size: zip.length, |
| 529 | }) |
| 530 | .returning(); |
| 531 | await expect(upload |
| 532 | StorageQuotaExceededError, |
| 533 | ); |
| 534 | |
| 535 | // one that died a day ago does not, and no longer holds its name either |
| 536 | await db |
| 537 | .update(schema.releaseAsset |
| 538 | .set({ createdAt: |
| 539 | .where(inArray(schema |
| 540 | await expect(upload |
| 541 | name: 'b.zip', |
| 542 | }); |
| 543 | await expect(quota |
| 544 | zip.length * 2 |
| 545 | ); |
| 546 | }); |
| 547 | |
| 548 | it('lets concurrent uploads fill the quota exactly, never past it', |
| 549 | limits({ STORAGE_QUOTA_BYTES: |
| 550 | const results = |
| 551 | ['a', 'b' |
| 552 | upload(release.id |
| 553 | ), |
| 554 | ); |
| 555 | expect( |
| 556 | results.filter((result |
| 557 | ).toHaveLength(2); |
| 558 | }); |
| 559 | |
| 560 | it('bills an organization repository to the organization, not its owner', |
| 561 | limits({ STORAGE_QUOTA_BYTES: |
| 562 | await upload(release |
| 563 | |
| 564 | await db.insert |
| 565 | id: 'org_release', |
| 566 | name: 'Release Org', |
| 567 | slug: 'release-org', |
| 568 | createdAt: new Date |
| 569 | }); |
| 570 | try { |
| 571 | await db.insert |
| 572 | id: 'member_release', |
| 573 | organizationId: 'org_release', |
| 574 | userId: OWNER, |
| 575 | role: 'owner', |
| 576 | createdAt: new Date |
| 577 | }); |
| 578 | await db |
| 579 | .update(schema.repository |
| 580 | .set({ organizationId: |
| 581 | .where(inArray(schema |
| 582 | |
| 583 | await expect(quota |
| 584 | await expect( |
| 585 | quota.usageOf({ |
| 586 | ).resolves.toBe( |
| 587 | } finally { |
| 588 | await db |
| 589 | .delete(schema.organization |
| 590 | .where(inArray(schema |
| 591 | } |
| 592 | }); |
| 593 | |
| 594 | it('hides a draft release asset from readers', |
| 595 | const draft = |
| 596 | ...owner, |
| 597 | body: { tagName: ' |
| 598 | }); |
| 599 | await upload(draft |
| 600 | |
| 601 | await expect( |
| 602 | assets.download({ |
| 603 | ).rejects.toBeInstanceOf( |
| 604 | await expect( |
| 605 | assets.download({ |
| 606 | ).resolves.toMatchObject( |
| 607 | }); |
| 608 | |
| 609 | it('removes stored files with their release', |
| 610 | await upload(release |
| 611 | await releases.deleteRelease |
| 612 | expect(s3.objects |
| 613 | }); |
| 614 | |
| 615 | it('reports usage to the account itself and its organization members only', |
| 616 | limits({ STORAGE_QUOTA_BYTES: |
| 617 | await upload(release |
| 618 | const storage = |
| 619 | |
| 620 | await expect(storage |
| 621 | usedBytes: zip.length, |
| 622 | quotaBytes: 1024 ** 3, |
| 623 | maxAssetBytes: 2 * 1024 |
| 624 | }); |
| 625 | await expect( |
| 626 | storage.usage('release-owner |
| 627 | ).rejects.toBeInstanceOf( |
| 628 | }); |
| 629 | }); |
| 630 | }); |