107 lines
4.4 KiB
JavaScript
107 lines
4.4 KiB
JavaScript
// Update maintained collections without replacing existing requests or examples.
|
|
require('./sync-mobile-compatibility');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const root = path.join(__dirname, '..', 'postman');
|
|
const event = (exec) => [{ listen: 'test', script: { type: 'text/javascript', exec } }];
|
|
const bearer = {
|
|
type: 'bearer',
|
|
bearer: [{ key: 'token', value: '{{accessToken}}', type: 'string' }],
|
|
};
|
|
const requests = [
|
|
{
|
|
name: 'Presign direct media upload',
|
|
request: {
|
|
method: 'POST',
|
|
auth: bearer,
|
|
header: [{ key: 'Content-Type', value: 'application/json' }],
|
|
url: '{{baseUrl}}/media/uploads/presign',
|
|
description:
|
|
'Requires S3 storage. Set uploadSize to the exact file size in bytes. Run this before Upload binary to S3. The URL expires after expiresIn seconds.',
|
|
body: {
|
|
mode: 'raw',
|
|
raw: '{\n "folder": "posts/video",\n "mimeType": "{{uploadMimeType}}",\n "size": {{uploadSize}}\n}',
|
|
options: { raw: { language: 'json' } },
|
|
},
|
|
},
|
|
event: event([
|
|
"pm.test('Status is 200', () => pm.response.to.have.status(200));",
|
|
'if (pm.response.code === 200) {',
|
|
' const json = pm.response.json(); const body = json.data ?? json;',
|
|
" pm.test('Upload contract', () => { pm.expect(body.method).to.eql('PUT'); pm.expect(body.uploadUrl).to.be.a('string'); pm.expect(body.storageKey).to.be.a('string'); });",
|
|
' for (const [key, value] of Object.entries({ directUploadUrl: body.uploadUrl, videoStorageKey: body.storageKey, uploadedMediaUrl: body.mediaUrl })) pm.environment.set(key, value);',
|
|
'}',
|
|
]),
|
|
},
|
|
{
|
|
name: 'Upload binary to S3',
|
|
request: {
|
|
method: 'PUT',
|
|
auth: { type: 'noauth' },
|
|
header: [{ key: 'Content-Type', value: '{{uploadMimeType}}' }],
|
|
url: '{{directUploadUrl}}',
|
|
description:
|
|
'Select a local binary file matching uploadSize and uploadMimeType. Keep the signed URL unchanged. Use videoStorageKey when creating a post or reel; inspect that endpoint for the other required fields.',
|
|
body: { mode: 'file', file: { src: '' } },
|
|
},
|
|
event: event([
|
|
"pm.test('S3 accepted upload', () => pm.expect(pm.response.code).to.be.oneOf([200, 204]));",
|
|
]),
|
|
},
|
|
{
|
|
name: 'Retry failed video processing',
|
|
request: {
|
|
method: 'POST',
|
|
auth: bearer,
|
|
header: [],
|
|
url: '{{baseUrl}}/posts/{{postId}}/video/retry',
|
|
description:
|
|
'Owner only. Requires an existing failed video job; otherwise returns 404. Poll GET /posts/{{postId}} for processingStatus. Limited to 5 requests per minute.',
|
|
},
|
|
event: event([
|
|
"pm.test('Status is 201', () => pm.response.to.have.status(201));",
|
|
"if (pm.response.code === 201) { const json = pm.response.json(); pm.test('Job pending', () => pm.expect((json.data ?? json).processingStatus).to.eql('pending')); }",
|
|
]),
|
|
},
|
|
];
|
|
for (const name of ['Oudelaa-Mobile', 'Oudelaa-Auth-Users-Posts']) {
|
|
const file = path.join(root, `${name}.postman_collection.json`);
|
|
const collection = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
const folderName = 'Direct Upload & Video Processing';
|
|
const folder = { name: folderName, item: requests };
|
|
const index = collection.item.findIndex((item) => item.name === folderName);
|
|
if (index < 0) collection.item.push(folder);
|
|
else collection.item[index] = folder;
|
|
collection.variable ??= [];
|
|
for (const [key, value] of Object.entries({
|
|
uploadMimeType: 'video/mp4',
|
|
uploadSize: '5242880',
|
|
directUploadUrl: '',
|
|
videoStorageKey: '',
|
|
uploadedMediaUrl: '',
|
|
})) {
|
|
if (!collection.variable.some((entry) => entry.key === key))
|
|
collection.variable.push({ key, value, type: 'string' });
|
|
}
|
|
fs.writeFileSync(file, JSON.stringify(collection, null, 2) + '\n');
|
|
}
|
|
console.log('Updated mobile and full API collections.');
|
|
|
|
// Postman v2.1 requires an event array, even for a single test script.
|
|
for (const name of fs
|
|
.readdirSync(root)
|
|
.filter((name) => name.endsWith('.postman_collection.json'))) {
|
|
const file = path.join(root, name);
|
|
const collection = JSON.parse(fs.readFileSync(file, 'utf8'));
|
|
let changed = false;
|
|
function normalize(item) {
|
|
if (item.event && !Array.isArray(item.event)) {
|
|
item.event = [item.event];
|
|
changed = true;
|
|
}
|
|
for (const child of item.item ?? []) normalize(child);
|
|
}
|
|
normalize(collection);
|
|
if (changed) fs.writeFileSync(file, JSON.stringify(collection, null, 2) + '\n');
|
|
}
|