Skip to content

Commit

Permalink
feat: add token model tests
Browse files Browse the repository at this point in the history
  • Loading branch information
saisilinus committed Apr 11, 2022
1 parent 7e02129 commit 537e553
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/modules/token/token.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export interface IToken {
blacklisted: boolean;
}

export interface NewToken {
token: string;
user: string;
type: string;
expires: Date;
}

export interface ITokenDoc extends IToken, Document {}

export interface ITokenModel extends Model<ITokenDoc> {
Expand Down
29 changes: 29 additions & 0 deletions src/modules/token/token.model.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import moment from 'moment';
import config from '../../config/config';
import { NewToken } from './token.interfaces';
import { userOne } from '../user/user.fixture';
import { userOneAccessToken } from './token.fixture';
import tokenTypes from './token.types';
import Token from './token.model';

describe('Token Model', () => {
const refreshTokenExpires = moment().add(config.jwt.refreshExpirationDays, 'days');
let newToken: NewToken;
beforeEach(() => {
newToken = {
token: userOneAccessToken,
user: userOne._id.toHexString(),
type: tokenTypes.REFRESH,
expires: refreshTokenExpires.toDate(),
};
});

test('should correctly validate a valid token', async () => {
await expect(new Token(newToken).validate()).resolves.toBeUndefined();
});

test('should throw a validation error if type is unknown', async () => {
newToken.type = 'invalidType';
await expect(new Token(newToken).validate()).rejects.toThrow();
});
});

0 comments on commit 537e553

Please sign in to comment.