Skip to content

Commit

Permalink
comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mwilde345 committed Nov 20, 2024
1 parent 6c0487e commit 51e7b07
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 3 additions & 2 deletions src/config/setup-test-container.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand Down
1 change: 0 additions & 1 deletion src/lib/fauna-account-client.mjs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
35 changes: 21 additions & 14 deletions test/authNZ.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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");
Expand All @@ -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;
});

Expand All @@ -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",
});
});

Expand Down Expand Up @@ -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",
});
});

Expand All @@ -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");
});
});
});

0 comments on commit 51e7b07

Please sign in to comment.