134 lines
3.0 KiB
JavaScript
134 lines
3.0 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
const { createApp } = require('../src/server');
|
|
|
|
function createResponseInfo() {
|
|
return {
|
|
code: 0,
|
|
message: 'success',
|
|
data: {
|
|
reportId: 'report-001',
|
|
status: 'SUCCESS',
|
|
},
|
|
};
|
|
}
|
|
|
|
async function withServer(repository, callback) {
|
|
const app = createApp({
|
|
repository,
|
|
allowedOrigin: 'https://yuntu.oceanengine.com',
|
|
});
|
|
|
|
const server = await new Promise((resolve) => {
|
|
const instance = app.listen(0, '127.0.0.1', () => resolve(instance));
|
|
});
|
|
|
|
const address = server.address();
|
|
const baseUrl = `http://127.0.0.1:${address.port}`;
|
|
|
|
try {
|
|
await callback(baseUrl);
|
|
} finally {
|
|
await new Promise((resolve, reject) => {
|
|
server.close((error) => {
|
|
if (error) {
|
|
reject(error);
|
|
return;
|
|
}
|
|
|
|
resolve();
|
|
});
|
|
});
|
|
}
|
|
}
|
|
|
|
test('GET /health returns ok status', async () => {
|
|
await withServer(
|
|
{
|
|
async save() {
|
|
throw new Error('save should not be called');
|
|
},
|
|
},
|
|
async (baseUrl) => {
|
|
const response = await fetch(`${baseUrl}/health`);
|
|
const body = await response.json();
|
|
|
|
assert.equal(response.status, 200);
|
|
assert.deepEqual(body, {
|
|
success: true,
|
|
data: {
|
|
status: 'ok',
|
|
},
|
|
});
|
|
},
|
|
);
|
|
});
|
|
|
|
test('POST /api/reports returns 400 when response info does not contain a report id', async () => {
|
|
await withServer(
|
|
{
|
|
async save() {
|
|
throw new Error('save should not be called');
|
|
},
|
|
},
|
|
async (baseUrl) => {
|
|
const response = await fetch(`${baseUrl}/api/reports`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
},
|
|
body: JSON.stringify({
|
|
code: 0,
|
|
data: {},
|
|
}),
|
|
});
|
|
const body = await response.json();
|
|
|
|
assert.equal(response.status, 400);
|
|
assert.equal(body.success, false);
|
|
assert.equal(body.error.code, 'VALIDATION_ERROR');
|
|
},
|
|
);
|
|
});
|
|
|
|
test('POST /api/reports persists raw response info and returns created response', async () => {
|
|
let savedRecord = null;
|
|
|
|
await withServer(
|
|
{
|
|
async save(record) {
|
|
savedRecord = record;
|
|
return {
|
|
reportId: record.report_id,
|
|
created: true,
|
|
};
|
|
},
|
|
},
|
|
async (baseUrl) => {
|
|
const responseInfo = createResponseInfo();
|
|
const response = await fetch(`${baseUrl}/api/reports`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'content-type': 'application/json',
|
|
},
|
|
body: JSON.stringify(responseInfo),
|
|
});
|
|
const body = await response.json();
|
|
|
|
assert.equal(response.status, 201);
|
|
assert.deepEqual(body, {
|
|
success: true,
|
|
data: {
|
|
reportId: 'report-001',
|
|
created: true,
|
|
},
|
|
});
|
|
assert.deepEqual(savedRecord, {
|
|
report_id: 'report-001',
|
|
report_info: responseInfo,
|
|
});
|
|
},
|
|
);
|
|
});
|