mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 02:55:58 +01:00 
			
		
		
		
	new method emptydb use emptydb instead of flushdb, flushdb removes indices for mongo which breaks search tests initialize meta.config properly in tests enable nodebb-plugin-dbsearch for tests
		
			
				
	
	
		
			77 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			77 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| 'use strict';
 | |
| /*global require, before, after*/
 | |
| 
 | |
| var assert = require('assert');
 | |
| var db = require('./mocks/databasemock');
 | |
| var async = require('async');
 | |
| var User = require('../src/user');
 | |
| var Groups = require('../src/groups');
 | |
| var Messaging = require('../src/messaging');
 | |
| var testUids;
 | |
| 
 | |
| describe('Messaging Library', function () {
 | |
| 	before(function (done) {
 | |
| 		// Create 3 users: 1 admin, 2 regular
 | |
| 		async.parallel([
 | |
| 			async.apply(User.create, { username: 'foo', password: 'barbar' }),	// admin
 | |
| 			async.apply(User.create, { username: 'baz', password: 'quuxquux' }),	// restricted user
 | |
| 			async.apply(User.create, { username: 'herp', password: 'derpderp' })	// regular user
 | |
| 		], function (err, uids) {
 | |
| 			if (err) {
 | |
| 				return done(err);
 | |
| 			}
 | |
| 
 | |
| 			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 not error out', function (done) {
 | |
| 			Messaging.canMessageUser(testUids[1], testUids[2], function (err) {
 | |
| 				assert.ifError(err);
 | |
| 				done();
 | |
| 			});
 | |
| 		});
 | |
| 
 | |
| 		it('should allow messages to be sent to an unrestricted user', function (done) {
 | |
| 			Messaging.canMessageUser(testUids[1], testUids[2], function (err) {
 | |
| 				assert.ifError(err);
 | |
| 				done();
 | |
| 			});
 | |
| 		});
 | |
| 
 | |
| 		it('should NOT allow messages to be sent to a restricted user', function (done) {
 | |
| 			User.setSetting(testUids[1], 'restrictChat', '1', function () {
 | |
| 				Messaging.canMessageUser(testUids[2], testUids[1], function (err) {
 | |
| 					assert.strictEqual(err.message, '[[error:chat-restricted]]');
 | |
| 					done();
 | |
| 				});
 | |
| 			});
 | |
| 		});
 | |
| 
 | |
| 		it('should always allow admins through', function (done) {
 | |
| 			Messaging.canMessageUser(testUids[0], testUids[1], function (err) {
 | |
| 				assert.ifError(err);
 | |
| 				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.canMessageUser(testUids[2], testUids[1], function (err) {
 | |
| 					assert.ifError(err);
 | |
| 					done();
 | |
| 				});
 | |
| 			});
 | |
| 		});
 | |
| 	});
 | |
| 
 | |
| 	after(function (done) {
 | |
| 		db.emptydb(done);
 | |
| 	});
 | |
| });
 |