-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the floating setTimeout in introspection client (#655)
* Fix the floating setTimeout * Add basic set of tests for ResponseCache
- Loading branch information
1 parent
33e7d41
commit 0a007b7
Showing
8 changed files
with
172 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
common/changes/@bentley/backend-itwin-client/fix-floating-setTimeout_2021-01-25-22-35.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@bentley/backend-itwin-client", | ||
"comment": "", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@bentley/backend-itwin-client", | ||
"email": "31107829+calebmshafer@users.noreply.github.com" | ||
} |
11 changes: 11 additions & 0 deletions
11
common/changes/@bentley/telemetry-client/fix-floating-setTimeout_2021-01-25-22-35.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"changes": [ | ||
{ | ||
"packageName": "@bentley/telemetry-client", | ||
"comment": "", | ||
"type": "none" | ||
} | ||
], | ||
"packageName": "@bentley/telemetry-client", | ||
"email": "31107829+calebmshafer@users.noreply.github.com" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Bentley Systems, Incorporated. All rights reserved. | ||
* See LICENSE.md in the project root for license terms and full copyright notice. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { assert } from "chai"; | ||
import * as sinon from "sinon"; | ||
import { MemoryIntrospectionResponseCache } from "../oidc/introspection/IntrospectionResponseCache"; | ||
import { IntrospectionResponse } from "../oidc/introspection/IntrospectionResponse"; | ||
|
||
describe("MemoryIntrospectionResponseCache", async () => { | ||
it("adds the token to the cache with a valid expiration", async () => { | ||
const testRes: IntrospectionResponse = { | ||
active: true, | ||
client_id: "test", // eslint-disable-line @typescript-eslint/naming-convention | ||
scope: "test", | ||
exp: Date.now() + 10000, // make the timeout long enough it won't be removed. | ||
}; | ||
|
||
const newCache = new MemoryIntrospectionResponseCache(); | ||
await newCache.add("test", testRes); | ||
|
||
const res = await newCache.get("test"); | ||
assert.isDefined(res); | ||
assert.equal(res?.client_id, "test"); | ||
}); | ||
|
||
it("does not add the token to the cache when the token is already expired", async () => { | ||
const testRes: IntrospectionResponse = { | ||
active: true, | ||
client_id: "test", // eslint-disable-line @typescript-eslint/naming-convention | ||
scope: "test", | ||
exp: new Date().getTime() / 1000 - 10000, // make the timeout prior to the time right now. | ||
}; | ||
|
||
const newCache = new MemoryIntrospectionResponseCache(); | ||
await newCache.add("test", testRes); | ||
|
||
const res = await newCache.get("test"); | ||
assert.isUndefined(res); | ||
}); | ||
|
||
it("does not add to the cache if missing an expiration in response object", async () => { | ||
const testRes: IntrospectionResponse = { | ||
active: true, | ||
client_id: "test", // eslint-disable-line @typescript-eslint/naming-convention | ||
scope: "test", | ||
}; | ||
|
||
const newCache = new MemoryIntrospectionResponseCache(); | ||
await newCache.add("test", testRes); | ||
|
||
const res = await newCache.get("test"); | ||
assert.isUndefined(res); | ||
}); | ||
|
||
it("adds the response to the cache and removes it after a timeout", async () => { | ||
const clock = sinon.useFakeTimers(); | ||
|
||
const testRes: IntrospectionResponse = { | ||
active: true, | ||
client_id: "test", // eslint-disable-line @typescript-eslint/naming-convention | ||
scope: "test", | ||
exp: (new Date().getTime() + 10) / 1000, | ||
}; | ||
|
||
const newCache = new MemoryIntrospectionResponseCache(); | ||
await newCache.add("test", testRes); | ||
|
||
let res = await newCache.get("test"); | ||
assert.isDefined(res); | ||
assert.equal(res?.client_id, "test"); | ||
|
||
// set clock to go past timeout | ||
clock.tick(100); | ||
|
||
// the key should be removed | ||
res = await newCache.get("test"); | ||
assert.isUndefined(res); | ||
|
||
clock.restore(); | ||
}); | ||
}); |