Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor commonHandler.js tests #187

Merged
merged 4 commits into from
Jul 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const config = {
setupFilesAfterEnv: ["<rootDir>/test/global.setup.jest.js"],
testMatch: [
"<rootDir>/test/*.unit.test.js",
"<rootDir>/test/handlers/**/**.js",
"<rootDir>/test/handlers/**/**.test.js",
],
},
{
Expand Down
148 changes: 0 additions & 148 deletions test/common_handler.unit.test.js

This file was deleted.

54 changes: 54 additions & 0 deletions test/handlers/common_handler/auth_fail.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const common = require("../../../src/handlers/common_handler.js");
const { Res, Req } = require("./express_fakes.js");

describe("Modifies res when invoking 'missingAuthJSON'", () => {

let EXPECTED_MESSAGE = "Requires authentication. Please update your token if you haven't done so recently.";
let EXPECTED_STATUS = 401;

test("via Bad Auth", async () => {
let res = new Res();
let req = new Req();

await common.authFail(req, res, { short: "Bad Auth" }, 0);

expect(res.statusCode).toBe(EXPECTED_STATUS);
expect(res.JSONObj.message).toBe(EXPECTED_MESSAGE);
});

test("via Auth Fail", async () => {
let res = new Res();
let req = new Req();

await common.authFail(req, res, { short: "Auth Fail" }, 0);

expect(res.statusCode).toBe(EXPECTED_STATUS);
expect(res.JSONObj.message).toBe(EXPECTED_MESSAGE);
});

test("via No Repo Access", async () => {
let res = new Res();
let req = new Req();

await common.authFail(req, res, { short: "No Repo Access" }, 0);

expect(res.statusCode).toBe(EXPECTED_STATUS);
expect(res.JSONObj.message).toBe(EXPECTED_MESSAGE);
});
});

describe("Modifies res when invoking serverError", () => {

let EXPECTED_STATUS = 500;
let EXPECTED_MESSAGE = "Application Error";

test("via DEFAULT", async () => {
let res = new Res();
let req = new Req();

await common.authFail(req, res, { short: "NOT_A_VALID_OPTION" }, 0);

expect(res.statusCode).toBe(EXPECTED_STATUS);
expect(res.JSONObj.message).toBe(EXPECTED_MESSAGE);
});
});
27 changes: 27 additions & 0 deletions test/handlers/common_handler/bad_package_json.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const common = require("../../../src/handlers/common_handler.js");
const { Res, Req } = require("./express_fakes.js");

const EXPECTED_MESSAGE = "The package.json at owner/repo isn't valid.";
const EXPECTED_STATUS = 400;

test("Modifies res when invoked directly", async () => {
let res = new Res();
let req = new Req();

await common.badPackageJSON(req, res);

expect(res.statusCode).toBe(EXPECTED_STATUS);
expect(res.JSONObj.message).toBe(EXPECTED_MESSAGE);
});

test("Modifies res when invoked indirectly", async () => {
let res = new Res();
let req = new Req();

await common.handleError(req, res, {
short: "Bad Package"
});

expect(res.statusCode).toBe(EXPECTED_STATUS);
expect(res.JSONObj.message).toBe(EXPECTED_MESSAGE);
});
27 changes: 27 additions & 0 deletions test/handlers/common_handler/bad_repo_json.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const common = require("../../../src/handlers/common_handler.js");
const { Res, Req } = require("./express_fakes.js");

const EXPECTED_MESSAGE = "That repo does not exist, isn't an atom package, or atombot does not have access.";
const EXPECTED_STATUS = 400;

test("Modifies res when invoked directly", async () => {
let res = new Res();
let req = new Req();

await common.badRepoJSON(req, res);

expect(res.statusCode).toBe(EXPECTED_STATUS);
expect(res.JSONObj.message).toBe(EXPECTED_MESSAGE);
});

test("Modifies res when invoked indirectly", async () => {
let res = new Res();
let req = new Req();

await common.handleError(req, res, {
short: "Bad Repo"
});

expect(res.statusCode).toBe(EXPECTED_STATUS);
expect(res.JSONObj.message).toBe(EXPECTED_MESSAGE);
});
29 changes: 29 additions & 0 deletions test/handlers/common_handler/express_fakes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// This is an impersonator of the ExpressJS Response Object.
// Who's goal is to very simply test the exact features we care about.
// Without overhead or bloat.
class Res {
constructor() {
this.statusCode = 0;
this.JSONObj = "";
}
json(obj) {
this.JSONObj = obj;
return this;
}
status(code) {
this.statusCode = code;
return this;
}
}

class Req {
constructor() {
this.ip = "0.0.0.0";
this.method = "TEST";
this.url = "/dev";
this.protocol = "DEV";
this.start = Date.now();
}
}

module.exports = { Res, Req };
Loading