|
@@ -0,0 +1,362 @@
|
|
1
|
+'use strict';
|
|
2
|
+
|
|
3
|
+const path = require('path'),
|
|
4
|
+ should = require('should');
|
|
5
|
+
|
|
6
|
+const agentFactory = require('./agent'),
|
|
7
|
+ dbHandle = require('./handle-database'),
|
|
8
|
+ models = require(path.resolve('./models'));
|
|
9
|
+
|
|
10
|
+voteTestFactory('idea');
|
|
11
|
+voteTestFactory('comment');
|
|
12
|
+
|
|
13
|
+/**
|
|
14
|
+ * We can test votes to different objects.
|
|
15
|
+ * @param {string} primary - what is the object we vote for? i.e. comment, idea
|
|
16
|
+ * @param {boolean} [only=false] - should we run only these tests or not? (describe vs. describe.only in mocha)
|
|
17
|
+ */
|
|
18
|
+function voteTestFactory(primary, only=false) {
|
|
19
|
+ const primarys = primary + 's';
|
|
20
|
+
|
|
21
|
+ const ds = (only) ? describe.only : describe;
|
|
22
|
+
|
|
23
|
+ function fillPrimary(data, count) {
|
|
24
|
+ switch (primary) {
|
|
25
|
+ case 'comment':
|
|
26
|
+ data.ideas = Array(1).fill([]);
|
|
27
|
+ data.ideaComments = Array(count).fill([0, 0]);
|
|
28
|
+ break;
|
|
29
|
+
|
|
30
|
+ default:
|
|
31
|
+ data[primarys] = Array(count).fill([{}, 0]);
|
|
32
|
+ }
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ ds(`votes for ${primarys}, ...`, () => {
|
|
36
|
+ let agent,
|
|
37
|
+ dbData,
|
|
38
|
+ existentPrimary,
|
|
39
|
+ primary0,
|
|
40
|
+ primary1,
|
|
41
|
+ loggedUser,
|
|
42
|
+ otherUser;
|
|
43
|
+
|
|
44
|
+ afterEach(async () => {
|
|
45
|
+ await dbHandle.clear();
|
|
46
|
+ });
|
|
47
|
+
|
|
48
|
+ beforeEach(() => {
|
|
49
|
+ agent = agentFactory();
|
|
50
|
+ });
|
|
51
|
+
|
|
52
|
+ describe(`POST /${primarys}/:id/votes (create a vote)`, () => {
|
|
53
|
+ let voteBody;
|
|
54
|
+
|
|
55
|
+ beforeEach(() => {
|
|
56
|
+ voteBody = { data: {
|
|
57
|
+ type: 'votes',
|
|
58
|
+ attributes: {
|
|
59
|
+ value: -1
|
|
60
|
+ }
|
|
61
|
+ } };
|
|
62
|
+ });
|
|
63
|
+
|
|
64
|
+ // put pre-data into database
|
|
65
|
+ beforeEach(async () => {
|
|
66
|
+ const data = {
|
|
67
|
+ users: 3, // how many users to make
|
|
68
|
+ verifiedUsers: [0, 1], // which users to make verified
|
|
69
|
+ };
|
|
70
|
+
|
|
71
|
+ fillPrimary(data, 1);
|
|
72
|
+
|
|
73
|
+ // create data in database
|
|
74
|
+ dbData = await dbHandle.fill(data);
|
|
75
|
+
|
|
76
|
+ loggedUser = dbData.users[0];
|
|
77
|
+ existentPrimary = dbData[primarys][0];
|
|
78
|
+ });
|
|
79
|
+
|
|
80
|
+ context('logged', () => {
|
|
81
|
+
|
|
82
|
+ beforeEach(() => {
|
|
83
|
+ agent = agentFactory.logged(loggedUser);
|
|
84
|
+ });
|
|
85
|
+
|
|
86
|
+ context('valid data', () => {
|
|
87
|
+ it('[all is fine] save the vote', async () => {
|
|
88
|
+ const response = await agent
|
|
89
|
+ .post(`/${primarys}/${existentPrimary.id}/votes`)
|
|
90
|
+ .send(voteBody)
|
|
91
|
+ .expect(201);
|
|
92
|
+
|
|
93
|
+ // saved into database?
|
|
94
|
+ const dbVote = await models.vote.read({ from: loggedUser.username, to: { type: primarys, id: existentPrimary.id } });
|
|
95
|
+ should(dbVote).ok();
|
|
96
|
+
|
|
97
|
+ // correct response?
|
|
98
|
+ should(response.body).match({
|
|
99
|
+ data: {
|
|
100
|
+ type: 'votes',
|
|
101
|
+ attributes: {
|
|
102
|
+ value: -1
|
|
103
|
+ },
|
|
104
|
+ relationships: {
|
|
105
|
+ from: { data: { type: 'users', id: loggedUser.username } },
|
|
106
|
+ to: { data: { type: primarys, id: existentPrimary.id } }
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+ });
|
|
110
|
+ });
|
|
111
|
+
|
|
112
|
+ it('[a vote already exists] 409', async () => {
|
|
113
|
+ await agent
|
|
114
|
+ .post(`/${primarys}/${existentPrimary.id}/votes`)
|
|
115
|
+ .send(voteBody)
|
|
116
|
+ .expect(201);
|
|
117
|
+
|
|
118
|
+ voteBody.data.attributes.value = 1;
|
|
119
|
+
|
|
120
|
+ const response = await agent
|
|
121
|
+ .post(`/${primarys}/${existentPrimary.id}/votes`)
|
|
122
|
+ .send(voteBody)
|
|
123
|
+ .expect(409);
|
|
124
|
+
|
|
125
|
+ should(response.body).deepEqual({
|
|
126
|
+ errors: [{
|
|
127
|
+ status: 409,
|
|
128
|
+ detail: 'duplicate vote'
|
|
129
|
+ }]
|
|
130
|
+ });
|
|
131
|
+ });
|
|
132
|
+
|
|
133
|
+ it(`[${primary} doesn't exist] 404`, async () => {
|
|
134
|
+ const response = await agent
|
|
135
|
+ .post(`/${primarys}/1111111/votes`)
|
|
136
|
+ .send(voteBody)
|
|
137
|
+ .expect(404);
|
|
138
|
+
|
|
139
|
+ should(response.body).deepEqual({
|
|
140
|
+ errors: [{
|
|
141
|
+ status: 404,
|
|
142
|
+ detail: `${primary} doesn't exist`
|
|
143
|
+ }]
|
|
144
|
+ });
|
|
145
|
+ });
|
|
146
|
+ });
|
|
147
|
+
|
|
148
|
+ context('invalid data', () => {
|
|
149
|
+ it('[not +1 or -1] 400', async () => {
|
|
150
|
+ voteBody.data.attributes.value = 0;
|
|
151
|
+
|
|
152
|
+ await agent
|
|
153
|
+ .post(`/${primarys}/${existentPrimary.id}/votes`)
|
|
154
|
+ .send(voteBody)
|
|
155
|
+ .expect(400);
|
|
156
|
+ });
|
|
157
|
+
|
|
158
|
+ it('[missing value in body] 400', async () => {
|
|
159
|
+ delete voteBody.data.attributes.value;
|
|
160
|
+
|
|
161
|
+ await agent
|
|
162
|
+ .post(`/${primarys}/${existentPrimary.id}/votes`)
|
|
163
|
+ .send(voteBody)
|
|
164
|
+ .expect(400);
|
|
165
|
+ });
|
|
166
|
+
|
|
167
|
+ it('[additional body parameters] 400', async () => {
|
|
168
|
+ voteBody.data.attributes.foo = 'bar';
|
|
169
|
+
|
|
170
|
+ await agent
|
|
171
|
+ .post(`/${primarys}/${existentPrimary.id}/votes`)
|
|
172
|
+ .send(voteBody)
|
|
173
|
+ .expect(400);
|
|
174
|
+ });
|
|
175
|
+
|
|
176
|
+ it(`[invalid ${primary} id] 400`, async () => {
|
|
177
|
+ await agent
|
|
178
|
+ .post(`/${primarys}/invalid--id/votes`)
|
|
179
|
+ .send(voteBody)
|
|
180
|
+ .expect(400);
|
|
181
|
+ });
|
|
182
|
+ });
|
|
183
|
+ });
|
|
184
|
+
|
|
185
|
+ context('not logged', () => {
|
|
186
|
+ it('403', async () => {
|
|
187
|
+ await agent
|
|
188
|
+ .post(`/${primarys}/${existentPrimary.id}/votes`)
|
|
189
|
+ .send(voteBody)
|
|
190
|
+ .expect(403);
|
|
191
|
+ });
|
|
192
|
+ });
|
|
193
|
+ });
|
|
194
|
+
|
|
195
|
+ describe(`DELETE /${primarys}/:id/votes/vote (remove a vote)`, () => {
|
|
196
|
+
|
|
197
|
+ beforeEach(async () => {
|
|
198
|
+ const data = {
|
|
199
|
+ users: 3, // how many users to make
|
|
200
|
+ verifiedUsers: [0, 1, 2], // which users to make verified
|
|
201
|
+ votes: [
|
|
202
|
+ [0, [primarys, 0], -1],
|
|
203
|
+ [1, [primarys, 0], 1]
|
|
204
|
+ ] // user, primary, value
|
|
205
|
+ };
|
|
206
|
+
|
|
207
|
+ fillPrimary(data, 2);
|
|
208
|
+
|
|
209
|
+ // create data in database
|
|
210
|
+ dbData = await dbHandle.fill(data);
|
|
211
|
+
|
|
212
|
+ [loggedUser, otherUser] = dbData.users;
|
|
213
|
+ [primary0, primary1] = dbData[primarys];
|
|
214
|
+ });
|
|
215
|
+
|
|
216
|
+ context('logged', () => {
|
|
217
|
+
|
|
218
|
+ beforeEach(() => {
|
|
219
|
+ agent = agentFactory.logged(loggedUser);
|
|
220
|
+ });
|
|
221
|
+
|
|
222
|
+ context('valid data', () => {
|
|
223
|
+ it('[vote exists] remove the vote and 204', async () => {
|
|
224
|
+ // first the vote should exist
|
|
225
|
+ const dbVote = await models.vote.read({ from: loggedUser.username, to: { type: primarys, id: primary0.id } });
|
|
226
|
+ should(dbVote).ok();
|
|
227
|
+
|
|
228
|
+ // then we remove the vote
|
|
229
|
+ await agent
|
|
230
|
+ .delete(`/${primarys}/${primary0.id}/votes/vote`)
|
|
231
|
+ .expect(204);
|
|
232
|
+
|
|
233
|
+ // then the vote should not exist
|
|
234
|
+ const dbVoteAfter = await models.vote.read({ from: loggedUser.username, to: { type: primarys, id: primary0.id } });
|
|
235
|
+ should(dbVoteAfter).not.ok();
|
|
236
|
+ // and other votes are still there
|
|
237
|
+ const dbOtherVote = await models.vote.read({ from: otherUser.username, to: { type: primarys, id: primary0.id } });
|
|
238
|
+ should(dbOtherVote).ok();
|
|
239
|
+ });
|
|
240
|
+
|
|
241
|
+ it('[vote doesn\'t exist] 404', async () => {
|
|
242
|
+ const response = await agent
|
|
243
|
+ .delete(`/${primarys}/${primary1.id}/votes/vote`)
|
|
244
|
+ .expect(404);
|
|
245
|
+
|
|
246
|
+ should(response.body).deepEqual({
|
|
247
|
+ errors: [{
|
|
248
|
+ status: 404,
|
|
249
|
+ detail: `vote or ${primary} doesn't exist`
|
|
250
|
+ }]
|
|
251
|
+ });
|
|
252
|
+
|
|
253
|
+ });
|
|
254
|
+
|
|
255
|
+ it(`[${primary} doesn't exist] 404`, async () => {
|
|
256
|
+ const response = await agent
|
|
257
|
+ .delete(`/${primarys}/111111/votes/vote`)
|
|
258
|
+ .expect(404);
|
|
259
|
+
|
|
260
|
+ should(response.body).deepEqual({
|
|
261
|
+ errors: [{
|
|
262
|
+ status: 404,
|
|
263
|
+ detail: `vote or ${primary} doesn't exist`
|
|
264
|
+ }]
|
|
265
|
+ });
|
|
266
|
+ });
|
|
267
|
+ });
|
|
268
|
+
|
|
269
|
+ context('invalid data', () => {
|
|
270
|
+ it(`[invalid ${primary} id] 400`, async () => {
|
|
271
|
+ await agent
|
|
272
|
+ .delete(`/${primarys}/invalid--id/votes/vote`)
|
|
273
|
+ .expect(400);
|
|
274
|
+ });
|
|
275
|
+ });
|
|
276
|
+ });
|
|
277
|
+
|
|
278
|
+ context('not logged', () => {
|
|
279
|
+ it('403', async () => {
|
|
280
|
+ await agent
|
|
281
|
+ .delete(`/${primarys}/${primary0.id}/votes/vote`)
|
|
282
|
+ .expect(403);
|
|
283
|
+ });
|
|
284
|
+ });
|
|
285
|
+ });
|
|
286
|
+
|
|
287
|
+ describe(`PATCH /${primarys}/:id/votes/vote (change vote value)`, () => {
|
|
288
|
+ it('maybe todo');
|
|
289
|
+ });
|
|
290
|
+
|
|
291
|
+ describe(`show amount of votes up and down and current user\`s vote when GET /${primarys}${(primary === 'comment') ? '' : '/:id'}`, () => {
|
|
292
|
+
|
|
293
|
+ let idea0,
|
|
294
|
+ url;
|
|
295
|
+
|
|
296
|
+ beforeEach(async () => {
|
|
297
|
+ const data = {
|
|
298
|
+ users: 5, // how many users to make
|
|
299
|
+ verifiedUsers: [0, 1, 2, 3, 4], // which users to make verified
|
|
300
|
+ votes: [
|
|
301
|
+ [0, [primarys, 0], -1],
|
|
302
|
+ [1, [primarys, 0], 1],
|
|
303
|
+ [2, [primarys, 0], 1],
|
|
304
|
+ [2, [primarys, 1], -1],
|
|
305
|
+ [3, [primarys, 0], -1],
|
|
306
|
+ [4, [primarys, 0], 1],
|
|
307
|
+ ] // user, primary, value
|
|
308
|
+ };
|
|
309
|
+
|
|
310
|
+ fillPrimary(data, 2);
|
|
311
|
+
|
|
312
|
+ // create data in database
|
|
313
|
+ dbData = await dbHandle.fill(data);
|
|
314
|
+
|
|
315
|
+ [loggedUser, otherUser] = dbData.users;
|
|
316
|
+ [primary0, primary1] = dbData[primarys];
|
|
317
|
+ [idea0] = dbData.ideas;
|
|
318
|
+ });
|
|
319
|
+
|
|
320
|
+ beforeEach(() => {
|
|
321
|
+ agent = agentFactory.logged(loggedUser);
|
|
322
|
+ });
|
|
323
|
+
|
|
324
|
+ beforeEach(() => {
|
|
325
|
+ url = (primary === 'comment') ? `/ideas/${idea0.id}/comments` : `/${primarys}/${primary0.id}`;
|
|
326
|
+ });
|
|
327
|
+
|
|
328
|
+ it('show amount of votes up and down', async () => {
|
|
329
|
+ const response = await agent
|
|
330
|
+ .get(url)
|
|
331
|
+ .expect(200);
|
|
332
|
+
|
|
333
|
+ const objectToTest = (primary === 'comment') ? response.body.data[0] : response.body.data;
|
|
334
|
+
|
|
335
|
+ should(objectToTest).match({ meta: { votesUp: 3, votesDown: 2 } });
|
|
336
|
+ });
|
|
337
|
+
|
|
338
|
+ it('show my vote', async () => {
|
|
339
|
+ const response = await agent
|
|
340
|
+ .get(url)
|
|
341
|
+ .expect(200);
|
|
342
|
+
|
|
343
|
+ const objectToTest = (primary === 'comment') ? response.body.data[0] : response.body.data;
|
|
344
|
+
|
|
345
|
+ should(objectToTest).match({ meta: { myVote: -1 } });
|
|
346
|
+ });
|
|
347
|
+
|
|
348
|
+ it('show 0 as my vote if I didn\'t vote', async () => {
|
|
349
|
+ const url2 = (primary === 'comment') ? `/ideas/${idea0.id}/comments` : `/${primarys}/${primary1.id}`;
|
|
350
|
+
|
|
351
|
+ const response = await agent
|
|
352
|
+ .get(url2)
|
|
353
|
+ .expect(200);
|
|
354
|
+
|
|
355
|
+ const objectToTest = (primary === 'comment') ? response.body.data[1] : response.body.data;
|
|
356
|
+
|
|
357
|
+ should(objectToTest).match({ meta: { myVote: 0 } });
|
|
358
|
+ });
|
|
359
|
+ });
|
|
360
|
+
|
|
361
|
+ });
|
|
362
|
+}
|