near-protocol-rewards
Version:
A transparent, metric-based rewards system for NEAR projects
52 lines (51 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const auth_1 = require("../../../src/middleware/auth");
const errors_1 = require("../../../src/types/errors");
describe("validateApiKey middleware", () => {
let req;
let res;
let next;
beforeEach(() => {
req = {
headers: {},
};
res = {
status: jest.fn().mockReturnThis(),
json: jest.fn(),
};
next = jest.fn();
});
it("should return 401 if API key is missing", () => {
(0, auth_1.validateApiKey)(req, res, next);
expect(res.status).toHaveBeenCalledWith(401);
expect(res.json).toHaveBeenCalledWith({
success: false,
error: {
code: errors_1.ErrorCode.UNAUTHORIZED,
message: "API key is required",
},
});
expect(next).not.toHaveBeenCalled();
});
it("should call next if API key is present", () => {
req.headers["x-api-key"] = "test-api-key";
(0, auth_1.validateApiKey)(req, res, next);
expect(next).toHaveBeenCalled();
expect(res.status).not.toHaveBeenCalled();
expect(res.json).not.toHaveBeenCalled();
});
it("should return 401 if API key is empty", () => {
req.headers["x-api-key"] = "";
(0, auth_1.validateApiKey)(req, res, next);
expect(res.status).toHaveBeenCalledWith(401);
expect(res.json).toHaveBeenCalledWith({
success: false,
error: {
code: errors_1.ErrorCode.UNAUTHORIZED,
message: "API key is required",
},
});
expect(next).not.toHaveBeenCalled();
});
});