Skip to content

Commit

Permalink
fix(test): cover token issuance and verification
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Apr 3, 2020
1 parent d33c359 commit 1bd0c8b
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions test/entity/auth/TestToken.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { NotFoundError } from '@apextoaster/js-utils';
import { expect } from 'chai';
import { ineeda } from 'ineeda';
import { JsonWebTokenError } from 'jsonwebtoken';

import { Token, TokenOptions } from '../../../src/entity/auth/Token';
import { User } from '../../../src/entity/auth/User';
import { describeLeaks, itLeaks } from '../../helpers/async';

const TEST_SECRET = 'test-secret';
const TEST_BACKDATE = 1_000;

describeLeaks('token entity', async () => {
itLeaks('should copy options', async () => {
const testProps = {
Expand Down Expand Up @@ -101,3 +105,39 @@ describeLeaks('token entity', async () => {
expect(() => token.session()).to.throw(NotFoundError);
});
});

describe('verify token static method', async () => {
it('should verify complete tokens', async () => {
const token = new Token({
audience: ['test'],
data: {},
expiresAt: new Date(Date.now() + TEST_BACKDATE),
grants: [],
issuer: 'test',
labels: {},
subject: 'test',
});
token.id = 'test';
const jwt = token.sign(TEST_SECRET);

expect(Token.verify(jwt, TEST_SECRET, {})).to.deep.include({
iss: 'test',
});
});

it('should reject expired tokens', async () => {
const token = new Token({
audience: ['test'],
data: {},
expiresAt: new Date(Date.now() - TEST_BACKDATE),
grants: [],
issuer: 'test',
labels: {},
subject: 'test',
});
token.id = 'test';
const jwt = token.sign(TEST_SECRET);

expect(() => Token.verify(jwt, TEST_SECRET, {})).to.throw(JsonWebTokenError);
});
});

0 comments on commit 1bd0c8b

Please sign in to comment.