Skip to content

Commit 257d30b

Browse files
committed
test: add integ tests
1 parent 30f2ae5 commit 257d30b

File tree

6 files changed

+180
-5
lines changed

6 files changed

+180
-5
lines changed

package-lock.json

Lines changed: 128 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"prepare": "test -d node_modules/husky && husky install || echo \"husky is not installed\"",
2222
"test": "npm run test:unit && npm run test:integ",
2323
"test:unit": "APP_CONFIG=./test/unit/utils/.app.json mocha test/unit/*.spec.js test/unit/**/*.spec.js test/unit/**/*.spec.js test/unit/**/**/*.spec.js --timeout=3000",
24-
"test:integ": "echo disabled for now - mocha test/integration/*.spec.js test/integration/**/*.spec.js --timeout=3000",
24+
"test:integ": "APP_CONFIG=./test/integration/.app.json mocha test/integration/*.spec.js test/integration/**/*.spec.js --timeout=3000",
2525
"printReportsLink": "echo Detailed unit test coverage report: file:///$(pwd)/coverage-unit/index.html && echo Detailed integration test coverage report: file:///$(pwd)/coverage-integration/index.html",
2626
"cover": "npm run cover:unit",
2727
"cover:unit": "c8 -c .nycrc.unit.json npm run test:unit && npm run --silent printReportsLink",
@@ -61,7 +61,8 @@
6161
"nodemon": "2.0.20"
6262
},
6363
"dependencies": {
64+
"@aicore/libcommonutils": "1.0.19",
6465
"fastify": "4.12.0",
65-
"@aicore/libcommonutils": "1.0.19"
66+
"node-fetch": "^3.3.0"
6667
}
6768
}

src/api/hello.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ const schema = {
55
type: 'object',
66
required: ['name'],
77
properties: {
8-
name: {type: 'string'}
8+
name: {
9+
type: 'string',
10+
minLength: 1,
11+
maxLength: 10
12+
}
913
}
1014
},
1115
response: {

test/integration/.app.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"port": 5000,
3+
"authKey": "hehe",
4+
"allowPublicAccess": false
5+
}

test/integration/hello.spec.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*global describe, it, before, after*/
2+
import {startServer, close} from "../../src/server.js";
3+
import * as chai from 'chai';
4+
import fetch from "node-fetch";
5+
6+
let expect = chai.expect;
7+
8+
describe('Integration Tests for hello api', function () {
9+
10+
before(async function () {
11+
await startServer();
12+
});
13+
14+
after(async function () {
15+
await close();
16+
});
17+
18+
it('should say hello', async function () {
19+
let output = await fetch("http://localhost:5000/hello?name=world", { method: 'GET', headers: {
20+
authorization: "Basic hehe"
21+
}});
22+
output = await output.json();
23+
expect(output).eql({message: "hello world"});
24+
});
25+
26+
it('should not say hello if unauthorised', async function () {
27+
let output = await fetch("http://localhost:5000/hello?name=world", { method: 'GET'});
28+
output = await output.json();
29+
expect(output).eql({
30+
"error": "Unauthorized",
31+
"message": "Wrong key",
32+
"statusCode": 401});
33+
});
34+
});

test/unit/api/hello.spec.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@ describe('unit Tests for hello api', function () {
1717
});
1818

1919
it('should validate schemas for sample request/responses', async function () {
20+
let request = getSimpleGETRequest();
2021
// request
2122
const requestValidator = AJV.compile(getHelloSchema().schema.querystring);
22-
expect(requestValidator(getSimpleGETRequest().query)).to.be.true;
23+
expect(requestValidator(request.query)).to.be.true;
24+
// message too long validation
25+
request.query.name = "a name that is too long";
26+
expect(requestValidator(request.query)).to.be.false;
2327
// response
2428
const successResponseValidator = AJV.compile(getHelloSchema().schema.response["200"]);
2529
let response = await hello(getSimpleGETRequest(), getSimpleGetReply());

0 commit comments

Comments
 (0)