forked from authts/oidc-client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAccessTokenEvents.test.ts
More file actions
147 lines (115 loc) · 4.05 KB
/
Copy pathAccessTokenEvents.test.ts
File metadata and controls
147 lines (115 loc) · 4.05 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
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import { Timer } from "./utils";
import { AccessTokenEvents } from "./AccessTokenEvents";
import type { User } from "./User";
describe("AccessTokenEvents", () => {
let subject: AccessTokenEvents;
let expiringTimer: StubTimer;
let expiredTimer: StubTimer;
beforeEach(() => {
expiringTimer = new StubTimer("stub expiring timer");
expiredTimer = new StubTimer("stub expired timer");
subject = new AccessTokenEvents({ expiringNotificationTimeInSeconds: 60 });
// access private members
Object.assign(subject, {
_expiringTimer: expiringTimer,
_expiredTimer: expiredTimer,
});
});
describe("constructor", () => {
it("should use default expiringNotificationTime", () => {
expect(subject["_expiringNotificationTimeInSeconds"]).toEqual(60);
});
});
describe("load", () => {
it("should cancel existing timers", async () => {
// act
await subject.load({} as User);
// assert
expect(expiringTimer.cancelWasCalled).toEqual(true);
expect(expiredTimer.cancelWasCalled).toEqual(true);
});
it("should initialize timers", async () => {
// act
await subject.load({
access_token:"token",
expires_in : 70,
} as User);
// assert
expect(expiringTimer.duration).toEqual(10);
expect(expiredTimer.duration).toEqual(71);
});
it("should immediately schedule expiring timer if expiration is soon", async () => {
// act
await subject.load({
access_token:"token",
expires_in : 10,
} as User);
// assert
expect(expiringTimer.duration).toEqual(1);
});
it("should not initialize expiring timer if already expired", async () => {
// act
await subject.load({
access_token:"token",
expires_in : 0,
} as User);
// assert
expect(expiringTimer.duration).toEqual(undefined);
});
it("should initialize expired timer if already expired", async () => {
// act
await subject.load({
access_token:"token",
expires_in : 0,
} as User);
// assert
expect(expiredTimer.duration).toEqual(1);
});
it("should not initialize timers if no access token", async () => {
// act
await subject.load({
expires_in : 70,
} as User);
// assert
expect(expiringTimer.duration).toEqual(undefined);
expect(expiredTimer.duration).toEqual(undefined);
});
it("should not initialize timers if no expiration on access token", async () => {
// act
await subject.load({
access_token:"token",
} as User);
// assert
expect(expiringTimer.duration).toEqual(undefined);
expect(expiredTimer.duration).toEqual(undefined);
});
});
describe("unload", () => {
it("should cancel timers", async () => {
// act
await subject.unload();
// assert
expect(expiringTimer.cancelWasCalled).toEqual(true);
expect(expiredTimer.cancelWasCalled).toEqual(true);
});
});
});
class StubTimer extends Timer {
cancelWasCalled: boolean;
duration: number | undefined;
constructor(name: string) {
super(name);
this.cancelWasCalled = false;
this.duration = undefined;
}
init(duration: number) {
this.duration = duration;
}
cancel() {
this.cancelWasCalled = true;
}
addHandler = jest.fn();
removeHandler = jest.fn();
}