Skip to content

Commit

Permalink
Update tests to validate behavior of a scope and --secret
Browse files Browse the repository at this point in the history
  • Loading branch information
macmv committed Oct 11, 2023
1 parent 3ba3de3 commit 930cea0
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/lib/fauna-command.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ class FaunaCommand extends Command {
);
}

connectionOptions.secret.databaseScope.push(path[i]);
connectionOptions.secret.appendScope(path[i]);
}

return this.getClient({
Expand Down
41 changes: 16 additions & 25 deletions test/commands/eval.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { expect, test } = require("@oclif/test");
const { withOpts, getEndpoint, matchFqlReq, withLegacyOpts } = require("../helpers/utils.js");
const { withOpts, getEndpoint, evalV10, matchFqlReq, withLegacyOpts } = require("../helpers/utils.js");
const { query: q } = require("faunadb");

describe("eval", () => {
Expand Down Expand Up @@ -37,29 +37,7 @@ describe("eval", () => {
expect(JSON.parse(ctx.stdout).data[0].targetDb).to.equal("root");
});

test
.nock(getEndpoint(), { allowUnmocked: true }, (api) => {
api
.post("/", matchFqlReq(q.Exists(q.Database("nested"))))
.reply(200, { resource: true });
mockQuery(api);
})
.stdout()
.command(
withOpts([
"eval",
"--version",
"4",
"--format",
"json",
"nested",
"Paginate(Collections())",
])
)
.it("runs eval on nested db", (ctx) => {
expect(JSON.parse(ctx.stdout).data[0].targetDb).to.equal("nested");
});

// FIXME: Need tests for nested DB, which can't be used with `--secret`.
test
.stderr()
.command(withOpts(["eval", "--version", "4", '[Add(1, 2), Abort("boom")]']))
Expand Down Expand Up @@ -115,6 +93,19 @@ describe("eval in v10", () => {
.it("runs eval in json tagged format", (ctx) => {
expect(JSON.parse(ctx.stdout)).to.deep.equal({ two: { "@int": "2" } });
});

test
.do(async () => {
// This can fail if `MyDB` already exists, but thats fine.
await evalV10("Database.create({ name: 'MyDB' })");
})
.stdout()
// --secret is passed by withOpts, so passing a scope should be disallowed.
.command(withOpts(["eval", "MyDB", "{ two: 3 }", "--format", "json-tagged"]))
.catch((e) => {
expect(e.message).to.equal("Cannot append scope to a secret from --secret");
})
.it("disallows setting --secret and scope");
});

function mockQuery(api) {
Expand All @@ -123,7 +114,7 @@ function mockQuery(api) {
.post("/", matchFqlReq(q.Now()))
.reply(200, { resource: new Date() })
.post("/", matchFqlReq(q.Paginate(q.Collections())))
.reply(200, function () {
.reply(200, function() {
const auth = this.req.headers.authorization[0].split(":");
return {
resource: {
Expand Down
19 changes: 18 additions & 1 deletion test/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fetch from "node-fetch";
const url = require("url");
const { query: q } = require("faunadb");
const env = process.env;
Expand Down Expand Up @@ -32,13 +33,29 @@ module.exports.withOpts = (cmd) => {
return cmd.concat(opts);
};

module.exports.getEndpoint = () =>
const getEndpoint = () =>
url.format({
protocol: env.FAUNA_SCHEME,
hostname: env.FAUNA_DOMAIN,
port: env.FAUNA_PORT,
});

module.exports.getEndpoint = getEndpoint;

module.exports.evalV10 = (query) => {
const endpoint = getEndpoint();
const secret = env.FAUNA_SECRET;
return fetch(new URL("/query/1", endpoint), {
method: "POST",
headers: {
Authorization: `Bearer ${secret}`,
},
body: JSON.stringify({
query,
}),
});
}

const fqlToJsonString = (fql) => JSON.stringify(q.wrap(fql));
module.exports.fqlToJsonString = fqlToJsonString;

Expand Down

0 comments on commit 930cea0

Please sign in to comment.