Skip to content

Commit

Permalink
fix: Check for valid entries in token cache remove and find (#244)
Browse files Browse the repository at this point in the history
Fails if user previously started interactive login process without finishing because it writes an invalid JSON file. This checks to make sure the parsed JSON is valid and that entries exists
  • Loading branch information
tbarlow12 authored Aug 16, 2019
1 parent 1b35a5d commit 660f8b7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
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

0 comments on commit 660f8b7

Please sign in to comment.