-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilesController.UploadFiles.test.js
229 lines (211 loc) · 6.96 KB
/
FilesController.UploadFiles.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
import fs from 'fs';
import chai from 'chai';
import chaiHttp from 'chai-http';
import sha1 from 'sha1';
import { ObjectId, MongoClient } from 'mongodb';
import { createClient } from 'redis';
import { promisify } from 'util';
import { v4 } from 'uuid';
import app from '../server';
chai.use(chaiHttp);
const { expect, request } = chai;
/**
* Test cases for FileController.js endpoints:
* 1. POST files
*/
describe('FileController.js tests - file upload endpoint', () => {
let dbClient;
let db;
let rdClient;
let asyncSet;
let asyncKeys;
let asyncDel;
const DB_HOST = process.env.DB_HOST || 'localhost';
const DB_PORT = process.env.BD_PORT || 27017;
const DATABASE = process.env.DB_DATABASE || 'files_manager';
const FOLDER_PATH = process.env.FOLDER_PATH || '/tmp/files_manager';
const initialPassword = 'supersecretFYI';
const hashedPassword = sha1(initialPassword);
const user = { _id: new ObjectId(), email: '[email protected]', password: hashedPassword };
const token = v4();
const tokenKey = `auth_${token}`;
const folder = {
_id: new ObjectId(),
name: 'poems',
type: 'folder',
parentId: '0',
userId: user._id,
isPublic: false,
};
before(() => new Promise((resolve) => {
// Connect to db and clear collections
dbClient = new MongoClient(`mongodb://${DB_HOST}:${DB_PORT}`, { useUnifiedTopology: true });
dbClient.connect(async (error, client) => {
if (error) throw error;
db = await client.db(DATABASE);
await db.collection('users').deleteMany({});
// Create test user
await db.collection('users').insertOne(user);
await db.collection('files').insertOne(folder);
// Connect to redis and clear keys
rdClient = createClient();
asyncSet = promisify(rdClient.set).bind(rdClient);
asyncKeys = promisify(rdClient.keys).bind(rdClient);
asyncDel = promisify(rdClient.del).bind(rdClient);
rdClient.on('connect', async () => {
await asyncSet(tokenKey, user._id.toString());
resolve();
});
});
}));
after(async () => {
// Delete files
fs.rmdirSync(FOLDER_PATH, { recursive: true });
// Clear db collections
await db.collection('users').deleteMany({});
await db.collection('files').deleteMany({});
await db.dropDatabase();
await dbClient.close();
// Clear redis keys and close connection
const tokens = await asyncKeys('auth_*');
const thumbnailJobs = await asyncKeys('bull*')
const deleteKeysOperations = [];
for (const key of tokens) {
deleteKeysOperations.push(asyncDel(key));
}
for (const key of thumbnailJobs) {
deleteKeysOperations.push(asyncDel(key));
}
await Promise.all(deleteKeysOperations);
rdClient.quit();
});
describe('POST /files', () => {
let file;
const data = Buffer.from('Hello World').toString('base64');
beforeEach(() => {
file = {
name: Math.random().toString(32).substring(2),
type: 'file',
isPublic: false,
data: data,
};
});
it('should add a file to the database with parentId=0', (done) => {
request(app)
.post('/files')
.set('X-Token', token)
.send(file)
.end((error, res) => {
const responseAttributes = ['id', 'userId', 'name', 'type', 'isPublic', 'parentId'];
expect(error).to.be.null;
expect(res).to.have.status(201);
expect(res.body).to.include.all.keys(responseAttributes);
expect(res.body.name).to.equal(file.name);
expect(res.body.type).to.equal(file.type);
expect(res.body.isPublic).to.equal(file.isPublic);
expect(res.body.parentId).to.equal(0);
expect(fs.existsSync(FOLDER_PATH)).to.be.true;
expect(fs.lstatSync(FOLDER_PATH).isDirectory()).to.be.true;
expect(fs.readdirSync(FOLDER_PATH)).to.have.lengthOf.greaterThan(0);
done();
});
});
it('should add a file to the database with a given parentId', (done) => {
file.parentId = folder._id.toString();
request(app)
.post('/files')
.set('X-Token', token)
.send(file)
.end((error, res) => {
expect(error).to.be.null;
expect(res).to.have.status(201);
expect(res.body.parentId).to.equal(folder._id.toString());
expect(fs.readdirSync(FOLDER_PATH).length).to.equal(2);
done();
});
});
it('should unauthorize uploads using wrong token', (done) => {
request(app)
.post('/files')
.set('X-Token', v4())
.send(file)
.end((error, res) => {
expect(error).to.be.null;
expect(res).to.have.status(401);
expect(res.body.error).to.equal('Unauthorized');
done();
});
});
it('should unauthorize uploads with missing name', (done) => {
delete file.name;
request(app)
.post('/files')
.set('X-Token', token)
.send(file)
.end((error, res) => {
expect(error).to.be.null;
expect(res).to.have.status(400);
expect(res.body.error).to.equal('Missing name');
done();
});
});
it('should unauthorize uploads with missing type', (done) => {
delete file.type;
request(app)
.post('/files')
.set('X-Token', token)
.send(file)
.end((error, res) => {
expect(error).to.be.null;
expect(res).to.have.status(400);
expect(res.body.error).to.equal('Missing type');
done();
});
});
it('should unauthorize uploads with missing data if they are files', (done) => {
delete file.data;
request(app)
.post('/files')
.set('X-Token', token)
.send(file)
.end((error, res) => {
expect(error).to.be.null;
expect(res).to.have.status(400);
expect(res.body.error).to.equal('Missing data');
done();
});
});
it('should unauthorize uploads if parentId is not linked to any document', (done) => {
file.parentId = new ObjectId().toString();
request(app)
.post('/files')
.set('X-Token', token)
.send(file)
.end((error, res) => {
expect(error).to.be.null;
expect(res).to.have.status(400);
expect(res.body.error).to.equal('Parent not found');
done();
});
});
it('should unauthorize uploads if parentId is for a file or image and not a folder', (done) => {
request(app)
.post('/files')
.set('X-Token', token)
.send(file)
.end((_error, res) => {
file.parentId = res.body.id;
request(app)
.post('/files')
.set('X-Token', token)
.send(file)
.end((error, res) => {
expect(error).to.be.null;
expect(res).to.have.status(400);
expect(res.body.error).to.equal('Parent is not a folder');
done();
});
});
});
});
});