@qelos/auth
Version:
Express Passport authentication service
42 lines (41 loc) • 1.96 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const auth_check_1 = require("../auth-check");
const express_res_mock_1 = require("../../../mocks/express-res.mock");
describe('Auth Check middlewares', () => {
describe('onlyAuthenticated', () => {
describe('When get request without a valid user', () => {
const emptyRequest = {};
it('should response error to user', () => {
const resMock = (0, express_res_mock_1.getExpressResMock)();
const nextMock = jest.fn();
(0, auth_check_1.onlyAuthenticated)(emptyRequest, resMock, nextMock);
expect(resMock.status).toBeCalledWith(401);
expect(resMock.json).toBeCalledWith({
message: 'you are not authorized. must be logged in.',
});
expect(resMock.end).toBeCalled();
});
it('should not trigger next middleware', () => {
const resMock = (0, express_res_mock_1.getExpressResMock)();
const nextMock = jest.fn();
(0, auth_check_1.onlyAuthenticated)(emptyRequest, resMock, nextMock);
expect(nextMock).not.toBeCalled();
});
});
describe('When get request with a valid user', () => {
const reqWithUser = {
userPayload: {},
};
it('should continue to next middleware', () => {
const resMock = (0, express_res_mock_1.getExpressResMock)();
const nextMock = jest.fn();
(0, auth_check_1.onlyAuthenticated)(reqWithUser, resMock, nextMock);
expect(nextMock).toBeCalled();
expect(nextMock.mock.calls.length).toBe(1);
// next function should be called with not arguments
expect(nextMock.mock.calls[0][0]).toBeUndefined();
});
});
});
});