| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | var async = require('async'); | 
					
						
							|  |  |  | var assert = require('assert'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | var groups = require('../src/groups'); | 
					
						
							|  |  |  | var user = require('../src/user'); | 
					
						
							|  |  |  | var blacklist = require('../src/meta/blacklist'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('blacklist', function () { | 
					
						
							|  |  |  | 	var adminUid; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 		user.create({ username: 'admin' }, function (err, uid) { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			adminUid = uid; | 
					
						
							|  |  |  | 			groups.join('administrators', adminUid, done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var socketBlacklist = require('../src/socket.io/blacklist'); | 
					
						
							|  |  |  | 	var rules = '1.1.1.1\n2.2.2.2\n::ffff:0:2.2.2.2\n127.0.0.1\n192.168.100.0/22'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should validate blacklist', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 		socketBlacklist.validate({ uid: adminUid }, { | 
					
						
							|  |  |  | 			rules: rules, | 
					
						
							| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | 		}, function (err, data) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should error if not admin', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 		socketBlacklist.save({ uid: 0 }, rules, function (err) { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | 			assert.equal(err.message, '[[error:no-privileges]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	it('should save blacklist', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-23 18:31:49 -07:00
										 |  |  | 		socketBlacklist.save({ uid: adminUid }, rules, function (err) { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 14:24:13 -05:00
										 |  |  | 	it('should pass ip test against blacklist', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | 		blacklist.test('3.3.3.3', function (err) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-30 14:24:13 -05:00
										 |  |  | 	it('should fail ip test against blacklist', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | 		blacklist.test('1.1.1.1', function (err) { | 
					
						
							|  |  |  | 			assert.equal(err.message, '[[error:blacklisted-ip]]'); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-02 19:27:42 -04:00
										 |  |  | 	it('should pass ip test and not crash with ipv6 address', function (done) { | 
					
						
							| 
									
										
										
										
											2017-11-30 14:24:13 -05:00
										 |  |  | 		blacklist.test('2001:db8:85a3:0:0:8a2e:370:7334', function (err) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-10-02 19:27:42 -04:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2017-02-22 14:53:44 +03:00
										 |  |  | }); |