| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | /*global require, before, after*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-16 21:51:42 +03:00
										 |  |  | 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; | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | describe('Messaging Library', function () { | 
					
						
							|  |  |  | 	before(function (done) { | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 		// Create 3 users: 1 admin, 2 regular
 | 
					
						
							|  |  |  | 		async.parallel([ | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 			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
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		], function (err, uids) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return done(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 			testUids = uids; | 
					
						
							|  |  |  | 			async.parallel([ | 
					
						
							|  |  |  | 				async.apply(Groups.join, 'administrators', uids[0]), | 
					
						
							|  |  |  | 				async.apply(User.setSetting, testUids[1], 'restrictChat', '1') | 
					
						
							|  |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('.canMessage()', function () { | 
					
						
							|  |  |  | 		it('should not error out', function (done) { | 
					
						
							|  |  |  | 			Messaging.canMessageUser(testUids[1], testUids[2], function (err) { | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should allow messages to be sent to an unrestricted user', function (done) { | 
					
						
							|  |  |  | 			Messaging.canMessageUser(testUids[1], testUids[2], function (err) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 					assert.strictEqual(err.message, '[[error:chat-restricted]]'); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should always allow admins through', function (done) { | 
					
						
							|  |  |  | 			Messaging.canMessageUser(testUids[0], testUids[1], function (err) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	after(function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-17 23:34:09 +03:00
										 |  |  | 		db.emptydb(done); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |