|
| 1 | +import type { ContainerClient } from '@azure/storage-blob' |
| 2 | +import type { Page } from '@playwright/test' |
| 3 | + |
| 4 | +import { BlobServiceClient } from '@azure/storage-blob' |
| 5 | +import { expect, test } from '@playwright/test' |
| 6 | +import dotenv from 'dotenv' |
| 7 | +import * as path from 'path' |
| 8 | +import { fileURLToPath } from 'url' |
| 9 | + |
| 10 | +import type { PayloadTestSDK } from '../../__helpers/shared/sdk/index.js' |
| 11 | + |
| 12 | +import { ensureCompilationIsDone, saveDocAndAssert } 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 { mediaWithDocPrefixSlug } from './collections/MediaWithDocPrefix.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 | +test.describe('storage-azure client uploads E2E', () => { |
| 24 | + let page: Page |
| 25 | + let mediaWithDocPrefixURL: AdminUrlUtil |
| 26 | + let payloadSDK: PayloadTestSDK<{ collections: Record<string, unknown> }> |
| 27 | + let containerClient: ContainerClient |
| 28 | + |
| 29 | + test.beforeAll(async ({ browser }, testInfo) => { |
| 30 | + testInfo.setTimeout(TEST_TIMEOUT_LONG) |
| 31 | + const { payload, serverURL } = await initPayloadE2ENoConfig({ dirname }) |
| 32 | + |
| 33 | + payloadSDK = payload as unknown as typeof payloadSDK |
| 34 | + mediaWithDocPrefixURL = new AdminUrlUtil(serverURL, mediaWithDocPrefixSlug) |
| 35 | + |
| 36 | + const blobService = BlobServiceClient.fromConnectionString( |
| 37 | + process.env.AZURE_STORAGE_CONNECTION_STRING!, |
| 38 | + ) |
| 39 | + |
| 40 | + await blobService.setProperties({ |
| 41 | + cors: [ |
| 42 | + { |
| 43 | + allowedHeaders: '*', |
| 44 | + allowedMethods: 'GET,PUT,POST,DELETE,OPTIONS,HEAD', |
| 45 | + allowedOrigins: '*', |
| 46 | + exposedHeaders: '*', |
| 47 | + maxAgeInSeconds: 3600, |
| 48 | + }, |
| 49 | + ], |
| 50 | + }) |
| 51 | + |
| 52 | + containerClient = blobService.getContainerClient(process.env.AZURE_STORAGE_CONTAINER_NAME!) |
| 53 | + await containerClient.createIfNotExists() |
| 54 | + |
| 55 | + // Clear leftover blobs from previous runs so the listBlobsFlat assertion |
| 56 | + // below only sees uploads from this test. |
| 57 | + for await (const blob of containerClient.listBlobsFlat()) { |
| 58 | + await containerClient.deleteBlob(blob.name) |
| 59 | + } |
| 60 | + |
| 61 | + const context = await browser.newContext() |
| 62 | + page = await context.newPage() |
| 63 | + await ensureCompilationIsDone({ page, serverURL }) |
| 64 | + }) |
| 65 | + |
| 66 | + /** |
| 67 | + * Drives the actual admin form through a clientUploads submit and asserts |
| 68 | + * that the user-defined `prefix.defaultValue` ends up on the saved doc — i.e. |
| 69 | + * the plugin no longer clobbers the user's callback (see getFields.ts). |
| 70 | + */ |
| 71 | + test('respects user-defined prefix.defaultValue when creating a doc via clientUploads', async () => { |
| 72 | + await page.goto(mediaWithDocPrefixURL.create) |
| 73 | + await page.setInputFiles('input[type="file"]', path.resolve(dirname, '../../uploads/image.png')) |
| 74 | + await saveDocAndAssert(page) |
| 75 | + |
| 76 | + const docId = page.url().split('/').pop()! |
| 77 | + |
| 78 | + const result = await payloadSDK.find({ |
| 79 | + collection: mediaWithDocPrefixSlug, |
| 80 | + where: { id: { equals: docId } }, |
| 81 | + }) |
| 82 | + |
| 83 | + const doc = result.docs[0] |
| 84 | + |
| 85 | + const blobNames: string[] = [] |
| 86 | + for await (const blob of containerClient.listBlobsFlat()) { |
| 87 | + blobNames.push(blob.name) |
| 88 | + } |
| 89 | + |
| 90 | + expect(blobNames).toEqual( |
| 91 | + expect.arrayContaining([expect.stringMatching(/^doc-[a-z0-9]{1,8}\/image\.png$/)]), |
| 92 | + ) |
| 93 | + |
| 94 | + expect(doc?.prefix).toMatch(/^doc-[a-z0-9]{1,8}$/) |
| 95 | + }) |
| 96 | +}) |
0 commit comments