Users cannot ignore themselves at service layer

This commit is contained in:
Benjamin Goering
2017-04-11 14:20:37 -07:00
parent 58736586e4
commit 7dfe12b874
2 changed files with 19 additions and 0 deletions
+3
View File
@@ -844,6 +844,9 @@ module.exports = class UsersService {
static ignoreUsers(userId, 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(userId)) {
throw new Error('Users cannot ignore themselves');
}
// TODO: For each usersToIgnore, make sure they exist?
return UserModel.update({id: userId}, {
+16
View File
@@ -53,6 +53,22 @@ describe('graph.mutations.ignoreUser', () => {
expect(myIgnoredUsers[0].username).to.equal(userToIgnore.username);
});
it('users cannot ignore themselves', async () => {
const user = await UsersService.createLocalUser('usernameA@example.com', 'password', 'usernameA');
const context = new Context({user});
const ignoreUserResponse = await graphql(schema, ignoreUserMutation, {}, context, {id: user.id});
expect(ignoreUserResponse.errors).to.not.be.empty;
// now check my ignored users
const myIgnoredUsersResponse = await graphql(schema, getMyIgnoredUsersQuery, {}, context, {});
if (myIgnoredUsersResponse.errors && myIgnoredUsersResponse.errors.length) {
console.error(myIgnoredUsersResponse.errors);
}
expect(myIgnoredUsersResponse.errors).to.be.empty;
const myIgnoredUsers = myIgnoredUsersResponse.data.myIgnoredUsers;
expect(myIgnoredUsers.length).to.equal(0);
});
});
describe('graph.mutations.stopIgnoringUser', () => {