-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add login and logout commands with apiKey handling #88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
| # Currently not working as not in production | ||
| REGISTRY_URL=https://api.nanoforge.dev | ||
This file contains hidden or 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 @@ | ||
| REGISTRY_URL=http://localhost:3000 |
This file contains hidden or 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 hidden or 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 hidden or 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,100 @@ | ||
| import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
| import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { runCli } from "./helpers/run-cli"; | ||
|
|
||
| const tmpDir = resolve(__dirname, "../.tmp-e2e-login"); | ||
| const RC_FILE = ".nanoforgerc"; | ||
| const FETCH_MOCK_PATH = resolve(__dirname, "./helpers/fetch-mock.mjs"); | ||
|
|
||
| function withMockedFetch(status = 200): NodeJS.ProcessEnv { | ||
| const existing = process.env.NODE_OPTIONS ?? ""; | ||
| return { | ||
| ...process.env, | ||
| NODE_OPTIONS: `${existing} --import ${FETCH_MOCK_PATH}`.trim(), | ||
| MOCK_REGISTRY_STATUS: String(status), | ||
| }; | ||
| } | ||
|
|
||
| function readLocalRc(dir: string): string { | ||
| const rcPath = resolve(dir, RC_FILE); | ||
| if (!existsSync(rcPath)) return ""; | ||
| return readFileSync(rcPath, "utf-8"); | ||
| } | ||
|
|
||
| beforeAll(() => { | ||
| mkdirSync(tmpDir, { recursive: true }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| rmSync(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("nf login --help", () => { | ||
| it("should display login command help", async () => { | ||
| const { stdout, exitCode } = await runCli(["login", "--help"]); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("login to Nanoforge registry"); | ||
| expect(stdout).toContain("--directory"); | ||
| expect(stdout).toContain("--local"); | ||
| expect(stdout).toContain("--api-key"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("nf login (local mode)", () => { | ||
| const dir = resolve(tmpDir, "login-local"); | ||
|
|
||
| beforeEach(() => { | ||
| mkdirSync(dir, { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("should write apiKey to local .nanoforgerc", async () => { | ||
| const fakeApiKey = "test-api-key-abc123"; | ||
| const { stdout, exitCode } = await runCli(["login", "--local", "-d", dir, "-k", fakeApiKey], { | ||
| env: withMockedFetch(), | ||
| }); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("NanoForge Login"); | ||
| expect(stdout).toContain("Login completed!"); | ||
|
|
||
| const rcContent = readLocalRc(dir); | ||
| expect(rcContent).toContain(fakeApiKey); | ||
| }); | ||
|
|
||
| it("should create the .nanoforgerc file in the specified directory", async () => { | ||
| await runCli(["login", "--local", "-d", dir, "-k", "test-api-key-create-file"], { | ||
| env: withMockedFetch(), | ||
| }); | ||
|
|
||
| expect(existsSync(resolve(dir, RC_FILE))).toBe(true); | ||
| }); | ||
|
|
||
| it("should overwrite apiKey on second login", async () => { | ||
| const firstKey = "first-api-key"; | ||
| const secondKey = "second-api-key"; | ||
|
|
||
| await runCli(["login", "--local", "-d", dir, "-k", firstKey], { env: withMockedFetch() }); | ||
| expect(readLocalRc(dir)).toContain(firstKey); | ||
|
|
||
| await runCli(["login", "--local", "-d", dir, "-k", secondKey], { env: withMockedFetch() }); | ||
| const rcContent = readLocalRc(dir); | ||
| expect(rcContent).toContain(secondKey); | ||
| expect(rcContent).not.toContain(firstKey); | ||
| }); | ||
|
|
||
| it("should fail when the registry rejects the api key", async () => { | ||
| const { exitCode } = await runCli(["login", "--local", "-d", dir, "-k", "invalid-key"], { | ||
| env: withMockedFetch(401), | ||
| }); | ||
|
|
||
| expect(exitCode).not.toBe(0); | ||
| expect(existsSync(resolve(dir, RC_FILE))).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or 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,101 @@ | ||
| import { existsSync, mkdirSync, readFileSync, rmSync } from "node:fs"; | ||
| import { resolve } from "node:path"; | ||
| import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it } from "vitest"; | ||
|
|
||
| import { runCli } from "./helpers/run-cli"; | ||
|
|
||
| const tmpDir = resolve(__dirname, "../.tmp-e2e-logout"); | ||
| const RC_FILE = ".nanoforgerc"; | ||
| const FETCH_MOCK_PATH = resolve(__dirname, "./helpers/fetch-mock.mjs"); | ||
|
|
||
| function withMockedFetch(): NodeJS.ProcessEnv { | ||
| const existing = process.env.NODE_OPTIONS ?? ""; | ||
| return { | ||
| ...process.env, | ||
| NODE_OPTIONS: `${existing} --import ${FETCH_MOCK_PATH}`.trim(), | ||
| }; | ||
| } | ||
|
|
||
| function readLocalRc(dir: string): string { | ||
| const rcPath = resolve(dir, RC_FILE); | ||
| if (!existsSync(rcPath)) return ""; | ||
| return readFileSync(rcPath, "utf-8"); | ||
| } | ||
|
|
||
| beforeAll(() => { | ||
| mkdirSync(tmpDir, { recursive: true }); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| rmSync(tmpDir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| describe("nf logout --help", () => { | ||
| it("should display logout command help", async () => { | ||
| const { stdout, exitCode } = await runCli(["logout", "--help"]); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("logout from Nanoforge registry"); | ||
| expect(stdout).toContain("--directory"); | ||
| expect(stdout).toContain("--local"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("nf logout (local mode)", () => { | ||
| const dir = resolve(tmpDir, "logout-local"); | ||
|
|
||
| beforeEach(() => { | ||
| mkdirSync(dir, { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("should clear apiKey from local .nanoforgerc after logout", async () => { | ||
| const fakeApiKey = "test-api-key-to-remove"; | ||
|
|
||
| await runCli(["login", "--local", "-d", dir, "-k", fakeApiKey], { env: withMockedFetch() }); | ||
| expect(readLocalRc(dir)).toContain(fakeApiKey); | ||
|
|
||
| const { stdout, exitCode } = await runCli(["logout", "--local", "-d", dir]); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| expect(stdout).toContain("NanoForge Logout"); | ||
| expect(stdout).toContain("Logout completed!"); | ||
|
|
||
| expect(readLocalRc(dir)).not.toContain(fakeApiKey); | ||
| }); | ||
|
|
||
| it("should succeed even when no prior login exists", async () => { | ||
| const { exitCode } = await runCli(["logout", "--local", "-d", dir]); | ||
|
|
||
| expect(exitCode).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe("nf login then logout (full flow, local mode)", () => { | ||
| const dir = resolve(tmpDir, "full-flow"); | ||
|
|
||
| beforeEach(() => { | ||
| mkdirSync(dir, { recursive: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| }); | ||
|
|
||
| it("should login and then logout successfully", async () => { | ||
| const fakeApiKey = "full-flow-api-key-xyz"; | ||
|
|
||
| const loginResult = await runCli(["login", "--local", "-d", dir, "-k", fakeApiKey], { | ||
| env: withMockedFetch(), | ||
| }); | ||
| expect(loginResult.exitCode).toBe(0); | ||
| expect(readLocalRc(dir)).toContain(fakeApiKey); | ||
|
|
||
| const logoutResult = await runCli(["logout", "--local", "-d", dir]); | ||
| expect(logoutResult.exitCode).toBe(0); | ||
| expect(readLocalRc(dir)).not.toContain(fakeApiKey); | ||
| }); | ||
| }); |
This file contains hidden or 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,18 @@ | ||
| /** | ||
| * Mocks globalThis.fetch for the CLI subprocess. | ||
| * Loaded via NODE_OPTIONS="--import /path/to/fetch-mock.mjs". | ||
| * | ||
| * Env vars: | ||
| * MOCK_REGISTRY_STATUS HTTP status to return (default: 200) | ||
| */ | ||
| const mockStatus = parseInt(process.env.MOCK_REGISTRY_STATUS ?? "200", 10); | ||
|
|
||
| globalThis.fetch = async (_url, _options) => { | ||
| const ok = mockStatus >= 200 && mockStatus < 300; | ||
| const body = ok ? JSON.stringify({ ok: true }) : JSON.stringify({ message: "Unauthorized" }); | ||
|
|
||
| return new Response(body, { | ||
| status: mockStatus, | ||
| headers: { "Content-Type": "application/json" }, | ||
| }); | ||
| }; |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.