-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.test.js
176 lines (140 loc) · 5.15 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
'use strict';
const request = require('supertest');
const app = require('./app');
function checkValidUserContent(res)
{
const jContent = res.body;
if(typeof jContent !== 'object'){
throw new Error('Not an object');
}
if(jContent['fname'] !== 'John'){
throw new Error('First name should be John');
}
if(jContent['lname'] !== 'Doe'){
throw new Error('Last name should be Doe');
}
if(jContent['username'] !== '[email protected]'){
console.log(jContent);
throw new Error('Username should be [email protected]');
}
}
function checkValidPostContent(res)
{
const jContent = res.body;
if(typeof jContent !== 'object'){
throw new Error('Not an object');
}
if(jContent['posttitle'] !== 'Sample Post 1'){
throw new Error('Post title should be Sample Post 1');
}
if(jContent['postauthor'] !== '[email protected]'){
throw new Error('Post author should be [email protected]');
}
if(jContent['postdate'] !== '2019-04-12'){
console.log(jContent);
throw new Error('Post date should be 2019-04-12');
}
}
// 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('&');
}
// Testing GET and POST user entities
describe('Test the users service', () => {
test('GET /users succeeds', () => {
return request(app)
.get('/users')
.expect(200);
});
test('GET /users returns JSON', () => {
return request(app)
.get('/users')
.expect('Content-type', /json/);
});
test('GET /users includes [email protected]', () => {
return request(app)
.get('/users')
.expect(/[email protected]/);
});
test('GET /users/[email protected] succeeds', () => {
return request(app)
.get('/users/[email protected]')
.expect(200);
});
test('GET /users/[email protected] returns JSON', () => {
return request(app)
.get('/users/[email protected]')
.expect('Content-type', /json/);
});
test('GET /users/[email protected] includes user details', () => {
return request(app)
.get('/users/[email protected]')
.expect(checkValidUserContent);
});
// test('POST /users needs access_token', () => {
// return request(app)
// .post('/people')
// .expect(403);
// });
test('POST /newuser cannot replicate user with same username', () => {
const params = {fname: 'John',
lname: 'Doe',
username: '[email protected]',
bio: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Lectus vestibulum mattis ullamcorper velit sed ullamcorper morbi tincidunt.'};
return request(app)
.post('/newuser')
.send(serialise(params))
.expect(400);
});
});
// Testing GET and POST blog post entities
describe('Test the blog posts service', () => {
test('GET /posts succeeds', () => {
return request(app)
.get('/users')
.expect(200);
});
test('GET /posts returns JSON', () => {
return request(app)
.get('/posts')
.expect('Content-type', /json/);
});
test('GET /posts includes Sample Post 1', () => {
return request(app)
.get('/posts')
.expect(/Sample Post 1/);
});
test('GET /posts/Sample Post 1 succeeds', () => {
return request(app)
.get('/posts/Sample Post 1')
.expect(200);
});
test('GET /posts/Sample Post 1 returns JSON', () => {
return request(app)
.get('/posts/Sample Post 1')
.expect('Content-type', /json/);
});
test('GET /posts/Sample Post 1 includes post details', () => {
return request(app)
.get('/posts/Sample Post 1')
.expect(checkValidPostContent);
});
// test('POST /users needs access_token', () => {
// return request(app)
// .post('/people')
// .expect(403);
// });
test('POST /createpost cannot replicate post with duplicate post title', () => {
const params = {
posttitle: 'Sample Post 1',
postauthor: '[email protected]',
postdate: '2019-04-12',
postcontent: 'Integer accumsan nunc quis lectus maximus, congue cursus dolor porta. Duis ultrices sapien non elit cursus, vitae vestibulum massa fermentum. Aliquam sed ligula viverra, imperdiet dolor ut, ornare lorem. Phasellus erat neque, viverra non ipsum at, mollis porta mi. Vivamus nulla turpis, rutrum non laoreet ut, molestie sed mi. Suspendisse nec pharetra tellus. Nam vitae nibh non nisi rutrum rhoncus. Aliquam ligula nulla, placerat ut lobortis vel, aliquam eu lectus. Nulla eget suscipit eros. Vivamus dignissim ornare vehicula. Nunc eleifend arcu est, vitae fringilla risus sodales ac. Nulla porta vestibulum elementum. Sed non porttitor massa, id ultrices elit. Donec gravida turpis eget laoreet aliquam.'
};
return request(app)
.post('/createpost')
.send(serialise(params))
.expect(400);
});
});