From 28f87cc776ec0fefdab0341cf9333b57d2adaebc Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 30 Oct 2014 17:04:16 -0400 Subject: [PATCH] added tests for messaging method .canMessage --- tests/messaging.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 tests/messaging.js diff --git a/tests/messaging.js b/tests/messaging.js new file mode 100644 index 0000000000..a5945de950 --- /dev/null +++ b/tests/messaging.js @@ -0,0 +1,62 @@ +var assert = require('assert'), + db = require('./mocks/databasemock'), + async = require('async'), + User = require('../src/user'), + Groups = require('../src/groups'), + Messaging = require('../src/messaging'), + testUids; + +describe("Messaging Library", function() { + before(function(done) { + // Create 3 users: 1 admin, 2 regular + async.parallel([ + async.apply(User.create, { username: 'foo', password: 'bar' }), // admin + async.apply(User.create, { username: 'baz', password: 'quux' }), // restricted user + async.apply(User.create, { username: 'herp', password: 'derp' }) // regular user + ], function(err, uids) { + testUids = uids; + async.parallel([ + async.apply(Groups.join, 'administrators', uids[0]), + async.apply(User.setSetting, testUids[1], 'restrictChat', '1') + ], done); + }); + }); + + describe(".canMessage()", function() { + it("should allow messages to be sent to an unrestricted user", function(done) { + Messaging.canMessage(testUids[1], testUids[2], function(err, allowed) { + assert.strictEqual(allowed, true, 'should be true, received ' + allowed); + done(); + }); + }); + + it("should NOT allow messages to be sent to a restricted user", function(done) { + User.setSetting(testUids[1], 'restrictChat', '1', function() { + Messaging.canMessage(testUids[2], testUids[1], function(err, allowed) { + assert.strictEqual(allowed, false, 'should be false, received ' + allowed); + done(); + }); + }); + }); + + it("should always allow admins through", function(done) { + Messaging.canMessage(testUids[0], testUids[1], function(err, allowed) { + assert.strictEqual(allowed, true, 'should be true, received ' + allowed); + done(); + }); + }); + + it("should allow messages to be sent to a restricted user if restricted user follows sender", function(done) { + User.follow(testUids[1], testUids[2], function() { + Messaging.canMessage(testUids[2], testUids[1], function(err, allowed) { + assert.strictEqual(allowed, true, 'should be true, received ' + allowed); + done(); + }); + }); + }); + }); + + after(function() { + db.flushdb(); + }); +});