From e940a0d1519f3974e0f5b72a293e3f5551a34041 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 29 Oct 2024 13:51:57 +0800 Subject: [PATCH] update tests --- Server/tests/services/networkService.test.js | 71 +++++++++++++++++--- Server/tests/services/statusService.test.js | 20 ++++++ 2 files changed, 82 insertions(+), 9 deletions(-) diff --git a/Server/tests/services/networkService.test.js b/Server/tests/services/networkService.test.js index dbc2986ee..f27f0ef79 100644 --- a/Server/tests/services/networkService.test.js +++ b/Server/tests/services/networkService.test.js @@ -2,7 +2,6 @@ import sinon from "sinon"; import NetworkService from "../../service/networkService.js"; import { expect } from "chai"; import http from "http"; -import exp from "constants"; describe("Network Service", () => { let axios, ping, logger, networkService; @@ -74,7 +73,7 @@ describe("Network Service", () => { }); describe("requestHttp", () => { it("should return a response object if http successful", async () => { - const job = { data: { url: "http://test.com", _id: "123" } }; + const job = { data: { url: "http://test.com", _id: "123", type: "http" } }; const httpResult = await networkService.requestHttp(job); expect(httpResult.monitorId).to.equal("123"); expect(httpResult.type).to.equal("http"); @@ -87,7 +86,7 @@ describe("Network Service", () => { networkService.timeRequest = sinon .stub() .resolves({ response: null, responseTime: 1, error }); - const job = { data: { url: "http://test.com", _id: "123" } }; + const job = { data: { url: "http://test.com", _id: "123", type: "http" } }; const httpResult = await networkService.requestHttp(job); expect(httpResult.monitorId).to.equal("123"); expect(httpResult.type).to.equal("http"); @@ -101,7 +100,7 @@ describe("Network Service", () => { networkService.timeRequest = sinon .stub() .resolves({ response: null, responseTime: 1, error }); - const job = { data: { url: "http://test.com", _id: "123" } }; + const job = { data: { url: "http://test.com", _id: "123", type: "http" } }; const httpResult = await networkService.requestHttp(job); expect(httpResult.monitorId).to.equal("123"); expect(httpResult.type).to.equal("http"); @@ -113,7 +112,7 @@ describe("Network Service", () => { describe("requestPagespeed", () => { it("should return a response object if pagespeed successful", async () => { - const job = { data: { url: "http://test.com", _id: "123" } }; + const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } }; const pagespeedResult = await networkService.requestPagespeed(job); expect(pagespeedResult.monitorId).to.equal("123"); expect(pagespeedResult.type).to.equal("pagespeed"); @@ -126,7 +125,7 @@ describe("Network Service", () => { networkService.timeRequest = sinon .stub() .resolves({ response: null, responseTime: 1, error }); - const job = { data: { url: "http://test.com", _id: "123" } }; + const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } }; const pagespeedResult = await networkService.requestPagespeed(job); expect(pagespeedResult.monitorId).to.equal("123"); expect(pagespeedResult.type).to.equal("pagespeed"); @@ -140,7 +139,7 @@ describe("Network Service", () => { networkService.timeRequest = sinon .stub() .resolves({ response: null, responseTime: 1, error }); - const job = { data: { url: "http://test.com", _id: "123" } }; + const job = { data: { url: "http://test.com", _id: "123", type: "pagespeed" } }; const pagespeedResult = await networkService.requestPagespeed(job); expect(pagespeedResult.monitorId).to.equal("123"); expect(pagespeedResult.type).to.equal("pagespeed"); @@ -150,6 +149,60 @@ describe("Network Service", () => { }); }); + describe("requestHardware", () => { + it("should return a response object if hardware successful", async () => { + const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } }; + const httpResult = await networkService.requestHardware(job); + expect(httpResult.monitorId).to.equal("123"); + expect(httpResult.type).to.equal("hardware"); + expect(httpResult.responseTime).to.be.a("number"); + expect(httpResult.status).to.be.true; + }); + it("should return a response object if hardware successful and job jas a secret", async () => { + const job = { + data: { + url: "http://test.com", + _id: "123", + type: "hardware", + secret: "my_secret", + }, + }; + const httpResult = await networkService.requestHardware(job); + expect(httpResult.monitorId).to.equal("123"); + expect(httpResult.type).to.equal("hardware"); + expect(httpResult.responseTime).to.be.a("number"); + expect(httpResult.status).to.be.true; + }); + it("should return a response object if hardware unsuccessful", async () => { + const error = new Error("Test error"); + error.response = { status: 404 }; + networkService.timeRequest = sinon + .stub() + .resolves({ response: null, responseTime: 1, error }); + const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } }; + const httpResult = await networkService.requestHardware(job); + expect(httpResult.monitorId).to.equal("123"); + expect(httpResult.type).to.equal("hardware"); + expect(httpResult.responseTime).to.be.a("number"); + expect(httpResult.status).to.be.false; + expect(httpResult.code).to.equal(404); + }); + it("should return a response object if hardware unsuccessful with unknown code", async () => { + const error = new Error("Test error"); + error.response = {}; + networkService.timeRequest = sinon + .stub() + .resolves({ response: null, responseTime: 1, error }); + const job = { data: { url: "http://test.com", _id: "123", type: "hardware" } }; + const httpResult = await networkService.requestHardware(job); + expect(httpResult.monitorId).to.equal("123"); + expect(httpResult.type).to.equal("hardware"); + expect(httpResult.responseTime).to.be.a("number"); + expect(httpResult.status).to.be.false; + expect(httpResult.code).to.equal(networkService.NETWORK_ERROR); + }); + }); + describe("getStatus", () => { beforeEach(() => { networkService.requestPing = sinon.stub(); @@ -179,10 +232,10 @@ describe("Network Service", () => { expect(networkService.requestHttp.notCalled).to.be.true; expect(networkService.requestPagespeed.calledOnce).to.be.true; }); - it("should call requestHandleHardware if type is hardware", () => { + it("should call requestHardware if type is hardware", () => { networkService.getStatus({ data: { type: "hardware" } }); + expect(networkService.requestHardware.calledOnce).to.be.true; expect(networkService.requestPing.notCalled).to.be.true; - expect(networkService.requestHttp.notCalled).to.be.true; expect(networkService.requestPagespeed.notCalled).to.be.true; }); it("should log an error if an unknown type is provided", () => { diff --git a/Server/tests/services/statusService.test.js b/Server/tests/services/statusService.test.js index df74ef715..19b0edf19 100644 --- a/Server/tests/services/statusService.test.js +++ b/Server/tests/services/statusService.test.js @@ -173,6 +173,26 @@ describe("StatusService", () => { tbt: 0, }); }); + it("should build a check for hardware type", () => { + const check = statusService.buildCheck({ + monitorId: "test", + type: "hardware", + status: true, + responseTime: 100, + code: 200, + message: "Test message", + payload: { cpu: "cpu", memory: "memory", disk: "disk", host: "host" }, + }); + expect(check.monitorId).to.equal("test"); + expect(check.status).to.be.true; + expect(check.statusCode).to.equal(200); + expect(check.responseTime).to.equal(100); + expect(check.message).to.equal("Test message"); + expect(check.cpu).to.equal("cpu"); + expect(check.memory).to.equal("memory"); + expect(check.disk).to.equal("disk"); + expect(check.host).to.equal("host"); + }); }); describe("insertCheck", () => { it("should log an error if one is thrown", async () => {