-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add service credential file handling and tests for CredentialsS…
…torage
- Loading branch information
1 parent
b0dae1b
commit 346f241
Showing
2 changed files
with
63 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { fs, type DirectoryJSON, vol } from "memfs"; | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
||
// Mock all fs promises functions | ||
vi.mock("node:fs/promises", () => fs.promises); | ||
|
||
// Use a mock homedir in order to reliably have a memfs home directory | ||
vi.mock("node:os", () => ({ homedir: () => "/home/codemod-test/" })); | ||
|
||
import { | ||
CredentialsStorage, | ||
CredentialsStorageType, | ||
} from "../src/credentials-storage.js"; | ||
|
||
describe("CredentialsStorage", () => { | ||
const testService = "codemod.com-test"; | ||
const testAccount = CredentialsStorageType.ACCOUNT; | ||
const testPassword = "test-password"; | ||
|
||
describe("when .codemod directory does not exist", () => { | ||
beforeEach(() => { | ||
vol.reset(); | ||
}); | ||
|
||
it("should return null without throwing an error", async () => { | ||
const storage = new CredentialsStorage(); | ||
const result = await storage.get(testAccount); | ||
|
||
expect(result).toBeNull(); | ||
}); | ||
}); | ||
|
||
describe("when .codemod directory exists", () => { | ||
beforeEach(() => { | ||
vol.reset(); | ||
const directory: DirectoryJSON = { | ||
[`${testService}:${testAccount}`]: testPassword, | ||
}; | ||
vol.fromJSON(directory, "/home/codemod-test/.codemod"); | ||
}); | ||
|
||
it("should retrieve existing credentials", async () => { | ||
const storage = new CredentialsStorage(); | ||
const result = await storage.get(testAccount); | ||
|
||
expect(result).toBe(testPassword); | ||
}); | ||
}); | ||
}); |