Skip to content

Commit

Permalink
getAllAccounts always writes to cache (#7378)
Browse files Browse the repository at this point in the history
Changes flag in getAllAccounts to prevent always writing to the cache
  • Loading branch information
shylasummers authored Nov 7, 2024
1 parent 056024e commit 411d815
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Fixes bug where getAllAccounts always writes to the cache",
"packageName": "@azure/msal-node",
"email": "[email protected]",
"dependentChangeType": "patch"
}
2 changes: 1 addition & 1 deletion lib/msal-node/src/cache/TokenCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class TokenCache implements ISerializableTokenCache, ITokenCache {
let cacheContext;
try {
if (this.persistence) {
cacheContext = new TokenCacheContext(this, true);
cacheContext = new TokenCacheContext(this, false);
await this.persistence.beforeCacheAccess(cacheContext);
}
return this.storage.getAllAccounts();
Expand Down
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 411d815

Please sign in to comment.