Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update tests for hardware check module #1262

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Docker/test/cerbot-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
version: "3"

services:
webserver:
image: nginx:latest
ports:
- 80:80
- 443:443
restart: always
volumes:
- ./nginx/conf.d/:/etc/nginx/conf.d/:ro
- ./certbot/www/:/var/www/certbot/:ro
certbot:
image: certbot/certbot:latest
volumes:
- ./certbot/www/:/var/www/certbot/:rw
- ./certbot/conf/:/etc/letsencrypt/:rw
depends_on:
- webserver
2 changes: 2 additions & 0 deletions Server/db/mongo/modules/hardwareCheckModule.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import HardwareCheck from "../../models/HardwareCheck.js";
import Monitor from "../../models/Monitor.js";
import logger from "../../../utils/logger.js";

const SERVICE_NAME = "hardwareCheckModule";
const createHardwareCheck = async (hardwareCheckData) => {
Expand All @@ -15,6 +16,7 @@ const createHardwareCheck = async (hardwareCheckData) => {
method: "createHardwareCheck",
details: `monitor ID: ${monitorId}`,
});
return null;
}

if (monitor.uptimePercentage === undefined) {
Expand Down
63 changes: 61 additions & 2 deletions Server/tests/db/hardwareCheckModule.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import sinon from "sinon";
import HardwareCheck from "../../db/models/HardwareCheck.js";
import { createHardwareCheck } from "../../db/mongo/modules/hardwareCheckModule.js";
import Monitor from "../../db/models/Monitor.js";
import logger from "../../utils/logger.js";

const mockHardwareCheck = {
data: {
Expand Down Expand Up @@ -42,10 +44,23 @@ const mockHardwareCheck = {
],
};

const mockMonitor = {
_id: "123",
uptimePercentage: 1,
status: true,
save: () => this,
};

describe("HardwareCheckModule", () => {
let hardwareCheckSaveStub;
let hardwareCheckSaveStub,
hardwareCheckCountDocumentsStub,
monitorFindByIdStub,
loggerStub;
beforeEach(() => {
loggerStub = sinon.stub(logger, "error");
hardwareCheckSaveStub = sinon.stub(HardwareCheck.prototype, "save");
monitorFindByIdStub = sinon.stub(Monitor, "findById");
hardwareCheckCountDocumentsStub = sinon.stub(HardwareCheck, "countDocuments");
});

afterEach(() => {
Expand All @@ -55,12 +70,23 @@ describe("HardwareCheckModule", () => {
describe("createHardwareCheck", () => {
it("should return a hardware check", async () => {
hardwareCheckSaveStub.resolves(mockHardwareCheck);
const hardwareCheck = await createHardwareCheck({});
monitorFindByIdStub.resolves(mockMonitor);
hardwareCheckCountDocumentsStub.resolves(1);
const hardwareCheck = await createHardwareCheck({ status: true });
expect(hardwareCheck).to.exist;
expect(hardwareCheck).to.deep.equal(mockHardwareCheck);
});
it("should return a hardware check for a check with status false", async () => {
hardwareCheckSaveStub.resolves(mockHardwareCheck);
monitorFindByIdStub.resolves(mockMonitor);
hardwareCheckCountDocumentsStub.resolves(1);
const hardwareCheck = await createHardwareCheck({ status: false });
expect(hardwareCheck).to.exist;
expect(hardwareCheck).to.deep.equal(mockHardwareCheck);
});
it("should handle an error", async () => {
const err = new Error("test error");
monitorFindByIdStub.resolves(mockMonitor);
hardwareCheckSaveStub.rejects(err);
try {
await createHardwareCheck({});
Expand All @@ -69,5 +95,38 @@ describe("HardwareCheckModule", () => {
expect(error).to.deep.equal(err);
}
});
it("should log an error if a monitor is not found", async () => {
monitorFindByIdStub.resolves(null);
const res = await createHardwareCheck({});
expect(loggerStub.calledOnce).to.be.true;
expect(res).to.be.null;
});
it("should handle a monitor with undefined uptimePercentage", async () => {
monitorFindByIdStub.resolves({ ...mockMonitor, uptimePercentage: undefined });
hardwareCheckSaveStub.resolves(mockHardwareCheck);
hardwareCheckCountDocumentsStub.resolves(1);
const res = await createHardwareCheck({});
expect(res).to.exist;
});
it("should handle a monitor with undefined uptimePercentage and true status", async () => {
monitorFindByIdStub.resolves({
...mockMonitor,
uptimePercentage: undefined,
});
hardwareCheckSaveStub.resolves(mockHardwareCheck);
hardwareCheckCountDocumentsStub.resolves(1);
const res = await createHardwareCheck({ status: true });
expect(res).to.exist;
});
it("should handle a monitor with undefined uptimePercentage and false status", async () => {
monitorFindByIdStub.resolves({
...mockMonitor,
uptimePercentage: undefined,
});
hardwareCheckSaveStub.resolves(mockHardwareCheck);
hardwareCheckCountDocumentsStub.resolves(1);
const res = await createHardwareCheck({ status: false });
expect(res).to.exist;
});
Comment on lines +98 to +130
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Knees weak, arms heavy, but these edge case tests need more assertions!

While the edge cases are well-identified, we should add more specific assertions to verify the uptimePercentage calculations.

  it("should handle a monitor with undefined uptimePercentage and true status", async () => {
    monitorFindByIdStub.resolves({
      ...mockMonitor,
      uptimePercentage: undefined,
    });
    hardwareCheckSaveStub.resolves(mockHardwareCheck);
    hardwareCheckCountDocumentsStub.resolves(1);
    const res = await createHardwareCheck({ status: true });
    expect(res).to.exist;
+   expect(monitorFindByIdStub.firstCall.returnValue.uptimePercentage).to.equal(1);
  });

  it("should handle a monitor with undefined uptimePercentage and false status", async () => {
    monitorFindByIdStub.resolves({
      ...mockMonitor,
      uptimePercentage: undefined,
    });
    hardwareCheckSaveStub.resolves(mockHardwareCheck);
    hardwareCheckCountDocumentsStub.resolves(1);
    const res = await createHardwareCheck({ status: false });
    expect(res).to.exist;
+   expect(monitorFindByIdStub.firstCall.returnValue.uptimePercentage).to.equal(0);
  });

Committable suggestion skipped: line range outside the PR's diff.

});
});
Loading