Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 39 additions & 1 deletion src/plugins/login/utils/simpleFileTokenCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ describe("Simple File Token Cache", () => {
writeFileSpy.mockRestore();
});

it("Doesn't fail if unable to parse JSON from file", () => {
it("Doesn't fail adding subs if unable to parse JSON from file", () => {
mockFs({
"slsTokenCache.json": JSON.stringify(""),
});
Expand All @@ -101,4 +101,42 @@ describe("Simple File Token Cache", () => {
expect(writeFileSpy).toBeCalledWith(tokenFilePath, JSON.stringify(expected));
writeFileSpy.mockRestore();
});

it("Doesn't fail removing entries if unable to parse JSON from file", () => {
mockFs({
"slsTokenCache.json": JSON.stringify(""),
});

const writeFileSpy = jest.spyOn(fs, "writeFileSync");
const testFileCache = new SimpleFileTokenCache(tokenFilePath);
const testSubs = MockFactory.createTestSubscriptions();
const testEntries = MockFactory.createTestTokenCacheEntries();

testFileCache.addSubs(testSubs);
testFileCache.remove(testEntries)

const expected = {
entries: [],
subscriptions: testSubs,
};

expect(writeFileSpy).toBeCalledWith(tokenFilePath, JSON.stringify(expected));
writeFileSpy.mockRestore();
});

it("Doesn't fail find if unable to parse JSON from file", () => {
mockFs({
"slsTokenCache.json": JSON.stringify(""),
});

const testFileCache = new SimpleFileTokenCache(tokenFilePath);
const testSubs = MockFactory.createTestSubscriptions();

testFileCache.addSubs(testSubs);
const cb = jest.fn();
const result = testFileCache.find({key: "value"}, cb);

expect(cb).toBeCalledWith(null, result);
expect(result).toEqual([]);
});
});
20 changes: 14 additions & 6 deletions src/plugins/login/utils/simpleFileTokenCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,27 @@ export class SimpleFileTokenCache implements adal.TokenCache {
}

public remove(entries: any[], callback?: (err?: Error, result?: null) => void) {
this.entries = this.entries.filter(e => {
return !Object.keys(entries[0]).every(key => e[key] === entries[0][key]);
});
this.entries = (this.entries)
?
this.entries.filter(e => {
return !Object.keys(entries[0]).every(key => e[key] === entries[0][key]);
})
:
[];
this.save();
if (callback) {
callback();
}
}

public find(query: any, callback: (err?: Error, result?: any[]) => void) {
let result = this.entries.filter(e => {
return Object.keys(query).every(key => e[key] === query[key]);
});
let result = (this.entries)
?
this.entries.filter(e => {
return Object.keys(query).every(key => e[key] === query[key]);
})
:
[];
callback(null, result);
return result;
}
Expand Down