@mimik/be-project-builder 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +94 -0
- package/eslint.config.js +71 -0
- package/index.js +206 -0
- package/lib/buildfile.js +290 -0
- package/lib/common.js +13 -0
- package/lib/configFiles.js +90 -0
- package/lib/helpers.js +311 -0
- package/lib/readFileSync.js +69 -0
- package/lib/scaffold.js +108 -0
- package/lib/writeFileSync.js +70 -0
- package/package.json +37 -0
- package/scaffoldFiles/local/commitMsgCheck.txt +3 -0
- package/scaffoldFiles/local/dotFiles.txt +3 -0
- package/scaffoldFiles/local/jsdoc.json +6 -0
- package/scaffoldFiles/local/scripts.txt +3 -0
- package/scaffoldFiles/local/setup.txt +3 -0
- package/scaffoldFiles/local/start-example.json +37 -0
- package/scaffoldFiles/local/testSetup.txt +3 -0
- package/scaffoldFiles/local/unScripts.txt +3 -0
- package/scaffoldFiles/package.json +51 -0
- package/scaffoldFiles/src/common.txt +16 -0
- package/scaffoldFiles/src/dbValidateHelper.txt +4 -0
- package/scaffoldFiles/src/index.txt +34 -0
- package/scaffoldFiles/src/infoController.txt +34 -0
- package/scaffoldFiles/src/infoProcessor.txt +26 -0
- package/scaffoldFiles/src/mustacheConfig.txt +57 -0
- package/scaffoldFiles/src/mustacheController.txt +20 -0
- package/scaffoldFiles/test/mustacheCommon.txt +44 -0
- package/scaffoldFiles/test/mustacheHttp-test-detached.txt +34 -0
- package/scaffoldFiles/test/mustacheHttp-test-normal.txt +36 -0
- package/scaffoldFiles/test/set-env.txt +7 -0
- package/scaffoldFiles/test/systemEndpoints.txt +87 -0
- package/scaffoldFiles/test/util.txt +36 -0
- package/test.sh +4 -0
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mimik/be-project-builder",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Project builder for mimik back end service ",
|
|
5
|
+
"main": "./index.js",
|
|
6
|
+
"bin": {
|
|
7
|
+
"be-project-builder": "./index.js"
|
|
8
|
+
},
|
|
9
|
+
"type": "module",
|
|
10
|
+
"engines": {
|
|
11
|
+
"node": ">=24.0.0"
|
|
12
|
+
},
|
|
13
|
+
"scripts": {
|
|
14
|
+
"lint": "eslint . --no-error-on-unmatched-pattern"
|
|
15
|
+
},
|
|
16
|
+
"dependencies": {
|
|
17
|
+
"@mimik/request-retry": "^4.0.6",
|
|
18
|
+
"ansi-colors": "4.1.3",
|
|
19
|
+
"comment-json": "4.5.1",
|
|
20
|
+
"dotenv": "17.2.3",
|
|
21
|
+
"js-base64": "3.7.8",
|
|
22
|
+
"js-yaml": "4.1.1",
|
|
23
|
+
"mustache": "4.2.0",
|
|
24
|
+
"prompts": "^2.4.2",
|
|
25
|
+
"swagger-client": "3.36.0",
|
|
26
|
+
"yaml": "2.8.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@eslint/js": "9.39.2",
|
|
30
|
+
"@mimik/eslint-plugin-document-env": "^2.0.8",
|
|
31
|
+
"@stylistic/eslint-plugin": "5.7.1",
|
|
32
|
+
"eslint-plugin-import": "2.32.0",
|
|
33
|
+
"globals": "17.1.0",
|
|
34
|
+
"husky": "9.1.7",
|
|
35
|
+
"jsdoc-to-markdown": "9.1.3"
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
// parameters defintiion to set are from:
|
|
3
|
+
// - ./src/configuration/config.js: for the custom parameters
|
|
4
|
+
// - @mimik/configuration: for the general parameters
|
|
5
|
+
// -- service description
|
|
6
|
+
"SERVER_PORT": 8080,
|
|
7
|
+
// "SERVER_ID": "an ID",
|
|
8
|
+
// -- server public address
|
|
9
|
+
"SERVER_PUBLIC_DOMAIN_NAME": "a domain name",
|
|
10
|
+
"SERVER_PUBLIC_PROTOCOL": "http:",
|
|
11
|
+
// -- logs
|
|
12
|
+
"LOG_LEVEL": "debug",
|
|
13
|
+
"CONSOLE_LEVEL": "debug",
|
|
14
|
+
"NO_STACK": "yes",
|
|
15
|
+
"LOG_MODE": "none",
|
|
16
|
+
// "LOG_MODE": "awsS3, awsKinesis, sumologic, all, none",
|
|
17
|
+
// "EXIT_DELAY: 2000,
|
|
18
|
+
// -- providers
|
|
19
|
+
// "locationNeeded": "yes",
|
|
20
|
+
// "CLOUD_PROVIDER": "AWS",
|
|
21
|
+
// // -- database
|
|
22
|
+
// TODO: add database parameters here
|
|
23
|
+
// "DATABASE_NAME": "a name",
|
|
24
|
+
// -- specific parameters
|
|
25
|
+
// TODO: add specific parameters here
|
|
26
|
+
// -- security
|
|
27
|
+
// "SERVER_SECURITY_SET": "off",
|
|
28
|
+
// -- dependencies, the audiences are generated by mST
|
|
29
|
+
// "mIDNeeded": "yes",
|
|
30
|
+
// TODO: add dependencies (except mID) as a base URL (xxx_URL) here
|
|
31
|
+
// EXAMPLE:
|
|
32
|
+
// "MSS_URL": "http://localhost:8070/mSS/v1",
|
|
33
|
+
// -- environment
|
|
34
|
+
// "REGISTRATION_SET: "off",
|
|
35
|
+
// "REGISTRATION_RETRY": 200000,
|
|
36
|
+
"NODE_ENV": "local"
|
|
37
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": "1.0.0",
|
|
3
|
+
"main": "./src/index.js",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"postinstall": "if [ \"$NODE_ENV\" != \"production\" ] && [ \"$NODE_ENV\" != \"prod\" ]\nthen\nnode ./local/scripts.js && node ./local/dotFiles.js\nfi",
|
|
7
|
+
"lint": "eslint . --no-error-on-unmatched-pattern",
|
|
8
|
+
"docs": "jsdoc2md ./src/configuration/config.js > README.md",
|
|
9
|
+
"pretest": "node ./local/testSetup.js",
|
|
10
|
+
"test": "mocha --reporter mochawesome --bail --exit --file test/src/set-env.js test/normal/",
|
|
11
|
+
"test-old": "mocha --reporter mochawesome --bail --exit test/",
|
|
12
|
+
"test-ci": "c8 --reporter=lcov --reporter=text npm test --exit",
|
|
13
|
+
"test-detached": "node ./local/testSetup.js && mocha --reporter mochawesome --bail --exit --file test/src/set-env.js test/detached/",
|
|
14
|
+
"prestart": "node ./local/setup.js",
|
|
15
|
+
"start": "sh server-start.sh",
|
|
16
|
+
"pre-push": "npm run docs && npm run lint && npm run test",
|
|
17
|
+
"pre-commit": "npm run docs && npm run lint",
|
|
18
|
+
"prepare": "if [ \"$NODE_ENV\" != \"production\" ] && [ \"$NODE_ENV\" != \"prod\" ] \nthen\nhusky\nfi"
|
|
19
|
+
},
|
|
20
|
+
"husky": {
|
|
21
|
+
"hooks": {
|
|
22
|
+
"pre-commit": "npm run pre-commit",
|
|
23
|
+
"pre-push": "npm run pre-push",
|
|
24
|
+
"commit-msg": "node ./local/commitMsgCheck $1"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"keywords": [
|
|
28
|
+
"swagger",
|
|
29
|
+
"mimik"
|
|
30
|
+
],
|
|
31
|
+
"license": "MIT",
|
|
32
|
+
"author": "mimik",
|
|
33
|
+
"private": true,
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@mimik/cluster": "5.0.1",
|
|
36
|
+
"@mimik/configuration": "6.0.6",
|
|
37
|
+
"@mimik/init": "6.0.2",
|
|
38
|
+
"@mimik/metrics-helper": "2.0.1",
|
|
39
|
+
"@mimik/response-helper": "4.0.4",
|
|
40
|
+
"@mimik/sumologic-winston-logger": "2.1.5",
|
|
41
|
+
"@mimik/swagger-helper": "5.0.2",
|
|
42
|
+
"@mimik/systeminfo": "4.0.1",
|
|
43
|
+
"express": "5.1.0"
|
|
44
|
+
},
|
|
45
|
+
"devDependencies": {
|
|
46
|
+
"@mimik/local": "7.1.2",
|
|
47
|
+
"@mimik/request-helper": "2.0.2",
|
|
48
|
+
"@mimik/test-helper": "3.0.3",
|
|
49
|
+
"body-parser": "2.2.0"
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/* eslint-disable no-magic-numbers */
|
|
2
|
+
const GET = 'get';
|
|
3
|
+
const POST = 'post';
|
|
4
|
+
const DELETE = 'delete';
|
|
5
|
+
const PUT = 'put';
|
|
6
|
+
|
|
7
|
+
const DECIMAL = 10;
|
|
8
|
+
// TODO: add other constants here
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
DECIMAL,
|
|
12
|
+
DELETE,
|
|
13
|
+
GET,
|
|
14
|
+
POST,
|
|
15
|
+
PUT,
|
|
16
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { APIRequestDuration, HTTPRequestDuration, register } from '@mimik/metrics-helper';
|
|
2
|
+
import cluster from '@mimik/cluster';
|
|
3
|
+
// import compression from 'compression'; // TODO: to be added if compression is needed. compression needs to added as a dependency in package.json
|
|
4
|
+
import config from './configuration/config.js';
|
|
5
|
+
// import { dbValidate } from './lib/dbValidateHelper.js'; // TODO: to be added if a database is used
|
|
6
|
+
import { dirname } from 'path';
|
|
7
|
+
import express from 'express';
|
|
8
|
+
import { fileURLToPath } from 'url';
|
|
9
|
+
import init from '@mimik/init';
|
|
10
|
+
|
|
11
|
+
const databaseOps = [];
|
|
12
|
+
// dababaseOps.push(dbValidation) // TODO: to be added if a database is used
|
|
13
|
+
|
|
14
|
+
const localDirname = dirname(fileURLToPath(import.meta.url));
|
|
15
|
+
const app = express();
|
|
16
|
+
|
|
17
|
+
app.use(express.json());
|
|
18
|
+
// app.use(compression()); // TODO: to be added if compression is needed, if the requestor supports: Accept-Encoding: gzip
|
|
19
|
+
|
|
20
|
+
const options = {
|
|
21
|
+
metrics: { APIRequestDuration, HTTPRequestDuration, register },
|
|
22
|
+
// TODO: add specific format here
|
|
23
|
+
// EXAMPLE:
|
|
24
|
+
// formats: {
|
|
25
|
+
// base64: {
|
|
26
|
+
// type: 'string',
|
|
27
|
+
// validate: /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/][AQgw]==|[A-Za-z0-9+/]{2}[AEIMQUYcgkosw048]=)?$/u,
|
|
28
|
+
// },
|
|
29
|
+
// },
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
init(app, localDirname, config, databaseOps, cluster(config, { HTTPRequestDuration }), options);
|
|
33
|
+
|
|
34
|
+
export default app;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { RESPONSE_CODE, getRichError, sendError, sendResult } from '@mimik/response-helper';
|
|
2
|
+
import { convertParams } from '@mimik/swagger-helper';
|
|
3
|
+
import processor from '../processors/infoProcessor.js';
|
|
4
|
+
import { register } from '@mimik/metrics-helper';
|
|
5
|
+
|
|
6
|
+
const getInfos = (con, req, res) => {
|
|
7
|
+
const options = convertParams(con);
|
|
8
|
+
const { types } = options;
|
|
9
|
+
|
|
10
|
+
if (!types || types.length === 0) {
|
|
11
|
+
sendError(getRichError('Parameter', 'types must have at least 1 item'), res, null, options);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
processor.getInfos(types, options.correlationId)
|
|
16
|
+
.then(result => sendResult(result, RESPONSE_CODE.OK, res, options))
|
|
17
|
+
.catch(err => sendError(err, res, null, options));
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const getMetrics = (con, req, res) => {
|
|
21
|
+
const options = convertParams(con, 'silly');
|
|
22
|
+
|
|
23
|
+
return register.metrics()
|
|
24
|
+
.then((result) => {
|
|
25
|
+
res.setHeader('Content-Type', register.contentType);
|
|
26
|
+
res.end(result);
|
|
27
|
+
})
|
|
28
|
+
.catch(err => sendError(err, res, null, options));
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export {
|
|
32
|
+
getInfos,
|
|
33
|
+
getMetrics,
|
|
34
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { filterConfig, healthInfo } from '@mimik/systeminfo';
|
|
2
|
+
import Promise from 'bluebird';
|
|
3
|
+
import config from '../configuration/config.js';
|
|
4
|
+
|
|
5
|
+
const getInfos = (options, correlationId) => {
|
|
6
|
+
const response = { data: {} };
|
|
7
|
+
|
|
8
|
+
return Promise.map(options, (option) => {
|
|
9
|
+
// TODO: add the specific options here
|
|
10
|
+
if (option === 'config') {
|
|
11
|
+
if (!response.data.system) response.data.system = {};
|
|
12
|
+
response.data.system.config = filterConfig(config);
|
|
13
|
+
return Promise.resolve(null);
|
|
14
|
+
}
|
|
15
|
+
if (option === 'healthCheck') {
|
|
16
|
+
if (!response.data.system) response.data.system = {};
|
|
17
|
+
response.data.system.healthCheck = healthInfo();
|
|
18
|
+
return Promise.resolve(null);
|
|
19
|
+
}
|
|
20
|
+
return Promise.resolve(null);
|
|
21
|
+
}).then(() => response);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export default {
|
|
25
|
+
getInfos,
|
|
26
|
+
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/* eslint-disable no-process-env */
|
|
2
|
+
import { DECIMAL } from '../lib/common.js';
|
|
3
|
+
import { URL } from 'url';
|
|
4
|
+
import process from 'process';
|
|
5
|
+
import { readFile } from 'fs/promises';
|
|
6
|
+
import { setConfig } from '@mimik/configuration';
|
|
7
|
+
|
|
8
|
+
const pack = JSON.parse(await readFile(new URL('../../package.json', import.meta.url)));
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* {{serviceType}} Configuration.
|
|
13
|
+
*
|
|
14
|
+
* @function config
|
|
15
|
+
* @returns {Object} configuration - Server configuration.
|
|
16
|
+
* @description The following environment variables are required to configure {{serviceType}}:
|
|
17
|
+
*
|
|
18
|
+
* | Env variable name | Description | Default | Comments |
|
|
19
|
+
* | ----------------- | ----------- | ------- | -------- |
|
|
20
|
+
* TODO: add the environment variables used here
|
|
21
|
+
*
|
|
22
|
+
* These values are in addition to those required by the
|
|
23
|
+
* [configuration](https://bitbucket.org/mimiktech/configuration) library.
|
|
24
|
+
*
|
|
25
|
+
* The API is available on
|
|
26
|
+
* [SwaggerHub](https://app.swaggerhub.com/apis/mimik/{{serviceType}}).
|
|
27
|
+
*/
|
|
28
|
+
export const conf = (() => {
|
|
29
|
+
const configuration = setConfig(pack, {
|
|
30
|
+
// TODO: to add if a database is used
|
|
31
|
+
// database: {
|
|
32
|
+
// type: 'mongodb',
|
|
33
|
+
// table: {
|
|
34
|
+
// TODO: add the mongo table here
|
|
35
|
+
// EXAMPLE:
|
|
36
|
+
// node: process.env.NODE_TABLE || 'a table name',
|
|
37
|
+
// },
|
|
38
|
+
},
|
|
39
|
+
// TODO: add dependencies if any here
|
|
40
|
+
// EXAMPLE:
|
|
41
|
+
// dependencies: {
|
|
42
|
+
// mSS: {
|
|
43
|
+
// url: process.env.MSS_URL,
|
|
44
|
+
// audience: process.env.MSS_AUDIENCE,
|
|
45
|
+
// },
|
|
46
|
+
// }
|
|
47
|
+
custom: {
|
|
48
|
+
// TODO: add the custom properties here
|
|
49
|
+
// EXAMPLE:
|
|
50
|
+
// searchRadius: parseFloat(process.env.SEARCH_RADIUS, DECIMAL) || 'a search radius number',
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return configuration;
|
|
55
|
+
})();
|
|
56
|
+
|
|
57
|
+
export default conf;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ERROR_CODE,
|
|
3
|
+
RESPONSE_CODE,
|
|
4
|
+
getRichError,
|
|
5
|
+
sendError,
|
|
6
|
+
sendResult,
|
|
7
|
+
} from '@mimik/response-helper';
|
|
8
|
+
import { convertParams } from '@mimik/swagger-helper';
|
|
9
|
+
|
|
10
|
+
{{#functions}}
|
|
11
|
+
const {{name}} = (con, req, res) => {
|
|
12
|
+
const options = convertParams(con);
|
|
13
|
+
// TODO: add controller logic here
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
{{/functions}}
|
|
17
|
+
export {
|
|
18
|
+
{{#functions}}{{name}}{{^last}},
|
|
19
|
+
{{/last}}{{/functions}},
|
|
20
|
+
};
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/* eslint-disable no-process-env, processDoc/validate-document-env */
|
|
2
|
+
import { SET_ON } from '../../src/lib/common.js';
|
|
3
|
+
import config from '../../src/configuration/config.js';
|
|
4
|
+
import process from 'process';
|
|
5
|
+
|
|
6
|
+
const getTestHelperConf = (app, isLocal, env) => {
|
|
7
|
+
const { serverSettings } = config;
|
|
8
|
+
const basePath = process.env.BASE_PATH;
|
|
9
|
+
const audience = `${serverSettings.public.protocol}//${serverSettings.public.domainName}${basePath}/clients/Generic-{{serviceType}}`;
|
|
10
|
+
|
|
11
|
+
// forcing security to be on
|
|
12
|
+
config.serverSettings.securitySet = SET_ON;
|
|
13
|
+
|
|
14
|
+
const fixtures = {
|
|
15
|
+
APP: app,
|
|
16
|
+
BASE_PATH: basePath,
|
|
17
|
+
CUSTOMER_NAME: env.CUSTOMER_NAME,
|
|
18
|
+
ENVIRONMENT: env.NODE_ENV,
|
|
19
|
+
IS_LOCAL: isLocal,
|
|
20
|
+
// TODO: add the specific fixtures here
|
|
21
|
+
{{SERVICE_TYPE}}_AUDIENCE: audience,
|
|
22
|
+
{{SERVICE_TYPE}}_TOKEN: env.{{SERVICE_TYPE}}_TOKEN,
|
|
23
|
+
{{SERVICE_TYPE}}_URL: env.{{SERVICE_TYPE}}_URL,
|
|
24
|
+
MIT_URL: env.MIT_URL,
|
|
25
|
+
MST_TOKEN: env.MST_TOKEN,
|
|
26
|
+
MST_URL: env.MST_URL,
|
|
27
|
+
USE_MOCK_PROFILES: env.USE_MOCK_PROFILES === 'yes',
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
return {
|
|
31
|
+
fixtures,
|
|
32
|
+
serviceConfiguration: config,
|
|
33
|
+
services: {
|
|
34
|
+
MIT: 'mIT',
|
|
35
|
+
MST: 'mST',
|
|
36
|
+
{{SERVICE_TYPE}}: '{{serviceType}}',
|
|
37
|
+
// TODO: add the specific services here
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export {
|
|
43
|
+
getTestHelperConf,
|
|
44
|
+
};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/* eslint-disable no-process-env, processDoc/validate-document-env */
|
|
2
|
+
import { importTest, runTests } from '@mimik/test-helper';
|
|
3
|
+
import { API_TEST_TIMEOUT } from '../../src/lib/common.js';
|
|
4
|
+
import { getTestHelperConf } from '../src/common.js';
|
|
5
|
+
import process from 'process';
|
|
6
|
+
import { readFileSync } from 'fs';
|
|
7
|
+
import { resolve } from 'path';
|
|
8
|
+
import { testJsonFile } from '@mimik/local';
|
|
9
|
+
|
|
10
|
+
const raw = readFileSync(`${testJsonFile}`, 'utf-8');
|
|
11
|
+
const testEnv = JSON.parse(raw);
|
|
12
|
+
|
|
13
|
+
const systemEndpoints = resolve('./test/src/systemEndpoints.js');
|
|
14
|
+
// TODO: add the test filenames here
|
|
15
|
+
|
|
16
|
+
const tests = () => {
|
|
17
|
+
describe('RUNNING API TEST FOR {{SERVICE_TYPE}} SERVICE (DETACHED MODE)', function apiTest() {
|
|
18
|
+
// eslint-disable-next-line no-invalid-this
|
|
19
|
+
this.timeout(API_TEST_TIMEOUT);
|
|
20
|
+
|
|
21
|
+
importTest('System Endpoint Tests', systemEndpoints);
|
|
22
|
+
// TODO: add the importTests here
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
const main = () => {
|
|
26
|
+
const isLocal = process.env.NODE_ENV === 'local';
|
|
27
|
+
const env = isLocal ? testEnv : process.env;
|
|
28
|
+
const app = isLocal ? `http://localhost:${env.SERVER_PORT}` : env.SERVER_URL;
|
|
29
|
+
const testHelperConfig = getTestHelperConf(app, isLocal, env);
|
|
30
|
+
|
|
31
|
+
runTests(testHelperConfig, tests);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
main();
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/* eslint-disable no-console, no-process-env, processDoc/validate-document-env */
|
|
2
|
+
import { importTest, runTests } from '@mimik/test-helper';
|
|
3
|
+
import { API_TEST_TIMEOUT } from '../../src/lib/common.js';
|
|
4
|
+
import app from '../../src/index.js';
|
|
5
|
+
import { getTestHelperConf } from '../src/common.js';
|
|
6
|
+
import process from 'process';
|
|
7
|
+
import { readFileSync } from 'fs';
|
|
8
|
+
import { resolve } from 'path';
|
|
9
|
+
import { testJsonFile } from '@mimik/local';
|
|
10
|
+
|
|
11
|
+
const raw = readFileSync(`${testJsonFile}`, 'utf-8');
|
|
12
|
+
const testEnv = JSON.parse(raw);
|
|
13
|
+
|
|
14
|
+
const systemEndpoints = resolve('./test/src/systemEndpoints.js');
|
|
15
|
+
// TODO: add the test filenames here
|
|
16
|
+
|
|
17
|
+
const tests = () => {
|
|
18
|
+
describe('RUNNING API TEST FOR {{SERVICE_TYPE}} SERVICE', function apiTest() {
|
|
19
|
+
// eslint-disable-next-line no-invalid-this
|
|
20
|
+
this.timeout(API_TEST_TIMEOUT);
|
|
21
|
+
|
|
22
|
+
importTest('System Endpoint Tests', systemEndpoints);
|
|
23
|
+
// TODO: add the importTests here
|
|
24
|
+
});
|
|
25
|
+
};
|
|
26
|
+
const main = async () => {
|
|
27
|
+
const isLocal = process.env.NODE_ENV === 'local';
|
|
28
|
+
const env = isLocal ? testEnv : process.env;
|
|
29
|
+
const testHelperConfig = await getTestHelperConf(app, isLocal, env);
|
|
30
|
+
|
|
31
|
+
runTests(testHelperConfig, tests);
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
main().catch((err) => {
|
|
35
|
+
console.error('Failed to run tests:', err);
|
|
36
|
+
});
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
/* eslint-disable no-process-env, processDoc/validate-document-env, import/no-extraneous-dependencies */
|
|
2
|
+
import { ERROR_CODE, RESPONSE_CODE } from '@mimik/response-helper';
|
|
3
|
+
import {
|
|
4
|
+
ROLES,
|
|
5
|
+
SERVICES,
|
|
6
|
+
getFixture,
|
|
7
|
+
getToken,
|
|
8
|
+
prepTokens,
|
|
9
|
+
} from '@mimik/test-helper';
|
|
10
|
+
import { base, baseAuth, basic } from './util.js';
|
|
11
|
+
import { GET } from '../../src/lib/common.js';
|
|
12
|
+
|
|
13
|
+
const TIMEOUT = getFixture('mochaSettings').timeout;
|
|
14
|
+
const types = 'types=config&types=healthCheck'; // TODO: add the specific types here
|
|
15
|
+
const adminToken = { role: ROLES.ADMIN, destType: SERVICES.{{SERVICE_TYPE}} };
|
|
16
|
+
// TODO: create system token here
|
|
17
|
+
// EXAMPLE:
|
|
18
|
+
// const systemToken = { role: ROLES.SYSTEM, origType: SERVICES.MST, destType: SERVICES.{{SERVICE_TYPE}} };
|
|
19
|
+
|
|
20
|
+
describe('Healthcheck, Metrics and Info Tests', function APITest() {
|
|
21
|
+
this.timeout(TIMEOUT);
|
|
22
|
+
// eslint-disable-next-line no-undef
|
|
23
|
+
before(async () => {
|
|
24
|
+
await prepTokens([adminToken, systemToken]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('should respond with 200 - get healthcheck', (done) => {
|
|
28
|
+
basic(GET, '/healthcheck')
|
|
29
|
+
.expect(RESPONSE_CODE.OK)
|
|
30
|
+
.end((err) => {
|
|
31
|
+
if (err) return done(err);
|
|
32
|
+
return done();
|
|
33
|
+
});
|
|
34
|
+
}).timeout(TIMEOUT);
|
|
35
|
+
|
|
36
|
+
it('should respond with 200 - get metrics', (done) => {
|
|
37
|
+
basic(GET, '/metrics')
|
|
38
|
+
.expect(RESPONSE_CODE.OK)
|
|
39
|
+
.end((err) => {
|
|
40
|
+
if (err) return done(err);
|
|
41
|
+
return done();
|
|
42
|
+
});
|
|
43
|
+
}).timeout(TIMEOUT);
|
|
44
|
+
|
|
45
|
+
it('should respond with 200 - get info (admin security)', (done) => {
|
|
46
|
+
baseAuth(GET, `/info?${types}`, getToken(adminToken))
|
|
47
|
+
.expect(RESPONSE_CODE.OK)
|
|
48
|
+
.end((err) => {
|
|
49
|
+
if (err) return done(err);
|
|
50
|
+
return done();
|
|
51
|
+
});
|
|
52
|
+
}).timeout(TIMEOUT);
|
|
53
|
+
|
|
54
|
+
it('should respond with 400 - empty types', (done) => {
|
|
55
|
+
baseAuth(GET, '/info', getToken(adminToken))
|
|
56
|
+
.query({ types: [] })
|
|
57
|
+
.expect(ERROR_CODE.PARAMETER)
|
|
58
|
+
.end(done);
|
|
59
|
+
}).timeout(TIMEOUT);
|
|
60
|
+
|
|
61
|
+
it('should respond with 400 - invalid types', (done) => {
|
|
62
|
+
baseAuth(GET, '/info', getToken(adminToken))
|
|
63
|
+
.query({ types: ['xyz'] })
|
|
64
|
+
.expect(ERROR_CODE.PARAMETER)
|
|
65
|
+
.end(done);
|
|
66
|
+
}).timeout(TIMEOUT);
|
|
67
|
+
|
|
68
|
+
it('should respond with 400 - get info with the wrong way to pass parameters', (done) => {
|
|
69
|
+
baseAuth(GET, '/info?types=healthCheck,config', getToken(adminToken))
|
|
70
|
+
.expect(ERROR_CODE.PARAMETER)
|
|
71
|
+
.end(done);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('should respond with 401 - no auth', (done) => {
|
|
75
|
+
base(GET, '/info')
|
|
76
|
+
.query({ types: ['healthCheck'] })
|
|
77
|
+
.expect(ERROR_CODE.UNAUTHORIZED)
|
|
78
|
+
.end(done);
|
|
79
|
+
}).timeout(TIMEOUT);
|
|
80
|
+
|
|
81
|
+
it('should respond with 403 - System Security', (done) => {
|
|
82
|
+
baseAuth(GET, '/info', getToken(systemToken))
|
|
83
|
+
.query({ types: ['healthCheck'] })
|
|
84
|
+
.expect(ERROR_CODE.FORBIDDEN)
|
|
85
|
+
.end(done);
|
|
86
|
+
}).timeout(TIMEOUT);
|
|
87
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/* eslint-disable no-process-env, processDoc/validate-document-env, import/no-extraneous-dependencies */
|
|
2
|
+
import { GENERIC_HEADER, getFixture } from '@mimik/test-helper';
|
|
3
|
+
import { expect } from 'chai';
|
|
4
|
+
import process from 'node:process';
|
|
5
|
+
import supertest from 'supertest';
|
|
6
|
+
|
|
7
|
+
const BASE_PATH = process.env.BASE_PATH || getFixture.get('BASE_PATH');
|
|
8
|
+
const app = getFixture('APP');
|
|
9
|
+
|
|
10
|
+
const basic = (method, path) => supertest(app)[method](path).set(GENERIC_HEADER);
|
|
11
|
+
const base = (method, path) => supertest(app)[method](`${BASE_PATH}${path}`).set(GENERIC_HEADER);
|
|
12
|
+
const baseAuth = (method, path, token) => base(method, path).set(GENERIC_HEADER).set('Authorization', token);
|
|
13
|
+
const handleError = (err, res, statusCode, message, cb) => {
|
|
14
|
+
if (err) return cb(err);
|
|
15
|
+
expect(res.body).to.be.an('object');
|
|
16
|
+
expect(res.body.statusCode).to.equal(statusCode);
|
|
17
|
+
if (message) expect(res.body.message).to.include(message);
|
|
18
|
+
return cb();
|
|
19
|
+
};
|
|
20
|
+
const audience = (type, audiences) => {
|
|
21
|
+
let result;
|
|
22
|
+
|
|
23
|
+
audiences.forEach((item) => {
|
|
24
|
+
if (item.type === type) result = item.audience;
|
|
25
|
+
});
|
|
26
|
+
return result;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export {
|
|
30
|
+
audience,
|
|
31
|
+
basic,
|
|
32
|
+
base,
|
|
33
|
+
baseAuth,
|
|
34
|
+
handleError,
|
|
35
|
+
expect,
|
|
36
|
+
};
|
package/test.sh
ADDED