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

NTRNL-450 - Add auth middleware #21

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
28 changes: 28 additions & 0 deletions src/middleware/authentication.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* eslint-disable indent */
/* eslint-disable @typescript-eslint/quotes */
mel-am marked this conversation as resolved.
Show resolved Hide resolved
import { NextFunction, Request, Response } from "express";
import { log } from "../utils/logger";
import { isFeatureEnabled } from "../utils/isFeatureEnabled";
import * as config from "../config";

export const authentication = (
req: Request,
res: Response,
next: NextFunction
) => {
try {
// if auth is not enabled, render the not available page
if (!isFeatureEnabled(config.FEATURE_FLAG_ENABLE_AUTH)) {
log.infoRequest(req, "sorry, auth service not available right now");
return res.render(config.NOT_AVAILABLE);
}

// If auth enabled
log.infoRequest(req, "some auth here soon!");

next();
} catch (err: any) {
log.errorRequest(req, err);
next(err);
}
};
94 changes: 94 additions & 0 deletions test/unit/middleware/authentication.middleware.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* eslint-disable indent */
mel-am marked this conversation as resolved.
Show resolved Hide resolved
/* eslint-disable @typescript-eslint/quotes */
jest.mock("../../../src/utils/isFeatureEnabled");
jest.mock("../../../src/utils/logger", () => ({
log: {
infoRequest: jest.fn(),
errorRequest: jest.fn(),
},
}));

import { describe, expect, test, jest, afterEach } from "@jest/globals";
import { Request, Response, NextFunction } from "express";

import * as config from "../../../src/config";

import { authentication } from "../../../src/middleware/authentication.middleware";
import { log } from "../../../src/utils/logger";
import { isFeatureEnabled } from "../../../src/utils/isFeatureEnabled";

import { GET_REQUEST_MOCK } from "../../mock/data";

const logInfoRequestMock = log.infoRequest as jest.Mock;
const logErrorRequestMock = log.errorRequest as jest.Mock;
const isFeatureEnabledMock = isFeatureEnabled as jest.Mock;

const req = GET_REQUEST_MOCK as Request;
const mockResponse = () => {
const res = {} as Response;
res.render = jest.fn() as any;
return res;
};
const next = jest.fn() as NextFunction;

describe("Authentication Middleware test suites", () => {
let res: any;

beforeEach(() => {
res = mockResponse();
});

afterEach(() => {
jest.resetAllMocks();
});

test("Should call next if auth feature is enabled", () => {
// enable auth flag
isFeatureEnabledMock.mockReturnValue(true);

authentication(req, res, next);

expect(logInfoRequestMock).toHaveBeenCalledTimes(1);
expect(logInfoRequestMock).toHaveBeenCalledWith(
req,
"some auth here soon!"
);
expect(next).toHaveBeenCalledTimes(1);
});

test("should call res.render with not-available view if auth feature is disabled", () => {
// disable auth flag
isFeatureEnabledMock.mockReturnValue(false);

authentication(req, res, next);

expect(logInfoRequestMock).toHaveBeenCalledTimes(1);
expect(logInfoRequestMock).toHaveBeenCalledWith(
req,
"sorry, auth service not available right now"
);
expect(res.render).toHaveBeenCalledTimes(1);
expect(res.render).toHaveBeenCalledWith(config.NOT_AVAILABLE);
});

test("should call next with error object if error is thrown", () => {
// disable auth flag so res.render is called
isFeatureEnabledMock.mockReturnValue(false);

// force an error to be thrown by res.render
const errMsg = "Error thrown by res.render!";
const resWithError = { ...res };
resWithError.render = jest.fn(() => {
throw new Error(errMsg);
});

authentication(req, resWithError, next);

const errObj = expect.objectContaining({ message: errMsg });

expect(logErrorRequestMock).toHaveBeenCalledTimes(1);
expect(logErrorRequestMock).toHaveBeenCalledWith(req, errObj);
expect(next).toHaveBeenCalledTimes(1);
expect(next).toHaveBeenCalledWith(errObj);
});
});