-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tsci auth set-token $TOKEN, tsci auth print-token (#39)
* feat: tsci auth set-token $TOKEN, tsci auth print-token * tests: print-token set-token * chore: comments * chore: format
- Loading branch information
Showing
4 changed files
with
84 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import type { Command } from "commander" | ||
import { cliConfig } from "lib/cli-config" | ||
|
||
export const registerAuthPrintToken = (program: Command) => { | ||
program.commands | ||
.find((c) => c.name() === "auth")! | ||
.command("print-token") | ||
.description("Prints your auth token") | ||
.action(() => { | ||
const token = cliConfig.get("sessionToken") | ||
if (!token) return console.log("You need to log in to access this.") | ||
console.log("Your Token:\n", token) | ||
}) | ||
} |
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,25 @@ | ||
import type { Command } from "commander" | ||
import { cliConfig } from "lib/cli-config" | ||
|
||
function validateJWTLength(token: string) { | ||
const parts = token.split(".") | ||
|
||
if (parts.length === 3 && parts.every((part) => part.length > 0)) { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
export const registerAuthSetToken = (program: Command) => { | ||
program.commands | ||
.find((c) => c.name() === "auth")! | ||
.command("set-token") | ||
.description("Explicitly set your auth token") | ||
.argument("<token>", "New token to manually configure") | ||
.action((token) => { | ||
if (!validateJWTLength(token)) | ||
return console.log("Invalid token provided") | ||
cliConfig.set("sessionToken", token) | ||
console.log("Token manually updated.") | ||
}) | ||
} |
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,41 @@ | ||
import { getCliTestFixture } from "../../fixtures/get-cli-test-fixture" | ||
import { test, expect } from "bun:test" | ||
|
||
const demoJwtToken = | ||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c" | ||
|
||
test("print and set token explicitly", async () => { | ||
const { runCommand } = await getCliTestFixture() | ||
|
||
// Test printing token without authentication | ||
const { stdout: invalidTokenPrintStdout, stderr: invalidTokenPrintStderr } = | ||
await runCommand("tsci auth print-token") | ||
expect(invalidTokenPrintStdout).toContain( | ||
"You need to log in to access this.", | ||
) | ||
expect(invalidTokenPrintStderr).toBeFalsy() | ||
|
||
// Test setting an invalid token | ||
const { stdout: invalidTokenSetStdout, stderr: invalidTokenSetStderr } = | ||
await runCommand("tsci auth set-token invalid-token-example") | ||
expect(invalidTokenSetStdout).toContain("Invalid token provided") | ||
expect(invalidTokenSetStderr).toBeFalsy() | ||
|
||
// Test setting a valid token | ||
const { stdout: validTokenSetStdout, stderr: validTokenSetStderr } = | ||
await runCommand(`tsci auth set-token ${demoJwtToken}`) | ||
expect(validTokenSetStdout).toContain("Token manually updated.") | ||
expect(validTokenSetStderr).toBeFalsy() | ||
|
||
// Test printing the valid token | ||
const { stdout: validTokenPrintStdout, stderr: validTokenPrintStderr } = | ||
await runCommand("tsci auth print-token") | ||
expect(validTokenPrintStdout).toContain(demoJwtToken) | ||
expect(validTokenPrintStderr).toBeFalsy() | ||
|
||
// Verify token is correctly stored and can be reused | ||
const { stdout: reprintTokenStdout } = await runCommand( | ||
"tsci auth print-token", | ||
) | ||
expect(reprintTokenStdout).toContain(demoJwtToken) | ||
}) |