-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular2-jwt.spec.js
More file actions
153 lines (153 loc) · 7.28 KB
/
Copy pathangular2-jwt.spec.js
File metadata and controls
153 lines (153 loc) · 7.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
"use strict";
require("core-js");
var angular2_jwt_1 = require("./angular2-jwt");
var angular2_jwt_2 = require("./angular2-jwt");
var angular2_jwt_3 = require("./angular2-jwt");
var rxjs_1 = require("rxjs");
var expiredToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjB9.m2OKoK5-Fnbbg4inMrsAQKsehq2wpQYim8695uLdogk";
var validToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjk5OTk5OTk5OTl9.K_lUwtGbvjCHP8Ff-gW9GykydkkXzHKRPbACxItvrFU";
var noExpiryToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ";
describe('AuthConfig', function () {
'use strict';
it('should have default values', function () {
var config = new angular2_jwt_1.AuthConfig().getConfig();
expect(config).toBeDefined();
expect(config.headerName).toBe("Authorization");
expect(config.headerPrefix).toBe("Bearer ");
expect(config.tokenName).toBe("id_token");
expect(config.noJwtError).toBe(false);
expect(config.noTokenScheme).toBe(false);
expect(config.globalHeaders).toEqual([]);
expect(config.tokenGetter).toBeDefined();
var token = "Token";
localStorage.setItem(config.tokenName, token);
expect(config.tokenGetter()).toBe(token);
});
it('should have default values', function () {
var configExpected = {
headerName: "Foo",
headerPrefix: "Bar",
tokenName: "token",
tokenGetter: function () { return "this is a token"; },
noJwtError: true,
globalHeaders: [{ "header": "value" }, { "header2": "value2" }],
noTokenScheme: true
};
var config = new angular2_jwt_1.AuthConfig(configExpected).getConfig();
expect(config).toBeDefined();
expect(config.headerName).toBe(configExpected.headerName);
expect(config.headerPrefix).toBe(configExpected.headerPrefix + " ");
expect(config.tokenName).toBe(configExpected.tokenName);
expect(config.noJwtError).toBe(configExpected.noJwtError);
expect(config.noTokenScheme).toBe(configExpected.noTokenScheme);
expect(config.globalHeaders).toEqual(configExpected.globalHeaders);
expect(config.tokenGetter).toBeDefined();
expect(config.tokenGetter()).toBe("this is a token");
});
});
describe('JwtHelper', function () {
'use strict';
describe('urlBase64Decode', function () {
});
describe('decodeToken', function () {
});
describe('getTokenExpirationDate', function () {
});
describe('isTokenExpired', function () {
var jwtHelper;
beforeEach(function () {
jwtHelper = new angular2_jwt_3.JwtHelper();
});
it('should return false when the token is not expired', function () {
var actual = jwtHelper.isTokenExpired(validToken);
expect(actual).toBe(false);
});
it('should return true when the token is expired', function () {
var actual = jwtHelper.isTokenExpired(expiredToken);
expect(actual).toBe(true);
});
it('should return false when the token doesn\'t have an expiry date', function () {
var actual = jwtHelper.isTokenExpired(noExpiryToken);
expect(actual).toBe(false);
});
// it('should return false when the token is expired, but within the grace period', ()=> {
// console.log("test start");
// // return a date that has expired 5 seconds ago
// jwtHelper.getTokenExpirationDate=(token:string)=>{
// const date=new Date(new Date().valueOf()-5000);
// console.log("token date",date);
// console.log("actual date",new Date());
// return date;
// };
// //token doesn't matter because we mocked getTokenExpirationDate
// const tokenExpired:boolean=jwtHelper.isTokenExpired("");
// expect(tokenExpired).toBe(true,"token should be expired");
// const tokenExpired:boolean=jwtHelper.isTokenExpired("",6);
// expect(tokenExpired).toBe(false,"token should be within the grace period");
// console.log("test end");
// });
// it('should return true when the token is expired and outside the grace period', ()=> {
// // return a date that has expired 5 seconds ago
// jwtHelper.getTokenExpirationDate=(token:string)=>new Date(new Date().valueOf()-5000);
// //token doesn't matter because we mocked getTokenExpirationDate
// const tokenExpired:boolean=jwtHelper.isTokenExpired("");
// expect(tokenExpired).toBe(false,"token should be expired");
// const tokenExpired:boolean=jwtHelper.isTokenExpired("",3);
// expect(tokenExpired).toBe(true,"token should not be within the grace period");
// });
});
});
describe('tokenNotExpired', function () {
'use strict';
it('should use the passed token when not expired', function () {
var actual = angular2_jwt_2.tokenNotExpired(null, validToken);
expect(actual).toBe(true);
});
it('should use the passed token when expired', function () {
var actual = angular2_jwt_2.tokenNotExpired(null, expiredToken);
expect(actual).toBe(false);
});
it('should use the passed tokenName when not expired', function () {
localStorage.setItem("Valid", validToken);
var actual = angular2_jwt_2.tokenNotExpired("Valid");
expect(actual).toBe(true);
});
it('should use the passed tokenName when expired', function () {
localStorage.setItem("Expired", expiredToken);
var actual = angular2_jwt_2.tokenNotExpired("Expired");
expect(actual).toBe(false);
});
it('should use the defaults when not expired', function () {
localStorage.setItem("id_token", validToken);
var actual = angular2_jwt_2.tokenNotExpired();
expect(actual).toBe(true);
});
it('should use the defaults when expired', function () {
localStorage.setItem("id_token", expiredToken);
var actual = angular2_jwt_2.tokenNotExpired();
expect(actual).toBe(false);
});
});
describe("AuthHttp", function () {
describe("request", function () {
it("handles tokenGetters returning string", function () {
var authHttp = new angular2_jwt_1.AuthHttp(new angular2_jwt_1.AuthConfig({
tokenGetter: function () { return validToken; }
}), null);
spyOn(authHttp, "requestWithToken").and.stub();
authHttp.request(null);
expect(authHttp["requestWithToken"]).toHaveBeenCalledWith(null, validToken);
});
it("handles tokenGetters returning Promise\<string\>", function (done) {
var authHttp = new angular2_jwt_1.AuthHttp(new angular2_jwt_1.AuthConfig({
tokenGetter: function () { return Promise.resolve(validToken); }
}), null);
spyOn(authHttp, "requestWithToken").and.returnValue(rxjs_1.Observable.of(""));
authHttp.request(null).subscribe(function () {
expect(authHttp["requestWithToken"]).toHaveBeenCalledWith(null, validToken);
done();
});
});
});
});
//# sourceMappingURL=angular2-jwt.spec.js.map