Implement cannot ignore staff in users service

`UsersService.ignoreUsers` now checks if any user provided is a staff
member. In case it is it throws a ErrCannotIgnoreStaff error.
This commit is contained in:
Daniel Janeiro
2017-10-07 22:35:54 +01:00
parent 0c7b35ffde
commit 7eaad1541b
2 changed files with 20 additions and 1 deletions
+7 -1
View File
@@ -2,6 +2,7 @@ const assert = require('assert');
const uuid = require('uuid');
const bcrypt = require('bcryptjs');
const errors = require('../errors');
const some = require('lodash/some');
const {
ROOT_URL
@@ -879,13 +880,18 @@ module.exports = class UsersService {
* @param {String} id the id of the user that is ignoring another users
* @param {Array<String>} usersToIgnore Array of user IDs to ignore
*/
static ignoreUsers(id, usersToIgnore) {
static async ignoreUsers(id, usersToIgnore) {
assert(Array.isArray(usersToIgnore), 'usersToIgnore is an array');
assert(usersToIgnore.every((u) => typeof u === 'string'), 'usersToIgnore is an array of string user IDs');
if (usersToIgnore.includes(id)) {
throw new Error('Users cannot ignore themselves');
}
const users = await UsersService.findByIdArray(usersToIgnore);
if (some(users, (user) => user.isStaff())) {
throw errors.ErrCannotIgnoreStaff;
}
// TODO: For each usersToIgnore, make sure they exist?
return UserModel.update({id}, {
$addToSet: {
+13
View File
@@ -164,6 +164,19 @@ describe('services.UsersService', () => {
const userAfterIgnoring2 = await UsersService.findById(user.id);
expect(userAfterIgnoring2.ignoresUsers.length).to.equal(2);
});
it('should not ignore a staff member', async () => {
const user = mockUsers[0];
const usersToIgnore = [mockUsers[1]];
await UsersService.addRoleToUser(usersToIgnore[0].id, 'STAFF');
try {
await UsersService.ignoreUsers(user.id, usersToIgnore.map((u) => u.id));
} catch (err) {
expect(err.status).to.equal(400);
expect(err.translation_key).to.equal('CANNOT_IGNORE_STAFF');
}
});
});
describe('#ban', () => {