|
| 1 | +import { |
| 2 | + createStandalonePostgresContainer, |
| 3 | + MinIOContainer, |
| 4 | + type StartedMinIOContainer, |
| 5 | +} from "@internal/testcontainers"; |
| 6 | +import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest"; |
| 7 | +import { seedTestEnvironment } from "./helpers/seedTestEnvironment"; |
| 8 | +import { seedTestUser } from "./helpers/seedTestSession"; |
| 9 | + |
| 10 | +import type * as Route from "~/routes/resources.packets.$environmentId.$"; |
| 11 | +import type * as Database from "~/db.server"; |
| 12 | +import type * as SessionStorage from "~/services/sessionStorage.server"; |
| 13 | +import type * as ObjectStore from "~/v3/objectStore.server"; |
| 14 | + |
| 15 | +vi.setConfig({ testTimeout: 60_000 }); |
| 16 | + |
| 17 | +let container: Awaited<ReturnType<typeof createStandalonePostgresContainer>>; |
| 18 | +let minio: StartedMinIOContainer; |
| 19 | +let loader: typeof Route.loader; |
| 20 | +let database: typeof Database; |
| 21 | +let sessionStorage: typeof SessionStorage; |
| 22 | +let objectStore: typeof ObjectStore; |
| 23 | + |
| 24 | +beforeAll(async () => { |
| 25 | + container = await createStandalonePostgresContainer(); |
| 26 | + minio = await new MinIOContainer().start(); |
| 27 | + const store = minio.getConnectionConfig(); |
| 28 | + |
| 29 | + vi.stubEnv("DATABASE_URL", container.url); |
| 30 | + vi.stubEnv("CONTROL_PLANE_DATABASE_URL", container.url); |
| 31 | + vi.stubEnv("DATABASE_READ_REPLICA_URL", container.url); |
| 32 | + vi.stubEnv("CONTROL_PLANE_DATABASE_READ_REPLICA_URL", container.url); |
| 33 | + vi.stubEnv("RBAC_FORCE_FALLBACK", "1"); |
| 34 | + vi.stubEnv("ENCRYPTION_KEY", "test-encryption-key-for-e2e!!!!!"); |
| 35 | + for (const prefix of ["OBJECT_STORE_", "OBJECT_STORE_S3_"]) { |
| 36 | + vi.stubEnv(`${prefix}BASE_URL`, store.baseUrl); |
| 37 | + vi.stubEnv(`${prefix}ACCESS_KEY_ID`, store.accessKeyId); |
| 38 | + vi.stubEnv(`${prefix}SECRET_ACCESS_KEY`, store.secretAccessKey); |
| 39 | + vi.stubEnv(`${prefix}REGION`, store.region); |
| 40 | + vi.stubEnv(`${prefix}SERVICE`, "s3"); |
| 41 | + } |
| 42 | + |
| 43 | + database = await import("~/db.server"); |
| 44 | + ({ loader } = await import("~/routes/resources.packets.$environmentId.$")); |
| 45 | + sessionStorage = await import("~/services/sessionStorage.server"); |
| 46 | + objectStore = await import("~/v3/objectStore.server"); |
| 47 | +}, 120_000); |
| 48 | + |
| 49 | +afterAll(async () => { |
| 50 | + await database?.$replica.$disconnect(); |
| 51 | + await database?.prisma.$disconnect(); |
| 52 | + await container?.container.stop(); |
| 53 | + await minio?.stop({ timeout: 0 }); |
| 54 | + vi.unstubAllEnvs(); |
| 55 | +}); |
| 56 | + |
| 57 | +beforeEach(async () => { |
| 58 | + await minio.resetBucket(); |
| 59 | +}); |
| 60 | + |
| 61 | +async function seedMember() { |
| 62 | + const { organization, project, environment } = await seedTestEnvironment(database.prisma); |
| 63 | + const user = await seedTestUser(database.prisma); |
| 64 | + await database.prisma.orgMember.create({ |
| 65 | + data: { userId: user.id, organizationId: organization.id, role: "MEMBER" }, |
| 66 | + }); |
| 67 | + const session = await sessionStorage.getSession(); |
| 68 | + session.set("user", { userId: user.id }); |
| 69 | + const cookie = (await sessionStorage.commitSession(session)).split(";")[0]; |
| 70 | + return { project, environment, cookie }; |
| 71 | +} |
| 72 | + |
| 73 | +async function putPacket( |
| 74 | + projectRef: string, |
| 75 | + envSlug: string, |
| 76 | + filename: string, |
| 77 | + body: string, |
| 78 | + forceNoPrefix = false |
| 79 | +) { |
| 80 | + const signed = await objectStore.generatePresignedRequest(projectRef, envSlug, filename, "PUT", { |
| 81 | + forceNoPrefix, |
| 82 | + }); |
| 83 | + if (!signed.success) throw new Error(signed.error); |
| 84 | + const response = await fetch(signed.request.url, { |
| 85 | + method: "PUT", |
| 86 | + headers: signed.request.headers, |
| 87 | + body, |
| 88 | + }); |
| 89 | + expect(response.status).toBe(200); |
| 90 | +} |
| 91 | + |
| 92 | +function download(environmentId: string, packet: string, cookie: string) { |
| 93 | + return loader({ |
| 94 | + request: new Request(`https://app.example.com/resources/packets/${environmentId}/${packet}`, { |
| 95 | + headers: { Cookie: cookie }, |
| 96 | + }), |
| 97 | + params: { environmentId, "*": packet }, |
| 98 | + context: {}, |
| 99 | + }); |
| 100 | +} |
| 101 | + |
| 102 | +describe("resources.packets download", () => { |
| 103 | + it("returns an offloaded s3:// packet as an attachment", async () => { |
| 104 | + const { project, environment, cookie } = await seedMember(); |
| 105 | + await putPacket(project.externalRef, environment.slug, "s3://run_abc/output.json", '{"ok":1}'); |
| 106 | + |
| 107 | + const response = await download(environment.id, "s3://run_abc/output.json", cookie); |
| 108 | + |
| 109 | + expect(response.status).toBe(200); |
| 110 | + expect(response.headers.get("Content-Disposition")).toBe('attachment; filename="output.json"'); |
| 111 | + expect(await response.text()).toBe('{"ok":1}'); |
| 112 | + }); |
| 113 | + |
| 114 | + it("returns a legacy unprefixed packet", async () => { |
| 115 | + const { project, environment, cookie } = await seedMember(); |
| 116 | + await putPacket( |
| 117 | + project.externalRef, |
| 118 | + environment.slug, |
| 119 | + "batch_123/item_0/payload.json", |
| 120 | + '{"legacy":true}', |
| 121 | + true |
| 122 | + ); |
| 123 | + |
| 124 | + const response = await download(environment.id, "batch_123/item_0/payload.json", cookie); |
| 125 | + |
| 126 | + expect(response.status).toBe(200); |
| 127 | + expect(await response.text()).toBe('{"legacy":true}'); |
| 128 | + }); |
| 129 | + |
| 130 | + it("reports a missing packet as 404 instead of downloading the store's error", async () => { |
| 131 | + const { environment, cookie } = await seedMember(); |
| 132 | + |
| 133 | + const response = await download(environment.id, "s3://run_missing/output.json", cookie); |
| 134 | + const body = await response.text(); |
| 135 | + |
| 136 | + expect(response.status).toBe(404); |
| 137 | + expect(response.headers.get("Content-Disposition")).toBeNull(); |
| 138 | + expect(body).not.toContain("<?xml"); |
| 139 | + expect(body).not.toContain("NoSuchKey"); |
| 140 | + }); |
| 141 | +}); |
0 commit comments