Skip to content

Commit

Permalink
Fix logout and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hagopj13 committed Aug 21, 2020
1 parent d22c2de commit 141de58
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/routes/v1/auth.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ module.exports = router;
* responses:
* "204":
* description: No content
* "401":
* $ref: '#/components/responses/Unauthorized'
* "404":
* $ref: '#/components/responses/NotFound'
*/

/**
Expand Down
9 changes: 4 additions & 5 deletions src/services/auth.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@ const loginUserWithEmailAndPassword = async (email, password) => {
* @returns {Promise}
*/
const logout = async (refreshToken) => {
try {
const refreshTokenDoc = await tokenService.verifyToken(refreshToken, 'refresh');
await refreshTokenDoc.remove();
} catch (error) {
throw new ApiError(httpStatus.UNAUTHORIZED, 'Please authenticate');
const refreshTokenDoc = await Token.findOne({ token: refreshToken, type: 'refresh', blacklisted: false });
if (!refreshTokenDoc) {
throw new ApiError(httpStatus.NOT_FOUND, 'Not found');
}
await refreshTokenDoc.remove();
};

/**
Expand Down
35 changes: 35 additions & 0 deletions tests/integration/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,41 @@ describe('Auth routes', () => {
});
});

describe('POST /v1/auth/logout', () => {
test('should return 204 if refresh token is valid', async () => {
await insertUsers([userOne]);
const expires = moment().add(config.jwt.refreshExpirationDays, 'days');
const refreshToken = tokenService.generateToken(userOne._id, expires);
await tokenService.saveToken(refreshToken, userOne._id, expires, 'refresh');

await request(app).post('/v1/auth/logout').send({ refreshToken }).expect(httpStatus.NO_CONTENT);

const dbRefreshTokenDoc = await Token.findOne({ token: refreshToken });
expect(dbRefreshTokenDoc).toBe(null);
});

test('should return 400 error if refresh token is missing from request body', async () => {
await request(app).post('/v1/auth/logout').send().expect(httpStatus.BAD_REQUEST);
});

test('should return 404 error if refresh token is not found in the database', async () => {
await insertUsers([userOne]);
const expires = moment().add(config.jwt.refreshExpirationDays, 'days');
const refreshToken = tokenService.generateToken(userOne._id, expires);

await request(app).post('/v1/auth/logout').send({ refreshToken }).expect(httpStatus.NOT_FOUND);
});

test('should return 404 error if refresh token is blacklisted', async () => {
await insertUsers([userOne]);
const expires = moment().add(config.jwt.refreshExpirationDays, 'days');
const refreshToken = tokenService.generateToken(userOne._id, expires);
await tokenService.saveToken(refreshToken, userOne._id, expires, 'refresh', true);

await request(app).post('/v1/auth/logout').send({ refreshToken }).expect(httpStatus.NOT_FOUND);
});
});

describe('POST /v1/auth/refresh-tokens', () => {
test('should return 200 and new auth tokens if refresh token is valid', async () => {
await insertUsers([userOne]);
Expand Down

0 comments on commit 141de58

Please sign in to comment.