|
| 1 | +import type { Page } from '@playwright/test' |
| 2 | + |
| 3 | +import { expect, test } from '@playwright/test' |
| 4 | +import dotenv from 'dotenv' |
| 5 | +import * as path from 'path' |
| 6 | +import { fileURLToPath } from 'url' |
| 7 | + |
| 8 | +import { |
| 9 | + ensureCompilationIsDone, |
| 10 | + exactText, |
| 11 | + saveDocAndAssert, |
| 12 | +} from '../../__helpers/e2e/helpers.js' |
| 13 | +import { AdminUrlUtil } from '../../__helpers/shared/adminUrlUtil.js' |
| 14 | +import { initPayloadE2ENoConfig } from '../../__helpers/shared/initPayloadE2ENoConfig.js' |
| 15 | +import { TEST_TIMEOUT_LONG } from '../../playwright.config.js' |
| 16 | +import { mediaSlug } from '../shared.js' |
| 17 | + |
| 18 | +const filename = fileURLToPath(import.meta.url) |
| 19 | +const dirname = path.dirname(filename) |
| 20 | + |
| 21 | +dotenv.config({ path: path.resolve(dirname, '../../plugin-cloud-storage/.env.emulated') }) |
| 22 | + |
| 23 | +// e.g. "localhost:4566" — used to detect file upload requests going directly to S3 |
| 24 | +const s3Endpoint = |
| 25 | + process.env.S3_ENDPOINT ?? process.env.AWS_ENDPOINT_URL ?? process.env.AWS_ENDPOINT_URL_S3 |
| 26 | + |
| 27 | +if (!s3Endpoint) { |
| 28 | + throw new Error('Missing S3 endpoint env (S3_ENDPOINT, AWS_ENDPOINT_URL, or AWS_ENDPOINT_URL_S3)') |
| 29 | +} |
| 30 | + |
| 31 | +const s3Host = new URL(s3Endpoint).host |
| 32 | +// image.png is 89 KB — any request with content-length above this threshold is a file upload |
| 33 | +const FILE_SIZE_THRESHOLD = 1_000 |
| 34 | + |
| 35 | +const mediaContainerSlug = 'media-container' |
| 36 | + |
| 37 | +test.describe('storage-s3 client uploads E2E', () => { |
| 38 | + let page: Page |
| 39 | + let mediaURL: AdminUrlUtil |
| 40 | + let mediaContainerURL: AdminUrlUtil |
| 41 | + let payloadHost: string |
| 42 | + |
| 43 | + test.beforeAll(async ({ browser }, testInfo) => { |
| 44 | + testInfo.setTimeout(TEST_TIMEOUT_LONG) |
| 45 | + const { serverURL } = await initPayloadE2ENoConfig({ dirname }) |
| 46 | + |
| 47 | + payloadHost = new URL(serverURL).host |
| 48 | + mediaURL = new AdminUrlUtil(serverURL, mediaSlug) |
| 49 | + mediaContainerURL = new AdminUrlUtil(serverURL, mediaContainerSlug) |
| 50 | + |
| 51 | + const context = await browser.newContext() |
| 52 | + page = await context.newPage() |
| 53 | + await ensureCompilationIsDone({ page, serverURL }) |
| 54 | + }) |
| 55 | + |
| 56 | + test('should complete a single client upload via the admin UI', async () => { |
| 57 | + await page.goto(mediaURL.create) |
| 58 | + await page.setInputFiles('input[type="file"]', path.resolve(dirname, '../../uploads/image.png')) |
| 59 | + await expect(page.locator('.file-field__filename')).toHaveValue('image.png') |
| 60 | + await saveDocAndAssert(page) |
| 61 | + }) |
| 62 | + |
| 63 | + test('should upload file directly to S3, not through the Payload server', async ({ browser }) => { |
| 64 | + const context = await browser.newContext() |
| 65 | + const testPage = await context.newPage() |
| 66 | + |
| 67 | + const largeRequestsToPayload: string[] = [] |
| 68 | + const putsToS3: string[] = [] |
| 69 | + |
| 70 | + testPage.on('request', (request) => { |
| 71 | + const url = request.url() |
| 72 | + const contentLength = parseInt(request.headers()['content-length'] ?? '0', 10) |
| 73 | + |
| 74 | + if (new URL(url).host === payloadHost && contentLength > FILE_SIZE_THRESHOLD) { |
| 75 | + largeRequestsToPayload.push(`${request.method()} ${url} (${contentLength} bytes)`) |
| 76 | + } |
| 77 | + if (new URL(url).host === s3Host && request.method() === 'PUT') { |
| 78 | + putsToS3.push(url) |
| 79 | + } |
| 80 | + }) |
| 81 | + |
| 82 | + await testPage.goto(mediaURL.create) |
| 83 | + await testPage.setInputFiles( |
| 84 | + 'input[type="file"]', |
| 85 | + path.resolve(dirname, '../../uploads/image.png'), |
| 86 | + ) |
| 87 | + await saveDocAndAssert(testPage) |
| 88 | + |
| 89 | + expect( |
| 90 | + largeRequestsToPayload, |
| 91 | + `File bytes were sent to Payload: ${largeRequestsToPayload.join(', ')}`, |
| 92 | + ).toHaveLength(0) |
| 93 | + |
| 94 | + expect( |
| 95 | + putsToS3.length, |
| 96 | + 'Expected at least one PUT request to the S3 endpoint', |
| 97 | + ).toBeGreaterThanOrEqual(1) |
| 98 | + |
| 99 | + await context.close() |
| 100 | + }) |
| 101 | + |
| 102 | + test('should bulk upload multiple files directly to S3, not through Payload', async ({ |
| 103 | + browser, |
| 104 | + }) => { |
| 105 | + const context = await browser.newContext() |
| 106 | + const testPage = await context.newPage() |
| 107 | + |
| 108 | + const largeRequestsToPayload: string[] = [] |
| 109 | + const putsToS3: string[] = [] |
| 110 | + |
| 111 | + testPage.on('request', (request) => { |
| 112 | + const url = request.url() |
| 113 | + const contentLength = parseInt(request.headers()['content-length'] ?? '0', 10) |
| 114 | + |
| 115 | + if (new URL(url).host === payloadHost && contentLength > FILE_SIZE_THRESHOLD) { |
| 116 | + largeRequestsToPayload.push(`${request.method()} ${url} (${contentLength} bytes)`) |
| 117 | + } |
| 118 | + if (new URL(url).host === s3Host && request.method() === 'PUT') { |
| 119 | + putsToS3.push(url) |
| 120 | + } |
| 121 | + }) |
| 122 | + |
| 123 | + await testPage.goto(mediaContainerURL.create) |
| 124 | + |
| 125 | + const createNewButton = testPage.locator('#field-files button', { |
| 126 | + hasText: exactText('Create New'), |
| 127 | + }) |
| 128 | + await expect(createNewButton).toBeVisible() |
| 129 | + await expect(createNewButton).toBeEnabled() |
| 130 | + await createNewButton.click() |
| 131 | + |
| 132 | + const bulkUploadModal = testPage.locator('#files-bulk-upload-drawer-slug-1') |
| 133 | + await expect(bulkUploadModal).toBeVisible() |
| 134 | + |
| 135 | + await bulkUploadModal |
| 136 | + .locator('.dropzone input[type="file"]') |
| 137 | + .setInputFiles([ |
| 138 | + path.resolve(dirname, '../../uploads/image.png'), |
| 139 | + path.resolve(dirname, '../../uploads/test-image.png'), |
| 140 | + ]) |
| 141 | + |
| 142 | + const saveButton = bulkUploadModal.locator('.bulk-upload--actions-bar__saveButtons button') |
| 143 | + await saveButton.click() |
| 144 | + |
| 145 | + await expect(bulkUploadModal).toBeHidden({ timeout: 30_000 }) |
| 146 | + |
| 147 | + expect(putsToS3.length, 'Expected one PUT to S3 per uploaded file').toBeGreaterThanOrEqual(2) |
| 148 | + |
| 149 | + expect( |
| 150 | + largeRequestsToPayload, |
| 151 | + `File bytes were sent to Payload: ${largeRequestsToPayload.join(', ')}`, |
| 152 | + ).toHaveLength(0) |
| 153 | + |
| 154 | + const items = testPage.locator('#field-files .upload--has-many__dragItem') |
| 155 | + await expect(items).toHaveCount(2) |
| 156 | + |
| 157 | + await context.close() |
| 158 | + }) |
| 159 | + |
| 160 | + test('should bulk upload files from the list view directly to S3, not through Payload', async ({ |
| 161 | + browser, |
| 162 | + }) => { |
| 163 | + const context = await browser.newContext() |
| 164 | + const testPage = await context.newPage() |
| 165 | + |
| 166 | + const largeRequestsToPayload: string[] = [] |
| 167 | + const putsToS3: string[] = [] |
| 168 | + |
| 169 | + testPage.on('request', (request) => { |
| 170 | + const url = request.url() |
| 171 | + const contentLength = parseInt(request.headers()['content-length'] ?? '0', 10) |
| 172 | + |
| 173 | + if (new URL(url).host === payloadHost && contentLength > FILE_SIZE_THRESHOLD) { |
| 174 | + largeRequestsToPayload.push(`${request.method()} ${url} (${contentLength} bytes)`) |
| 175 | + } |
| 176 | + if (new URL(url).host === s3Host && request.method() === 'PUT') { |
| 177 | + putsToS3.push(url) |
| 178 | + } |
| 179 | + }) |
| 180 | + |
| 181 | + await testPage.goto(mediaURL.list) |
| 182 | + await expect(testPage.locator('.list-header__title')).toBeVisible() |
| 183 | + |
| 184 | + const bulkUploadButton = testPage.locator('.list-header__title-actions button', { |
| 185 | + hasText: 'Bulk Upload', |
| 186 | + }) |
| 187 | + await expect(bulkUploadButton).toBeEnabled() |
| 188 | + |
| 189 | + // Click and retry until dropzone appears (handles hydration timing) |
| 190 | + const dropzoneInput = testPage.locator('.dropzone input[type="file"]') |
| 191 | + await expect(async () => { |
| 192 | + await bulkUploadButton.click() |
| 193 | + await expect(dropzoneInput).toBeAttached({ timeout: 1500 }) |
| 194 | + }).toPass({ timeout: 5000, intervals: [500] }) |
| 195 | + |
| 196 | + await testPage.setInputFiles('.dropzone input[type="file"]', [ |
| 197 | + path.resolve(dirname, '../../uploads/image.png'), |
| 198 | + path.resolve(dirname, '../../uploads/test-image.png'), |
| 199 | + ]) |
| 200 | + |
| 201 | + const bulkUploadModal = testPage.locator('#media-bulk-upload-drawer-slug-1') |
| 202 | + const saveButton = bulkUploadModal.locator('.bulk-upload--actions-bar__saveButtons button') |
| 203 | + await expect(saveButton).toBeVisible() |
| 204 | + await saveButton.click() |
| 205 | + |
| 206 | + await expect(bulkUploadModal).toBeHidden({ timeout: 30_000 }) |
| 207 | + |
| 208 | + expect(putsToS3.length, 'Expected one PUT to S3 per uploaded file').toBeGreaterThanOrEqual(2) |
| 209 | + |
| 210 | + expect( |
| 211 | + largeRequestsToPayload, |
| 212 | + `File bytes were sent to Payload: ${largeRequestsToPayload.join(', ')}`, |
| 213 | + ).toHaveLength(0) |
| 214 | + |
| 215 | + await context.close() |
| 216 | + }) |
| 217 | +}) |
0 commit comments