tests!!! glorious tests!!!

re: #6463
v1.18.x
Julian Lam 7 years ago
parent a367b698e3
commit 032ec8b443

@ -30,7 +30,7 @@ module.exports = function (User) {
} }
// for debugging // for debugging
blocked = [2]; // blocked = [2];
blocked = blocked.map(uid => parseInt(uid, 10)).filter(Boolean); blocked = blocked.map(uid => parseInt(uid, 10)).filter(Boolean);
User.blocks._cache.set(uid, blocked); User.blocks._cache.set(uid, blocked);
@ -41,6 +41,10 @@ module.exports = function (User) {
User.blocks.add = function (targetUid, uid, callback) { User.blocks.add = function (targetUid, uid, callback) {
async.waterfall([ async.waterfall([
async.apply(db.sortedSetAdd.bind(db), 'uid:' + uid + ':blocked_uids', Date.now(), targetUid), 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), async.apply(User.blocks.list, uid),
], callback); ], callback);
}; };
@ -48,6 +52,10 @@ module.exports = function (User) {
User.blocks.remove = function (targetUid, uid, callback) { User.blocks.remove = function (targetUid, uid, callback) {
async.waterfall([ async.waterfall([
async.apply(db.sortedSetRemove.bind(db), 'uid:' + uid + ':blocked_uids', targetUid), 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), async.apply(User.blocks.list, uid),
], callback); ], callback);
}; };

@ -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();
});
});
});
});
}); });

Loading…
Cancel
Save