-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
321 lines (257 loc) · 7.3 KB
/
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
const chai = require('chai');
const async = require('async');
const chaiHttp = require('chai-http');
const server = require('../src');
const models = require('../src/models');
const config = require('../src/config.json');
const sequelize = models.sequelize;
const should = chai.should();
chai.use(chaiHttp);
// For drop tables and clean start
//sequelize.sync({force: true})
//For each test case we are creating a new poll,
//using below setupdata
const pollSetUpData = {
title: 'Poll test - mocha',
candidates: [
{name: 'A'},
{name: 'B'},
{name: 'C'},
{name: 'D'}
]};
/**
* Scenario: Count Me Up should setup new poll
* Should create a new poll
*
*/
describe('Scenario: Count Me Up should setup new poll', () => {
const setup = chai
.request(server)
.post('/api/poll/create')
.send(pollSetUpData);
const successResponseTest = (done) => (err, res) => {
res.should.have.status(201);
res.body.should.be.a('object');
if(done) done();
}
it('Should create a new poll', (done) => {
setup.end(successResponseTest(done));
});
});
/**
* Scenario: Count Me Up accepts a vote
* Given I am count me up
* When I receive a vote for candidate A from voter A
* And voter A has not voted before
* Then I register that vote and return a 201 response
*
*/
describe('Scenario: Count Me Up accepts a vote', () => {
it('Then I register that vote and return a 201 response', (done) => {
async.waterfall([cb => {
chai
.request(server)
.post('/api/poll/create')
.send(pollSetUpData)
.end((err, res) => {
cb(null, res.body)
});
}, body => {
chai
.request(server)
.post('/api/poll/up')
.send({
poll_id : body.result.id,
candidate_id: body.candidatesIds[0],
email: 'chai@mocha.dev'
})
.end((err, res) => {
res.should.have.status(201);
res.body.should.be.a('object');
if(done) done();
});
}]);
});
});
/**
* Scenario: Count Me Up only accepts 3 votes per user
* Given I am count me up
* And I have received 3 votes for candidate A from voter B
* When I receive a vote for candidate A from voter b
* Then I return a 403 response
* And I do not register that vote
*
*/
describe('Scenario: Count Me Up only accepts 3 votes per user', () => {
const vote = (body, cb) => {
chai
.request(server)
.post('/api/poll/up')
.send({
poll_id : body.result.id,
candidate_id: body.candidatesIds[0],
email: 'chai@mocha.dev'
})
.end(() => {
cb(null, body);
});
}
it('Should return 403 when I am trying to register for 4th time', (done) => {
async.waterfall([cb => { //create new poll
chai
.request(server)
.post('/api/poll/create')
.send(pollSetUpData)
.end((err, res) => {
cb(null, res.body)
});
},
vote, //first vote
vote, //second vote
vote, //third vote
(body) => { //fourth vote
chai
.request(server)
.post('/api/poll/up')
.send({
poll_id : body.result.id,
candidate_id: body.candidatesIds[0],
email: 'chai@mocha.dev'
})
.end((err, res) => {
res.should.have.status(403);
res.body.should.be.a('object');
if(done) done();
});
}]);
});
});
/**
* Scenario: Count Me Up only accepts 3 votes per user regardless of candidate
* Given I am count me up
* And I have received 2 votes for candidate A from voter B
* And I have received 1 vote for candidate D from voter B
* When I receive a vote for candidate D from voter B
* Then I return a 403 response
* And I do not register that vote
*
*/
describe('Scenario: Count Me Up only accepts 3 votes per user regardless of candidate', () => {
const vote = index => (body, cb) => {
chai
.request(server)
.post('/api/poll/up')
.send({
poll_id : body.result.id,
candidate_id: body.candidatesIds[index],
email: 'chai@mocha.dev'
})
.end(() => {
cb(null, body);
});
}
it('Should return 403 when I am trying to register for 4th time regardless of candidate', (done) => {
async.waterfall([cb => { //create new poll
chai
.request(server)
.post('/api/poll/create')
.send(pollSetUpData)
.end((err, res) => {
cb(null, res.body)
});
},
vote(0), //first vote to first candidate
vote(1), //second vote to second candidate
vote(2), //third vote to third candidate
(body) => { //fourth vote
chai
.request(server)
.post('/api/poll/up')
.send({
poll_id : body.result.id,
candidate_id: body.candidatesIds[0],
email: 'chai@mocha.dev'
})
.end((err, res) => {
res.should.have.status(403);
res.body.should.be.a('object');
if(done) done();
});
}]);
});
});
/**
* Scenario: Count Me Up returns the voting results
* Given I am count me up
* And I have received 20,000,000 votes for 4 candidates
* And the votes are split:
* | candidate | votes |
* | A | 8,000,000 |
* | B | 2,000,000 |
* | C | 6,000,000 |
* | D | 4,000,000 |
*
* When I receive a request for the overall result
* Then I return the correct result
* And the response time is under 1 second
*
*/
describe('Scenario: Count Me Up returns the voting results', () => {
const NO_OF_REQ = 15;
it('Should retrun poll data', (done) => {
async.waterfall([cb => { //create new poll
chai
.request(server)
.post('/api/poll/create')
.send(pollSetUpData)
.end((err, res) => {
cb(null, res.body)
});
},
(body, cb) => {
//req array
const requests = [];
const vote = (payload) =>
chai
.request(server)
.post('/api/poll/up')
.send(payload);
//crate a random email for each request
const randomEmail = () => {
const chars = 'abcdefghijklmnopqrstuvwxyz1234567890_';
let string = '';
for(let i=0; i<15; i++) {
string += chars[Math.floor(Math.random() * chars.length)];
}
return `${string}@loadtest.dev`;
}
//create n promise
for(let i = 0; i < NO_OF_REQ; i++) {
const cid = Math.floor(Math.random() * 4);
const req = vote({
poll_id : body.result.id,
candidate_id: body.candidatesIds[cid],
email: randomEmail()
});
requests.push(req);
}
//callback after req completed
Promise.all(requests).then(() => cb(null, body));
},
(body) => {
chai
.request(server)
.get(`/api/poll/${body.result.id}`)
.end((err, res) => {
res.body.should.be.a('array');
const t = res.body.reduce((a,b) => ( { total: a.total + b.total } ) );
t.total.should.equal(NO_OF_REQ);
//just to check logs
console.log(`Total votes: ${t.total}`);
console.log(['Candidates Id, Total']);
console.log(res.body.map(ele => `${ele.candidates_id} - ${ele.total}`));
if(done) done();
});
}]);
});
});