Update tests
This commit is contained in:
1 parent
1fc2bd49c8
commit
084315c377
2 files changed
+23
-27
No files matched your search
@@ -148,4 +148,11 @@ describe("controllerUtils - fetchMonitorCertificate", () => {
|
|||||||
const result = await fetchMonitorCertificate(sslChecker, monitor);
|
const result = await fetchMonitorCertificate(sslChecker, monitor);
|
||||||
expect(result).to.deep.equal({ validTo: "2022-01-01" });
|
expect(result).to.deep.equal({ validTo: "2022-01-01" });
|
||||||
});
|
});
|
||||||
|
it("should throw an error if a ssl-checker returns null", async () => {
|
||||||
|
sslChecker.returns(null);
|
||||||
|
await fetchMonitorCertificate(sslChecker, monitor).catch((error) => {
|
||||||
|
expect(error).to.be.an("error");
|
||||||
|
expect(error.message).to.equal("Certificate not found");
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -169,19 +169,6 @@ describe("Monitor Controller - getMonitorCertificate", () => {
|
|||||||
})
|
})
|
||||||
).to.be.true;
|
).to.be.true;
|
||||||
});
|
});
|
||||||
it("should return success message and data if all operations succeed with an invalid cert", async () => {
|
|
||||||
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
|
||||||
fetchMonitorCertificate.returns({});
|
|
||||||
await getMonitorCertificate(req, res, next, fetchMonitorCertificate);
|
|
||||||
expect(res.status.firstCall.args[0]).to.equal(200);
|
|
||||||
expect(
|
|
||||||
res.json.calledOnceWith({
|
|
||||||
success: true,
|
|
||||||
msg: successMessages.MONITOR_CERTIFICATE,
|
|
||||||
data: { certificateDate: "N/A" },
|
|
||||||
})
|
|
||||||
).to.be.true;
|
|
||||||
});
|
|
||||||
it("should return an error if fetchMonitorCertificate fails", async () => {
|
it("should return an error if fetchMonitorCertificate fails", async () => {
|
||||||
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
req.db.getMonitorById.returns({ url: "https://www.google.com" });
|
||||||
fetchMonitorCertificate.throws(new Error("Certificate error"));
|
fetchMonitorCertificate.throws(new Error("Certificate error"));
|
||||||
@@ -462,38 +449,40 @@ describe("Monitor Controller - createMonitor", () => {
|
|||||||
describe("Monitor Controllor - checkEndpointResolution", () => {
|
describe("Monitor Controllor - checkEndpointResolution", () => {
|
||||||
let req, res, next, dnsResolveStub;
|
let req, res, next, dnsResolveStub;
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
req = { query: { monitorURL: 'https://example.com' } };
|
req = { query: { monitorURL: "https://example.com" } };
|
||||||
res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
|
res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
|
||||||
next = sinon.stub();
|
next = sinon.stub();
|
||||||
dnsResolveStub = sinon.stub(dns, 'resolve');
|
dnsResolveStub = sinon.stub(dns, "resolve");
|
||||||
});
|
});
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
dnsResolveStub.restore();
|
dnsResolveStub.restore();
|
||||||
});
|
});
|
||||||
it('should resolve the URL successfully', async () => {
|
it("should resolve the URL successfully", async () => {
|
||||||
dnsResolveStub.callsFake((hostname, callback) => callback(null));
|
dnsResolveStub.callsFake((hostname, callback) => callback(null));
|
||||||
await checkEndpointResolution(req, res, next);
|
await checkEndpointResolution(req, res, next);
|
||||||
expect(res.status.calledWith(200)).to.be.true;
|
expect(res.status.calledWith(200)).to.be.true;
|
||||||
expect(res.json.calledWith({
|
expect(
|
||||||
success: true,
|
res.json.calledWith({
|
||||||
msg: 'URL resolved successfully',
|
success: true,
|
||||||
})).to.be.true;
|
msg: "URL resolved successfully",
|
||||||
|
})
|
||||||
|
).to.be.true;
|
||||||
expect(next.called).to.be.false;
|
expect(next.called).to.be.false;
|
||||||
});
|
});
|
||||||
it("should return an error if DNS resolution fails", async () => {
|
it("should return an error if DNS resolution fails", async () => {
|
||||||
const dnsError = new Error("DNS resolution failed");
|
const dnsError = new Error("DNS resolution failed");
|
||||||
dnsError.code = 'ENOTFOUND';
|
dnsError.code = "ENOTFOUND";
|
||||||
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
|
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
|
||||||
await checkEndpointResolution(req, res, next);
|
await checkEndpointResolution(req, res, next);
|
||||||
expect(next.calledOnce).to.be.true;
|
expect(next.calledOnce).to.be.true;
|
||||||
const errorPassedToNext = next.getCall(0).args[0];
|
const errorPassedToNext = next.getCall(0).args[0];
|
||||||
expect(errorPassedToNext).to.be.an.instanceOf(Error);
|
expect(errorPassedToNext).to.be.an.instanceOf(Error);
|
||||||
expect(errorPassedToNext.message).to.include('DNS resolution failed');
|
expect(errorPassedToNext.message).to.include("DNS resolution failed");
|
||||||
expect(errorPassedToNext.code).to.equal('ENOTFOUND');
|
expect(errorPassedToNext.code).to.equal("ENOTFOUND");
|
||||||
expect(errorPassedToNext.status).to.equal(500);
|
expect(errorPassedToNext.status).to.equal(500);
|
||||||
});
|
});
|
||||||
it('should reject with an error if query validation fails', async () => {
|
it("should reject with an error if query validation fails", async () => {
|
||||||
req.query.monitorURL = 'invalid-url';
|
req.query.monitorURL = "invalid-url";
|
||||||
await checkEndpointResolution(req, res, next);
|
await checkEndpointResolution(req, res, next);
|
||||||
expect(next.calledOnce).to.be.true;
|
expect(next.calledOnce).to.be.true;
|
||||||
const error = next.getCall(0).args[0];
|
const error = next.getCall(0).args[0];
|
||||||
|
|||||||
Reference in new issue
Block a user