refactor: rename email-server-worker to email-worker
Rename directory from email-server-worker to email-worker for clarity and brevity. Update all references in CLAUDE.md documentation.
This commit is contained in:
29
email-worker/tests/__mocks__/prom-client.ts
Normal file
29
email-worker/tests/__mocks__/prom-client.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { LabelValues } from "prom-client";
|
||||
|
||||
export class Counter {
|
||||
public inc() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
export class Histogram<T extends string> {
|
||||
startTimer(labels?: LabelValues<T>): (labels?: LabelValues<T>) => void {
|
||||
return((labels?: LabelValues<T>) => { });
|
||||
}
|
||||
}
|
||||
|
||||
class Register {
|
||||
public setDefaultLabels(labels: Object) {
|
||||
|
||||
}
|
||||
|
||||
public metrics(): Promise<string> {
|
||||
return(Promise.resolve(""));
|
||||
}
|
||||
|
||||
public get contentType() {
|
||||
return("");
|
||||
}
|
||||
}
|
||||
|
||||
export const register = new Register();
|
||||
33
email-worker/tests/helpers/mockHttpContext.ts
Normal file
33
email-worker/tests/helpers/mockHttpContext.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { Request, Response, NextFunction } from 'express';
|
||||
import { NgitLocals } from "../../src/types/NgitLocals";
|
||||
|
||||
interface IMockHttpContext {
|
||||
reqPath?:string
|
||||
headersSent?:boolean
|
||||
writableEnded?:boolean
|
||||
method?:string
|
||||
}
|
||||
|
||||
export const mockHttpContext = ({reqPath="/", headersSent=false, writableEnded=false, method="GET"}:IMockHttpContext|undefined = {}) => {
|
||||
const req = {
|
||||
path:reqPath,
|
||||
method,
|
||||
url:`https://localhost${reqPath}`,
|
||||
params: {},
|
||||
} as unknown as Request;
|
||||
|
||||
const res = {
|
||||
end: jest.fn(),
|
||||
status: jest.fn(),
|
||||
setHeader: jest.fn(),
|
||||
locals: {
|
||||
stopPrometheusTimer: jest.fn(),
|
||||
} as unknown as NgitLocals,
|
||||
headersSent,
|
||||
writableEnded,
|
||||
} as unknown as Response;
|
||||
|
||||
const next:NextFunction = jest.fn();
|
||||
|
||||
return({req,res,next})
|
||||
}
|
||||
118
email-worker/tests/routers/errorRouter.spec.ts
Normal file
118
email-worker/tests/routers/errorRouter.spec.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
|
||||
import { errorRouter } from '../../src/routes/errorRouter';
|
||||
import createError from "http-errors";
|
||||
import { mockHttpContext } from "../helpers/mockHttpContext";
|
||||
|
||||
describe("errorRouter", () => {
|
||||
|
||||
beforeEach(() => {
|
||||
mockWrite.mockClear();
|
||||
});
|
||||
|
||||
test("u slučaju greške 404 mora vratiti string poruku 'page not found'", async () => {
|
||||
const err = createError(404)
|
||||
const {req,res,next} = mockHttpContext();
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(404);
|
||||
expect(res.setHeader).toHaveBeenCalledWith('Content-Type', "text/html");
|
||||
expect(res.end).toHaveBeenCalledWith("page not found");
|
||||
});
|
||||
|
||||
test("u slučaju greške 404 mora logirati request, response i tekst greške", async () => {
|
||||
const err = createError(404)
|
||||
const reqPath = "/neki-path/";
|
||||
const {req,res,next} = mockHttpContext({ reqPath });
|
||||
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.locals.logger.info).toHaveBeenCalledWith("response", "page not found");
|
||||
expect(res.locals.logger.error).toHaveBeenCalledWith(err.name, "page "+req.path+" not found");
|
||||
});
|
||||
|
||||
test("ako su header-i već poslani, ne smiju biti poslani još jednom", async () => {
|
||||
const err = createError(404)
|
||||
const {req,res,next} = mockHttpContext({ headersSent:true, writableEnded:true });
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.status).not.toHaveBeenCalled();
|
||||
expect(res.setHeader).not.toHaveBeenCalled();
|
||||
expect(res.end).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("ako NIJE već pozvana [end] metoda, treba je pozvati", async () => {
|
||||
const err = createError(404)
|
||||
const {req,res,next} = mockHttpContext({ headersSent:true, writableEnded:false });
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.status).not.toHaveBeenCalled();
|
||||
expect(res.setHeader).not.toHaveBeenCalled();
|
||||
expect(res.end).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("mora zaustaviti Prometheus Timer", async () => {
|
||||
const err = createError(404)
|
||||
const {req,res,next} = mockHttpContext({ headersSent:true, writableEnded:false });
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.locals.stopPrometheusTimer).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("u slučaju greške 500 mora vratiti string poruku 'internal server error'", async () => {
|
||||
const err = createError(500)
|
||||
const {req,res,next} = mockHttpContext();
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(500);
|
||||
expect(res.setHeader).toHaveBeenCalledWith('Content-Type', "text/html");
|
||||
expect(res.end).toHaveBeenCalledWith("internal server error");
|
||||
});
|
||||
|
||||
test("u slučaju greške 400 mora vratiti string poruku 'bad request' i logirati grešku", async () => {
|
||||
const errorMessage = "mock error text 1";
|
||||
const err = createError(400, errorMessage);
|
||||
const {req,res,next} = mockHttpContext();
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(400);
|
||||
expect(res.setHeader).toHaveBeenCalledWith('Content-Type', "text/html");
|
||||
expect(res.end).toHaveBeenCalledWith("bad request");
|
||||
|
||||
expect(res.locals.logger.errorwrite).toHaveBeenCalledWith(err.name, errorMessage);
|
||||
});
|
||||
|
||||
test("u slučaju greške 401 mora vratiti string poruku 'unauthorized' i logirati grešku", async () => {
|
||||
const errorMessage = "mock error text 2";
|
||||
const err = createError(401, errorMessage)
|
||||
const {req,res,next} = mockHttpContext();
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(401);
|
||||
expect(res.setHeader).toHaveBeenCalledWith('Content-Type', "text/html");
|
||||
expect(res.end).toHaveBeenCalledWith("unauthorized");
|
||||
|
||||
expect(res.locals.logger.error).toHaveBeenCalledWith(err.name, errorMessage);
|
||||
});
|
||||
|
||||
test("u slučaju greške 403 mora vratiti string poruku 'forbidden' i logirati grešku", async () => {
|
||||
const errorMessage = "mock error text 3";
|
||||
const err = createError(403, errorMessage);
|
||||
const {req,res,next} = mockHttpContext();
|
||||
|
||||
await errorRouter(err, req, res, next);
|
||||
|
||||
expect(res.status).toHaveBeenCalledWith(403);
|
||||
expect(res.setHeader).toHaveBeenCalledWith('Content-Type', "text/html");
|
||||
expect(res.end).toHaveBeenCalledWith("forbidden");
|
||||
|
||||
expect(res.locals.logger.error).toHaveBeenCalledWith(err.name, errorMessage);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user