Skip to content

Commit

Permalink
worked on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
akdombrowski committed Nov 12, 2023
1 parent b7e8c31 commit b66a4ce
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 93 deletions.
22 changes: 20 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--timeout", "999999", "--colors", "${workspaceFolder}/test"],
"args": [
"--timeout",
"999999",
"--colors",
"${workspaceFolder}/test",
"-R",
"spec",
"--config",
"${workspaceFolder}/.mocharc.yaml"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": ["<node_internals>/**/*.js"]
Expand All @@ -26,7 +35,16 @@
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--timeout", "999999", "--colors", "${file}"],
"args": [
"--timeout",
"999999",
"--colors",
"${file}",
"-R",
"spec",
"--config",
"${workspaceFolder}/.mocharc.yaml"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"skipFiles": ["<node_internals>/**/*.js"]
Expand Down
4 changes: 2 additions & 2 deletions cli/index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

96 changes: 45 additions & 51 deletions cli_lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,96 +63,90 @@ var cli = /*#__PURE__*/function () {
return _context2.abrupt("return", HELP_TEXT);
case 6:
if (!(arg2 === "-c" || arg2 === "--clipboard" || !arg2)) {
_context2.next = 32;
_context2.next = 34;
break;
}
if (!clipboard) {
_context2.next = 27;
_context2.next = 29;
break;
}
_context2.prev = 8;
_context2.next = 11;
return decode(clipboard);
case 11:
decoded = _context2.sent;
// Show what we found in the clipboard
console.log("Decoding: ");
console.log(clipboard);
_context2.next = 13;
return decode(clipboard);
case 13:
decoded = _context2.sent;
// Show the decoded jwt.
console.log(decoded);
return _context2.abrupt("return", decoded);
case 18:
_context2.prev = 18;
_context2.t0 = _context2["catch"](8);
console.error("I found an error :(");
console.error("Couldn't decode what was in clipboard. Pass in a JWT as the first argument or copy a JWT to your clipboard");
console.error("what's on your clipboard? ");
console.error(clipboard);
throw _context2.t0;
case 25:
_context2.next = 30;
break;
console.error("Couldn't decode what was in your clipboard. You might try copying the JWT again.");
console.error("What's the problem? ");
console.error(" ", _context2.t0.message);
console.error("What's on your clipboard? ");
console.error(" ", clipboard);
return _context2.abrupt("return", GENERIC_ERROR_CODE);
case 27:
_context2.next = 32;
break;
case 29:
console.error("I found an error :(");
console.error("Nothing in your clipboard. Pass in a JWT as the first argument or copy a JWT to your clipboard");
console.error("Didn't find anything in your clipboard.");
return _context2.abrupt("return", GENERIC_ERROR_CODE);
case 30:
_context2.next = 68;
break;
case 32:
_context2.next = 52;
break;
case 34:
if (!(arg2 === "-b" || arg2 === "--base64url")) {
_context2.next = 50;
_context2.next = 51;
break;
}
if (arg3) {
_context2.next = 37;
_context2.next = 39;
break;
}
console.error("I found an error :(");
console.error("No argument passed in to encode.");
return _context2.abrupt("return", GENERIC_ERROR_CODE);
case 37:
_context2.prev = 37;
case 39:
_context2.prev = 39;
bases64urlified = (0, _lib.base64URLEncode)(arg3);
console.log(bases64urlified);
return _context2.abrupt("return", bases64urlified);
case 43:
_context2.prev = 43;
_context2.t1 = _context2["catch"](37);
case 45:
_context2.prev = 45;
_context2.t1 = _context2["catch"](39);
console.error("I found an error :(");
console.error("base64url encoding failed:", _context2.t1.message);
throw _context2.t1;
case 48:
_context2.next = 68;
case 49:
_context2.next = 52;
break;
case 50:
if (!arg2) {
_context2.next = 65;
break;
case 51:
if (arg2) {
try {
_decoded = (0, _lib.jwtDecode)(arg2);
console.log("Decoding: \n" + arg2);
console.log(_decoded);
// return decoded;
} catch (e) {
console.error("I found an error :(.");
// console.error(e, e.message);
console.error(e.message);
}
} else {
console.error("I found an error :(.");
console.error("Nothing in clipboard and no arguments given. Pass in a JWT as the first argument or copy a JWT to your clipboard");
}
_context2.prev = 51;
_decoded = (0, _lib.jwtDecode)(arg2);
console.log("Decoding: \n" + arg2);
console.log(_decoded);
return _context2.abrupt("return", _decoded);
case 58:
_context2.prev = 58;
_context2.t2 = _context2["catch"](51);
console.error("I found an error :(.");
console.error(_context2.t2, _context2.t2.message);
throw _context2.t2;
case 63:
_context2.next = 68;
break;
case 65:
console.error("I found an error :(.");
console.error("Nothing in clipboard and no arguments given. Pass in a JWT as the first argument or copy a JWT to your clipboard");
throw new Error("Nothing in clipboard and no arguments given. Pass in a JWT as the first argument or copy a JWT to your clipboard");
case 68:
case 52:
case "end":
return _context2.stop();
}
}, _callee2, null, [[8, 18], [37, 43], [51, 58]]);
}, _callee2, null, [[8, 18], [39, 45]]);
}));
return function cli(_x2, _x3) {
return _ref2.apply(this, arguments);
Expand Down
4 changes: 3 additions & 1 deletion test/base64URLEncode.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { base64URLEncode } from "../src/index.js";
import crypto from "crypto";
import { expect } from "./index.js";
import chai from "chai";

const expect = chai.expect;

describe("#base64URLEncode()", function () {
context("when string literal is used", function () {
Expand Down
4 changes: 3 additions & 1 deletion test/bothJWTEncodeAndDecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import {
hs256Sign,
createHeaderPayload,
} from "../src/index.js";
import { expect } from "./index.js";
import chai from "chai";

const expect = chai.expect;

describe("#jwtEncode() and #jwtDecode()", function () {
describe("JWT encoding then decoding", function () {
Expand Down
57 changes: 31 additions & 26 deletions test/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,50 @@ import { base64URLEncode } from "../src/index.js";
import crypto from "crypto";
import cli, { HELP_TEXT } from "../cli/index.js";
import sinon from "sinon";
import { expect } from "./index.js";
import chai from "chai";

const expect = chai.expect;

describe("#cli()", function () {
let sandbox;
let log;
let err;
before(function () {
sandbox = sinon.createSandbox();
log = sandbox.spy(console, "log");
err = sandbox.spy(console, "error");
});

beforeEach(function () {
sandbox.restore();
log.resetHistory();
err.resetHistory();
});

describe("using help", function () {
context("when argument is -h", function () {
it("shows the help screen", function () {
const log = sandbox.spy(console, "log");
// Mock process.argv
const processArgv = [0, 0, "-h"];
cli(null, [0, 0, "-h"]);
expect(log.calledOnceWith(HELP_TEXT)).to.be.true;
expect(log.calledWith(HELP_TEXT)).to.be.true;
});
});

context("when argument is --help", function () {
it("shows the help screen", function () {
const log = sandbox.spy(console, "log");
// Mock process.argv
const processArgv = [0, 0, "-h"];
cli(null, [0, 0, "-h"]);
expect(log.calledOnceWith(HELP_TEXT)).to.be.true;
const processArgv = [0, 0, "--help"];
cli(null, [0, 0, "--help"]);
expect(log.calledWith(HELP_TEXT)).to.be.true;
});
});
});

describe("mock using clipboard", function () {
context("when 'clipboard' contains a jwt", function () {
it("decodes the jwt", async function () {
const log = sandbox.spy(console, "log");
const myCli = cli;
const spy = sandbox.spy(myCli);
const cliSpy = sandbox.spy(myCli);

const clipboard =
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c";
Expand All @@ -49,22 +54,27 @@ describe("#cli()", function () {
payload: { sub: "1234567890", name: "John Doe", iat: 1516239022 },
signature: "SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c",
};
const cliOutput = await spy(clipboard, null);
const cliOutput = await cliSpy(clipboard, null);

expect(
spy.calledOnceWith(
cliSpy.calledWith(
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
)
),
"Check that the encoded jwt matches with what was called"
).to.be.true;
expect(log.calledWith("Decoding: ")).to.be.true;
expect(
log.calledWith(
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
)
log.calledWith("Decoding: "),
"Decoding starting message logged to console"
).to.be.true;
// expect(
// log.calledWith(
// "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
// )
// ).to.be.true;

expect(
log.getCall(2).firstArg,
"decoded jwt was logged to the console"
"wanted this to match___" + log.getCall(2).firstArg + "___"
).to.be.deep.equal(expectedOutput);
expect(expectedOutput).to.be.deep.equal(cliOutput);
});
Expand All @@ -73,20 +83,17 @@ describe("#cli()", function () {
context("when 'clipboard' doesn't contain a jwt", function () {
context("when it's not an actual JWT", function () {
it("throws a SyntaxError", async function () {
const err = sandbox.spy(console, "error");
const clipboard = "abc.abc.abc";
let syntaxErr;

await cli(clipboard, null);
expect(err.called).to.be.true;

expect(err.called).to.be.true;
expect(err.calledWith("I found an error :(")).to.be.true;
});
});

context("when it doesn't contain a '.'", function () {
it("logs an error: \"Need at least one '.'\"", async function () {
const err = sandbox.spy(console, "error");
const spy = sandbox.spy(cli);
const clipboard = "abc";

Expand All @@ -102,7 +109,6 @@ describe("#cli()", function () {
describe("#base64urlEncode()", function () {
context("when first given argument is -b", function () {
it("properly base64url encodes the input", async function () {
const log = sandbox.spy(console, "log");
const spy = sandbox.spy(cli);

const b64u =
Expand All @@ -126,7 +132,7 @@ describe("#cli()", function () {
// Only called cli function once
expect(spy.calledOnce, "cli function should be called once").to.be.true;
expect(
spy.calledOnceWith(null, processArgv),
spy.calledWith(null, processArgv),
"cli should be called with -b and a string: " + processArgv
).to.be.true;

Expand All @@ -144,7 +150,7 @@ describe("#cli()", function () {

// Cli outputs to console
expect(
log.calledOnceWith(cliOut),
log.calledWith(cliOut),
"the encoded string was output to the console"
).to.be.true;

Expand All @@ -161,7 +167,6 @@ describe("#cli()", function () {

context("when first given argument is --base64url", function () {
it("properly base64url encodes the input", async function () {
const log = sandbox.spy(console, "log");
const spy = sandbox.spy(cli);

const b64u =
Expand Down
11 changes: 3 additions & 8 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import chai from "chai";
import {
jwtDecode,
} from "../src/index.js";
import { spawn } from "child_process";
import { jwtDecode } from "../src/index.js";

export const expect = chai.expect;
chai.config.includeStack = true;
const expect = chai.expect;
// chai.config.includeStack = true;

describe("JWT decoding", function () {
describe("#jwtDecode()", function () {
Expand Down Expand Up @@ -111,5 +108,3 @@ describe("JWT decoding", function () {
});
});
});


4 changes: 3 additions & 1 deletion test/jwtDecode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import {
hs256Verify,
} from "../src/index.js";
import crypto from "crypto";
import { expect } from "./index.js";
import chai from "chai";

const expect = chai.expect;

describe("JWT encoding", function () {
describe("#jwtEncode()", function () {
Expand Down
Loading

0 comments on commit b66a4ce

Please sign in to comment.