Skip to content

Commit 0033b44

Browse files
author
Noelle Daley
committed
cleanup: pull setExpiration settings into own method & add tests
1 parent 9d63357 commit 0033b44

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

ui/app/services/auth.js

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -218,20 +218,19 @@ export default Service.extend({
218218
},
219219

220220
calculateExpiration(resp, now) {
221-
let tokenExpirationEpoch;
222221
const ttl = resp.ttl || resp.lease_duration;
222+
const tokenExpirationEpoch = resp.expire_time ? new Date(resp.expire_time).getTime() : now + ttl * 1e3;
223223

224-
if (resp.expire_time) {
225-
const expireTime = resp.expire_time;
226-
tokenExpirationEpoch = new Date(expireTime).getTime();
224+
return { ttl, tokenExpirationEpoch };
225+
},
226+
227+
setExpirationSettings(resp, now) {
228+
if (resp.renewable) {
229+
this.set('expirationCalcTS', now);
230+
this.set('allowExpiration', false);
227231
} else {
228-
tokenExpirationEpoch = now + ttl * 1e3;
232+
this.set('allowExpiration', true);
229233
}
230-
231-
return {
232-
ttl,
233-
tokenExpirationEpoch,
234-
};
235234
},
236235

237236
calculateRootNamespace(currentNamespace, namespace_path, backend) {
@@ -305,17 +304,12 @@ export default Service.extend({
305304
);
306305

307306
const now = this.now();
308-
Object.assign(data, this.calculateExpiration(resp, now));
309307

310-
if (resp.renewable) {
311-
this.set('expirationCalcTS', now);
312-
this.set('allowExpiration', false);
313-
} else {
314-
this.set('allowExpiration', true);
315-
}
308+
Object.assign(data, this.calculateExpiration(resp, now));
309+
this.setExpirationSettings(resp, now);
316310

317-
// don't allow token expiration in testing
318-
// this ensures we don't try to call renew-self within tests
311+
// ensure we don't call renew-self within tests
312+
// this is intentionally not included in setExpirationSettings so we can unit test that method
319313
if (Ember.testing) this.set('allowExpiration', false);
320314

321315
if (!data.displayName) {

ui/tests/integration/services/auth-test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,6 @@ module('Integration | Service | auth', function (hooks) {
452452
undefined,
453453
'expiration is calculated for batch tokens'
454454
);
455-
assert.true(this.service.allowExpiration, 'allowExpiration is true for batch tokens');
456455
});
457456

458457
test('batch tokens generated by auth methods', async function (assert) {
@@ -468,7 +467,6 @@ module('Integration | Service | auth', function (hooks) {
468467
undefined,
469468
'expiration is calculated for batch tokens'
470469
);
471-
assert.true(this.service.allowExpiration, 'allowExpiration is true for batch tokens');
472470
});
473471
});
474472

ui/tests/unit/services/auth-test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,28 @@ module('Unit | Service | auth', function (hooks) {
5050
);
5151
});
5252
});
53+
54+
module('#setExpirationSettings', function () {
55+
test('#setExpirationSettings for a renewable token', function (assert) {
56+
const now = Date.now();
57+
const ttl = 30;
58+
const response = { ttl, renewable: true };
59+
60+
this.service.setExpirationSettings(response, now);
61+
62+
assert.false(this.service.allowExpiration, 'sets allowExpiration to false');
63+
assert.strictEqual(this.service.expirationCalcTS, now, 'sets expirationCalcTS to now');
64+
});
65+
66+
test('#setExpirationSettings for a non-renewable token', function (assert) {
67+
const now = Date.now();
68+
const ttl = 30;
69+
const response = { ttl, renewable: false };
70+
71+
this.service.setExpirationSettings(response, now);
72+
73+
assert.true(this.service.allowExpiration, 'sets allowExpiration to true');
74+
assert.strictEqual(this.service.expirationCalcTS, null, 'keeps expirationCalcTS as null');
75+
});
76+
});
5377
});

0 commit comments

Comments
 (0)