diff --git a/package.json b/package.json index 8569715a..bab51cc4 100644 --- a/package.json +++ b/package.json @@ -66,7 +66,7 @@ "pretest": "npm run format && npm run lint", "lint": "eslint . --fix", "test": "mocha --recursive ./test --require ./test/mocha-root-hooks.mjs --reporter spec --reporter mocha-junit-reporter", - "test:local": "mocha --recursive ./test/authNZ.mjs --require ./test/mocha-root-hooks.mjs", + "test:local": "mocha --recursive ./test --require ./test/mocha-root-hooks.mjs", "build": "npm run build:app && npm run build:sea", "build:app": "esbuild --bundle ./src/user-entrypoint.mjs --platform=node --outfile=./dist/cli.cjs --format=cjs --inject:./sea/import-meta-url.js --define:import.meta.url=importMetaUrl", "build:sea": "node ./sea/build.cjs", diff --git a/src/config/setup-test-container.mjs b/src/config/setup-test-container.mjs index 289d3053..1bb8d247 100644 --- a/src/config/setup-test-container.mjs +++ b/src/config/setup-test-container.mjs @@ -9,6 +9,7 @@ import { f, InMemoryWritableStream } from "../../test/helpers.mjs"; import { parseYargs } from "../cli.mjs"; import { makeAccountRequest } from "../lib/account.mjs"; import { makeFaunaRequest } from "../lib/db.mjs"; +import { AccountKey, SecretKey } from "../lib/file-util.mjs"; import buildLogger from "../lib/logger.mjs"; import { injectables, setupCommonContainer } from "./setup-container.mjs"; @@ -62,8 +63,8 @@ export function setupTestContainer() { ), accountClient: awilix.asFunction(stub()), oauthClient: awilix.asFunction(stub()), - accountCreds: awilix.asFunction(stub()), - secretCreds: awilix.asFunction(stub()), + accountCreds: awilix.asClass(AccountKey).scoped(), + secretCreds: awilix.asClass(SecretKey).scoped(), // in tests, let's exit by throwing errorHandler: awilix.asValue((error, exitCode) => { error.code = exitCode; diff --git a/src/lib/fauna-account-client.mjs b/src/lib/fauna-account-client.mjs index ab1be0f0..ba2b1ab5 100644 --- a/src/lib/fauna-account-client.mjs +++ b/src/lib/fauna-account-client.mjs @@ -1,7 +1,6 @@ //@ts-check import { container } from "../cli.mjs"; -import { InvalidCredsError, UnauthorizedError } from "./misc.mjs"; /** * Class representing a client for interacting with the Fauna account API. diff --git a/test/authNZ.mjs b/test/authNZ.mjs index 55456a97..6ee946f9 100644 --- a/test/authNZ.mjs +++ b/test/authNZ.mjs @@ -6,7 +6,6 @@ import sinon, { stub } from "sinon"; import { run } from "../src/cli.mjs"; import { setupTestContainer as setupContainer } from "../src/config/setup-test-container.mjs"; import { authNZMiddleware, setAccountKey } from "../src/lib/auth/authNZ.mjs"; -import { AccountKey, SecretKey } from "../src/lib/file-util.mjs"; import { InvalidCredsError } from "../src/lib/misc.mjs"; import { f } from "./helpers.mjs"; @@ -33,8 +32,6 @@ describe("authNZMiddleware", function () { container = setupContainer(); container.register({ accountClient: awilix.asFunction(mockAccountClient).scoped(), - accountCreds: awilix.asClass(AccountKey).scoped(), - secretCreds: awilix.asClass(SecretKey).scoped(), }); fetch = container.resolve("fetch"); logger = container.resolve("logger"); @@ -53,14 +50,19 @@ describe("authNZMiddleware", function () { await run("db list", scope); const exit = scope.resolve("exit"); const accountCreds = scope.resolve("accountCreds"); + const stdout = container.resolve("stdoutStream"); + const stderr = container.resolve("stderrStream"); accountCreds.get = stub().throws(new InvalidCredsError()); await authNZMiddleware(argv); - expect(logger.stderr.args[0][0]).to.include("not signed in or has expired"); - expect(logger.stdout.args[0][0]).to.include( + expect(stdout.getWritten()).to.contain( "To sign in, run:\n\nfauna login --profile test-profile\n", ); + expect(stderr.getWritten()).to.contain( + 'The requested profile "test-profile" is not signed in or has expired.\nPlease re-authenticate', + ); + expect(exit.calledOnce).to.be.true; }); @@ -84,9 +86,12 @@ describe("authNZMiddleware", function () { await authNZMiddleware(argv); expect(accountClient.refreshSession.calledOnce).to.be.true; expect(accountCreds.save.calledOnce).to.be.true; - expect(accountCreds.save.args[0][0].creds).to.deep.equal({ - account_key: "new-account-key", - refresh_token: "new-refresh-token", + expect(accountCreds.save).to.have.been.calledWith({ + creds: { + account_key: "new-account-key", + refresh_token: "new-refresh-token", + }, + key: "test-profile", }); }); @@ -136,11 +141,13 @@ describe("authNZMiddleware", function () { await authNZMiddleware(argv); // Check that setDBKey was called and secrets were saved expect(secretCreds.save.called).to.be.true; - expect(secretCreds.save.args[0][0].key).to.equal("valid-account-key"); - expect(secretCreds.save.args[0][0].creds).to.deep.equal({ - path: "test-db", - role: "admin", - secret: "new-db-key", + expect(secretCreds.save).to.have.been.calledWith({ + creds: { + path: "test-db", + role: "admin", + secret: "new-db-key", + }, + key: "valid-account-key", }); }); @@ -157,7 +164,7 @@ describe("authNZMiddleware", function () { // Verify the cleanup secrets logic expect(secretCreds.delete.calledOnce).to.be.true; - expect(secretCreds.delete.args[0][0]).to.equal("old-account-key"); + expect(secretCreds.delete).to.have.been.calledWith("old-account-key"); }); }); });