123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662 |
- 'use strict';
- const fs = require('fs');
- const md5 = require('md5');
- const path = require('path');
- const request = require('supertest');
- const server = require('./server');
- const clearUploadsDir = server.clearUploadsDir;
- const fileDir = server.fileDir;
- const uploadDir = server.uploadDir;
- const mockFiles = ['car.png', 'tree.png', 'basketball.png'];
- let mockUser = {
- firstName: 'Joe',
- lastName: 'Schmo',
- email: 'joe@mailinator.com'
- };
- // Reset response body.uploadDir/uploadPath for testing.
- const resetBodyUploadData = (res)=>{
- res.body.uploadDir = '';
- res.body.uploadPath = '';
- };
- describe('Test Directory Cleaning Method', function() {
- it('emptied "uploads" directory', function(done) {
- clearUploadsDir();
- let filesFound = fs.readdirSync(uploadDir).length;
- done(filesFound ? `Directory not empty. Found ${filesFound} files.` : null);
- });
- });
- describe('Test Single File Upload', function() {
- const app = server.setup();
- for (let i = 0; i < mockFiles.length; i++) {
- let fileName = mockFiles[i];
- it(`upload ${fileName} with POST`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end(function(err) {
- if (err) {
- return done(err);
- }
- fs.stat(uploadedFilePath, done);
- });
- });
- it(`upload ${fileName} with PUT`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end(function(err) {
- if (err) {
- return done(err);
- }
- fs.stat(uploadedFilePath, done);
- });
- });
- }
- it('fail when no files were attached', function(done) {
- request(app)
- .post('/upload/single')
- .expect(400)
- .end(done);
- });
- it('fail when using GET', function(done) {
- let filePath = path.join(fileDir, mockFiles[0]);
- request(app)
- .get('/upload/single')
- .attach('testFile', filePath)
- .expect(400)
- .end(done);
- });
- it('fail when using HEAD', function(done) {
- let filePath = path.join(fileDir, mockFiles[0]);
- request(app)
- .head('/upload/single')
- .attach('testFile', filePath)
- .expect(400)
- .end(done);
- });
- });
- describe('Test Single File Upload w/ .mv()', function() {
- const app = server.setup();
- for (let i = 0; i < mockFiles.length; i++) {
- let fileName = mockFiles[i];
- it(`upload ${fileName} with POST w/ .mv()`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end(function(err) {
- if (err) {
- return done(err);
- }
- fs.stat(uploadedFilePath, done);
- });
- });
- it(`upload ${fileName} with PUT w/ .mv()`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end(function(err) {
- if (err) {
- return done(err);
- }
- fs.stat(uploadedFilePath, done);
- });
- });
- }
- });
- describe('Test Single File Upload with useTempFiles option.', function() {
- const app = server.setup({
- useTempFiles: true,
- tempFileDir: '/tmp/'
- });
- for (let i = 0; i < mockFiles.length; i++) {
- let fileName = mockFiles[i];
- it(`upload ${fileName} with POST`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end((err) => {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- it(`upload ${fileName} with PUT`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end((err) => {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- }
- it('fail when no files were attached', function(done) {
- request(app)
- .post('/upload/single')
- .expect(400)
- .end(done);
- });
- it('fail when using GET', function(done) {
- let filePath = path.join(fileDir, mockFiles[0]);
- request(app)
- .get('/upload/single')
- .attach('testFile', filePath)
- .expect(400)
- .end(done);
- });
- it('fail when using HEAD', function(done) {
- let filePath = path.join(fileDir, mockFiles[0]);
- request(app)
- .head('/upload/single')
- .attach('testFile', filePath)
- .expect(400)
- .end(done);
- });
- });
- describe('Test Single File Upload with useTempFiles option and empty tempFileDir.', function() {
- const app = server.setup({
- useTempFiles: true,
- tempFileDir: ''
- });
- for (let i = 0; i < mockFiles.length; i++) {
- let fileName = mockFiles[i];
- it(`upload ${fileName} with POST`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end((err) => {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- }
- });
- describe('Test Single File Upload w/ .mv() Promise', function() {
- const app = server.setup();
- for (let i = 0; i < mockFiles.length; i++) {
- let fileName = mockFiles[i];
- it(`upload ${fileName} with POST w/ .mv() Promise`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single/promise')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end((err) => {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- it(`upload ${fileName} with PUT w/ .mv() Promise`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single/promise')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end((err) => {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- }
- it('fail when no files were attached', function(done) {
- request(app)
- .post('/upload/single')
- .expect(400)
- .end(done);
- });
- it('fail when using GET', function(done) {
- let filePath = path.join(fileDir, mockFiles[0]);
- request(app)
- .get('/upload/single')
- .attach('testFile', filePath)
- .expect(400)
- .end(done);
- });
- it('fail when using HEAD', function(done) {
- let filePath = path.join(fileDir, mockFiles[0]);
- request(app)
- .head('/upload/single')
- .attach('testFile', filePath)
- .expect(400)
- .end(done);
- });
- });
- describe('Test Single File Upload w/ .mv() Promise and useTempFiles set to true', function() {
- const app = server.setup({
- useTempFiles: true,
- tempFileDir: '/tmp/'
- });
- for (let i = 0; i < mockFiles.length; i++) {
- let fileName = mockFiles[i];
- it(`upload ${fileName} with POST w/ .mv() Promise`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single/promise')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end((err) => {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- it(`upload ${fileName} with PUT w/ .mv() Promise`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single/promise')
- .attach('testFile', filePath)
- .expect(resetBodyUploadData)
- .expect(200, {
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- })
- .end((err) => {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- }
- it('fail when no files were attached', function(done) {
- request(app)
- .post('/upload/single')
- .expect(400)
- .end(done);
- });
- it('fail when using GET', function(done) {
- let filePath = path.join(fileDir, mockFiles[0]);
- request(app)
- .get('/upload/single')
- .attach('testFile', filePath)
- .expect(400)
- .end(done);
- });
- it('fail when using HEAD', function(done) {
- let filePath = path.join(fileDir, mockFiles[0]);
- request(app)
- .head('/upload/single')
- .attach('testFile', filePath)
- .expect(400)
- .end(done);
- });
- });
- describe('Test Multi-File Upload', function() {
- const app = server.setup();
- it('upload multiple files with POST', function(done) {
- let req = request(app).post('/upload/multiple');
- clearUploadsDir();
- let expectedResult = [];
- let expectedResultSorted = [];
- let uploadedFilesPath = [];
- mockFiles.forEach((fileName, index) => {
- let filePath = path.join(fileDir, fileName);
- let fileStat = fs.statSync(filePath);
- uploadedFilesPath.push(path.join(uploadDir, fileName));
- expectedResult.push({
- name:fileName,
- md5: md5(fs.readFileSync(filePath)),
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- });
- req.attach(`testFile${index+1}`, filePath);
- });
- req
- .expect((res) => {
- res.body.forEach(fileInfo => {
- fileInfo.uploadDir = '';
- fileInfo.uploadPath = '';
- let index = mockFiles.indexOf(fileInfo.name);
- expectedResultSorted.push(expectedResult[index]);
- });
- })
- .expect(200, expectedResultSorted)
- .end((err) => {
- if (err) return done(err);
- fs.stat(uploadedFilesPath[0], (err) => {
- if (err) return done(err);
- fs.stat(uploadedFilesPath[1], function(err) {
- if (err) return done(err);
- fs.stat(uploadedFilesPath[2], done);
- });
- });
- });
- });
- });
- describe('Test File Array Upload', function() {
- const app = server.setup();
- it('upload array of files with POST', function(done) {
- let req = request(app).post('/upload/array');
- clearUploadsDir();
- let expectedResult = [];
- let expectedResultSorted = [];
- let uploadedFilesPath = [];
- mockFiles.forEach((fileName) => {
- let filePath = path.join(fileDir, fileName);
- let fileStat = fs.statSync(filePath);
- uploadedFilesPath.push(path.join(uploadDir, fileName));
- expectedResult.push({
- name:fileName,
- md5: md5(fs.readFileSync(filePath)),
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- });
- req.attach('testFiles', filePath);
- });
- req
- .expect((res)=>{
- res.body.forEach(fileInfo => {
- fileInfo.uploadDir = '';
- fileInfo.uploadPath = '';
- let index = mockFiles.indexOf(fileInfo.name);
- expectedResultSorted.push(expectedResult[index]);
- });
- })
- .expect(200, expectedResultSorted)
- .end((err) => {
- if (err) return done(err);
- uploadedFilesPath.forEach((uploadedFilePath) => {
- fs.statSync(uploadedFilePath);
- });
- done();
- });
- });
- });
- describe('Test Upload With Fields', function() {
- const app = server.setup();
- for (let i = 0; i < mockFiles.length; i++) {
- let fileName = mockFiles[i];
- it(`upload ${fileName} and submit fields at the same time with POST`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileHash = md5(fileBuffer);
- let fileStat = fs.statSync(filePath);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .post('/upload/single/withfields')
- .attach('testFile', filePath)
- .field('firstName', mockUser.firstName)
- .field('lastName', mockUser.lastName)
- .field('email', mockUser.email)
- .expect(resetBodyUploadData)
- .expect(200, {
- firstName: mockUser.firstName,
- lastName: mockUser.lastName,
- email: mockUser.email,
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- },
- function(err) {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- it(`upload ${fileName} and submit fields at the same time with PUT`, function(done) {
- let filePath = path.join(fileDir, fileName);
- let fileBuffer = fs.readFileSync(filePath);
- let fileStat = fs.statSync(filePath);
- let fileHash = md5(fileBuffer);
- let uploadedFilePath = path.join(uploadDir, fileName);
- clearUploadsDir();
- request(app)
- .put('/upload/single/withfields')
- .attach('testFile', filePath)
- .field('firstName', mockUser.firstName)
- .field('lastName', mockUser.lastName)
- .field('email', mockUser.email)
- .expect(resetBodyUploadData)
- .expect(200, {
- firstName: mockUser.firstName,
- lastName: mockUser.lastName,
- email: mockUser.email,
- name: fileName,
- md5: fileHash,
- size: fileStat.size,
- uploadDir: '',
- uploadPath: ''
- },
- function(err) {
- if (err) return done(err);
- fs.stat(uploadedFilePath, done);
- });
- });
- }
- });
|