Skip to content

Commit

Permalink
ZMS-85 (#495)
Browse files Browse the repository at this point in the history
* added new file to test git repo setup

* remove test.txt
Add api-tests-table.md file to keep track of api endpoints, method and their test count + test types (and their count)

* add js script to parse files in the /lib/api folder and retrieve a map of apiPath -> apiMethod

* delete parseApiFiles.js as it is unneeded

* add global (per test run) beforeEach and after hooks to collect test data and create an overview table

* update tests to conform to new test title structure

* add expectations to tests

* remove old api-tests-table.md that was handmade

* add expectations to tests in the api-test.js file

* add GET endpoint /api-methods/:arg to fetch all api endpoints for testing purposes only

* add and fix and refactor _globals-test.js file in the api test/api folder. Use global beforeEach and after hook to generate test overview table in api-test-overview.md file

* add missing newline at the end of file

* fix with prettier

* api.js fix style, remove unnecessary comments, remove :arg

* make first post,put,delete,get regex case insensitive, fix call to /api-methods

* _globals-test.js fixes

* fix some test titles

---------

Co-authored-by: Nikolai Ovtsinnikov <[email protected]>
  • Loading branch information
NickOvt and NickOvt authored Sep 14, 2023
1 parent 1c17f5f commit c60373b
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 108 deletions.
11 changes: 11 additions & 0 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,17 @@ module.exports = done => {
webhooksRoutes(db, server);
settingsRoutes(db, server, settingsHandler);

if (process.env.NODE_ENV === 'test') {
server.get(
{ name: 'api-methods', path: '/api-methods' },
tools.responseWrapper(async (req, res) => {
res.charSet('utf-8');

return res.json(server.router.getRoutes());
})
);
}

server.on('error', err => {
if (!started) {
started = true;
Expand Down
104 changes: 104 additions & 0 deletions test/_globals-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
'use strict';

const supertest = require('supertest');
const fs = require('fs');

const config = require('wild-config');

const server = supertest.agent(`http://127.0.0.1:${config.api.port}`);

const titles = [];
const unsupportedTitles = [];

// global beforeEach to run before EVERY test
beforeEach('Get test data before each test', async function () {
const currentTestTitle = this.test.ctx.currentTest.title; // eslint-disable-line no-invalid-this
if (/\b(POST|PUT|DELETE|GET)\b/i.test(currentTestTitle) && /success|failure/.test(currentTestTitle)) {
titles.push(currentTestTitle);
} else {
unsupportedTitles.push(currentTestTitle);
}
});

// eslint-disable-next-line no-undef
after('Generate test overview table after all tests', async () => {
const data = await server.get('/api-methods');

const routes = data.body;

const mapApiMethodToSpec = {};
let content = '| API path | API method | Test count | Has positive test? | Has Negative test? |\n';
content += '| --- | :---: | --- | --- | --- | \n';

for (const routeName in routes) {
const route = routes[routeName];
const method = route.spec.method;
const path = route.spec.path;

mapApiMethodToSpec[`${method.toLowerCase()}_${path}`] = {
method,
path,
name: route.spec.name || route.name,
testCount: 0,
positiveTestCount: 0,
negativeTestCount: 0
};
}

for (const title of titles) {
const titleSplit = title.split(/\s+/);
const method = titleSplit[1].toLowerCase();
const path = titleSplit[2];
const expectedResult = titleSplit[4];

// missing method or path (string is too short to be accepted as valid test title)
if (!method || !path) {
continue;
}

const data = mapApiMethodToSpec[`${method}_${path.replace(/{/g, ':').replace(/}/g, '')}`];

// wrong path or wrong data etc. (no such route, can't construct route from test title)
if (!data) {
unsupportedTitles.push(title);
continue;
}

data.testCount++;
if (expectedResult) {
if (expectedResult === 'success') {
data.positiveTestCount++;
} else if (expectedResult === 'failure') {
data.negativeTestCount++;
}
}
}

const sortedData = Object.values(mapApiMethodToSpec).sort((a, b) => {
// 1) sort by test count
// 2) sort by path

if (a.testCount < b.testCount) {
return 1;
} else if (a.testCount > b.testCount) {
return -1;
} else if (a.path > b.path) {
return 1;
} else if (a.path < b.path) {
return -1;
} else {
return 0;
}
});

for (const data of sortedData) {
content += `| \`${data.path}\` | \`${data.method}\` | ${data.testCount} | ${data.positiveTestCount > 0 ? '✅' : '❌'} (${data.positiveTestCount}) | ${
data.negativeTestCount > 0 ? '✅' : '❌'
} (${data.negativeTestCount}) |\n`;
}

console.log(__dirname);
await fs.promises.writeFile(__dirname + '/../api-tests-overview.md', content);

console.log('These titles were not included in the overview as they are wrong format:', unsupportedTitles);
});
Loading

0 comments on commit c60373b

Please sign in to comment.