Skip to content

Commit

Permalink
feat: tsci auth set-token $TOKEN, tsci auth print-token (#39)
Browse files Browse the repository at this point in the history
* feat: tsci auth set-token $TOKEN, tsci auth print-token

* tests: print-token set-token

* chore: comments

* chore: format
  • Loading branch information
ArnavK-09 authored Jan 25, 2025
1 parent 1ba511d commit f0f7cfa
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cli/auth/print-token/register.ts
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)
})
}
25 changes: 25 additions & 0 deletions cli/auth/set-token/register.ts
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.")
})
}
4 changes: 4 additions & 0 deletions cli/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { perfectCli } from "perfect-cli"
import pkg from "../package.json"
import semver from "semver"
import { registerExport } from "./export/register"
import { registerAuthPrintToken } from "./auth/print-token/register"
import { registerAuthSetToken } from "./auth/set-token/register"

const program = new Command()

Expand All @@ -30,6 +32,8 @@ registerClone(program)
registerAuth(program)
registerAuthLogin(program)
registerAuthLogout(program)
registerAuthPrintToken(program)
registerAuthSetToken(program)

registerConfig(program)
registerConfigPrint(program)
Expand Down
41 changes: 41 additions & 0 deletions tests/cli/token/token.test.ts
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)
})

0 comments on commit f0f7cfa

Please sign in to comment.