Skip to content

Commit

Permalink
Add delete user tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Nov 5, 2019
1 parent f7a7aab commit e28cee0
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions tests/integration/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,70 @@ describe('User routes', () => {
});
});

describe('DELETE /v1/users/:userId', () => {
test('should return 204 if data is ok', async () => {
await insertUsers([userOne]);

await request(app)
.delete(`/v1/users/${userOne._id}`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.send()
.expect(httpStatus.NO_CONTENT);

const dbUser = await User.findById(userOne._id);
expect(dbUser).toBeNull();
});

test('should return 401 error if access token is missing', async () => {
await insertUsers([userOne]);

await request(app)
.delete(`/v1/users/${userOne._id}`)
.send()
.expect(httpStatus.UNAUTHORIZED);
});

test('should return 403 error if user is trying to delete another user', async () => {
await insertUsers([userOne, userTwo]);

await request(app)
.delete(`/v1/users/${userTwo._id}`)
.set('Authorization', `Bearer ${userOneAccessToken}`)
.send()
.expect(httpStatus.FORBIDDEN);
});

test('should return 204 if admin is trying to delete another user', async () => {
await insertUsers([userOne, admin]);

await request(app)
.delete(`/v1/users/${userOne._id}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.send()
.expect(httpStatus.NO_CONTENT);
});

test('should return 400 error if userId is not a valid mongo id', async () => {
await insertUsers([admin]);

await request(app)
.delete('/v1/users/invalidId')
.set('Authorization', `Bearer ${adminAccessToken}`)
.send()
.expect(httpStatus.BAD_REQUEST);
});

test('should return 404 error if user already is not found', async () => {
await insertUsers([admin]);

await request(app)
.delete(`/v1/users/${userOne._id}`)
.set('Authorization', `Bearer ${adminAccessToken}`)
.send()
.expect(httpStatus.NOT_FOUND);
});
});

describe('PATCH /v1/users/:userId', () => {
test('should return 200 and successfully update user if data is ok', async () => {
await insertUsers([userOne]);
Expand Down

0 comments on commit e28cee0

Please sign in to comment.