feat: implement comprehensive notification engine with policies, preferences, and automated dispatching services
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
import { Request, Response } from "express";
|
||||
import { systemTokenMiddleware } from "../systemToken";
|
||||
|
||||
describe("systemTokenMiddleware", () => {
|
||||
const originalSystemKey = process.env.SYSTEM_KEY;
|
||||
const originalApiKey = process.env.API_KEY;
|
||||
|
||||
afterEach(() => {
|
||||
process.env.SYSTEM_KEY = originalSystemKey;
|
||||
process.env.API_KEY = originalApiKey;
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
function createResponse(): Response {
|
||||
return {
|
||||
status: jest.fn().mockReturnThis(),
|
||||
json: jest.fn().mockReturnThis(),
|
||||
} as unknown as Response;
|
||||
}
|
||||
|
||||
it("allows requests with a valid system token", async () => {
|
||||
process.env.SYSTEM_KEY = "system-token";
|
||||
const request = { body: { systemToken: "system-token" } } as Request;
|
||||
const response = createResponse();
|
||||
const next = jest.fn();
|
||||
|
||||
await systemTokenMiddleware(request, response, next);
|
||||
|
||||
expect(next).toHaveBeenCalledTimes(1);
|
||||
expect(response.status).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects requests without a valid system token", async () => {
|
||||
process.env.SYSTEM_KEY = "system-token";
|
||||
const request = { body: { systemToken: "wrong-token" } } as Request;
|
||||
const response = createResponse();
|
||||
const next = jest.fn();
|
||||
|
||||
await systemTokenMiddleware(request, response, next);
|
||||
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(response.status).toHaveBeenCalledWith(401);
|
||||
expect(response.json).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ desc: "Token de sistema inválido" })
|
||||
);
|
||||
});
|
||||
|
||||
it("rejects requests when no server-side system token is configured", async () => {
|
||||
delete process.env.SYSTEM_KEY;
|
||||
delete process.env.API_KEY;
|
||||
const request = { body: { systemToken: "system-token" } } as Request;
|
||||
const response = createResponse();
|
||||
const next = jest.fn();
|
||||
|
||||
await systemTokenMiddleware(request, response, next);
|
||||
|
||||
expect(next).not.toHaveBeenCalled();
|
||||
expect(response.status).toHaveBeenCalledWith(401);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,19 @@
|
||||
import { NextFunction, Request, Response } from "express";
|
||||
import { ApiValidationError } from "../Models/Server.Error.model";
|
||||
|
||||
export async function systemTokenMiddleware(
|
||||
request: Request,
|
||||
response: Response,
|
||||
next: NextFunction
|
||||
): Promise<void | Response | ApiValidationError> {
|
||||
const expectedSystemToken = process.env.SYSTEM_KEY || process.env.API_KEY;
|
||||
const systemToken = request.body?.systemToken;
|
||||
|
||||
if (!expectedSystemToken || typeof systemToken !== "string" || systemToken !== expectedSystemToken) {
|
||||
return response
|
||||
.status(401)
|
||||
.json(new ApiValidationError(401, "Token de sistema inválido"));
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
Reference in New Issue
Block a user