Skip to content

Commit 389dc71

Browse files
committed
build class to wrap token expiration events

1 parent 4f7d8ed commit 389dc71

2 files changed

Lines changed: 134 additions & 0 deletions

File tree

‎src/AccessTokenEvents.js‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
import Log from './Log';
5+
import Timer from './Timer';
6+
7+
const DefaultExpiringNotificationTime = 60;
8+
9+
export default class AccessTokenEvents {
10+
11+
constructor({
12+
expiringNotificationTime = DefaultExpiringNotificationTime,
13+
expiringTimer = new Timer(),
14+
expiredTimer = new Timer()
15+
} = {}) {
16+
this._expiringNotificationTime = expiringNotificationTime;
17+
18+
this._expiring = expiringTimer;
19+
this._expired = expiredTimer;
20+
}
21+
22+
init(container) {
23+
this.cancel();
24+
25+
if (container && container.access_token) {
26+
var duration = container.expires_in;
27+
28+
this._expiring.init(duration - this._expiringNotificationTime);
29+
this._expired.init(duration + 1);
30+
}
31+
}
32+
33+
cancel() {
34+
this._expiring.cancel();
35+
this._expired.cancel();
36+
}
37+
38+
addExpiring(cb) {
39+
this._expiring.addHandler(cb);
40+
}
41+
removeExpiring(cb) {
42+
this._expiring.removeHandler(cb);
43+
}
44+
45+
addExpired(cb) {
46+
this._expired.addHandler(cb);
47+
}
48+
removeExpired(cb) {
49+
this._expired.removeHandler(cb);
50+
}
51+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
import AccessTokenEvents from '../../src/AccessTokenEvents';
5+
6+
import chai from 'chai';
7+
chai.should();
8+
let assert = chai.assert;
9+
10+
class StubTimer {
11+
12+
constructor() {
13+
this.cancelWasCalled = false;
14+
}
15+
16+
init(duration) {
17+
this.duration = duration;
18+
}
19+
20+
cancel() {
21+
this.cancelWasCalled = true;
22+
}
23+
24+
addHandler(){}
25+
removeHandler(){}
26+
}
27+
28+
describe("AccessTokenEvents", function () {
29+
30+
let subject;
31+
let expiringTimer;
32+
let expiredTimer;
33+
34+
beforeEach(function () {
35+
expiringTimer = new StubTimer();
36+
expiredTimer = new StubTimer();
37+
subject = new AccessTokenEvents({
38+
expiredTimer, expiringTimer
39+
});
40+
});
41+
42+
describe("constructor", function () {
43+
44+
it("should use default expiringNotificationTime", function () {
45+
subject._expiringNotificationTime.should.equal(60);
46+
});
47+
48+
});
49+
50+
describe("init", function () {
51+
52+
it("should cancel existing timers", function () {
53+
subject.init();
54+
55+
expiringTimer.cancelWasCalled.should.be.true;
56+
expiredTimer.cancelWasCalled.should.be.true;
57+
});
58+
59+
it("should initialize timers", function () {
60+
subject.init({
61+
access_token:"token",
62+
expires_in : 70
63+
});
64+
65+
expiringTimer.duration.should.equal(10);
66+
expiredTimer.duration.should.equal(71);
67+
});
68+
69+
});
70+
71+
describe("cancel", function () {
72+
73+
it("should cancel timers", function () {
74+
75+
subject.cancel();
76+
77+
expiringTimer.cancelWasCalled.should.be.true;
78+
expiredTimer.cancelWasCalled.should.be.true;
79+
});
80+
81+
});
82+
83+
});

0 commit comments

Comments
 (0)