mirror of
				https://github.com/NodeBB/NodeBB.git
				synced 2025-10-31 19:15:58 +01:00 
			
		
		
		
	flag controller tests
This commit is contained in:
		| @@ -7,61 +7,62 @@ var categories = require('../categories'); | |||||||
| var flags = require('../flags'); | var flags = require('../flags'); | ||||||
| var analytics = require('../analytics'); | var analytics = require('../analytics'); | ||||||
|  |  | ||||||
| var modsController = { | var modsController = module.exports; | ||||||
| 	flags: {}, | modsController.flags = {}; | ||||||
| }; |  | ||||||
|  |  | ||||||
| modsController.flags.list = function (req, res, next) { | modsController.flags.list = function (req, res, next) { | ||||||
| 	async.parallel({ | 	var filters; | ||||||
| 		isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid), | 	var hasFilter; | ||||||
| 		moderatedCids: async.apply(user.getModeratedCids, req.uid), | 	async.waterfall([ | ||||||
| 	}, function (err, results) { | 		function (next) { | ||||||
| 		if (err) { | 			async.parallel({ | ||||||
| 			return next(err); | 				isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, req.uid), | ||||||
| 		} else if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) { | 				moderatedCids: async.apply(user.getModeratedCids, req.uid), | ||||||
| 			return next(new Error('[[error:no-privileges]]')); | 			}, next); | ||||||
| 		} | 		}, | ||||||
|  | 		function (results, next) { | ||||||
| 		if (!results.isAdminOrGlobalMod && results.moderatedCids.length) { | 			if (!(results.isAdminOrGlobalMod || !!results.moderatedCids.length)) { | ||||||
| 			res.locals.cids = results.moderatedCids; | 				return next(new Error('[[error:no-privileges]]')); | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// Parse query string params for filters |  | ||||||
| 		var hasFilter = false; |  | ||||||
| 		var valid = ['assignee', 'state', 'reporterId', 'type', 'targetUid', 'cid', 'quick']; |  | ||||||
| 		var filters = valid.reduce(function (memo, cur) { |  | ||||||
| 			if (req.query.hasOwnProperty(cur)) { |  | ||||||
| 				memo[cur] = req.query[cur]; |  | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
| 			return memo; | 			if (!results.isAdminOrGlobalMod && results.moderatedCids.length) { | ||||||
| 		}, {}); | 				res.locals.cids = results.moderatedCids; | ||||||
| 		hasFilter = !!Object.keys(filters).length; |  | ||||||
|  |  | ||||||
| 		if (res.locals.cids) { |  | ||||||
| 			if (!filters.cid) { |  | ||||||
| 				// If mod and no cid filter, add filter for their modded categories |  | ||||||
| 				filters.cid = res.locals.cids; |  | ||||||
| 			} else if (Array.isArray(filters.cid)) { |  | ||||||
| 				// Remove cids they do not moderate |  | ||||||
| 				filters.cid = filters.cid.filter(function (cid) { |  | ||||||
| 					return res.locals.cids.indexOf(String(cid)) !== -1; |  | ||||||
| 				}); |  | ||||||
| 			} else if (res.locals.cids.indexOf(String(filters.cid)) === -1) { |  | ||||||
| 				filters.cid = res.locals.cids; |  | ||||||
| 				hasFilter = false; |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		async.parallel({ |  | ||||||
| 			flags: async.apply(flags.list, filters, req.uid), |  | ||||||
| 			analytics: async.apply(analytics.getDailyStatsForSet, 'analytics:flags', Date.now(), 30), |  | ||||||
| 			categories: async.apply(categories.buildForSelect, req.uid), |  | ||||||
| 		}, function (err, data) { |  | ||||||
| 			if (err) { |  | ||||||
| 				return next(err); |  | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
|  | 			// Parse query string params for filters | ||||||
|  | 			hasFilter = false; | ||||||
|  | 			var valid = ['assignee', 'state', 'reporterId', 'type', 'targetUid', 'cid', 'quick']; | ||||||
|  | 			filters = valid.reduce(function (memo, cur) { | ||||||
|  | 				if (req.query.hasOwnProperty(cur)) { | ||||||
|  | 					memo[cur] = req.query[cur]; | ||||||
|  | 				} | ||||||
|  |  | ||||||
|  | 				return memo; | ||||||
|  | 			}, {}); | ||||||
|  | 			hasFilter = !!Object.keys(filters).length; | ||||||
|  |  | ||||||
|  | 			if (res.locals.cids) { | ||||||
|  | 				if (!filters.cid) { | ||||||
|  | 					// If mod and no cid filter, add filter for their modded categories | ||||||
|  | 					filters.cid = res.locals.cids; | ||||||
|  | 				} else if (Array.isArray(filters.cid)) { | ||||||
|  | 					// Remove cids they do not moderate | ||||||
|  | 					filters.cid = filters.cid.filter(function (cid) { | ||||||
|  | 						return res.locals.cids.indexOf(String(cid)) !== -1; | ||||||
|  | 					}); | ||||||
|  | 				} else if (res.locals.cids.indexOf(String(filters.cid)) === -1) { | ||||||
|  | 					filters.cid = res.locals.cids; | ||||||
|  | 					hasFilter = false; | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			async.parallel({ | ||||||
|  | 				flags: async.apply(flags.list, filters, req.uid), | ||||||
|  | 				analytics: async.apply(analytics.getDailyStatsForSet, 'analytics:flags', Date.now(), 30), | ||||||
|  | 				categories: async.apply(categories.buildForSelect, req.uid), | ||||||
|  | 			}, next); | ||||||
|  | 		}, | ||||||
|  | 		function (data) { | ||||||
| 			// If res.locals.cids is populated, then slim down the categories list | 			// If res.locals.cids is populated, then slim down the categories list | ||||||
| 			if (res.locals.cids) { | 			if (res.locals.cids) { | ||||||
| 				data.categories = data.categories.filter(function (category) { | 				data.categories = data.categories.filter(function (category) { | ||||||
| @@ -92,8 +93,8 @@ modsController.flags.list = function (req, res, next) { | |||||||
| 				filters: filters, | 				filters: filters, | ||||||
| 				title: '[[pages:flags]]', | 				title: '[[pages:flags]]', | ||||||
| 			}); | 			}); | ||||||
| 		}); | 		}, | ||||||
| 	}); | 	], next); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| modsController.flags.detail = function (req, res, next) { | modsController.flags.detail = function (req, res, next) { | ||||||
| @@ -124,5 +125,3 @@ modsController.flags.detail = function (req, res, next) { | |||||||
| 		})); | 		})); | ||||||
| 	}); | 	}); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| module.exports = modsController; |  | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ var async = require('async'); | |||||||
| var user = require('../user'); | var user = require('../user'); | ||||||
| var flags = require('../flags'); | var flags = require('../flags'); | ||||||
|  |  | ||||||
| var SocketFlags = {}; | var SocketFlags = module.exports; | ||||||
|  |  | ||||||
| SocketFlags.create = function (socket, data, callback) { | SocketFlags.create = function (socket, data, callback) { | ||||||
| 	if (!socket.uid) { | 	if (!socket.uid) { | ||||||
| @@ -98,5 +98,3 @@ SocketFlags.appendNote = function (socket, data, callback) { | |||||||
| 		}, | 		}, | ||||||
| 	], callback); | 	], callback); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| module.exports = SocketFlags; |  | ||||||
|   | |||||||
| @@ -18,6 +18,7 @@ describe('Admin Controllers', function () { | |||||||
| 	var pid; | 	var pid; | ||||||
| 	var adminUid; | 	var adminUid; | ||||||
| 	var regularUid; | 	var regularUid; | ||||||
|  | 	var moderatorUid; | ||||||
| 	var jar; | 	var jar; | ||||||
|  |  | ||||||
| 	before(function (done) { | 	before(function (done) { | ||||||
| @@ -35,12 +36,16 @@ describe('Admin Controllers', function () { | |||||||
| 			regularUid: function (next) { | 			regularUid: function (next) { | ||||||
| 				user.create({ username: 'regular' }, next); | 				user.create({ username: 'regular' }, next); | ||||||
| 			}, | 			}, | ||||||
|  | 			moderatorUid: function (next) { | ||||||
|  | 				user.create({ username: 'moderator', password: 'modmod' }, next); | ||||||
|  | 			}, | ||||||
| 		}, function (err, results) { | 		}, function (err, results) { | ||||||
| 			if (err) { | 			if (err) { | ||||||
| 				return done(err); | 				return done(err); | ||||||
| 			} | 			} | ||||||
| 			adminUid = results.adminUid; | 			adminUid = results.adminUid; | ||||||
| 			regularUid = results.regularUid; | 			regularUid = results.regularUid; | ||||||
|  | 			moderatorUid = results.moderatorUid; | ||||||
| 			cid = results.category.cid; | 			cid = results.category.cid; | ||||||
|  |  | ||||||
| 			topics.post({ uid: adminUid, title: 'test topic title', content: 'test topic content', cid: results.category.cid }, function (err, result) { | 			topics.post({ uid: adminUid, title: 'test topic title', content: 'test topic content', cid: results.category.cid }, function (err, result) { | ||||||
| @@ -128,7 +133,7 @@ describe('Admin Controllers', function () { | |||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
| 	it('should 404 for edit/email page if user does not exist', function (done) { | 	it('should 404 for edit/email page if user does not exist', function (done) { | ||||||
| 		request(nconf.get('url') + '/api/user/doesnotexist/edit/email', { jar: jar, json: true }, function (err, res, body) { | 		request(nconf.get('url') + '/api/user/doesnotexist/edit/email', { jar: jar, json: true }, function (err, res) { | ||||||
| 			assert.ifError(err); | 			assert.ifError(err); | ||||||
| 			assert.equal(res.statusCode, 404); | 			assert.equal(res.statusCode, 404); | ||||||
| 			done(); | 			done(); | ||||||
| @@ -446,6 +451,74 @@ describe('Admin Controllers', function () { | |||||||
| 		}); | 		}); | ||||||
| 	}); | 	}); | ||||||
|  |  | ||||||
|  | 	describe('mods page', function () { | ||||||
|  | 		var moderatorJar; | ||||||
|  |  | ||||||
|  | 		before(function (done) { | ||||||
|  | 			helpers.loginUser('moderator', 'modmod', function (err, _jar) { | ||||||
|  | 				assert.ifError(err); | ||||||
|  | 				moderatorJar = _jar; | ||||||
|  |  | ||||||
|  | 				groups.join('cid:' + cid + ':privileges:mods', moderatorUid, done); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		it('should error with no privileges', function (done) { | ||||||
|  | 			request(nconf.get('url') + '/api/flags', { json: true }, function (err, res, body) { | ||||||
|  | 				assert.ifError(err); | ||||||
|  | 				assert.equal(body.error, '[[error:no-privileges]]'); | ||||||
|  | 				done(); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		it('should load flags page data', function (done) { | ||||||
|  | 			request(nconf.get('url') + '/api/flags', { jar: moderatorJar, json: true }, function (err, res, body) { | ||||||
|  | 				assert.ifError(err); | ||||||
|  | 				assert(body); | ||||||
|  | 				assert(body.flags); | ||||||
|  | 				assert(body.categories); | ||||||
|  | 				assert(body.filters); | ||||||
|  | 				assert.equal(body.categories[cid], 'Test Category'); | ||||||
|  | 				assert.equal(body.filters.cid.indexOf(cid), -1); | ||||||
|  | 				done(); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		it('should return invalid data if flag does not exist', function (done) { | ||||||
|  | 			request(nconf.get('url') + '/api/flags/123123123', { jar: moderatorJar, json: true }, function (err, res, body) { | ||||||
|  | 				assert.ifError(err); | ||||||
|  | 				assert.equal(body.error, '[[error:invalid-data]]'); | ||||||
|  | 				done(); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		it('should error with not enough reputation to flag', function (done) { | ||||||
|  | 			var socketFlags = require('../src/socket.io/flags'); | ||||||
|  |  | ||||||
|  | 			socketFlags.create({ uid: regularUid }, { id: pid, type: 'post', reason: 'spam' }, function (err) { | ||||||
|  | 				assert.equal(err.message, '[[error:not-enough-reputation-to-flag]]'); | ||||||
|  | 				done(); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  |  | ||||||
|  | 		it('should return flag details', function (done) { | ||||||
|  | 			var meta = require('../src/meta'); | ||||||
|  | 			var socketFlags = require('../src/socket.io/flags'); | ||||||
|  | 			var oldValue = meta.config['privileges:flag']; | ||||||
|  | 			meta.config['privileges:flag'] = 0; | ||||||
|  | 			socketFlags.create({ uid: regularUid }, { id: pid, type: 'post', reason: 'spam' }, function (err, data) { | ||||||
|  | 				meta.config['privileges:flag'] = oldValue; | ||||||
|  | 				assert.ifError(err); | ||||||
|  | 				request(nconf.get('url') + '/api/flags/' + data.flagId, { jar: moderatorJar, json: true }, function (err, res, body) { | ||||||
|  | 					assert.ifError(err); | ||||||
|  | 					assert(body); | ||||||
|  | 					assert.equal(body.reporter.username, 'regular'); | ||||||
|  | 					done(); | ||||||
|  | 				}); | ||||||
|  | 			}); | ||||||
|  | 		}); | ||||||
|  | 	}); | ||||||
|  |  | ||||||
| 	after(function (done) { | 	after(function (done) { | ||||||
| 		db.emptydb(done); | 		db.emptydb(done); | ||||||
| 	}); | 	}); | ||||||
|   | |||||||
| @@ -16,6 +16,7 @@ var Meta = require('../src/meta'); | |||||||
|  |  | ||||||
| describe('Flags', function () { | describe('Flags', function () { | ||||||
| 	before(function (done) { | 	before(function (done) { | ||||||
|  | 		Groups.resetCache(); | ||||||
| 		// Create some stuff to flag | 		// Create some stuff to flag | ||||||
| 		async.waterfall([ | 		async.waterfall([ | ||||||
| 			async.apply(User.create, { username: 'testUser', password: 'abcdef', email: 'b@c.com' }), | 			async.apply(User.create, { username: 'testUser', password: 'abcdef', email: 'b@c.com' }), | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user