Our team has built an API for providing internal web services to our organization. I have built a mocha test suite for testing the code base, and am currently running it via the CLI.
We have a corporate monitoring service that regularly tests a '/webcheck' endpoint for each server, and notifies us if any of these servers are down.
Currently that endpoint simply responds with a 'overall_status_code: ok' string, which tells us that the server is running, and nothing more. The other endpoints could be messed up, and we wouldn't know it.
I'd like to tie this endpoint into my mocha test suite such that it only returns the 'overall_status_code: ok' if all of the tests pass.
Anyone have hints how I could do this? Basically, how to invoke my tests from within node?
Here's my test code in case you're interested:
const expect = require('chai').expect;
const app = require('../server/app.js');
const supertest = require('supertest');
const request = supertest.agent(app);
describe('Active Directory Module', () => {
it('/ad/members endpoint should return: \n -> an array of members for a given group \n -> each element returned should be an object \n -> John Doe should be the first user in the array', (done) => {
request.get('/ad/members?group=%27CHQ%20Service%20Management%27')
.expect(200)
.end((err, res) => {
let result = JSON.parse(res.text);
expect(Array.isArray(result)).to.be.true;
expect(typeof(result[0]) === 'object').to.be.true;
expect(result[0].name === 'John Doe').to.be.true;
done();
})
})
it('/ad/membership endpoint should return: \n -> an array of groups for a given user \n -> each element returned should be an object \n -> \'Domain Users\' should be the first element in the array', (done) => {
request.get('/ad/membership?user=%27chq-mikeru%27')
.expect(200)
.end((err, res) => {
let result = JSON.parse(res.text);
// let result = res;
expect(Array.isArray(result)).to.be.true;
expect(typeof(result[0]) === 'object').to.be.true;
expect(result[0].name === 'Domain Users').to.be.true;
done();
})
})
it('/ad/userinfo endpoint should return: \n -> an object\n -> Should include property called \'Given Name\'\n -> given \'chq-stephenro\' the returned object should contain a property \'UserPrincipleName\' with value \'chq-stephenro@corp.company.com\'', (done) =>{
request.get('/ad/userinfo?user=%27chq-stephenho%27')
.expect(200)
.end((err, res) => {
let result = JSON.parse(res.text);
expect(typeof(result) === 'object').to.be.true;
expect(result['GivenName']).to.not.be.undefined;
expect(result['UserPrincipalName'] === 'chq-stephenro@corp.company.com').to.be.true;
done();
})
})
})
describe('Database Querying Module', () => {
it('/db/ci-co-conflict-checker should return \n --> an array\n --> of arrays\n --> For each element:\n * the first, third, and fourth elements should be numbers\n * the second element should be a string\n * the fifth element should be a string', (done) => {
request.get('/db/ci-co-conflict-checker/?cis=%27CA%20Service%20Catalog%27,%27CA%20Service%20Desk%20Manager%27&start=1533106860&end=1535699040')
.expect(200)
.end((err, res) => {
let result = res.body;
expect(Array.isArray(result)).to.be.true;
expect(Array.isArray(result[0])).to.be.true;
for ( var i = 0; i < result.length; i++ ) {
for (var j = 0; j < result[i].length; j++ ) {
if ( j === 0 || j === 2 || j === 3) {
expect(typeof(result[i][j]) === 'number').to.be.true;
}
if ( j === 1 || j === 4 ) {
expect (typeof(result[i][j]) === 'string').to.be.true;
}
}
}
done();
})
})
})
Yes, you only need a client rest to nodejs a compare the result in expect, but it's not a unit test, is a integration test.