| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* globals require, before, after, describe, it*/ | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | var assert = require('assert'); | 
					
						
							|  |  |  | var async = require('async'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var db = require('./mocks/databasemock'); | 
					
						
							|  |  |  | var Flags = require('../src/flags'); | 
					
						
							|  |  |  | var Categories = require('../src/categories'); | 
					
						
							|  |  |  | var Topics = require('../src/topics'); | 
					
						
							| 
									
										
										
										
											2016-12-06 20:28:54 -05:00
										 |  |  | var Posts = require('../src/posts'); | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | var User = require('../src/user'); | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | var Groups = require('../src/groups'); | 
					
						
							| 
									
										
										
										
											2016-12-06 20:28:54 -05:00
										 |  |  | var Meta = require('../src/meta'); | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | describe('Flags', function () { | 
					
						
							|  |  |  | 	before(function (done) { | 
					
						
							| 
									
										
										
										
											2017-05-09 14:31:32 -04:00
										 |  |  | 		Groups.resetCache(); | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | 		// Create some stuff to flag
 | 
					
						
							|  |  |  | 		async.waterfall([ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 			async.apply(User.create, { username: 'testUser', password: 'abcdef', email: 'b@c.com' }), | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | 			function (uid, next) { | 
					
						
							|  |  |  | 				Categories.create({ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					name: 'test category', | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | 				}, function (err, category) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return done(err); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					Topics.post({ | 
					
						
							|  |  |  | 						cid: category.cid, | 
					
						
							|  |  |  | 						uid: uid, | 
					
						
							|  |  |  | 						title: 'Topic to flag', | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 						content: 'This is flaggable content', | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | 					}, next); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-12-07 12:07:22 -05:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (topicData, next) { | 
					
						
							|  |  |  | 				User.create({ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					username: 'testUser2', password: 'abcdef', email: 'c@d.com', | 
					
						
							| 
									
										
										
										
											2016-12-07 12:07:22 -05:00
										 |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (uid, next) { | 
					
						
							|  |  |  | 				Groups.join('administrators', uid, next); | 
					
						
							| 
									
										
										
										
											2016-12-14 15:00:41 -05:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function (next) { | 
					
						
							|  |  |  | 				User.create({ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					username: 'unprivileged', password: 'abcdef', email: 'd@e.com', | 
					
						
							| 
									
										
										
										
											2016-12-14 15:00:41 -05:00
										 |  |  | 				}, next); | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | 		], done); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 	describe('.create()', function () { | 
					
						
							|  |  |  | 		it('should create a flag and return its data', function (done) { | 
					
						
							|  |  |  | 			Flags.create('post', 1, 1, 'Test flag', function (err, flagData) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				var compare = { | 
					
						
							|  |  |  | 					flagId: 1, | 
					
						
							|  |  |  | 					uid: 1, | 
					
						
							|  |  |  | 					targetId: 1, | 
					
						
							|  |  |  | 					type: 'post', | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					description: 'Test flag', | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				for (var key in compare) { | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					if (compare.hasOwnProperty(key)) { | 
					
						
							|  |  |  | 						assert.ok(flagData[key]); | 
					
						
							| 
									
										
										
										
											2016-12-19 12:31:55 -05:00
										 |  |  | 						assert.equal(flagData[key], compare[key]); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should add the flag to the byCid zset for category 1 if it is of type post', function (done) { | 
					
						
							|  |  |  | 			db.isSortedSetMember('flags:byCid:' + 1, 1, function (err, isMember) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.ok(isMember); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2017-01-12 11:31:59 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should add the flag to the byPid zset for pid 1 if it is of type post', function (done) { | 
					
						
							|  |  |  | 			db.isSortedSetMember('flags:byPid:' + 1, 1, function (err, isMember) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.ok(isMember); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-19 11:16:03 -05:00
										 |  |  | 	describe('.exists()', function () { | 
					
						
							|  |  |  | 		it('should return Boolean True if a flag matching the flag hash already exists', function (done) { | 
					
						
							|  |  |  | 			Flags.exists('post', 1, 1, function (err, exists) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.strictEqual(true, exists); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return Boolean False if a flag matching the flag hash does not already exists', function (done) { | 
					
						
							|  |  |  | 			Flags.exists('post', 1, 2, function (err, exists) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.strictEqual(false, exists); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.targetExists()', function () { | 
					
						
							|  |  |  | 		it('should return Boolean True if the targeted element exists', function (done) { | 
					
						
							|  |  |  | 			Flags.targetExists('post', 1, function (err, exists) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.strictEqual(true, exists); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return Boolean False if the targeted element does not exist', function (done) { | 
					
						
							|  |  |  | 			Flags.targetExists('post', 15, function (err, exists) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.strictEqual(false, exists); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 	describe('.get()', function () { | 
					
						
							|  |  |  | 		it('should retrieve and display a flag\'s data', function (done) { | 
					
						
							|  |  |  | 			Flags.get(1, function (err, flagData) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				var compare = { | 
					
						
							|  |  |  | 					flagId: 1, | 
					
						
							|  |  |  | 					uid: 1, | 
					
						
							|  |  |  | 					targetId: 1, | 
					
						
							|  |  |  | 					type: 'post', | 
					
						
							|  |  |  | 					description: 'Test flag', | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					state: 'open', | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				for (var key in compare) { | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					if (compare.hasOwnProperty(key)) { | 
					
						
							|  |  |  | 						assert.ok(flagData[key]); | 
					
						
							| 
									
										
										
										
											2016-12-19 12:31:55 -05:00
										 |  |  | 						assert.equal(flagData[key], compare[key]); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.list()', function () { | 
					
						
							|  |  |  | 		it('should show a list of flags (with one item)', function (done) { | 
					
						
							|  |  |  | 			Flags.list({}, 1, function (err, flags) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.ok(Array.isArray(flags)); | 
					
						
							|  |  |  | 				assert.equal(flags.length, 1); | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				Flags.get(flags[0].flagId, function (err, flagData) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.equal(flags[0].flagId, flagData.flagId); | 
					
						
							|  |  |  | 					assert.equal(flags[0].description, flagData.description); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-14 15:00:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | 		describe('(with filters)', function () { | 
					
						
							|  |  |  | 			it('should return a filtered list of flags if said filters are passed in', function (done) { | 
					
						
							|  |  |  | 				Flags.list({ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					state: 'open', | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | 				}, 1, function (err, flags) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.ok(Array.isArray(flags)); | 
					
						
							| 
									
										
										
										
											2016-12-19 12:31:55 -05:00
										 |  |  | 					assert.strictEqual(1, parseInt(flags[0].flagId, 10)); | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it('should return no flags if a filter with no matching flags is used', function (done) { | 
					
						
							|  |  |  | 				Flags.list({ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					state: 'rejected', | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | 				}, 1, function (err, flags) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.ok(Array.isArray(flags)); | 
					
						
							|  |  |  | 					assert.strictEqual(0, flags.length); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it('should return a flag when filtered by cid 1', function (done) { | 
					
						
							|  |  |  | 				Flags.list({ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					cid: 1, | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | 				}, 1, function (err, flags) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.ok(Array.isArray(flags)); | 
					
						
							|  |  |  | 					assert.strictEqual(1, flags.length); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it('shouldn\'t return a flag when filtered by cid 2', function (done) { | 
					
						
							|  |  |  | 				Flags.list({ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					cid: 2, | 
					
						
							| 
									
										
										
										
											2016-12-14 15:53:57 -05:00
										 |  |  | 				}, 1, function (err, flags) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.ok(Array.isArray(flags)); | 
					
						
							|  |  |  | 					assert.strictEqual(0, flags.length); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2016-12-14 15:00:41 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2017-01-03 13:38:06 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			it('should return a flag when filtered by both cid 1 and 2', function (done) { | 
					
						
							|  |  |  | 				Flags.list({ | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					cid: [1, 2], | 
					
						
							| 
									
										
										
										
											2017-01-03 13:38:06 -05:00
										 |  |  | 				}, 1, function (err, flags) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.ok(Array.isArray(flags)); | 
					
						
							|  |  |  | 					assert.strictEqual(1, flags.length); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it('should return one flag if filtered by both cid 1 and 2 and open state', function (done) { | 
					
						
							|  |  |  | 				Flags.list({ | 
					
						
							|  |  |  | 					cid: [1, 2], | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					state: 'open', | 
					
						
							| 
									
										
										
										
											2017-01-03 13:38:06 -05:00
										 |  |  | 				}, 1, function (err, flags) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.ok(Array.isArray(flags)); | 
					
						
							|  |  |  | 					assert.strictEqual(1, flags.length); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			it('should return no flag if filtered by both cid 1 and 2 and non-open state', function (done) { | 
					
						
							|  |  |  | 				Flags.list({ | 
					
						
							|  |  |  | 					cid: [1, 2], | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					state: 'resolved', | 
					
						
							| 
									
										
										
										
											2017-01-03 13:38:06 -05:00
										 |  |  | 				}, 1, function (err, flags) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert.ok(Array.isArray(flags)); | 
					
						
							|  |  |  | 					assert.strictEqual(0, flags.length); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2016-12-14 15:00:41 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.update()', function () { | 
					
						
							|  |  |  | 		it('should alter a flag\'s various attributes and persist them to the database', function (done) { | 
					
						
							|  |  |  | 			Flags.update(1, 1, { | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				state: 'wip', | 
					
						
							|  |  |  | 				assignee: 1, | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 			}, function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				db.getObjectFields('flag:1', ['state', 'assignee'], function (err, data) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						throw err; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					assert.strictEqual('wip', data.state); | 
					
						
							| 
									
										
										
										
											2016-12-19 12:31:55 -05:00
										 |  |  | 					assert.ok(!isNaN(parseInt(data.assignee, 10))); | 
					
						
							|  |  |  | 					assert.strictEqual(1, parseInt(data.assignee, 10)); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should persist to the flag\'s history', function (done) { | 
					
						
							|  |  |  | 			Flags.getHistory(1, function (err, history) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					throw err; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				history.forEach(function (change) { | 
					
						
							|  |  |  | 					switch (change.attribute) { | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					case 'state': | 
					
						
							|  |  |  | 						assert.strictEqual('[[flags:state-wip]]', change.value); | 
					
						
							|  |  |  | 						break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					case 'assignee': | 
					
						
							|  |  |  | 						assert.strictEqual(1, change.value); | 
					
						
							|  |  |  | 						break; | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.getTarget()', function () { | 
					
						
							|  |  |  | 		it('should return a post\'s data if queried with type "post"', function (done) { | 
					
						
							|  |  |  | 			Flags.getTarget('post', 1, 1, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				var compare = { | 
					
						
							|  |  |  | 					uid: 1, | 
					
						
							|  |  |  | 					pid: 1, | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					content: 'This is flaggable content', | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				for (var key in compare) { | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					if (compare.hasOwnProperty(key)) { | 
					
						
							|  |  |  | 						assert.ok(data[key]); | 
					
						
							| 
									
										
										
										
											2016-12-19 12:31:55 -05:00
										 |  |  | 						assert.equal(data[key], compare[key]); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should return a user\'s data if queried with type "user"', function (done) { | 
					
						
							|  |  |  | 			Flags.getTarget('user', 1, 1, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				var compare = { | 
					
						
							|  |  |  | 					uid: 1, | 
					
						
							|  |  |  | 					username: 'testUser', | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					email: 'b@c.com', | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				for (var key in compare) { | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					if (compare.hasOwnProperty(key)) { | 
					
						
							|  |  |  | 						assert.ok(data[key]); | 
					
						
							| 
									
										
										
										
											2016-12-19 12:31:55 -05:00
										 |  |  | 						assert.equal(data[key], compare[key]); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-19 11:16:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		it('should return a plain object with no properties if the target no longer exists', function (done) { | 
					
						
							|  |  |  | 			Flags.getTarget('user', 15, 1, function (err, data) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert.strictEqual(0, Object.keys(data).length); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.validate()', function () { | 
					
						
							|  |  |  | 		it('should error out if type is post and post is deleted', function (done) { | 
					
						
							|  |  |  | 			Posts.delete(1, 1, function (err) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					throw err; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Flags.validate({ | 
					
						
							|  |  |  | 					type: 'post', | 
					
						
							|  |  |  | 					id: 1, | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					uid: 1, | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				}, function (err) { | 
					
						
							|  |  |  | 					assert.ok(err); | 
					
						
							|  |  |  | 					assert.strictEqual('[[error:post-deleted]]', err.message); | 
					
						
							|  |  |  | 					Posts.restore(1, 1, done); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should not pass validation if flag threshold is set and user rep does not meet it', function (done) { | 
					
						
							|  |  |  | 			Meta.configs.set('privileges:flag', '50', function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Flags.validate({ | 
					
						
							|  |  |  | 					type: 'post', | 
					
						
							|  |  |  | 					id: 1, | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					uid: 3, | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				}, function (err) { | 
					
						
							|  |  |  | 					assert.ok(err); | 
					
						
							|  |  |  | 					assert.strictEqual('[[error:not-enough-reputation-to-flag]]', err.message); | 
					
						
							|  |  |  | 					Meta.configs.set('privileges:flag', 0, done); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.appendNote()', function () { | 
					
						
							|  |  |  | 		it('should add a note to a flag', function (done) { | 
					
						
							|  |  |  | 			Flags.appendNote(1, 1, 'this is my note', function (err) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				db.getSortedSetRange('flag:1:notes', 0, -1, function (err, notes) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						throw err; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					assert.strictEqual('[1,"this is my note"]', notes[0]); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should be a JSON string', function (done) { | 
					
						
							|  |  |  | 			db.getSortedSetRange('flag:1:notes', 0, -1, function (err, notes) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					throw err; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				try { | 
					
						
							|  |  |  | 					JSON.parse(notes[0]); | 
					
						
							|  |  |  | 				} catch (e) { | 
					
						
							|  |  |  | 					assert.ifError(e); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.getNotes()', function () { | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			// Add a second note
 | 
					
						
							|  |  |  | 			Flags.appendNote(1, 1, 'this is the second note', done); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('return should match a predefined spec', function (done) { | 
					
						
							|  |  |  | 			Flags.getNotes(1, function (err, notes) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				var compare = { | 
					
						
							|  |  |  | 					uid: 1, | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					content: 'this is my note', | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				}; | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				var data = notes[1]; | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				for (var key in compare) { | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 					if (compare.hasOwnProperty(key)) { | 
					
						
							|  |  |  | 						assert.ok(data[key]); | 
					
						
							|  |  |  | 						assert.strictEqual(data[key], compare[key]); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should retrieve a list of notes, from newest to oldest', function (done) { | 
					
						
							|  |  |  | 			Flags.getNotes(1, function (err, notes) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 				assert(notes[0].datetime > notes[1].datetime); | 
					
						
							|  |  |  | 				assert.strictEqual('this is the second note', notes[0].content); | 
					
						
							|  |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.appendHistory()', function () { | 
					
						
							|  |  |  | 		var entries; | 
					
						
							|  |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			db.sortedSetCard('flag:1:history', function (err, count) { | 
					
						
							|  |  |  | 				entries = count; | 
					
						
							|  |  |  | 				done(err); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		it('should add a new entry into a flag\'s history', function (done) { | 
					
						
							| 
									
										
										
										
											2016-12-14 15:00:41 -05:00
										 |  |  | 			Flags.appendHistory(1, 1, { | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				state: 'rejected', | 
					
						
							| 
									
										
										
										
											2016-12-14 15:00:41 -05:00
										 |  |  | 			}, function (err) { | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				assert.ifError(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				Flags.getHistory(1, function (err, history) { | 
					
						
							|  |  |  | 					if (err) { | 
					
						
							|  |  |  | 						throw err; | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					assert.strictEqual(entries + 1, history.length); | 
					
						
							|  |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	describe('.getHistory()', function () { | 
					
						
							|  |  |  | 		it('should retrieve a flag\'s history', function (done) { | 
					
						
							|  |  |  | 			Flags.getHistory(1, function (err, history) { | 
					
						
							|  |  |  | 				assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-12-14 15:00:41 -05:00
										 |  |  | 				assert.strictEqual(history[0].fields.state, '[[flags:state-rejected]]'); | 
					
						
							| 
									
										
										
										
											2016-12-13 14:24:09 -05:00
										 |  |  | 				done(); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	describe('(websockets)', function () { | 
					
						
							|  |  |  | 		var SocketFlags = require('../src/socket.io/flags.js'); | 
					
						
							| 
									
										
										
										
											2017-02-24 12:55:07 -05:00
										 |  |  | 		var tid; | 
					
						
							|  |  |  | 		var pid; | 
					
						
							|  |  |  | 		var flag; | 
					
						
							| 
									
										
										
										
											2016-12-01 09:24:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 		before(function (done) { | 
					
						
							|  |  |  | 			Topics.post({ | 
					
						
							|  |  |  | 				cid: 1, | 
					
						
							|  |  |  | 				uid: 1, | 
					
						
							|  |  |  | 				title: 'Another topic', | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 				content: 'This is flaggable content', | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 			}, function (err, topic) { | 
					
						
							|  |  |  | 				tid = topic.postData.tid; | 
					
						
							|  |  |  | 				pid = topic.postData.pid; | 
					
						
							| 
									
										
										
										
											2016-12-01 09:24:49 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 				done(err); | 
					
						
							| 
									
										
										
										
											2016-12-01 09:24:49 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2016-12-06 20:28:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 		describe('.create()', function () { | 
					
						
							|  |  |  | 			it('should create a flag with no errors', function (done) { | 
					
						
							|  |  |  | 				SocketFlags.create({ uid: 2 }, { | 
					
						
							| 
									
										
										
										
											2016-12-06 20:28:54 -05:00
										 |  |  | 					type: 'post', | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 					id: pid, | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					reason: 'foobar', | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 				}, function (err, flagObj) { | 
					
						
							|  |  |  | 					flag = flagObj; | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-12-06 20:28:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 					Flags.exists('post', pid, 1, function (err, exists) { | 
					
						
							|  |  |  | 						assert.ifError(err); | 
					
						
							|  |  |  | 						assert(true); | 
					
						
							|  |  |  | 						done(); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2016-12-06 20:28:54 -05:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 		describe('.update()', function () { | 
					
						
							|  |  |  | 			it('should update a flag\'s properties', function (done) { | 
					
						
							|  |  |  | 				SocketFlags.update({ uid: 2 }, { | 
					
						
							| 
									
										
										
										
											2016-12-19 12:31:55 -05:00
										 |  |  | 					flagId: 2, | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 					data: [{ | 
					
						
							|  |  |  | 						name: 'state', | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 						value: 'wip', | 
					
						
							|  |  |  | 					}], | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 				}, function (err, history) { | 
					
						
							|  |  |  | 					assert.ifError(err); | 
					
						
							|  |  |  | 					assert(Array.isArray(history)); | 
					
						
							|  |  |  | 					assert(history[0].fields.hasOwnProperty('state')); | 
					
						
							|  |  |  | 					assert.strictEqual('[[flags:state-wip]]', history[0].fields.state); | 
					
						
							| 
									
										
										
										
											2016-12-06 20:28:54 -05:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 		describe('.appendNote()', function () { | 
					
						
							|  |  |  | 			it('should append a note to the flag', function (done) { | 
					
						
							|  |  |  | 				SocketFlags.appendNote({ uid: 2 }, { | 
					
						
							| 
									
										
										
										
											2016-12-19 12:31:55 -05:00
										 |  |  | 					flagId: 2, | 
					
						
							| 
									
										
										
										
											2017-02-24 12:47:46 -05:00
										 |  |  | 					note: 'lorem ipsum dolor sit amet', | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 				}, function (err, data) { | 
					
						
							| 
									
										
										
										
											2016-12-19 09:50:46 -05:00
										 |  |  | 					assert.ifError(err); | 
					
						
							| 
									
										
										
										
											2016-12-13 12:11:51 -05:00
										 |  |  | 					assert(data.hasOwnProperty('notes')); | 
					
						
							|  |  |  | 					assert(Array.isArray(data.notes)); | 
					
						
							|  |  |  | 					assert.strictEqual('lorem ipsum dolor sit amet', data.notes[0].content); | 
					
						
							|  |  |  | 					assert.strictEqual(2, data.notes[0].uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					assert(data.hasOwnProperty('history')); | 
					
						
							|  |  |  | 					assert(Array.isArray(data.history)); | 
					
						
							|  |  |  | 					assert.strictEqual(1, Object.keys(data.history[0].fields).length); | 
					
						
							|  |  |  | 					assert(data.history[0].fields.hasOwnProperty('notes')); | 
					
						
							| 
									
										
										
										
											2016-12-06 20:28:54 -05:00
										 |  |  | 					done(); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-11-25 15:09:52 -05:00
										 |  |  | }); |