96 lines
6.5 KiB
JavaScript
96 lines
6.5 KiB
JavaScript
// Add migration contracts without removing historical requests or stored responses.
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const root = path.join(__dirname, '..', 'postman');
|
|
const exampleFile = path.join(root, 'mobile-compatibility-response-examples.json');
|
|
const captured = fs.existsSync(exampleFile) ? JSON.parse(fs.readFileSync(exampleFile, 'utf8')) : undefined;
|
|
const bearer = { type: 'bearer', bearer: [{ key: 'token', value: '{{accessToken}}', type: 'string' }] };
|
|
const jsonRequest = (name, method, route, body, description, auth = bearer) => ({
|
|
name, request: {
|
|
method, auth, url: `{{baseUrl}}${route}`, description,
|
|
header: body ? [{ key: 'Content-Type', value: 'application/json' }] : [],
|
|
...(body ? { body: { mode: 'raw', raw: JSON.stringify(body, null, 2), options: { raw: { language: 'json' } } } } : {}),
|
|
},
|
|
});
|
|
const saveVariable = (item, key) => {
|
|
item.event = [{ listen: 'test', script: { type: 'text/javascript', exec: [
|
|
'if (pm.response.code >= 200 && pm.response.code < 300) {',
|
|
' const json = pm.response.json(); const body = json.data ?? json;',
|
|
` pm.expect(body.${key}).to.be.a('string').and.not.empty;`,
|
|
` pm.environment.set('${key}', body.${key});`,
|
|
'}',
|
|
] } }];
|
|
return item;
|
|
};
|
|
const noauth = { type: 'noauth' };
|
|
const start = saveVariable(jsonRequest('Signup start', 'POST', '/auth/signup/start', {
|
|
email: '{{email}}', password: '{{password}}', confirmPassword: '{{password}}',
|
|
}, '201. Sends OTP; no user/session yet. signupId is saved automatically. Production does not return debugCode.', noauth), 'signupId');
|
|
const verify = saveVariable(jsonRequest('Signup verify', 'POST', '/auth/signup/verify', {
|
|
signupId: '{{signupId}}', email: '{{email}}', code: '{{signupCode}}',
|
|
}, '200. Saves a temporary signupToken, not an access token. Enter the emailed six-digit signupCode.', noauth), 'signupToken');
|
|
const complete = {
|
|
name: 'Signup complete (Multipart)',
|
|
request: {
|
|
method: 'POST', auth: noauth, header: [], url: '{{baseUrl}}/auth/signup/complete',
|
|
description: '200. No Bearer required. Required: signupToken, name. Other fields optional. Repeated musicRoles[]/musicGenres[]/favoriteInstruments[]/favoriteMaqamat[] are accepted; enable only real selected values. avatarFile is optional, max 5 MiB. See MOBILE-BACKEND-HANDOFF.md.',
|
|
body: { mode: 'formdata', formdata: [
|
|
{ key: 'signupToken', value: '{{signupToken}}', type: 'text' },
|
|
{ key: 'name', value: 'Test Musician', type: 'text' },
|
|
...['stageName', 'username', 'bio', 'location', 'experienceLevel', 'latitude', 'longitude', 'isPrivate', 'musicRoles[]', 'musicGenres[]', 'favoriteInstruments[]', 'favoriteMaqamat[]'].map((key) => ({ key, value: '', type: 'text', disabled: true })),
|
|
{ key: 'avatarFile', type: 'file', src: '', disabled: true },
|
|
] },
|
|
},
|
|
event: [{ listen: 'test', script: { type: 'text/javascript', exec: [
|
|
'if (pm.response.code === 200) {',
|
|
' const json = pm.response.json(); const body = json.data ?? json;',
|
|
' for (const key of ["accessToken", "refreshToken"]) { pm.expect(body[key]).to.be.a("string").and.not.empty; pm.environment.set(key, body[key]); }',
|
|
'}',
|
|
] } }],
|
|
};
|
|
const drafts = {
|
|
name: 'Save draft (Multipart)', request: {
|
|
method: 'POST', auth: bearer, header: [], url: '{{baseUrl}}/posts/drafts',
|
|
description: '201. Saves a private draft in postdrafts; never publishes or notifies. CreatePostDto text fields are optional; empty drafts accepted. Same direct media field names/limits as posts. One media family. videoStorageKey is not accepted. No draft list/edit/publish API is claimed.',
|
|
body: { mode: 'formdata', formdata: [
|
|
{ key: 'content', value: 'Unfinished draft', type: 'text' },
|
|
{ key: 'type', value: 'text', type: 'text' },
|
|
{ key: 'isDraft', value: 'true', type: 'text' },
|
|
{ key: 'hashtags[]', value: 'music', type: 'text', disabled: true },
|
|
...['imageFiles', 'videoFile', 'audioFile', 'coverImageFile'].map((key) => ({ key, type: 'file', src: '', disabled: true })),
|
|
] },
|
|
},
|
|
};
|
|
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 folder = { name: 'Mobile compatibility and three-stage signup', item: [
|
|
start, verify, complete, drafts,
|
|
jsonRequest('Report options', 'GET', '/reports/options', null, '200. Object with target-specific reasons [{id,value,label,targetType,target}] and targetTypes. Submit reason=value to POST /reports. Labels are English; clients may localize by value.'),
|
|
jsonRequest('Remove follower', 'DELETE', '/users/{{followerId}}/followers', null, '200. followerId is the account to remove from the authenticated user followers, not the current user.'),
|
|
jsonRequest('Share to profile (existing mobile semantics)', 'POST', '/posts/{{postId}}/share', { target: 'profile', caption: 'Shared with my profile' }, '200. Records a profile share; response has shareId,postId,shareCount,isSharedByMe,shareUrl. Dedicated /share/profile creates a repost and is not an alias. Keep the existing mobile call.'),
|
|
jsonRequest('Invalid refresh token', 'POST', '/auth/refresh', { refreshToken: 'invalid-token-with-at-least-twenty-characters' }, 'Negative test: expects 401. Too-short values are rejected by DTO validation with 400.', noauth),
|
|
] };
|
|
const exampleNames = { 'Signup complete (Multipart)': 'Signup complete', 'Save draft (Multipart)': 'Save draft' };
|
|
for (const item of folder.item) {
|
|
const example = captured?.examples.find((entry) => entry.name === (exampleNames[item.name] ?? item.name));
|
|
if (example) item.response = [{
|
|
name: `${example.name} — local integration test (sanitized)`,
|
|
originalRequest: item.request,
|
|
status: example.status >= 400 ? 'Unauthorized' : example.status === 201 ? 'Created' : 'OK',
|
|
code: example.status,
|
|
header: [{ key: 'Content-Type', value: 'application/json' }],
|
|
_postman_previewlanguage: 'json',
|
|
body: JSON.stringify(example.response, null, 2),
|
|
}];
|
|
}
|
|
const index = collection.item.findIndex((item) => item.name === folder.name);
|
|
if (index < 0) collection.item.push(folder); else collection.item[index] = folder;
|
|
collection.variable ??= [];
|
|
for (const key of ['signupId', 'signupCode', 'signupToken', 'followerId']) {
|
|
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 compatibility contracts.');
|