-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy pathtoken-cache.test.ts
69 lines (60 loc) · 2.66 KB
/
token-cache.test.ts
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
import { InstallationTokenCache } from "./installation-token-cache";
import { AuthToken, TEN_MINUTES } from "./auth-token";
import { AppTokenHolder } from "./app-token-holder";
import { getInstallationId } from "./installation-id";
import { keyLocator } from "./key-locator";
import { mocked } from "jest-mock";
import { Subscription } from "~/src/models/subscription";
import { envVars } from "config/env";
import fs from "fs";
import path from "path";
jest.mock("./key-locator");
jest.mock("~/src/config/feature-flags");
describe("InstallationTokenCache & AppTokenHolder", () => {
const githubInstallationId = 123456;
const date = new Date(2021, 10, 25, 10, 0);
const in10Minutes = new Date(date.getTime() + TEN_MINUTES);
beforeEach(() => {
jest.useFakeTimers("modern");
});
afterEach(() => {
jest.useRealTimers();
});
it("should not cache any tokens when testing InstallationTokenCache", async () => {
const installationTokenCache = new InstallationTokenCache();
const initialInstallationToken = new AuthToken("initial installation token", in10Minutes);
const generateInitialInstallationToken = jest.fn().mockImplementation(() => Promise.resolve(initialInstallationToken));
jest.setSystemTime(date);
const token1 = await installationTokenCache.getInstallationToken(githubInstallationId, undefined, generateInitialInstallationToken);
const token2 = await installationTokenCache.getInstallationToken(githubInstallationId, undefined, generateInitialInstallationToken);
expect(token1).toEqual(initialInstallationToken);
expect(token2).toEqual(initialInstallationToken);
expect(generateInitialInstallationToken).toHaveBeenCalledTimes(2);
});
it("should not cache any tokens when testing AppTokenHolder", async () => {
mocked(keyLocator).mockImplementation(async () => {
return fs.readFileSync(path.resolve(process.cwd(), envVars.PRIVATE_KEY_PATH)).toString();
});
await Subscription.install({
host: "http://github.com",
installationId: 1234,
hashedClientKey: "client-key",
gitHubAppId: undefined
});
await Subscription.install({
host: "http://github.com",
installationId: 4711,
hashedClientKey: "client-key",
gitHubAppId: undefined
});
const appTokenHolder = new AppTokenHolder();
jest.setSystemTime(new Date(2021, 10, 25, 10, 0));
const token1 = await appTokenHolder.getAppToken(getInstallationId(1234), jiraHost);
expect(token1).toBeTruthy();
const token2 = await appTokenHolder.getAppToken(getInstallationId(4711), jiraHost);
expect(token2).toBeTruthy();
const token3 = await appTokenHolder.getAppToken(getInstallationId(4711), jiraHost, 1);
expect(token3).toBeTruthy();
expect(keyLocator).toHaveBeenCalledTimes(3);
});
});