Skip to content

Node.js based forum software built for the modern web

tests for #6455

Julian Lam5943389b7abf
diff --git a/src/posts/uploads.js b/src/posts/uploads.js
index 42c90477a9..610296beec 100644
--- a/src/posts/uploads.js
+++ b/src/posts/uploads.js
@@ -46,8 +46,8 @@ module.exports = function (Posts) {
Posts.uploads.associate = function (pid, filePaths, callback) {
// Adds an upload to a post's sorted set of uploads
const now = Date.now();
- const scores = filePaths.map(() => now);
filePaths = !Array.isArray(filePaths) ? [filePaths] : filePaths;
+ const scores = filePaths.map(() => now);
db.sortedSetAdd('post:' + pid + ':uploads', scores, filePaths, callback);
};
diff --git a/test/posts.js b/test/posts.js
index 84114d0a91..db79a401cf 100644
--- a/test/posts.js
+++ b/test/posts.js
@@ -877,4 +877,121 @@ describe('Post\'s', function () {
], done);
});
});
+
+ describe('upload methods', function () {
+ var pid;
+
+ before(function (done) {
+ topics.post({
+ uid: 1,
+ cid: 1,
+ title: 'topic with some images',
+ content: 'here is an image [alt text](/assets/uploads/files/abracadabra.png) and another [alt text](/assets/uploads/files/shazam.jpg)',
+ }, function (err, topicPostData) {
+ assert.ifError(err);
+ pid = topicPostData.postData.pid;
+ done();
+ });
+ });
+
+ describe('.sync()', function () {
+ it('should properly add new images to the post\'s zset', function (done) {
+ posts.uploads.sync(pid, function (err) {
+ assert.ifError(err);
+
+ db.sortedSetCard('post:' + pid + ':uploads', function (err, length) {
+ assert.ifError(err);
+ assert.strictEqual(2, length);
+ done();
+ });
+ });
+ });
+
+ it('should remove an image if it is edited out of the post', function (done) {
+ async.series([
+ function (next) {
+ posts.edit({
+ pid: pid,
+ uid: 1,
+ content: 'here is an image [alt text](/assets/uploads/files/abracadabra.png)... AND NO MORE!',
+ }, next);
+ },
+ async.apply(posts.uploads.sync, pid),
+ ], function (err) {
+ assert.ifError(err);
+ db.sortedSetCard('post:' + pid + ':uploads', function (err, length) {
+ assert.ifError(err);
+ assert.strictEqual(1, length);
+ done();
+ });
+ });
+ });
+ });
+
+ describe('.list()', function () {
+ it('should display the uploaded files for a specific post', function (done) {
+ posts.uploads.list(pid, function (err, uploads) {
+ assert.ifError(err);
+ assert.equal(true, Array.isArray(uploads));
+ assert.strictEqual(1, uploads.length);
+ assert.equal('string', typeof uploads[0]);
+ done();
+ });
+ });
+ });
+
+ describe('.associate()', function () {
+ it('should add an image to the post\'s maintained list of uploads', function (done) {
+ async.waterfall([
+ async.apply(posts.uploads.associate, pid, 'whoa.gif'),
+ async.apply(posts.uploads.list, pid),
+ ], function (err, uploads) {
+ assert.ifError(err);
+ assert.strictEqual(2, uploads.length);
+ assert.strictEqual('whoa.gif', uploads[1]);
+ done();
+ });
+ });
+
+ it('should allow arrays to be passed in', function (done) {
+ async.waterfall([
+ async.apply(posts.uploads.associate, pid, ['amazeballs.jpg', 'wut.txt']),
+ async.apply(posts.uploads.list, pid),
+ ], function (err, uploads) {
+ assert.ifError(err);
+ assert.strictEqual(4, uploads.length);
+ assert.strictEqual('amazeballs.jpg', uploads[2]);
+ assert.strictEqual('wut.txt', uploads[3]);
+ done();
+ });
+ });
+ });
+
+ describe('.dissociate()', function () {
+ it('should remove an image from the post\'s maintained list of uploads', function (done) {
+ async.waterfall([
+ async.apply(posts.uploads.dissociate, pid, 'whoa.gif'),
+ async.apply(posts.uploads.list, pid),
+ ], function (err, uploads) {
+ assert.ifError(err);
+ assert.strictEqual(3, uploads.length);
+ assert.strictEqual(false, uploads.includes('whoa.gif'));
+ done();
+ });
+ });
+
+ it('should allow arrays to be passed in', function (done) {
+ async.waterfall([
+ async.apply(posts.uploads.dissociate, pid, ['amazeballs.jpg', 'wut.txt']),
+ async.apply(posts.uploads.list, pid),
+ ], function (err, uploads) {
+ assert.ifError(err);
+ assert.strictEqual(1, uploads.length);
+ assert.strictEqual(false, uploads.includes('amazeballs.jpg'));
+ assert.strictEqual(false, uploads.includes('wut.txt'));
+ done();
+ });
+ });
+ });
+ });
});