From c043cfebd66c10b29be7ec50f9ad10eda33e3f0c Mon Sep 17 00:00:00 2001
From: Julian Lam <julian@nodebb.org>
Date: Fri, 4 Dec 2020 16:42:39 -0500
Subject: [PATCH] fix: added back missing topic thumb tests that were removed
 in last commit

---
 src/controllers/helpers.js      |  5 +++++
 src/controllers/uploads.js      |  4 ++--
 src/controllers/write/topics.js |  4 ++--
 test/topics/thumbs.js           | 25 +++++++++++++++++++++++++
 4 files changed, 34 insertions(+), 4 deletions(-)

diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js
index 0c34f9cbbb..517035f0f8 100644
--- a/src/controllers/helpers.js
+++ b/src/controllers/helpers.js
@@ -470,6 +470,11 @@ helpers.generateError = (statusCode, message) => {
 			payload.status.code = 'not-implemented';
 			payload.status.message = message || 'The route you are trying to call is not implemented yet, please try again tomorrow';
 			break;
+
+		case 503:
+			payload.status.code = 'service-unavailable';
+			payload.status.message = message || 'The route you are trying to call is not currently available due to a server configuration';
+			break;
 	}
 
 	return payload;
diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js
index be1ff5529e..8055c02b98 100644
--- a/src/controllers/uploads.js
+++ b/src/controllers/uploads.js
@@ -109,10 +109,10 @@ async function resizeImage(fileObj) {
 	return fileObj;
 }
 
-uploadsController.uploadThumb = async function (req, res, next) {
+uploadsController.uploadThumb = async function (req, res) {
 	if (!meta.config.allowTopicsThumbnail) {
 		deleteTempFiles(req.files.files);
-		return next(new Error('[[error:topic-thumbnails-are-disabled]]'));
+		return helpers.formatApiResponse(503, res, new Error('[[error:topic-thumbnails-are-disabled]]'));
 	}
 
 	return await uploadsController.upload(req, res, async function (uploadedFile) {
diff --git a/src/controllers/write/topics.js b/src/controllers/write/topics.js
index 53cfbcc037..974e6d1734 100644
--- a/src/controllers/write/topics.js
+++ b/src/controllers/write/topics.js
@@ -94,13 +94,13 @@ Topics.getThumbs = async (req, res) => {
 	helpers.formatApiResponse(200, res, await topics.thumbs.get(req.params.tid));
 };
 
-Topics.addThumb = async (req, res, next) => {
+Topics.addThumb = async (req, res) => {
 	await checkThumbPrivileges({ tid: req.params.tid, uid: req.user.uid, res });
 	if (res.headersSent) {
 		return;
 	}
 
-	const files = await uploadsController.uploadThumb(req, res, next);	// response is handled here
+	const files = await uploadsController.uploadThumb(req, res);	// response is handled here
 
 	// Add uploaded files to topic zset
 	if (files && files.length) {
diff --git a/test/topics/thumbs.js b/test/topics/thumbs.js
index 05350bc3e9..b48f1a43c1 100644
--- a/test/topics/thumbs.js
+++ b/test/topics/thumbs.js
@@ -193,5 +193,30 @@ describe('Topic thumbs', () => {
 				done();
 			});
 		});
+
+		it('should fail if thumbnails are not enabled', (done) => {
+			meta.config.allowTopicsThumbnail = 0;
+
+			helpers.uploadFile(`${nconf.get('url')}/api/v3/topics/${uuid}/thumbs`, path.join(__dirname, '../files/test.png'), {}, adminJar, adminCSRF, function (err, res, body) {
+				assert.ifError(err);
+				assert.strictEqual(res.statusCode, 503);
+				assert(body && body.status);
+				assert.strictEqual(body.status.message, '[[error:topic-thumbnails-are-disabled]]');
+				done();
+			});
+		});
+
+		it('should fail if file is not image', function (done) {
+			meta.config.allowTopicsThumbnail = 1;
+
+			helpers.uploadFile(`${nconf.get('url')}/api/v3/topics/${uuid}/thumbs`, path.join(__dirname, '../files/503.html'), {}, adminJar, adminCSRF, function (err, res, body) {
+				assert.ifError(err);
+				console.log(body);
+				assert.strictEqual(res.statusCode, 500);
+				assert(body && body.status);
+				assert.strictEqual(body.status.message, '[[error:invalid-file]]');
+				done();
+			});
+		});
 	});
 });