| 
									
										
										
										
											2015-01-03 20:07:09 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-17 21:55:19 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-24 00:42:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | var async = require('async'); | 
					
						
							|  |  |  | var assert = require('assert'); | 
					
						
							| 
									
										
										
										
											2016-10-18 00:14:28 +03:00
										 |  |  | var nconf = require('nconf'); | 
					
						
							|  |  |  | var request = require('request'); | 
					
						
							| 
									
										
										
										
											2013-10-16 14:08:31 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | var db = require('./mocks/databasemock'); | 
					
						
							| 
									
										
										
										
											2013-10-16 14:08:31 -04:00
										 |  |  | var Categories = require('../src/categories'); | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | var Topics = require('../src/topics'); | 
					
						
							|  |  |  | var User = require('../src/user'); | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | var groups = require('../src/groups'); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | var privileges = require('../src/privileges'); | 
					
						
							| 
									
										
										
										
											2013-10-12 11:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | describe('Categories', function () { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:11:44 +03:00
										 |  |  | 	var categoryObj; | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 	var posterUid; | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 	var adminUid; | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	before(function (done) { | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 		groups.resetCache(); | 
					
						
							| 
									
										
										
										
											2017-05-26 17:16:59 -06:00
										 |  |  | 		async.series({ | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 			posterUid: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				User.create({ username: 'poster' }, next); | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			adminUid: function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				User.create({ username: 'admin' }, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 		}, function (err, results) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			posterUid = results.posterUid; | 
					
						
							|  |  |  | 			adminUid = results.adminUid; | 
					
						
							|  |  |  | 			groups.join('administrators', adminUid, done); | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-10-12 11:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-06 13:21:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 00:14:28 +03:00
										 |  |  | 	it('should create a new category', function (done) { | 
					
						
							|  |  |  | 		Categories.create({ | 
					
						
							|  |  |  | 			name: 'Test Category', | 
					
						
							|  |  |  | 			description: 'Test category created by testing script', | 
					
						
							|  |  |  | 			icon: 'fa-check', | 
					
						
							|  |  |  | 			blockclass: 'category-blue', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			order: '5', | 
					
						
							| 
									
										
										
										
											2016-10-18 00:14:28 +03:00
										 |  |  | 		}, function (err, category) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			categoryObj = category; | 
					
						
							|  |  |  | 			done(); | 
					
						
							| 
									
										
										
										
											2013-10-12 11:06:43 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 00:14:28 +03:00
										 |  |  | 	it('should retrieve a newly created category by its ID', function (done) { | 
					
						
							|  |  |  | 		Categories.getCategoryById({ | 
					
						
							|  |  |  | 			cid: categoryObj.cid, | 
					
						
							|  |  |  | 			set: 'cid:' + categoryObj.cid + ':tids', | 
					
						
							|  |  |  | 			reverse: true, | 
					
						
							|  |  |  | 			start: 0, | 
					
						
							|  |  |  | 			stop: -1, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			uid: 0, | 
					
						
							| 
									
										
										
										
											2016-10-18 00:14:28 +03:00
										 |  |  | 		}, function (err, categoryData) { | 
					
						
							|  |  |  | 			assert.equal(err, null); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			assert(categoryData); | 
					
						
							|  |  |  | 			assert.equal(categoryObj.name, categoryData.name); | 
					
						
							|  |  |  | 			assert.equal(categoryObj.description, categoryData.description); | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 00:14:28 +03:00
										 |  |  | 			done(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2013-10-12 11:06:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-18 00:14:28 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	it('should load a category route', function (done) { | 
					
						
							|  |  |  | 		request(nconf.get('url') + '/category/' + categoryObj.cid + '/test-category', function (err, response, body) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			assert.equal(response.statusCode, 200); | 
					
						
							|  |  |  | 			assert(body); | 
					
						
							|  |  |  | 			done(); | 
					
						
							| 
									
										
										
										
											2013-10-12 11:06:43 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('Categories.getRecentTopicReplies', function () { | 
					
						
							|  |  |  | 		it('should not throw', function (done) { | 
					
						
							| 
									
										
										
										
											2016-08-26 19:33:16 +03:00
										 |  |  | 			Categories.getCategoryById({ | 
					
						
							|  |  |  | 				cid: categoryObj.cid, | 
					
						
							|  |  |  | 				set: 'cid:' + categoryObj.cid + ':tids', | 
					
						
							|  |  |  | 				reverse: true, | 
					
						
							|  |  |  | 				start: 0, | 
					
						
							|  |  |  | 				stop: -1, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				uid: 0, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			}, function (err, categoryData) { | 
					
						
							| 
									
										
										
										
											2016-08-26 19:33:16 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				Categories.getRecentTopicReplies(categoryData, 0, function (err) { | 
					
						
							| 
									
										
										
										
											2016-08-26 19:33:16 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('.getCategoryTopics', function () { | 
					
						
							|  |  |  | 		it('should return a list of topics', function (done) { | 
					
						
							| 
									
										
										
										
											2014-11-06 18:14:07 -05:00
										 |  |  | 			Categories.getCategoryTopics({ | 
					
						
							|  |  |  | 				cid: categoryObj.cid, | 
					
						
							| 
									
										
										
										
											2015-01-08 13:47:15 -05:00
										 |  |  | 				set: 'cid:' + categoryObj.cid + ':tids', | 
					
						
							|  |  |  | 				reverse: true, | 
					
						
							| 
									
										
										
										
											2014-11-06 18:14:07 -05:00
										 |  |  | 				start: 0, | 
					
						
							|  |  |  | 				stop: 10, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				uid: 0, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			}, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-26 17:17:34 -05:00
										 |  |  | 				assert(Array.isArray(result.topics)); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				assert(result.topics.every(function (topic) { | 
					
						
							| 
									
										
										
										
											2013-10-12 11:06:43 -04:00
										 |  |  | 					return topic instanceof Object; | 
					
						
							|  |  |  | 				})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-11-06 18:14:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should return a list of topics by a specific user', function (done) { | 
					
						
							| 
									
										
										
										
											2014-11-06 18:14:07 -05:00
										 |  |  | 			Categories.getCategoryTopics({ | 
					
						
							|  |  |  | 				cid: categoryObj.cid, | 
					
						
							| 
									
										
										
										
											2015-01-08 13:47:15 -05:00
										 |  |  | 				set: 'cid:' + categoryObj.cid + ':uid:' + 1 + ':tids', | 
					
						
							|  |  |  | 				reverse: true, | 
					
						
							| 
									
										
										
										
											2014-11-06 18:14:07 -05:00
										 |  |  | 				start: 0, | 
					
						
							|  |  |  | 				stop: 10, | 
					
						
							|  |  |  | 				uid: 0, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				targetUid: 1, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			}, function (err, result) { | 
					
						
							| 
									
										
										
										
											2016-08-16 19:46:59 +02:00
										 |  |  | 				assert.equal(err, null); | 
					
						
							| 
									
										
										
										
											2014-11-06 18:14:07 -05:00
										 |  |  | 				assert(Array.isArray(result.topics)); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				assert(result.topics.every(function (topic) { | 
					
						
							| 
									
										
										
										
											2014-11-06 18:14:07 -05:00
										 |  |  | 					return topic instanceof Object && topic.uid === '1'; | 
					
						
							|  |  |  | 				})); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-10-12 11:06:43 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	describe('Categories.moveRecentReplies', function () { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 		var moveCid; | 
					
						
							|  |  |  | 		var moveTid; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 			async.parallel({ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				category: function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					Categories.create({ | 
					
						
							|  |  |  | 						name: 'Test Category 2', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 						description: 'Test category created by testing script', | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					}, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				topic: function (next) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					Topics.post({ | 
					
						
							|  |  |  | 						uid: posterUid, | 
					
						
							|  |  |  | 						cid: categoryObj.cid, | 
					
						
							|  |  |  | 						title: 'Test Topic Title', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 						content: 'The content of test topic', | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					}, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					return done(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				moveCid = results.category.cid; | 
					
						
							|  |  |  | 				moveTid = results.topic.topicData.tid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 				Topics.reply({ uid: posterUid, content: 'test post', tid: moveTid }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					done(err); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		it('should move posts from one category to another', function (done) { | 
					
						
							|  |  |  | 			Categories.moveRecentReplies(moveTid, categoryObj.cid, moveCid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 				db.getSortedSetRange('cid:' + categoryObj.cid + ':pids', 0, -1, function (err, pids) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(pids.length, 0); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 					db.getSortedSetRange('cid:' + moveCid + ':pids', 0, -1, function (err, pids) { | 
					
						
							| 
									
										
										
										
											2016-10-11 16:06:42 +03:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert.equal(pids.length, 2); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 	describe('socket methods', function () { | 
					
						
							|  |  |  | 		var socketCategories = require('../src/socket.io/categories'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			Topics.post({ | 
					
						
							|  |  |  | 				uid: posterUid, | 
					
						
							|  |  |  | 				cid: categoryObj.cid, | 
					
						
							|  |  |  | 				title: 'Test Topic Title', | 
					
						
							|  |  |  | 				content: 'The content of test topic', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				tags: ['nodebb'], | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 			}, done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get recent replies in category', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getRecentReplies({ uid: posterUid }, categoryObj.cid, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get categories', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.get({ uid: posterUid }, {}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get watched categories', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getWatchedCategories({ uid: posterUid }, {}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load more topics', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.loadMore({ uid: posterUid }, { cid: categoryObj.cid, after: 0, author: 'poster', tag: 'nodebb' }, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data.topics)); | 
					
						
							|  |  |  | 				assert.equal(data.topics[0].user.username, 'poster'); | 
					
						
							|  |  |  | 				assert.equal(data.topics[0].tags[0].value, 'nodebb'); | 
					
						
							|  |  |  | 				assert.equal(data.topics[0].category.cid, categoryObj.cid); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load page count', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getPageCount({ uid: posterUid }, categoryObj.cid, function (err, pageCount) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(pageCount, 1); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load page count', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getTopicCount({ uid: posterUid }, categoryObj.cid, function (err, topicCount) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(topicCount, 2); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load category by privilege', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getCategoriesByPrivilege({ uid: posterUid }, 'find', function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get move categories', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getMoveCategories({ uid: posterUid }, {}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should ignore category', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.ignore({ uid: posterUid }, categoryObj.cid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				Categories.isIgnored([categoryObj.cid], posterUid, function (err, isIgnored) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(isIgnored[0], true); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should watch category', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.watch({ uid: posterUid }, categoryObj.cid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				Categories.isIgnored([categoryObj.cid], posterUid, function (err, isIgnored) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(isIgnored[0], false); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should check if user is moderator', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.isModerator({ uid: posterUid }, {}, function (err, isModerator) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(!isModerator); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 01:31:47 -07:00
										 |  |  | 		it('should get category data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getCategory({ uid: posterUid }, categoryObj.cid, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(categoryObj.cid, data.cid); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 	describe('admin socket methods', function () { | 
					
						
							|  |  |  | 		var socketCategories = require('../src/socket.io/admin/categories'); | 
					
						
							|  |  |  | 		var cid; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.create({ uid: adminUid }, { | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 				name: 'update name', | 
					
						
							|  |  |  | 				description: 'update description', | 
					
						
							|  |  |  | 				parentCid: categoryObj.cid, | 
					
						
							|  |  |  | 				icon: 'fa-check', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				order: '5', | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 			}, function (err, category) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 				cid = category.cid; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return error with invalid data', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.update({ uid: adminUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error if you try to set parent as self', function (done) { | 
					
						
							|  |  |  | 			var updateData = {}; | 
					
						
							|  |  |  | 			updateData[cid] = { | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				parentCid: cid, | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.update({ uid: adminUid }, updateData, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:cant-set-self-as-parent]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should update category data', function (done) { | 
					
						
							|  |  |  | 			var updateData = {}; | 
					
						
							|  |  |  | 			updateData[cid] = { | 
					
						
							|  |  |  | 				name: 'new name', | 
					
						
							|  |  |  | 				description: 'new description', | 
					
						
							|  |  |  | 				parentCid: 0, | 
					
						
							|  |  |  | 				order: 3, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				icon: 'fa-hammer', | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 			}; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.update({ uid: adminUid }, updateData, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-23 13:38:20 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				Categories.getCategoryData(cid, function (err, data) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(data.name, updateData[cid].name); | 
					
						
							|  |  |  | 					assert.equal(data.description, updateData[cid].description); | 
					
						
							|  |  |  | 					assert.equal(data.parentCid, updateData[cid].parentCid); | 
					
						
							|  |  |  | 					assert.equal(data.order, updateData[cid].order); | 
					
						
							|  |  |  | 					assert.equal(data.icon, updateData[cid].icon); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-25 17:46:29 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should purge category', function (done) { | 
					
						
							|  |  |  | 			Categories.create({ | 
					
						
							|  |  |  | 				name: 'purge me', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				description: 'update description', | 
					
						
							| 
									
										
										
										
											2016-11-25 17:46:29 +03:00
										 |  |  | 			}, function (err, category) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				Topics.post({ | 
					
						
							|  |  |  | 					uid: posterUid, | 
					
						
							|  |  |  | 					cid: category.cid, | 
					
						
							|  |  |  | 					title: 'Test Topic Title', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 					content: 'The content of test topic', | 
					
						
							| 
									
										
										
										
											2016-11-25 17:46:29 +03:00
										 |  |  | 				}, function (err) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketCategories.purge({ uid: adminUid }, category.cid, function (err) { | 
					
						
							| 
									
										
										
										
											2016-11-25 17:46:29 +03:00
										 |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should get all categories', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getAll({ uid: adminUid }, {}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-03-03 21:04:01 +03:00
										 |  |  | 				assert(data); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get all category names', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getNames({ uid: adminUid }, {}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(Array.isArray(data)); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should give privilege', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.setPrivilege({ uid: adminUid }, { cid: categoryObj.cid, privilege: ['groups:topics:delete'], set: true, member: 'registered-users' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				privileges.categories.can('topics:delete', categoryObj.cid, posterUid, function (err, canDeleteTopcis) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(canDeleteTopcis); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should remove privilege', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.setPrivilege({ uid: adminUid }, { cid: categoryObj.cid, privilege: 'groups:topics:delete', set: false, member: 'registered-users' }, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				privileges.categories.can('topics:delete', categoryObj.cid, posterUid, function (err, canDeleteTopcis) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(!canDeleteTopcis); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should get privilege settings', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketCategories.getPrivilegeSettings({ uid: adminUid }, categoryObj.cid, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(data); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should copy privileges to children', function (done) { | 
					
						
							|  |  |  | 			var parentCid; | 
					
						
							|  |  |  | 			var child1Cid; | 
					
						
							|  |  |  | 			var child2Cid; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					Categories.create({ name: 'parent' }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					parentCid = category.cid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					Categories.create({ name: 'child1', parentCid: parentCid }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					child1Cid = category.cid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					Categories.create({ name: 'child2', parentCid: child1Cid }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					child2Cid = category.cid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketCategories.setPrivilege({ uid: adminUid }, { cid: parentCid, privilege: 'groups:topics:delete', set: true, member: 'registered-users' }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketCategories.copyPrivilegesToChildren({ uid: adminUid }, parentCid, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					privileges.categories.can('topics:delete', child2Cid, posterUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (canDelete, next) { | 
					
						
							|  |  |  | 					assert(canDelete); | 
					
						
							|  |  |  | 					next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should copy settings from', function (done) { | 
					
						
							|  |  |  | 			var child1Cid; | 
					
						
							|  |  |  | 			var parentCid; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					Categories.create({ name: 'parent', description: 'copy me' }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					parentCid = category.cid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					Categories.create({ name: 'child1' }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					child1Cid = category.cid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketCategories.copySettingsFrom({ uid: adminUid }, { fromCid: parentCid, toCid: child1Cid }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (canDelete, next) { | 
					
						
							|  |  |  | 					Categories.getCategoryField(child1Cid, 'description', next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (description, next) { | 
					
						
							|  |  |  | 					assert.equal(description, 'copy me'); | 
					
						
							|  |  |  | 					next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should copy privileges from', function (done) { | 
					
						
							|  |  |  | 			var child1Cid; | 
					
						
							|  |  |  | 			var parentCid; | 
					
						
							|  |  |  | 			async.waterfall([ | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					Categories.create({ name: 'parent', description: 'copy me' }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					parentCid = category.cid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					Categories.create({ name: 'child1' }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (category, next) { | 
					
						
							|  |  |  | 					child1Cid = category.cid; | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketCategories.setPrivilege({ uid: adminUid }, { cid: parentCid, privilege: 'groups:topics:delete', set: true, member: 'registered-users' }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 					socketCategories.copyPrivilegesFrom({ uid: adminUid }, { fromCid: parentCid, toCid: child1Cid }, next); | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function (next) { | 
					
						
							|  |  |  | 					privileges.categories.can('topics:delete', child1Cid, posterUid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function (canDelete, next) { | 
					
						
							|  |  |  | 					assert(canDelete); | 
					
						
							|  |  |  | 					next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2016-12-02 18:08:08 +03:00
										 |  |  | 			], done); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-02 16:10:07 +03:00
										 |  |  | 	it('should get active users', function (done) { | 
					
						
							|  |  |  | 		Categories.create({ | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			name: 'test', | 
					
						
							| 
									
										
										
										
											2016-12-02 16:10:07 +03:00
										 |  |  | 		}, function (err, category) { | 
					
						
							|  |  |  | 			assert.ifError(err); | 
					
						
							|  |  |  | 			Topics.post({ | 
					
						
							|  |  |  | 				uid: posterUid, | 
					
						
							|  |  |  | 				cid: category.cid, | 
					
						
							|  |  |  | 				title: 'Test Topic Title', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				content: 'The content of test topic', | 
					
						
							| 
									
										
										
										
											2016-12-02 16:10:07 +03:00
										 |  |  | 			}, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				Categories.getActiveUsers(category.cid, function (err, uids) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(uids[0], posterUid); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('tag whitelist', function () { | 
					
						
							|  |  |  | 		var cid; | 
					
						
							|  |  |  | 		var socketTopics = require('../src/socket.io/topics'); | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			Categories.create({ | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				name: 'test', | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 			}, function (err, category) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				cid = category.cid; | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should error if data is invalid', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.isTagAllowed({ uid: posterUid }, null, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 				assert.equal(err.message, '[[error:invalid-data]]'); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return true if category whitelist is empty', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.isTagAllowed({ uid: posterUid }, { tag: 'notallowed', cid: cid }, function (err, allowed) { | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(allowed); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 		it('should add tags to category whitelist', function (done) { | 
					
						
							|  |  |  | 			var data = {}; | 
					
						
							|  |  |  | 			data[cid] = { | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				tagWhitelist: 'nodebb,jquery,javascript', | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 			}; | 
					
						
							|  |  |  | 			Categories.update(data, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.getSortedSetRange('cid:' + cid + ':tag:whitelist', 0, -1, function (err, tagWhitelist) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.deepEqual(['nodebb', 'jquery', 'javascript'], tagWhitelist); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return false if category whitelist does not have tag', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.isTagAllowed({ uid: posterUid }, { tag: 'notallowed', cid: cid }, function (err, allowed) { | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(!allowed); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return true if category whitelist has tag', function (done) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			socketTopics.isTagAllowed({ uid: posterUid }, { tag: 'nodebb', cid: cid }, function (err, allowed) { | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(allowed); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should post a topic with only allowed tags', function (done) { | 
					
						
							|  |  |  | 			Topics.post({ | 
					
						
							|  |  |  | 				uid: posterUid, | 
					
						
							|  |  |  | 				cid: cid, | 
					
						
							|  |  |  | 				title: 'Test Topic Title', | 
					
						
							|  |  |  | 				content: 'The content of test topic', | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				tags: ['nodebb', 'jquery', 'notallowed'], | 
					
						
							| 
									
										
										
										
											2016-12-09 18:53:08 +03:00
										 |  |  | 			}, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(data.topicData.tags.length, 2); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-02 16:10:07 +03:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-21 13:47:34 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-03 21:04:01 +03:00
										 |  |  | 	describe('privileges', function () { | 
					
						
							|  |  |  | 		var privileges = require('../src/privileges'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return empty array if uids is empty array', function (done) { | 
					
						
							|  |  |  | 			privileges.categories.filterUids('find', categoryObj.cid, [], function (err, uids) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(uids.length, 0); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should filter uids by privilege', function (done) { | 
					
						
							|  |  |  | 			privileges.categories.filterUids('find', categoryObj.cid, [1, 2, 3, 4], function (err, uids) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual(uids, [1, 2]); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load user privileges', function (done) { | 
					
						
							|  |  |  | 			privileges.categories.userPrivileges(categoryObj.cid, 1, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual(data, { | 
					
						
							|  |  |  | 					find: false, | 
					
						
							|  |  |  | 					'posts:delete': false, | 
					
						
							|  |  |  | 					read: false, | 
					
						
							|  |  |  | 					'topics:reply': false, | 
					
						
							|  |  |  | 					'topics:read': false, | 
					
						
							|  |  |  | 					'topics:create': false, | 
					
						
							|  |  |  | 					'topics:delete': false, | 
					
						
							|  |  |  | 					'posts:edit': false, | 
					
						
							| 
									
										
										
										
											2017-06-15 14:02:51 -04:00
										 |  |  | 					'upload:post:file': false, | 
					
						
							|  |  |  | 					'upload:post:image': false, | 
					
						
							|  |  |  | 					purge: false, | 
					
						
							|  |  |  | 					moderate: false, | 
					
						
							| 
									
										
										
										
											2017-03-03 21:04:01 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should load group privileges', function (done) { | 
					
						
							|  |  |  | 			privileges.categories.groupPrivileges(categoryObj.cid, 'registered-users', function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.deepEqual(data, { | 
					
						
							|  |  |  | 					'groups:find': true, | 
					
						
							|  |  |  | 					'groups:posts:edit': true, | 
					
						
							|  |  |  | 					'groups:topics:delete': false, | 
					
						
							|  |  |  | 					'groups:topics:create': true, | 
					
						
							|  |  |  | 					'groups:topics:reply': true, | 
					
						
							|  |  |  | 					'groups:posts:delete': true, | 
					
						
							|  |  |  | 					'groups:read': true, | 
					
						
							|  |  |  | 					'groups:topics:read': true, | 
					
						
							| 
									
										
										
										
											2017-06-15 14:02:51 -04:00
										 |  |  | 					'groups:upload:post:file': false, | 
					
						
							|  |  |  | 					'groups:upload:post:image': true, | 
					
						
							|  |  |  | 					'groups:purge': false, | 
					
						
							|  |  |  | 					'groups:moderate': false, | 
					
						
							| 
									
										
										
										
											2017-03-03 21:04:01 +03:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return false if cid is falsy', function (done) { | 
					
						
							|  |  |  | 			privileges.categories.isUserAllowedTo('find', null, adminUid, function (err, isAllowed) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.equal(isAllowed, false); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-24 00:42:34 -05:00
										 |  |  | }); |