Skip to content

Commit 323c91b

Browse files
committed
tests(unit): improve coverage for TokenModel
1 parent 92cc613 commit 323c91b

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed

test/unit/models/token-model_test.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,107 @@
11
const TokenModel = require('../../../lib/models/token-model');
2+
const InvalidArgumentError = require('../../../lib/errors/invalid-argument-error');
23
const should = require('chai').should();
34
/**
45
* Test `Server`.
56
*/
67

78
describe('Model', function() {
89
describe('constructor()', function() {
10+
it('throws, if data is empty', function () {
11+
try {
12+
new TokenModel();
13+
should.fail();
14+
} catch (e) {
15+
e.should.be.an.instanceOf(InvalidArgumentError);
16+
e.message.should.equal('Missing parameter: `accessToken`');
17+
}
18+
});
19+
it('throws, if `accessToken` is missing', function () {
20+
const atExpiresAt = new Date();
21+
atExpiresAt.setHours(new Date().getHours() + 1);
22+
23+
const data = {
24+
client: 'bar',
25+
user: 'tar',
26+
accessTokenExpiresAt: atExpiresAt
27+
};
28+
29+
try {
30+
new TokenModel(data);
31+
should.fail();
32+
} catch (e) {
33+
e.should.be.an.instanceOf(InvalidArgumentError);
34+
e.message.should.equal('Missing parameter: `accessToken`');
35+
}
36+
});
37+
it('throws, if `client` is missing', function () {
38+
const atExpiresAt = new Date();
39+
atExpiresAt.setHours(new Date().getHours() + 1);
40+
41+
const data = {
42+
accessToken: 'foo',
43+
user: 'tar',
44+
accessTokenExpiresAt: atExpiresAt
45+
};
46+
47+
try {
48+
new TokenModel(data);
49+
should.fail();
50+
} catch (e) {
51+
e.should.be.an.instanceOf(InvalidArgumentError);
52+
e.message.should.equal('Missing parameter: `client`');
53+
}
54+
});
55+
it('throws, if `user` is missing', function () {
56+
const atExpiresAt = new Date();
57+
atExpiresAt.setHours(new Date().getHours() + 1);
58+
59+
const data = {
60+
accessToken: 'foo',
61+
client: 'bar',
62+
accessTokenExpiresAt: atExpiresAt
63+
};
64+
65+
try {
66+
new TokenModel(data);
67+
should.fail();
68+
} catch (e) {
69+
e.should.be.an.instanceOf(InvalidArgumentError);
70+
e.message.should.equal('Missing parameter: `user`');
71+
}
72+
});
73+
it('throws, if `accessTokenExpiresAt` is not a Date', function () {
74+
const data = {
75+
accessToken: 'foo',
76+
client: 'bar',
77+
user: 'tar',
78+
accessTokenExpiresAt: '11/10/2023'
79+
};
80+
81+
try {
82+
new TokenModel(data);
83+
should.fail();
84+
} catch (e) {
85+
e.should.be.an.instanceOf(InvalidArgumentError);
86+
e.message.should.equal('Invalid parameter: `accessTokenExpiresAt`');
87+
}
88+
});
89+
it('throws, if `refreshTokenExpiresAt` is not a Date', function () {
90+
const data = {
91+
accessToken: 'foo',
92+
client: 'bar',
93+
user: 'tar',
94+
refreshTokenExpiresAt: '11/10/2023'
95+
};
96+
97+
try {
98+
new TokenModel(data);
99+
should.fail();
100+
} catch (e) {
101+
e.should.be.an.instanceOf(InvalidArgumentError);
102+
e.message.should.equal('Invalid parameter: `refreshTokenExpiresAt`');
103+
}
104+
});
9105
it('should calculate `accessTokenLifetime` if `accessTokenExpiresAt` is set', function() {
10106
const atExpiresAt = new Date();
11107
atExpiresAt.setHours(new Date().getHours() + 1);

0 commit comments

Comments
 (0)