From 032ec8b443fd65be55a4d414f6db908489cc599f Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 20 Apr 2018 14:12:15 -0400 Subject: [PATCH] tests!!! glorious tests!!! re: #6463 --- src/user/blocks.js | 10 ++++- test/user.js | 110 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) diff --git a/src/user/blocks.js b/src/user/blocks.js index 7cd2f1da76..bd4879136f 100644 --- a/src/user/blocks.js +++ b/src/user/blocks.js @@ -30,7 +30,7 @@ module.exports = function (User) { } // for debugging - blocked = [2]; + // blocked = [2]; blocked = blocked.map(uid => parseInt(uid, 10)).filter(Boolean); User.blocks._cache.set(uid, blocked); @@ -41,6 +41,10 @@ module.exports = function (User) { User.blocks.add = function (targetUid, uid, callback) { async.waterfall([ async.apply(db.sortedSetAdd.bind(db), 'uid:' + uid + ':blocked_uids', Date.now(), targetUid), + function (next) { + User.blocks._cache.del(uid); + setImmediate(next); + }, async.apply(User.blocks.list, uid), ], callback); }; @@ -48,6 +52,10 @@ module.exports = function (User) { User.blocks.remove = function (targetUid, uid, callback) { async.waterfall([ async.apply(db.sortedSetRemove.bind(db), 'uid:' + uid + ':blocked_uids', targetUid), + function (next) { + User.blocks._cache.del(uid); + setImmediate(next); + }, async.apply(User.blocks.list, uid), ], callback); }; diff --git a/test/user.js b/test/user.js index f96205a5e4..1b3ef100d8 100644 --- a/test/user.js +++ b/test/user.js @@ -1753,4 +1753,114 @@ describe('User', function () { }); }); }); + + describe('user blocking methods', function (done) { + let blockeeUid; + before(function (done) { + User.create({ + username: 'blockee', + email: 'blockee@example.org', + fullname: 'Block me', + }, function (err, uid) { + blockeeUid = uid; + done(err); + }); + }); + + describe('.remove()', function () { + it('should unblock a uid', function (done) { + User.blocks.remove(blockeeUid, 1, function (err, blocked_uids) { + assert.ifError(err); + assert.strictEqual(Array.isArray(blocked_uids), true); + assert.strictEqual(blocked_uids.length, 0); + done(); + }); + }); + }); + + describe('.add()', function () { + it('should block a uid', function (done) { + User.blocks.add(blockeeUid, 1, function (err, blocked_uids) { + assert.ifError(err); + assert.strictEqual(Array.isArray(blocked_uids), true); + assert.strictEqual(blocked_uids.length, 1); + assert.strictEqual(blocked_uids.includes(blockeeUid), true); + done(); + }); + }); + }); + + describe('.list()', function () { + it('should return a list of blocked uids', function (done) { + User.blocks.list(1, function (err, blocked_uids) { + assert.ifError(err); + assert.strictEqual(Array.isArray(blocked_uids), true); + assert.strictEqual(blocked_uids.length, 1); + assert.strictEqual(blocked_uids.includes(blockeeUid), true); + done(); + }); + }); + }); + + describe('.filter()', function () { + it('should remove entries by blocked uids and return filtered set', function (done) { + User.blocks.filter(1, [{ + foo: 'foo', + uid: blockeeUid, + }, { + foo: 'bar', + uid: 1, + }, { + foo: 'baz', + uid: blockeeUid, + }], function (err, filtered) { + assert.ifError(err); + assert.strictEqual(Array.isArray(filtered), true); + assert.strictEqual(filtered.length, 1); + assert.equal(filtered[0].uid, 1); + done(); + }); + }); + + it('should allow property argument to be passed in to customise checked property', function (done) { + User.blocks.filter(1, 'fromuid', [{ + foo: 'foo', + fromuid: blockeeUid, + }, { + foo: 'bar', + fromuid: 1, + }, { + foo: 'baz', + fromuid: blockeeUid, + }], function (err, filtered) { + assert.ifError(err); + assert.strictEqual(Array.isArray(filtered), true); + assert.strictEqual(filtered.length, 1); + assert.equal(filtered[0].fromuid, 1); + done(); + }); + }); + + it('should not process invalid sets', function (done) { + User.blocks.filter(1, [{ foo: 'foo' }, { foo: 'bar' }, { foo: 'baz' }], function (err, filtered) { + assert.ifError(err); + assert.strictEqual(Array.isArray(filtered), true); + assert.strictEqual(filtered.length, 3); + filtered.forEach(function (obj) { + assert.strictEqual(obj.hasOwnProperty('foo'), true); + }); + done(); + }); + }); + + it('should process plain sets that just contain uids', function (done) { + User.blocks.filter(1, [1, blockeeUid], function (err, filtered) { + assert.ifError(err); + assert.strictEqual(filtered.length, 1); + assert.strictEqual(filtered[0], 1); + done(); + }); + }); + }); + }); });