|
@@ -0,0 +1,311 @@
|
|
1
|
+const supertest = require('supertest'),
|
|
2
|
+ should = require('should'),
|
|
3
|
+ fs = require('fs'),
|
|
4
|
+ crypto = require('crypto'),
|
|
5
|
+ { promisify } = require('util'),
|
|
6
|
+ path = require('path'),
|
|
7
|
+ rimraf = promisify(require('rimraf')),
|
|
8
|
+ sizeOf = promisify(require('image-size')),
|
|
9
|
+ typeOf = require('image-type');
|
|
10
|
+
|
|
11
|
+const app = require(path.resolve('./app')),
|
|
12
|
+ dbHandle = require(path.resolve('./test/handleDatabase')),
|
|
13
|
+ { clearTemporary } = require(path.resolve('./jobs/files'));
|
|
14
|
+
|
|
15
|
+const agent = supertest.agent(app);
|
|
16
|
+
|
|
17
|
+describe('/users/:username/avatar', function () {
|
|
18
|
+
|
|
19
|
+ let dbData;
|
|
20
|
+
|
|
21
|
+ function beforeEachPopulate(data) {
|
|
22
|
+ // put pre-data into database
|
|
23
|
+ beforeEach(async function () {
|
|
24
|
+ // create data in database
|
|
25
|
+ dbData = await dbHandle.fill(data);
|
|
26
|
+ });
|
|
27
|
+
|
|
28
|
+ afterEach(async function () {
|
|
29
|
+ await dbHandle.clear();
|
|
30
|
+ });
|
|
31
|
+ }
|
|
32
|
+
|
|
33
|
+ let loggedUser, otherUser;
|
|
34
|
+
|
|
35
|
+ beforeEachPopulate({
|
|
36
|
+ users: 2, // how many users to make
|
|
37
|
+ verifiedUsers: [0, 1] // which users to make verified
|
|
38
|
+ });
|
|
39
|
+
|
|
40
|
+ beforeEach(function () {
|
|
41
|
+ [loggedUser, otherUser] = dbData.users;
|
|
42
|
+ });
|
|
43
|
+
|
|
44
|
+ describe('GET', function () {
|
|
45
|
+
|
|
46
|
+ context('logged', function () {
|
|
47
|
+
|
|
48
|
+ context(':username exists', function () {
|
|
49
|
+
|
|
50
|
+ it('[nothing uploaded] responds with 200 and a default identicon (identicon.js)', async function () {
|
|
51
|
+ const response = await agent
|
|
52
|
+ .get(`/users/${otherUser.username}/avatar`)
|
|
53
|
+ .set('Content-Type', 'application/vnd.api+json')
|
|
54
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
55
|
+ .expect(200)
|
|
56
|
+ .expect('Content-Type', /^application\/vnd\.api\+json/);
|
|
57
|
+
|
|
58
|
+ // the request returns a json:
|
|
59
|
+ // {
|
|
60
|
+ // data: {
|
|
61
|
+ // type: 'user-avatars'
|
|
62
|
+ // id: username
|
|
63
|
+ // attributes: {
|
|
64
|
+ // format,
|
|
65
|
+ // base64
|
|
66
|
+ // }
|
|
67
|
+ // }
|
|
68
|
+ // }
|
|
69
|
+ // check the proper format attribute
|
|
70
|
+
|
|
71
|
+ should(response).have.propertyByPath('body', 'data', 'type').eql('user-avatars');
|
|
72
|
+ should(response).have.propertyByPath('body', 'data', 'id').eql('user1');
|
|
73
|
+ should(response).have.propertyByPath('body', 'data', 'attributes', 'format').eql('png');
|
|
74
|
+ should(response).have.propertyByPath('body', 'data', 'attributes', 'base64');
|
|
75
|
+
|
|
76
|
+ // compare hash of the base64 image representation
|
|
77
|
+ const data = response.body.data.attributes.base64;
|
|
78
|
+ const hash = crypto.createHash('sha256').update(data).digest('hex');
|
|
79
|
+
|
|
80
|
+ should(hash).equal('7d76d24aee7faf11a3494ae91b577d94cbb5320cec1b2fd04187fff1197915bb');
|
|
81
|
+
|
|
82
|
+ });
|
|
83
|
+
|
|
84
|
+ it('[png uploaded] responds with 200 and a jpeg image');
|
|
85
|
+
|
|
86
|
+ it('[jpeg uploaded] responds with 200 and a jpeg image');
|
|
87
|
+
|
|
88
|
+ });
|
|
89
|
+
|
|
90
|
+ context(':username doesn\'t exist', function () {
|
|
91
|
+
|
|
92
|
+ it('responds with 404', async function () {
|
|
93
|
+ await agent
|
|
94
|
+ .get('/users/nonexistent-user/avatar')
|
|
95
|
+ .set('Content-Type', 'application/vnd.api+json')
|
|
96
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
97
|
+ .expect(404)
|
|
98
|
+ .expect('Content-Type', /^application\/vnd\.api\+json/);
|
|
99
|
+ });
|
|
100
|
+
|
|
101
|
+ });
|
|
102
|
+ });
|
|
103
|
+
|
|
104
|
+ context('not logged', function () {
|
|
105
|
+
|
|
106
|
+ it('responds with 403', async function () {
|
|
107
|
+ await agent
|
|
108
|
+ .get('/users/nonexistent-user/avatar')
|
|
109
|
+ .set('Content-Type', 'application/vnd.api+json')
|
|
110
|
+ .expect(403)
|
|
111
|
+ .expect('Content-Type', /^application\/vnd\.api\+json/);
|
|
112
|
+ });
|
|
113
|
+
|
|
114
|
+ });
|
|
115
|
+ });
|
|
116
|
+
|
|
117
|
+ describe('PATCH', function () {
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+ // fs functions changed to promise
|
|
121
|
+ const stat = promisify(fs.stat);
|
|
122
|
+ const readdir = promisify(fs.readdir);
|
|
123
|
+ const unlink = promisify(fs.unlink);
|
|
124
|
+ const readFile = promisify(fs.readFile);
|
|
125
|
+ const fsp = { // fs promisified
|
|
126
|
+ open: promisify(fs.open),
|
|
127
|
+ close: promisify(fs.close)
|
|
128
|
+ };
|
|
129
|
+
|
|
130
|
+ // clear ./uploads/ folder
|
|
131
|
+ afterEach(async () => {
|
|
132
|
+ const files = await readdir(path.resolve('./uploads'));
|
|
133
|
+ const filePromises = files.map(file => unlink(path.resolve(`./uploads/${file}`)));
|
|
134
|
+ await Promise.all(filePromises);
|
|
135
|
+ const filesAfter = await readdir(path.resolve('./uploads'));
|
|
136
|
+
|
|
137
|
+ // check that the /uploads folder is empty
|
|
138
|
+ should(filesAfter).Array().length(0);
|
|
139
|
+ });
|
|
140
|
+
|
|
141
|
+ // clear ./files/avatars/
|
|
142
|
+ afterEach(async () => {
|
|
143
|
+
|
|
144
|
+ const avatarsDir = path.resolve('./files/avatars');
|
|
145
|
+ const folders = await readdir(avatarsDir);
|
|
146
|
+ const delPromises = folders.map(folder => rimraf(`./files/avatars/${folder}`));
|
|
147
|
+ await Promise.all(delPromises);
|
|
148
|
+
|
|
149
|
+ // check that the /files/avatars folder is empty
|
|
150
|
+ should(await readdir(avatarsDir)).Array().length(0);
|
|
151
|
+ });
|
|
152
|
+
|
|
153
|
+ context('logged as :username', function () {
|
|
154
|
+
|
|
155
|
+ context('good data type (png, jpeg)', function () {
|
|
156
|
+
|
|
157
|
+ it('[png] responds with 204 and saves the image as jpg', async () => {
|
|
158
|
+ await agent
|
|
159
|
+ .patch(`/users/${loggedUser.username}/avatar`)
|
|
160
|
+ .attach('avatar', './test/img/avatar.png')
|
|
161
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
162
|
+ .set('Content-Type', 'image/jpeg')
|
|
163
|
+ .expect(204);
|
|
164
|
+
|
|
165
|
+ // check images' existence
|
|
166
|
+ const expectedSizes = [16, 32, 64, 128, 256, 512];
|
|
167
|
+ const imagePromises = expectedSizes.map(size => stat(path.resolve(`./files/avatars/${loggedUser.username}/${size}`)));
|
|
168
|
+
|
|
169
|
+ await Promise.all(imagePromises);
|
|
170
|
+
|
|
171
|
+ const buffer512 = await readFile(path.resolve(`./files/avatars/${loggedUser.username}/512`));
|
|
172
|
+ should(typeOf(buffer512)).deepEqual({ ext: 'jpg', mime: 'image/jpeg' });
|
|
173
|
+ });
|
|
174
|
+
|
|
175
|
+ it('[jpeg] responds with 204 and saves the image', async () => {
|
|
176
|
+ await agent
|
|
177
|
+ .patch(`/users/${loggedUser.username}/avatar`)
|
|
178
|
+ .attach('avatar', './test/img/avatar.jpg')
|
|
179
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
180
|
+ .set('Content-Type', 'image/jpeg')
|
|
181
|
+ .expect(204);
|
|
182
|
+
|
|
183
|
+ // check images' existence
|
|
184
|
+ const expectedSizes = [16, 32, 64, 128, 256, 512];
|
|
185
|
+ const imagePromises = expectedSizes.map(size => stat(path.resolve(`./files/avatars/${loggedUser.username}/${size}`)));
|
|
186
|
+
|
|
187
|
+ await Promise.all(imagePromises);
|
|
188
|
+ });
|
|
189
|
+
|
|
190
|
+ it('delete the temporary file from ./uploads', async () => {
|
|
191
|
+ await agent
|
|
192
|
+ .patch(`/users/${loggedUser.username}/avatar`)
|
|
193
|
+ .attach('avatar', './test/img/avatar.jpg')
|
|
194
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
195
|
+ .set('Content-Type', 'image/jpeg')
|
|
196
|
+ .expect(204);
|
|
197
|
+
|
|
198
|
+ const files = await readdir(path.resolve('./uploads'));
|
|
199
|
+ should(files).Array().length(0);
|
|
200
|
+
|
|
201
|
+ });
|
|
202
|
+
|
|
203
|
+ it('crops and resizes the image to square 512x512px before saving', async () => {
|
|
204
|
+ await agent
|
|
205
|
+ .patch(`/users/${loggedUser.username}/avatar`)
|
|
206
|
+ .attach('avatar', './test/img/avatar.jpg')
|
|
207
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
208
|
+ .set('Content-Type', 'image/jpeg')
|
|
209
|
+ .expect(204);
|
|
210
|
+
|
|
211
|
+ const { width, height } = await sizeOf(path.resolve(`./files/avatars/${loggedUser.username}/512`));
|
|
212
|
+ should(width).eql(512);
|
|
213
|
+ should(height).eql(512);
|
|
214
|
+ });
|
|
215
|
+ });
|
|
216
|
+
|
|
217
|
+ context('bad data', function () {
|
|
218
|
+
|
|
219
|
+ it('[unsupported mime type] 400', async () => {
|
|
220
|
+ await agent
|
|
221
|
+ .patch(`/users/${loggedUser.username}/avatar`)
|
|
222
|
+ .attach('avatar', './test/img/bad-avatar.jpg')
|
|
223
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
224
|
+ .set('Content-Type', 'image/jpeg')
|
|
225
|
+ .expect(400);
|
|
226
|
+ });
|
|
227
|
+
|
|
228
|
+ it('[too large image (over 2MB)] 400', async () => {
|
|
229
|
+ await agent
|
|
230
|
+ .patch(`/users/${loggedUser.username}/avatar`)
|
|
231
|
+ .attach('avatar', './test/img/large-avatar.jpg')
|
|
232
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
233
|
+ .set('Content-Type', 'image/jpeg')
|
|
234
|
+ .expect(400);
|
|
235
|
+ });
|
|
236
|
+
|
|
237
|
+ it('deletes the temporary file from ./uploads', async () => {
|
|
238
|
+ await agent
|
|
239
|
+ .patch(`/users/${loggedUser.username}/avatar`)
|
|
240
|
+ .attach('avatar', './test/img/bad-avatar.jpg')
|
|
241
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
242
|
+ .set('Content-Type', 'image/jpeg')
|
|
243
|
+ .expect(400);
|
|
244
|
+
|
|
245
|
+ await agent
|
|
246
|
+ .patch(`/users/${loggedUser.username}/avatar`)
|
|
247
|
+ .attach('avatar', './test/img/large-avatar.jpg')
|
|
248
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
249
|
+ .set('Content-Type', 'image/jpeg')
|
|
250
|
+ .expect(400);
|
|
251
|
+
|
|
252
|
+ const files = await readdir(path.resolve('./uploads'));
|
|
253
|
+ should(files).Array().length(0);
|
|
254
|
+ });
|
|
255
|
+
|
|
256
|
+ });
|
|
257
|
+ });
|
|
258
|
+
|
|
259
|
+ context('not logged as :username', function () {
|
|
260
|
+
|
|
261
|
+ it('responds with 403', async () => {
|
|
262
|
+ await agent
|
|
263
|
+ .patch(`/users/${otherUser.username}/avatar`)
|
|
264
|
+ .attach('avatar', './test/img/avatar.jpg')
|
|
265
|
+ .auth(loggedUser.username, loggedUser.password)
|
|
266
|
+ .set('Content-Type', 'image/jpeg')
|
|
267
|
+ .expect(403);
|
|
268
|
+ });
|
|
269
|
+
|
|
270
|
+ });
|
|
271
|
+
|
|
272
|
+ describe('job: regularly clear all temp files older than 1 minute', () => {
|
|
273
|
+ it('should leave the /uploads folder empty', async () => {
|
|
274
|
+ const uploads = path.resolve('./uploads');
|
|
275
|
+
|
|
276
|
+ // first there should be some files
|
|
277
|
+ const filePromises = [0, 1, 2, 3, 4].map(async name => fsp.close(await fsp.open(path.resolve(`./uploads/${name}`), 'w')));
|
|
278
|
+ await Promise.all(filePromises);
|
|
279
|
+
|
|
280
|
+ const files = await readdir(uploads);
|
|
281
|
+ should(files).Array().length(5);
|
|
282
|
+
|
|
283
|
+ // then we run the job
|
|
284
|
+ await clearTemporary();
|
|
285
|
+
|
|
286
|
+ // then there should be no files
|
|
287
|
+ const filesAfter = await readdir(uploads);
|
|
288
|
+ should(filesAfter).Array().length(0);
|
|
289
|
+ });
|
|
290
|
+ });
|
|
291
|
+
|
|
292
|
+ });
|
|
293
|
+
|
|
294
|
+ describe('DELETE', function () {
|
|
295
|
+
|
|
296
|
+ context('logged as :username', function () {
|
|
297
|
+
|
|
298
|
+ it('[data on server] responds with 204 and deletes the avatar from server');
|
|
299
|
+
|
|
300
|
+ it('[no user image] responds with 204');
|
|
301
|
+
|
|
302
|
+ });
|
|
303
|
+
|
|
304
|
+ context('not logged as :username', function () {
|
|
305
|
+
|
|
306
|
+ it('responds with 403');
|
|
307
|
+
|
|
308
|
+ });
|
|
309
|
+
|
|
310
|
+ });
|
|
311
|
+});
|