Skip to content
This repository was archived by the owner on Dec 4, 2021. It is now read-only.

Commit abd72de

Browse files
authored
Refactored API tests wth SceRpcTester (#58)
1 parent 441e7b5 commit abd72de

4 files changed

Lines changed: 131 additions & 106 deletions

File tree

test/client/2DPrinting.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ const tools = require('../util/tools.js');
1010
const PrintFunctions =
1111
require('../../client/printing/print_client');
1212
const sinon = require('sinon');
13+
const SceApiTester = require('../util/SceApiTester');
1314

1415
let app = null;
16+
let test = null;
1517
const expect = chai.expect;
1618

1719
chai.should();
@@ -31,6 +33,7 @@ describe('2DPrinting', () => {
3133

3234
before(done => {
3335
app = tools.initializeServer(__dirname + '/../../client/api/2DPrinting.js');
36+
test = new SceApiTester(app);
3437
done();
3538
});
3639

@@ -45,32 +48,17 @@ describe('2DPrinting', () => {
4548
});
4649

4750
describe('/POST sendPrintRequest', () => {
48-
it('Should return statuscode 200 when it prints', done => {
51+
it('Should return statuscode 200 when it prints', async () => {
4952
sendPrintRequestMock.resolves(SUCCESS_MESSAGE);
50-
chai
51-
.request(app)
52-
.post('/SceRpcApi/Printer/sendPrintRequest')
53-
.send(TEXT_REQUEST)
54-
.then(function(res) {
55-
expect(res).to.have.status(OK);
56-
done();
57-
})
58-
.catch(err => {
59-
throw err;
60-
});
53+
const result = await test.sendPostRequest(
54+
'/SceRpcApi/Printer/sendPrintRequest', TEXT_REQUEST);
55+
expect(result).to.have.status(OK);
6156
});
62-
it('Should return statuscode 400 when the RPC fails', done => {
57+
it('Should return statuscode 400 when the RPC fails', async () => {
6358
sendPrintRequestMock.rejects(ERROR_MESSAGE);
64-
chai
65-
.request(app)
66-
.post('/SceRpcApi/Printer/sendPrintRequest')
67-
.then(function(res) {
68-
expect(res).to.have.status(BAD_REQUEST);
69-
done();
70-
})
71-
.catch(err => {
72-
throw err;
73-
});
59+
const result = await test.sendPostRequest(
60+
'/SceRpcApi/Printer/sendPrintRequest', '');
61+
expect(result).to.have.status(BAD_REQUEST);
7462
});
7563
});
7664
});

test/client/3DPrinting.js

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ const tools = require('../util/tools.js');
88
const send3dPrintRequest =
99
require('../../client/printing_3d/print_3d_client');
1010
const sinon = require('sinon');
11+
const SceApiTester = require('../util/SceApiTester');
1112

1213
let app = null;
14+
let test = null;
1315
const expect = chai.expect;
1416

1517
chai.should();
@@ -29,6 +31,7 @@ describe('3DPrinting', () => {
2931

3032
before(done => {
3133
app = tools.initializeServer(__dirname + '/../../client/api/3DPrinting.js');
34+
test = new SceApiTester(app);
3235
done();
3336
});
3437

@@ -43,32 +46,17 @@ describe('3DPrinting', () => {
4346
});
4447

4548
describe('/POST print3dModel', () => {
46-
it('Should return statuscode 200 when request is completed', done => {
49+
it('Should return statuscode 200 when request is completed', async () => {
4750
send3DPrintRequestMock.resolves(SUCCESS_MESSAGE);
48-
chai
49-
.request(app)
50-
.post('/SceRpcApi/3dPrinter/print3dModel')
51-
.send(TEXT_REQUEST)
52-
.then(function(res) {
53-
expect(res).to.have.status(OK);
54-
done();
55-
})
56-
.catch(err => {
57-
throw err;
58-
});
51+
const result = await test.sendPostRequest(
52+
'/SceRpcApi/3dPrinter/print3dModel', TEXT_REQUEST);
53+
expect(result).to.have.status(OK);
5954
});
60-
it('Should return statuscode 400 if the RPC fails', done => {
55+
it('Should return statuscode 400 if the RPC fails', async () => {
6156
send3DPrintRequestMock.rejects(ERROR_MESSAGE);
62-
chai
63-
.request(app)
64-
.post('/SceRpcApi/3dPrinter/print3dModel')
65-
.then(function(res) {
66-
expect(res).to.have.status(BAD_REQUEST);
67-
done();
68-
})
69-
.catch(err => {
70-
throw err;
71-
});
57+
const result = await test.sendPostRequest(
58+
'/SceRpcApi/3dPrinter/print3dModel', '');
59+
expect(result).to.have.status(BAD_REQUEST);
7260
});
7361
});
7462
});

test/client/LedSign.js

Lines changed: 30 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ const LedSignFunctions =
1111
require('../../client/ledsign/led_sign_client');
1212
const LoggingFunctions = require('../../client/util/logging-helpers');
1313
const sinon = require('sinon');
14+
const SceApiTester = require('../util/SceApiTester');
15+
const { response } = require('express');
1416

1517
let app = null;
18+
let test = null;
1619
const expect = chai.expect;
1720

1821
chai.should();
@@ -68,6 +71,7 @@ describe('LedSign', () => {
6871

6972
before(done => {
7073
app = tools.initializeServer(__dirname + '/../../client/api/LedSign.js');
74+
test = new SceApiTester(app);
7175
done();
7276
});
7377

@@ -86,20 +90,14 @@ describe('LedSign', () => {
8690
});
8791

8892
describe('/POST healthCheck', () => {
93+
const officer = 'thai';
8994
let signResponse = null;
90-
it('Should return statusCode 200 when the sign is up', done => {
95+
it('Should return statusCode 200 when the sign is up', async () => {
9196
healthCheckMock.resolves(SUCCESS_MESSAGE);
92-
chai
93-
.request(app)
94-
.post('/SceRpcApi/LedSign/healthCheck')
95-
.then(function(res) {
96-
signResponse = res.body;
97-
expect(res).to.have.status(OK);
98-
done();
99-
})
100-
.catch(err => {
101-
throw err;
102-
});
97+
const response = await test.sendPostRequest(
98+
'/SceRpcApi/LedSign/healthCheck', officer);
99+
expect(response).to.have.status(OK);
100+
signResponse = response.body;
103101
});
104102
it('Should return the correct values when modified', done => {
105103
healthCheckMock.resolves(SUCCESS_MESSAGE);
@@ -112,65 +110,37 @@ describe('LedSign', () => {
112110
expect(signResponse.borderColor).to.equal(VALID_SIGN_REQUEST.borderColor);
113111
done();
114112
});
115-
it('Should return statusCode 404 when the sign is down', done => {
113+
it('Should return statusCode 404 when the sign is down', async () => {
116114
healthCheckMock.resolves(false);
117-
chai
118-
.request(app)
119-
.post('/SceRpcApi/LedSign/healthCheck')
120-
.then(function(res) {
121-
expect(res).to.have.status(NOT_FOUND);
122-
done();
123-
})
124-
.catch(err => {
125-
throw err;
126-
});
115+
const response = await test.sendPostRequest(
116+
'/SceRpcApi/LedSign/healthCheck', officer);
117+
expect(response).to.have.status(NOT_FOUND);
127118
});
128119
});
129120

130121
describe('/POST updateSignText', () => {
131-
it('Should return statusCode 200 when the sign text is updated', done => {
132-
addSignLogStub.resolves(SUCCESS_MESSAGE);
133-
updateSignTextMock.resolves(SUCCESS_MESSAGE);
134-
chai
135-
.request(app)
136-
.post('/SceRpcApi/LedSign/updateSignText')
137-
.send(VALID_SIGN_REQUEST)
138-
.then(function(res) {
139-
expect(res).to.have.status(OK);
140-
done();
141-
})
142-
.catch(err => {
143-
throw err;
144-
});
145-
});
146-
it('Should return statusCode 404 when the sign is down', done => {
122+
it('Should return statusCode 200 when the sign text is updated',
123+
async () => {
124+
addSignLogStub.resolves(SUCCESS_MESSAGE);
125+
updateSignTextMock.resolves(SUCCESS_MESSAGE);
126+
const response = await test.sendPostRequest(
127+
'/SceRpcApi/LedSign/updateSignText', VALID_SIGN_REQUEST);
128+
expect(response).to.have.status(OK);
129+
});
130+
it('Should return statusCode 404 when the sign is down', async () => {
147131
addSignLogStub.resolves(SUCCESS_MESSAGE);
148132
updateSignTextMock.rejects(ERROR_MESSAGE);
149-
chai
150-
.request(app)
151-
.post('/SceRpcApi/LedSign/updateSignText')
152-
.then(function(res) {
153-
expect(res).to.have.status(NOT_FOUND);
154-
done();
155-
})
156-
.catch(err => {
157-
throw err;
158-
});
133+
const response = await test.sendPostRequest(
134+
'/SceRpcApi/LedSign/updateSignText', INVALID_SIGN_REQUEST);
135+
expect(response).to.have.status(NOT_FOUND);
159136
});
160137
it('Should return statuscode 400 when we can\'t log sign activity',
161-
done => {
138+
async () => {
162139
addSignLogStub.resolves(ERROR_MESSAGE);
163140
updateSignTextMock.rejects(ERROR_MESSAGE);
164-
chai
165-
.request(app)
166-
.post('/SceRpcApi/LedSign/updateSignText')
167-
.then(function(res) {
168-
expect(res).to.have.status(BAD_REQUEST);
169-
done();
170-
})
171-
.catch(err => {
172-
throw err;
173-
});
141+
const response = await test.sendPostRequest(
142+
'/SceRpcApi/LedSign/updateSignText', {});
143+
expect(response).to.have.status(BAD_REQUEST);
174144
});
175145
});
176146
});

test/util/SceApiTester.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
process.env.NODE_ENV = 'test';
2+
3+
const chai = require('chai');
4+
const chaiHttp = require('chai-http');
5+
chai.should();
6+
chai.use(chaiHttp);
7+
8+
/**
9+
* Handles any POST and GET request made during API testing.
10+
*/
11+
class SceApiTester{
12+
constructor(app){
13+
this.app = app;
14+
}
15+
16+
/**
17+
* Creates a chai POST request to test an API route.
18+
* @param {String} endpoint contains the path of the route being tested.
19+
* @param {Object} params the parameters specified for the test.
20+
* @returns {Promise} the outcome of the API call.
21+
*/
22+
async sendPostRequest(endpoint, params){
23+
let response = null;
24+
await chai
25+
.request(this.app)
26+
.post(endpoint)
27+
.send(params)
28+
.then(function(res) {
29+
response = res;
30+
})
31+
.catch(err =>{
32+
throw err;
33+
});
34+
return response;
35+
}
36+
37+
/**
38+
* Creates a chai POST request with an access token to test an API route.
39+
* @param {Object} token the access token for the API call
40+
* @param {String} endpoint contains the path of the route being tested.
41+
* @param {Object} params the parameters specified for the test.
42+
* @returns {Promise} the outcome of the API call.
43+
*/
44+
async sendPostRequestWithToken(token, endpoint, params){
45+
let response = null;
46+
await chai
47+
.request(this.app)
48+
.post(endpoint)
49+
.send({ token, ...params })
50+
.then(function(res) {
51+
response = res;
52+
})
53+
.catch(err =>{
54+
throw err;
55+
});
56+
return response;
57+
}
58+
59+
/**
60+
* Creates a chai GET request to test an API route.
61+
* @param {String} endpoint contains the path of the route being tested.
62+
* @returns {Promise} the outcome of the API call.
63+
*/
64+
async sendGetRequest(endpoint){
65+
let response = null;
66+
await chai
67+
.request(this.app)
68+
.get(endpoint)
69+
.then(function(res) {
70+
response = res;
71+
})
72+
.catch(err =>{
73+
throw err;
74+
});
75+
return response;
76+
}
77+
}
78+
79+
module.exports = SceApiTester;

0 commit comments

Comments
 (0)