Skip to content

Commit

Permalink
checking test issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Behzad-rabiei committed Mar 7, 2024
1 parent 975b58d commit bc063bd
Show file tree
Hide file tree
Showing 8 changed files with 2,557 additions and 2,560 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ COPY . .
RUN npm ci

FROM base AS test
CMD [ "npx", "jest", "--coverage" ]
CMD [ "npx", "jest", "--detectOpenHandles", "--coverage" ]

FROM base AS build
RUN npm run build
Expand Down
1,044 changes: 522 additions & 522 deletions __tests__/integration/announcement.test.ts

Large diffs are not rendered by default.

246 changes: 123 additions & 123 deletions __tests__/integration/auth.test.ts
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);
});
});
});
Loading

0 comments on commit bc063bd

Please sign in to comment.