forked from sclorg/nodejs-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.test.js
85 lines (69 loc) · 2.12 KB
/
app.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict';
const request = require('supertest');
const app = require('./app');
function checkDeliaDerbyshire(res)
{
const jContent = res.body;
if(typeof jContent !== 'object'){
throw new Error('not an object');
}
if(jContent['surname'] !== 'Derbyshire'){
console.log(jContent);
throw new Error('surname should be Derbyshire');
}
if(jContent['forename'] !== 'Delia'){
throw new Error('forename should be Delia');
}
}
// thanks to Nico Tejera at https://stackoverflow.com/questions/1714786/query-string-encoding-of-a-javascript-object
// returns something like "access_token=concertina&username=bobthebuilder"
function serialise(obj){
return Object.keys(obj).map(k => `${encodeURIComponent(k)}=${encodeURIComponent(obj[k])}`).join('&');
}
describe('Test the people service', () => {
test('GET /people succeeds', () => {
return request(app)
.get('/people')
.expect(200);
});
test('GET /people returns JSON', () => {
return request(app)
.get('/people')
.expect('Content-type', /json/);
});
test('GET /people includes doctorwhocomposer', () => {
return request(app)
.get('/people')
.expect(/doctorwhocomposer/);
});
test('GET /people/doctorwhocomposer succeeds', () => {
return request(app)
.get('/people/doctorwhocomposer')
.expect(200);
});
test('GET /people/doctorwhocomposer returns JSON', () => {
return request(app)
.get('/people/doctorwhocomposer')
.expect('Content-type', /json/);
});
test('GET /people/doctorwhocomposer includes name details', () => {
return request(app)
.get('/people/doctorwhocomposer')
.expect(checkDeliaDerbyshire);
});
test('POST /people needs access_token', () => {
return request(app)
.post('/people')
.expect(403);
});
test('POST /people cannot replicate', () => {
const params = {access_token: 'concertina',
username: 'doctorwhocomposer',
forename: 'Bob',
surname: 'Builder'};
return request(app)
.post('/people')
.send(serialise(params))
.expect(400);
});
});