Skip to content

Commit

Permalink
added ForbiddenError
Browse files Browse the repository at this point in the history
  • Loading branch information
nik-dange committed Jan 30, 2024
1 parent 7410ef9 commit 5d72c43
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
6 changes: 3 additions & 3 deletions services/UserAccountService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BadRequestError, NotFoundError } from 'routing-controllers';
import { BadRequestError, ForbiddenError, NotFoundError } from 'routing-controllers';
import { Service } from 'typedi';
import { InjectManager } from 'typeorm-typedi-extensions';
import { EntityManager } from 'typeorm';
Expand Down Expand Up @@ -230,15 +230,15 @@ export default class UserAccountService {

// Prevent a user from demoting themselves
if (currentUser.email === userEmail) {
throw new BadRequestError('Cannot alter own access level');
throw new ForbiddenError('Cannot alter own access level');
}

const userToUpdate = emailToUserMap[userEmail];
const oldAccess = userToUpdate.accessType;

// Prevent users from promoting to admin or demoting from admin
if (oldAccess === 'ADMIN' || accessType === 'ADMIN') {
throw new BadRequestError('Cannot alter access level of admin users');
throw new ForbiddenError('Cannot alter access level of admin users');
}

const updatedUser = await userRepository.upsertUser(userToUpdate, { accessType });
Expand Down
6 changes: 3 additions & 3 deletions tests/admin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ describe('updating user access level', () => {
{ user: secondAdmin.email, accessType: UserAccessType.MERCH_STORE_MANAGER },
],
}, admin);
}).rejects.toThrow(BadRequestError);
}).rejects.toThrow(ForbiddenError);

const repository = conn.getRepository(UserModel);
const secondAdminFromDatabase = await repository.findOne({ email: secondAdmin.email });
Expand All @@ -337,7 +337,7 @@ describe('updating user access level', () => {
{ user: regularUser.email, accessType: UserAccessType.ADMIN },
],
}, admin);
}).rejects.toThrow(BadRequestError);
}).rejects.toThrow(ForbiddenError);

const regularUserFromDatabase = await repository.findOne({ email: regularUser.email });

Expand Down Expand Up @@ -404,7 +404,7 @@ describe('updating user access level', () => {
{ user: admin.email, accessType: UserAccessType.STANDARD },
],
}, admin);
}).rejects.toThrow(BadRequestError);
}).rejects.toThrow(ForbiddenError);

const repository = conn.getRepository(UserModel);
const existingAdmin = await repository.find({
Expand Down

0 comments on commit 5d72c43

Please sign in to comment.