more tests for admin uploads

v1.18.x
Barış Soner Uşaklı 8 years ago
parent 29c0b8337e
commit 66dee3452f

@ -14,7 +14,7 @@ var plugins = require('../../plugins');
var allowedImageTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif', 'image/svg+xml'];
var uploadsController = {};
var uploadsController = module.exports;
uploadsController.uploadCategoryPicture = function (req, res, next) {
var uploadedFile = req.files.files[0];
@ -23,11 +23,7 @@ uploadsController.uploadCategoryPicture = function (req, res, next) {
try {
params = JSON.parse(req.body.params);
} catch (e) {
fs.unlink(uploadedFile.path, function (err) {
if (err) {
winston.error(err);
}
});
deleteTempFile(uploadedFile.path);
return next(e);
}
@ -43,11 +39,7 @@ uploadsController.uploadFavicon = function (req, res, next) {
if (validateUpload(req, res, next, uploadedFile, allowedTypes)) {
file.saveFileToLocal('favicon.ico', 'system', uploadedFile.path, function (err, image) {
fs.unlink(uploadedFile.path, function (err) {
if (err) {
winston.error(err);
}
});
deleteTempFile(uploadedFile.path);
if (err) {
return next(err);
}
@ -80,11 +72,7 @@ uploadsController.uploadTouchIcon = function (req, res, next) {
}),
], next);
}, function (err) {
fs.unlink(uploadedFile.path, function (err) {
if (err) {
winston.error(err);
}
});
deleteTempFile(uploadedFile.path);
if (err) {
return next(err);
@ -108,18 +96,19 @@ uploadsController.uploadSound = function (req, res, next) {
return next(Error('[[error:invalid-data]]'));
}
file.saveFileToLocal(uploadedFile.name, 'sounds', uploadedFile.path, function (err) {
async.waterfall([
function (next) {
file.saveFileToLocal(uploadedFile.name, 'sounds', uploadedFile.path, next);
},
function (uploadedSound, next) {
meta.sounds.build(next);
},
], function (err) {
deleteTempFile(uploadedFile.path);
if (err) {
return next(err);
}
meta.sounds.build(function (err) {
if (err) {
return next(err);
}
res.json([{}]);
});
res.json([{}]);
});
};
@ -142,12 +131,7 @@ function upload(name, req, res, next) {
function validateUpload(req, res, next, uploadedFile, allowedTypes) {
if (allowedTypes.indexOf(uploadedFile.type) === -1) {
fs.unlink(uploadedFile.path, function (err) {
if (err) {
winston.error(err);
}
});
deleteTempFile(uploadedFile.path);
res.json({ error: '[[error:invalid-image-type, ' + allowedTypes.join(', ') + ']]' });
return false;
}
@ -156,24 +140,27 @@ function validateUpload(req, res, next, uploadedFile, allowedTypes) {
}
function uploadImage(filename, folder, uploadedFile, req, res, next) {
function done(err, image) {
fs.unlink(uploadedFile.path, function (err) {
if (err) {
winston.error(err);
async.waterfall([
function (next) {
if (plugins.hasListeners('filter:uploadImage')) {
plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.user.uid }, next);
} else {
file.saveFileToLocal(filename, folder, uploadedFile.path, next);
}
});
},
], function (err, image) {
deleteTempFile(uploadedFile.path);
if (err) {
return next(err);
}
res.json([{ name: uploadedFile.name, url: image.url.startsWith('http') ? image.url : nconf.get('relative_path') + image.url }]);
}
if (plugins.hasListeners('filter:uploadImage')) {
plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.user.uid }, done);
} else {
file.saveFileToLocal(filename, folder, uploadedFile.path, done);
}
});
}
module.exports = uploadsController;
function deleteTempFile(path) {
fs.unlink(path, function (err) {
if (err) {
winston.error(err);
}
});
}

@ -89,7 +89,9 @@ module.exports = function (Meta) {
},
], cb);
},
], callback);
], function (err) {
callback(err);
});
});
};

Binary file not shown.

@ -173,6 +173,23 @@ describe('Upload Controllers', function () {
});
});
it('should fail to upload invalid file type', function (done) {
helpers.uploadFile(nconf.get('url') + '/api/admin/category/uploadpicture', path.join(__dirname, '../test/files/503.html'), { params: JSON.stringify({ cid: cid }) }, jar, csrf_token, function (err, res, body) {
assert.ifError(err);
console.log(body);
assert.equal(body.error, '[[error:invalid-image-type, image/png, image/jpeg, image/pjpeg, image/jpg, image/gif, image/svg+xml]]');
done();
});
});
it('should fail to upload category image with invalid json params', function (done) {
helpers.uploadFile(nconf.get('url') + '/api/admin/category/uploadpicture', path.join(__dirname, '../test/files/test.png'), { params: 'invalid json' }, jar, csrf_token, function (err, res, body) {
assert.ifError(err);
assert.equal(body.error, 'Unexpected token i in JSON at position 0');
done();
});
});
it('should upload category image', function (done) {
helpers.uploadFile(nconf.get('url') + '/api/admin/category/uploadpicture', path.join(__dirname, '../test/files/test.png'), { params: JSON.stringify({ cid: cid }) }, jar, csrf_token, function (err, res, body) {
assert.ifError(err);
@ -183,6 +200,26 @@ describe('Upload Controllers', function () {
});
});
it('should fail to upload invalid sound file', function (done) {
helpers.uploadFile(nconf.get('url') + '/api/admin/upload/sound', path.join(__dirname, '../test/files/test.png'), { }, jar, csrf_token, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 500);
assert.equal(body.error, '[[error:invalid-data]]');
done();
});
});
it('should upload sound file', function (done) {
helpers.uploadFile(nconf.get('url') + '/api/admin/upload/sound', path.join(__dirname, '../test/files/test.wav'), { }, jar, csrf_token, function (err, res, body) {
assert.ifError(err);
assert.equal(res.statusCode, 200);
assert(body);
console.log(body);
done();
});
});
it('should upload favicon', function (done) {
helpers.uploadFile(nconf.get('url') + '/api/admin/uploadfavicon', path.join(__dirname, '../test/files/favicon.ico'), {}, jar, csrf_token, function (err, res, body) {
assert.ifError(err);

Loading…
Cancel
Save