| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | /*global require, before, after*/ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | var assert = require('assert'), | 
					
						
							|  |  |  | 	db = require('./mocks/databasemock'), | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	User = require('../src/user'), | 
					
						
							|  |  |  | 	Groups = require('../src/groups'), | 
					
						
							|  |  |  | 	Messaging = require('../src/messaging'), | 
					
						
							|  |  |  | 	testUids; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | describe('Messaging Library', function() { | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 	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) { | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 	describe('.canMessage()', function() { | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | 		it('should not error out', function(done) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 			Messaging.canMessageUser(testUids[1], testUids[2], function(err) { | 
					
						
							| 
									
										
										
										
											2014-10-31 13:22:42 -04:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 		it('should allow messages to be sent to an unrestricted user', function(done) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 			Messaging.canMessageUser(testUids[1], testUids[2], function(err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 		it('should NOT allow messages to be sent to a restricted user', function(done) { | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 			User.setSetting(testUids[1], 'restrictChat', '1', function() { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 				Messaging.canMessageUser(testUids[2], testUids[1], function(err) { | 
					
						
							|  |  |  | 					assert.strictEqual(err.message, '[[error:chat-restricted]]'); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 		it('should always allow admins through', function(done) { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 			Messaging.canMessageUser(testUids[0], testUids[1], function(err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 		it('should allow messages to be sent to a restricted user if restricted user follows sender', function(done) { | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 			User.follow(testUids[1], testUids[2], function() { | 
					
						
							| 
									
										
										
										
											2016-02-29 10:36:20 +02:00
										 |  |  | 				Messaging.canMessageUser(testUids[2], testUids[1], function(err) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-12 13:49:41 +03:00
										 |  |  | 	after(function(done) { | 
					
						
							|  |  |  | 		db.flushdb(done); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:04:16 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }); |