Update tests

This commit is contained in:
Alex Holliday committed 2024-10-24 11:37:24 +08:00
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);
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;
});
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 () => {
req.db.getMonitorById.returns({ url: "https://www.google.com" });
fetchMonitorCertificate.throws(new Error("Certificate error"));
@@ -462,38 +449,40 @@ describe("Monitor Controller - createMonitor", () => {
describe("Monitor Controllor - checkEndpointResolution", () => {
let req, res, next, dnsResolveStub;
beforeEach(() => {
req = { query: { monitorURL: 'https://example.com' } };
req = { query: { monitorURL: "https://example.com" } };
res = { status: sinon.stub().returnsThis(), json: sinon.stub() };
next = sinon.stub();
dnsResolveStub = sinon.stub(dns, 'resolve');
dnsResolveStub = sinon.stub(dns, "resolve");
});
afterEach(() => {
dnsResolveStub.restore();
});
it('should resolve the URL successfully', async () => {
it("should resolve the URL successfully", async () => {
dnsResolveStub.callsFake((hostname, callback) => callback(null));
await checkEndpointResolution(req, res, next);
expect(res.status.calledWith(200)).to.be.true;
expect(res.json.calledWith({
success: true,
msg: 'URL resolved successfully',
})).to.be.true;
expect(
res.json.calledWith({
success: true,
msg: "URL resolved successfully",
})
).to.be.true;
expect(next.called).to.be.false;
});
it("should return an error if DNS resolution fails", async () => {
const dnsError = new Error("DNS resolution failed");
dnsError.code = 'ENOTFOUND';
dnsError.code = "ENOTFOUND";
dnsResolveStub.callsFake((hostname, callback) => callback(dnsError));
await checkEndpointResolution(req, res, next);
expect(next.calledOnce).to.be.true;
const errorPassedToNext = next.getCall(0).args[0];
expect(errorPassedToNext).to.be.an.instanceOf(Error);
expect(errorPassedToNext.message).to.include('DNS resolution failed');
expect(errorPassedToNext.code).to.equal('ENOTFOUND');
const errorPassedToNext = next.getCall(0).args[0];
expect(errorPassedToNext).to.be.an.instanceOf(Error);
expect(errorPassedToNext.message).to.include("DNS resolution failed");
expect(errorPassedToNext.code).to.equal("ENOTFOUND");
expect(errorPassedToNext.status).to.equal(500);
});
it('should reject with an error if query validation fails', async () => {
req.query.monitorURL = 'invalid-url';
it("should reject with an error if query validation fails", async () => {
req.query.monitorURL = "invalid-url";
await checkEndpointResolution(req, res, next);
expect(next.calledOnce).to.be.true;
const error = next.getCall(0).args[0];