Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
shylasummers committed Nov 6, 2024
1 parent 703f9a4 commit ebf3444
Showing 1 changed file with 55 additions and 4 deletions.
59 changes: 55 additions & 4 deletions lib/msal-node/test/cache/TokenCache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
} from "@azure/msal-common";
import { NodeStorage } from "../../src/cache/NodeStorage";
import { TokenCache } from "../../src/cache/TokenCache";
import { promises as fs } from "fs";
import { existsSync, watch, promises, FSWatcher } from "fs";
import { version, name } from "../../package.json";
import {
DEFAULT_CRYPTO_IMPLEMENTATION,
Expand All @@ -24,6 +24,7 @@ const msalCommon: MSALCommonModule = jest.requireActual(
describe("TokenCache tests", () => {
let logger: Logger;
let storage: NodeStorage;
let watcher: FSWatcher;

beforeEach(() => {
const loggerOptions = {
Expand All @@ -50,6 +51,12 @@ describe("TokenCache tests", () => {
jest.restoreAllMocks();
});

afterEach(() => {
if (watcher) {
watcher.close();
}
});

it("Constructor tests builds default token cache", async () => {
const tokenCache = new TokenCache(storage, logger);
expect(tokenCache).toBeInstanceOf(TokenCache);
Expand Down Expand Up @@ -115,15 +122,15 @@ describe("TokenCache tests", () => {
it("TokenCache beforeCacheAccess and afterCacheAccess", async () => {
const beforeCacheAccess = async (context: TokenCacheContext) => {
context.tokenCache.deserialize(
await fs.readFile(
await promises.readFile(
"./test/cache/cache-test-files/cache-unrecognized-entities.json",
"utf-8"
)
);
};
const cachePath = "./test/cache/cache-test-files/temp-cache.json";
const afterCacheAccess = async (context: TokenCacheContext) => {
await fs.writeFile(cachePath, context.tokenCache.serialize());
await promises.writeFile(cachePath, context.tokenCache.serialize());
};

const cachePlugin: ICachePlugin = {
Expand Down Expand Up @@ -153,7 +160,7 @@ describe("TokenCache tests", () => {

// try and clean up
try {
await fs.unlink(cachePath);
await promises.unlink(cachePath);
} catch (err) {
const errnoException = err as NodeJS.ErrnoException;
if (errnoException.code == "ENOENT") {
Expand All @@ -164,6 +171,50 @@ describe("TokenCache tests", () => {
}
});

it("getAllAccounts doesn't write to cache", async () => {
const cachePath =
"./test/cache/cache-test-files/cache-unrecognized-entities.json";
if (existsSync(cachePath)) {
watcher = watch(cachePath, (eventType: string) => {
if (eventType === "change") {
throw new Error("test cache changed");
}
});
} else {
throw new Error("error in watching test cache");
}

const beforeCacheAccess = jest.fn(
async (context: TokenCacheContext) => {
if (context.hasChanged == true) {
throw new Error("hasChanged should be false");
}
return promises.readFile(cachePath, "utf-8").then((data) => {
context.tokenCache.deserialize(data);
});
}
);

const afterCacheAccess = jest.fn(async (context: TokenCacheContext) => {
if (context.hasChanged == true) {
throw new Error("hasChanged should be false");
}
return Promise.resolve();
});

const cachePlugin: ICachePlugin = {
beforeCacheAccess,
afterCacheAccess,
};

const tokenCache = new TokenCache(storage, logger, cachePlugin);

const accounts = await tokenCache.getAllAccounts();
expect(accounts.length).toBe(1);
expect(beforeCacheAccess).toHaveBeenCalled();
expect(afterCacheAccess).toHaveBeenCalled();
});

it("should return an empty KV store if TokenCache is empty", () => {
const tokenCache = new TokenCache(storage, logger);

Expand Down

0 comments on commit ebf3444

Please sign in to comment.