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
79 changes: 61 additions & 18 deletions src/plugins/login/utils/simpleFileTokenCache.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import fs from "fs";
import path from "path";
import os from "os";
import mockFs from "mock-fs";
import { MockFactory } from "../../../test/mockFactory";
import { SimpleFileTokenCache } from "./simpleFileTokenCache";
Expand All @@ -8,7 +10,7 @@ describe("Simple File Token Cache", () => {

let fileContent = {
entries: [],
subscriptions: [],
subscriptions: []
};

afterEach(() => {
Expand All @@ -23,18 +25,47 @@ describe("Simple File Token Cache", () => {

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

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

it("Create .azure default directory if it doesn't exist", () => {
const expected = path.join(os.homedir(), ".azure");

mockFs();

const writeFileSpy = jest.spyOn(fs, "writeFileSync");
writeFileSpy.mockImplementation(() => undefined);

const mkdirSpy = jest.spyOn(fs, "mkdirSync");
mkdirSpy.mockImplementation(() => undefined);

const existsSpy = jest.spyOn(fs, "existsSync");
existsSpy.mockImplementation(() => false);

const readFileSpy = jest.spyOn(fs, "readFileSync");
readFileSpy.mockImplementation(() => "{\"entries\": []}");

new SimpleFileTokenCache();
expect(mkdirSpy).toBeCalledWith(expected);

mkdirSpy.mockRestore();
existsSpy.mockRestore();
readFileSpy.mockRestore();
writeFileSpy.mockRestore();
});

it("Load file on creation if available", () => {
fileContent.entries = MockFactory.createTestTokenCacheEntries();

mockFs({
"slsTokenCache.json": JSON.stringify(fileContent),
"slsTokenCache.json": JSON.stringify(fileContent)
});

const readFileSpy = jest.spyOn(fs, "readFileSync");
Expand All @@ -43,7 +74,7 @@ describe("Simple File Token Cache", () => {
expect(readFileSpy).toBeCalled();
expect(tokenCache.first()).not.toBeNull();
readFileSpy.mockRestore();
})
});

it("Saves to file after token is added", () => {
mockFs();
Expand All @@ -56,11 +87,14 @@ describe("Simple File Token Cache", () => {

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

expect(tokenCache.isEmpty()).toBe(false);
expect(writeFileSpy).toBeCalledWith(tokenFilePath, JSON.stringify(expected));
expect(writeFileSpy).toBeCalledWith(
tokenFilePath,
JSON.stringify(expected)
);
writeFileSpy.mockRestore();
});

Expand All @@ -75,16 +109,19 @@ describe("Simple File Token Cache", () => {

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

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

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

const writeFileSpy = jest.spyOn(fs, "writeFileSync");
Expand All @@ -95,16 +132,19 @@ describe("Simple File Token Cache", () => {

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

expect(writeFileSpy).toBeCalledWith(tokenFilePath, JSON.stringify(expected));
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(""),
"slsTokenCache.json": JSON.stringify("")
});

const writeFileSpy = jest.spyOn(fs, "writeFileSync");
Expand All @@ -113,28 +153,31 @@ describe("Simple File Token Cache", () => {
const testEntries = MockFactory.createTestTokenCacheEntries();

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

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

expect(writeFileSpy).toBeCalledWith(tokenFilePath, JSON.stringify(expected));
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(""),
"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);
const result = testFileCache.find({ key: "value" }, cb);

expect(cb).toBeCalledWith(null, result);
expect(result).toEqual([]);
Expand Down
7 changes: 6 additions & 1 deletion src/plugins/login/utils/simpleFileTokenCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import path from "path";
import os from "os";
import * as adal from "adal-node";

const CONFIG_DIRECTORY = path.join(os.homedir(), ".azure");
let CONFIG_DIRECTORY = path.join(os.homedir(), ".azure");
const DEFAULT_SLS_TOKEN_FILE = path.join(CONFIG_DIRECTORY, "slsTokenCache.json");

export class SimpleFileTokenCache implements adal.TokenCache {
private entries: any[] = [];
private subscriptions: any[] = [];

public constructor(private tokenPath: string = DEFAULT_SLS_TOKEN_FILE) {
if(tokenPath === DEFAULT_SLS_TOKEN_FILE && !fs.existsSync(CONFIG_DIRECTORY)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: spacing - Run auto-format

CONFIG_DIRECTORY = path.join(os.homedir(), ".azure");
this.tokenPath = CONFIG_DIRECTORY;
fs.mkdirSync(CONFIG_DIRECTORY);
}
this.load();
}

Expand Down
1 change: 0 additions & 1 deletion src/services/azureKeyVaultService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ describe("Azure Key Vault Service", () => {
}) as any;

FunctionAppService.prototype.get = jest.fn(() => {
console.log("testing");
return {
identity: {
tenantId: "tid",
Expand Down