Skip to content

Commit

Permalink
adding more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgirges committed Feb 20, 2017
1 parent 9e3361a commit 9785534
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/multipartUploads.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ const mockFiles = [
'basketball.png'
];

let mockUser = {
firstName: 'Joe',
lastName: 'Schmo',
email: 'joe@mailinator.com'
};

describe('Test Directory Cleaning Method', function() {
it('emptied "uploads" directory', function(done) {
clearUploadsDir();
Expand Down Expand Up @@ -151,3 +157,34 @@ describe('Test File Array Upload', function() {
});
});
});

describe('Test Upload With Fields', function() {
for (let i = 0; i < mockFiles.length; i++) {
let fileName = mockFiles[i];

it(`upload ${fileName} and submit fields at the same time`, function(done) {
let filePath = path.join(fileDir, fileName);
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(200, {
firstName: mockUser.firstName,
lastName: mockUser.lastName,
email: mockUser.email
},
function(err, res) {
if (err)
return done(err);

fs.stat(uploadedFilePath, done);
});
});
}
});
31 changes: 31 additions & 0 deletions test/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,37 @@ app.all('/upload/single', function(req, res) {
});
});

app.all('/upload/single/withfields', function(req, res) {
if (!req.files)
return res.status(400).send('No files were uploaded.');

if (!req.body)
return res.status(400).send('No request body found');

if (!req.body.firstName || !req.body.firstName.trim())
return res.status(400).send('Invalid first name');

if (!req.body.lastName || !req.body.lastName.trim())
return res.status(400).send('Invalid last name');

if (!req.body.email || !req.body.email.trim())
return res.status(400).send('Invalid email');

let testFile = req.files.testFile;
let uploadPath = path.join(uploadDir, testFile.name);

testFile.mv(uploadPath, function(err) {
if (err)
return res.status(500).send(err);

res.json({
firstName: req.body.firstName,
lastName: req.body.lastName,
email: req.body.email
});
});
});

app.all('/upload/multiple', function(req, res) {
if (!req.files)
return res.status(400).send('No files were uploaded.');
Expand Down

0 comments on commit 9785534

Please sign in to comment.