-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
975b58d
commit bc063bd
Showing
8 changed files
with
2,557 additions
and
2,560 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,123 +1,123 @@ | ||
// import request from 'supertest'; | ||
// import httpStatus from 'http-status'; | ||
// import moment from 'moment'; | ||
// import app from '../../src/app'; | ||
// import config from '../../src/config'; | ||
// import { tokenService } from '../../src/services'; | ||
// import setupTestDB from '../utils/setupTestDB'; | ||
// import { tokenTypes } from '../../src/config/tokens'; | ||
// import { userOne, insertUsers } from '../fixtures/user.fixture'; | ||
// import { Token } from '@togethercrew.dev/db'; | ||
|
||
// setupTestDB(); | ||
|
||
// describe('Auth routes', () => { | ||
// describe('POST /api/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: userOne._id }, expires, tokenTypes.REFRESH); | ||
// await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
// await request(app).post('/api/v1/auth/logout').send({ refreshToken }).expect(httpStatus.NO_CONTENT); | ||
|
||
// const dbRefreshTokenDoc = await Token.findOne({ token: refreshToken }); | ||
// expect(dbRefreshTokenDoc).toBe(null); | ||
// }); | ||
|
||
// 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, expires, tokenTypes.REFRESH); | ||
|
||
// await request(app).post('/api/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, expires, tokenTypes.REFRESH); | ||
// await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH, true); | ||
|
||
// await request(app).post('/api/v1/auth/logout').send({ refreshToken }).expect(httpStatus.NOT_FOUND); | ||
// }); | ||
|
||
// test('should return 400 if refresh token does not send', async () => { | ||
// await request(app).post('/api/v1/auth/logout').send().expect(httpStatus.BAD_REQUEST); | ||
// }); | ||
// }); | ||
|
||
// describe('POST /api/v1/auth/refresh-tokens', () => { | ||
// test('should return 200 and new auth tokens if refresh token is valid', async () => { | ||
// await insertUsers([userOne]); | ||
// const expires = moment().add(config.jwt.refreshExpirationDays, 'days'); | ||
// const refreshToken = tokenService.generateToken({ ...userOne, id: userOne._id }, expires, tokenTypes.REFRESH); | ||
// await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
// const res = await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.OK); | ||
|
||
// expect(res.body).toEqual({ | ||
// access: { token: expect.anything(), expires: expect.anything() }, | ||
// refresh: { token: expect.anything(), expires: expect.anything() }, | ||
// }); | ||
|
||
// const dbRefreshTokenDoc = await Token.findOne({ token: res.body.refresh.token }); | ||
// expect(dbRefreshTokenDoc).toMatchObject({ type: tokenTypes.REFRESH, user: userOne._id, blacklisted: false }); | ||
|
||
// const dbRefreshTokenCount = await Token.countDocuments(); | ||
// expect(dbRefreshTokenCount).toBe(1); | ||
// }); | ||
|
||
// test('should return 401 error if refresh token is signed using an invalid secret', async () => { | ||
// await insertUsers([userOne]); | ||
// const expires = moment().add(config.jwt.refreshExpirationDays, 'days'); | ||
// const refreshToken = tokenService.generateToken( | ||
// { ...userOne, id: userOne._id }, | ||
// expires, | ||
// tokenTypes.REFRESH, | ||
// 'invalidSecret', | ||
// ); | ||
// await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
// await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
// }); | ||
|
||
// test('should return 401 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: userOne._id }, expires, tokenTypes.REFRESH); | ||
|
||
// await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
// }); | ||
|
||
// test('should return 401 error if refresh token is blacklisted', async () => { | ||
// await insertUsers([userOne]); | ||
// const expires = moment().add(config.jwt.refreshExpirationDays, 'days'); | ||
// const refreshToken = tokenService.generateToken({ ...userOne, id: userOne._id }, expires, tokenTypes.REFRESH); | ||
// await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH, true); | ||
|
||
// await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
// }); | ||
|
||
// test('should return 401 error if refresh token is expired', async () => { | ||
// await insertUsers([userOne]); | ||
// const expires = moment().subtract(1, 'minutes'); | ||
// const refreshToken = tokenService.generateToken({ ...userOne, id: userOne._id }, expires, tokenTypes.REFRESH); | ||
// await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
// await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
// }); | ||
|
||
// test('should return 401 error if user is not found', async () => { | ||
// const expires = moment().add(config.jwt.refreshExpirationDays, 'days'); | ||
// const refreshToken = tokenService.generateToken({ ...userOne, id: userOne._id }, expires, tokenTypes.REFRESH); | ||
// await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
// await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
// }); | ||
|
||
// test('should return 400 if refresh token does not send', async () => { | ||
// await request(app).post('/api/v1/auth/refresh-tokens').send().expect(httpStatus.BAD_REQUEST); | ||
// }); | ||
// }); | ||
// }); | ||
import request from 'supertest'; | ||
import httpStatus from 'http-status'; | ||
import moment from 'moment'; | ||
import app from '../../src/app'; | ||
import config from '../../src/config'; | ||
import { tokenService } from '../../src/services'; | ||
import setupTestDB from '../utils/setupTestDB'; | ||
import { tokenTypes } from '../../src/config/tokens'; | ||
import { userOne, insertUsers } from '../fixtures/user.fixture'; | ||
import { Token } from '@togethercrew.dev/db'; | ||
|
||
setupTestDB(); | ||
|
||
describe('Auth routes', () => { | ||
describe('POST /api/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: userOne._id }, expires, tokenTypes.REFRESH); | ||
await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
await request(app).post('/api/v1/auth/logout').send({ refreshToken }).expect(httpStatus.NO_CONTENT); | ||
|
||
const dbRefreshTokenDoc = await Token.findOne({ token: refreshToken }); | ||
expect(dbRefreshTokenDoc).toBe(null); | ||
}); | ||
|
||
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, expires, tokenTypes.REFRESH); | ||
|
||
await request(app).post('/api/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, expires, tokenTypes.REFRESH); | ||
await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH, true); | ||
|
||
await request(app).post('/api/v1/auth/logout').send({ refreshToken }).expect(httpStatus.NOT_FOUND); | ||
}); | ||
|
||
test('should return 400 if refresh token does not send', async () => { | ||
await request(app).post('/api/v1/auth/logout').send().expect(httpStatus.BAD_REQUEST); | ||
}); | ||
}); | ||
|
||
describe('POST /api/v1/auth/refresh-tokens', () => { | ||
test('should return 200 and new auth tokens if refresh token is valid', async () => { | ||
await insertUsers([userOne]); | ||
const expires = moment().add(config.jwt.refreshExpirationDays, 'days'); | ||
const refreshToken = tokenService.generateToken({ ...userOne, id: userOne._id }, expires, tokenTypes.REFRESH); | ||
await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
const res = await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.OK); | ||
|
||
expect(res.body).toEqual({ | ||
access: { token: expect.anything(), expires: expect.anything() }, | ||
refresh: { token: expect.anything(), expires: expect.anything() }, | ||
}); | ||
|
||
const dbRefreshTokenDoc = await Token.findOne({ token: res.body.refresh.token }); | ||
expect(dbRefreshTokenDoc).toMatchObject({ type: tokenTypes.REFRESH, user: userOne._id, blacklisted: false }); | ||
|
||
const dbRefreshTokenCount = await Token.countDocuments(); | ||
expect(dbRefreshTokenCount).toBe(1); | ||
}); | ||
|
||
test('should return 401 error if refresh token is signed using an invalid secret', async () => { | ||
await insertUsers([userOne]); | ||
const expires = moment().add(config.jwt.refreshExpirationDays, 'days'); | ||
const refreshToken = tokenService.generateToken( | ||
{ ...userOne, id: userOne._id }, | ||
expires, | ||
tokenTypes.REFRESH, | ||
'invalidSecret', | ||
); | ||
await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
}); | ||
|
||
test('should return 401 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: userOne._id }, expires, tokenTypes.REFRESH); | ||
|
||
await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
}); | ||
|
||
test('should return 401 error if refresh token is blacklisted', async () => { | ||
await insertUsers([userOne]); | ||
const expires = moment().add(config.jwt.refreshExpirationDays, 'days'); | ||
const refreshToken = tokenService.generateToken({ ...userOne, id: userOne._id }, expires, tokenTypes.REFRESH); | ||
await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH, true); | ||
|
||
await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
}); | ||
|
||
test('should return 401 error if refresh token is expired', async () => { | ||
await insertUsers([userOne]); | ||
const expires = moment().subtract(1, 'minutes'); | ||
const refreshToken = tokenService.generateToken({ ...userOne, id: userOne._id }, expires, tokenTypes.REFRESH); | ||
await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
}); | ||
|
||
test('should return 401 error if user is not found', async () => { | ||
const expires = moment().add(config.jwt.refreshExpirationDays, 'days'); | ||
const refreshToken = tokenService.generateToken({ ...userOne, id: userOne._id }, expires, tokenTypes.REFRESH); | ||
await tokenService.saveToken(refreshToken, userOne._id, expires, tokenTypes.REFRESH); | ||
|
||
await request(app).post('/api/v1/auth/refresh-tokens').send({ refreshToken }).expect(httpStatus.UNAUTHORIZED); | ||
}); | ||
|
||
test('should return 400 if refresh token does not send', async () => { | ||
await request(app).post('/api/v1/auth/refresh-tokens').send().expect(httpStatus.BAD_REQUEST); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.