From 7d138c6be2cf9c00b428a127cb3919210aeeefac Mon Sep 17 00:00:00 2001 From: Jialecl Date: Tue, 17 Sep 2024 12:04:20 +0200 Subject: [PATCH] Added and improved coverage to tests + fixed vulnerabilities --- lib/jest.config.cjs | 10 +- lib/package-lock.json | 71 +++---- lib/package.json | 6 +- lib/test/hal-api-caller.test.js | 45 ++-- lib/test/mocks/get-response.json | 355 +++++++++++++++++++++++++++++++ 5 files changed, 416 insertions(+), 71 deletions(-) create mode 100644 lib/test/mocks/get-response.json diff --git a/lib/jest.config.cjs b/lib/jest.config.cjs index 31ec9c7..9259879 100644 --- a/lib/jest.config.cjs +++ b/lib/jest.config.cjs @@ -3,14 +3,14 @@ module.exports = { coverageThreshold: { global: { // global thresholds - branches: 20, - functions: 20, - lines: 20, - statements: 20, + branches: 50, + functions: 50, + lines: 80, + statements: 80, }, }, collectCoverage: true, - collectCoverageFrom: ["**/src/**/*.{js}", "!**/node_modules/**", "!**/coverage/**", "!**/test/**"], + collectCoverageFrom: ["src/*.js", "src/**/*.js", "!src/main.js", "!**/node_modules/**"], testURL: "http://localhost", testEnvironment: "jsdom", transform: { diff --git a/lib/package-lock.json b/lib/package-lock.json index 426613b..3bc720b 100644 --- a/lib/package-lock.json +++ b/lib/package-lock.json @@ -2678,28 +2678,6 @@ "@babel/types": "^7.20.7" } }, - "node_modules/@types/eslint": { - "version": "8.56.6", - "resolved": "https://registry.npmjs.org/@types/eslint/-/eslint-8.56.6.tgz", - "integrity": "sha512-ymwc+qb1XkjT/gfoQwxIeHZ6ixH23A+tCT2ADSA/DPVKzAjwYkTXBMCQ/f6fe4wEa85Lhp26VPeUxI7wMhAi7A==", - "dev": true, - "peer": true, - "dependencies": { - "@types/estree": "*", - "@types/json-schema": "*" - } - }, - "node_modules/@types/eslint-scope": { - "version": "3.7.7", - "resolved": "https://registry.npmjs.org/@types/eslint-scope/-/eslint-scope-3.7.7.tgz", - "integrity": "sha512-MzMFlSLBqNF2gcHWO0G1vP/YQyfvrxZ0bF+u7mzUdZ1/xK4A4sru+nraZz5i3iEIk1l1uyicaDVTB4QbbEkAYg==", - "dev": true, - "peer": true, - "dependencies": { - "@types/eslint": "*", - "@types/estree": "*" - } - }, "node_modules/@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", @@ -3218,10 +3196,10 @@ "node": ">=0.4.0" } }, - "node_modules/acorn-import-assertions": { - "version": "1.9.0", - "resolved": "https://registry.npmjs.org/acorn-import-assertions/-/acorn-import-assertions-1.9.0.tgz", - "integrity": "sha512-cmMwop9x+8KFhxvKrKfPYmN6/pKTYYHBqLa0DfvVZcKMJWNyWLnaqND7dx/qn66R7ewM1UX5XMaDVP5wlVTaVA==", + "node_modules/acorn-import-attributes": { + "version": "1.9.5", + "resolved": "https://registry.npmjs.org/acorn-import-attributes/-/acorn-import-attributes-1.9.5.tgz", + "integrity": "sha512-n02Vykv5uA3eHGM/Z2dQrcD56kL8TyDb2p1+0P83PClMnC/nc+anbQRhIOWnSq4Ke/KvDPrY3C9hDtC/A3eHnQ==", "dev": true, "peer": true, "peerDependencies": { @@ -3356,9 +3334,9 @@ "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" }, "node_modules/axios": { - "version": "1.6.8", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz", - "integrity": "sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==", + "version": "1.7.7", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.7.7.tgz", + "integrity": "sha512-S4kL7XrjgBmvdGut0sN3yJxqYzrDOnivkBiN0OFs6hLiUam3UPvswUo0kqGyhqUZGEOytHyumEdXsAkgCOUf3Q==", "dependencies": { "follow-redirects": "^1.15.6", "form-data": "^4.0.0", @@ -4188,9 +4166,9 @@ } }, "node_modules/enhanced-resolve": { - "version": "5.16.0", - "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.16.0.tgz", - "integrity": "sha512-O+QWCviPNSSLAD9Ucn8Awv+poAkqn3T1XY5/N7kR7rQO9yfSGWkYZDwpJ+iKF7B8rxaQKWngSqACpgzeapSyoA==", + "version": "5.17.1", + "resolved": "https://registry.npmjs.org/enhanced-resolve/-/enhanced-resolve-5.17.1.tgz", + "integrity": "sha512-LMHl3dXhTcfv8gM4kEzIUeTQ+7fpdA0l2tUf34BddXPkz2A5xJ5L/Pchd5BL6rdccM9QGvu0sWZzK1Z1t4wwyg==", "dev": true, "peer": true, "dependencies": { @@ -7468,12 +7446,12 @@ } }, "node_modules/micromatch": { - "version": "4.0.5", - "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.5.tgz", - "integrity": "sha512-DMy+ERcEW2q8Z2Po+WNXuw3c5YaUSFjAO5GsJqfEl7UjvtIuFKO6ZrKvcItdy98dwFI2N1tg3zNIdKaQT+aNdA==", + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", "dev": true, "dependencies": { - "braces": "^3.0.2", + "braces": "^3.0.3", "picomatch": "^2.3.1" }, "engines": { @@ -8888,27 +8866,26 @@ } }, "node_modules/webpack": { - "version": "5.90.3", - "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.90.3.tgz", - "integrity": "sha512-h6uDYlWCctQRuXBs1oYpVe6sFcWedl0dpcVaTf/YF67J9bKvwJajFulMVSYKHrksMB3I/pIagRzDxwxkebuzKA==", + "version": "5.94.0", + "resolved": "https://registry.npmjs.org/webpack/-/webpack-5.94.0.tgz", + "integrity": "sha512-KcsGn50VT+06JH/iunZJedYGUJS5FGjow8wb9c0v5n1Om8O1g4L6LjtfxwlXIATopoQu+vOXXa7gYisWxCoPyg==", "dev": true, "peer": true, "dependencies": { - "@types/eslint-scope": "^3.7.3", "@types/estree": "^1.0.5", - "@webassemblyjs/ast": "^1.11.5", - "@webassemblyjs/wasm-edit": "^1.11.5", - "@webassemblyjs/wasm-parser": "^1.11.5", + "@webassemblyjs/ast": "^1.12.1", + "@webassemblyjs/wasm-edit": "^1.12.1", + "@webassemblyjs/wasm-parser": "^1.12.1", "acorn": "^8.7.1", - "acorn-import-assertions": "^1.9.0", + "acorn-import-attributes": "^1.9.5", "browserslist": "^4.21.10", "chrome-trace-event": "^1.0.2", - "enhanced-resolve": "^5.15.0", + "enhanced-resolve": "^5.17.1", "es-module-lexer": "^1.2.1", "eslint-scope": "5.1.1", "events": "^3.2.0", "glob-to-regexp": "^0.4.1", - "graceful-fs": "^4.2.9", + "graceful-fs": "^4.2.11", "json-parse-even-better-errors": "^2.3.1", "loader-runner": "^4.2.0", "mime-types": "^2.1.27", @@ -8916,7 +8893,7 @@ "schema-utils": "^3.2.0", "tapable": "^2.1.1", "terser-webpack-plugin": "^5.3.10", - "watchpack": "^2.4.0", + "watchpack": "^2.4.1", "webpack-sources": "^3.2.3" }, "bin": { diff --git a/lib/package.json b/lib/package.json index 6612379..1f21bd3 100644 --- a/lib/package.json +++ b/lib/package.json @@ -13,9 +13,9 @@ "copy": "node ./scripts/build/copy-distribution-files.js", "start": "rollup -c -w", "build": "npm run bundle && npm run copy", - "test": "jest --transformIgnorePatterns node_modules/?!axios/", - "test:watch": "npm test -- --watch --coverage", - "test:coverage": "npm test -- --coverage --coverageReporters=cobertura", + "test": "jest --collect-coverage --transformIgnorePatterns node_modules/?!axios/", + "test:watch": "npm test --watch --coverage", + "test:coverage": "npm test --collect-coverage --coverageReporters=cobertura", "test:coverage-dashboard": "npm test --collect-coverage" }, "author": "DXC Halstack team", diff --git a/lib/test/hal-api-caller.test.js b/lib/test/hal-api-caller.test.js index 67634a3..5b415f9 100644 --- a/lib/test/hal-api-caller.test.js +++ b/lib/test/hal-api-caller.test.js @@ -1,11 +1,12 @@ -import axios from 'axios'; -import MockAdapter from 'axios-mock-adapter'; +import axios from "axios"; +import MockAdapter from "axios-mock-adapter"; // Import the function to be tested -import apicaller from '../src/hal-api-caller'; -import response from './mocks/options-response.json'; +import apicaller from "../src/hal-api-caller"; +import optionsResponse from "./mocks/options-response.json"; +import getResponse from "./mocks/get-response.json"; -describe('options function', () => { +describe("options function", () => { let mockAxios; beforeEach(() => { @@ -16,16 +17,11 @@ describe('options function', () => { mockAxios.restore(); }); - it('should make a successful OPTIONS request and return HalResponse', async () => { - const url = 'https://example.com'; - const headers = { CustomHeader: 'CustomValue' }; - const responseData = response; - const responseHeaders = { 'Content-Type': 'application/json' }; - const expectedHalResponse = { - body: { _options: responseData }, - headers: responseHeaders, - status: 200, - }; + it("should make a successful OPTIONS request and return HalResponse", async () => { + const url = "https://example.com"; + const headers = { CustomHeader: "CustomValue" }; + const responseData = optionsResponse; + const responseHeaders = { "Content-Type": "application/json" }; // Mocking the Axios request and response mockAxios.onOptions(url).reply(200, responseData, responseHeaders); @@ -34,9 +30,26 @@ describe('options function', () => { const result = await apicaller.options({ url, headers }); // Assertions + expect(result).toBeDefined(); + expect(result.halResource).toBeDefined(); + expect(result.halResource.getTitle()).toEqual("Options available on Quote ID"); + }); + it("should make a successful GET request and return HalResponse", async () => { + const url = "https://example.com"; + const headers = { CustomHeader: "CustomValue" }; + const responseData = getResponse; + const responseHeaders = { "Content-Type": "application/json" }; + + // Mocking the Axios request and response + mockAxios.onGet(url).reply(200, responseData, responseHeaders); + + // Calling the function + const result = await apicaller.get({ url, headers }); + + // Assertions expect(result).toBeDefined(); expect(result.halResource).toBeDefined(); - expect(result.halResource.getTitle()).toEqual('Options available on Quote ID'); + expect(result.halResource.getItems().length).toBe(10); }); }); diff --git a/lib/test/mocks/get-response.json b/lib/test/mocks/get-response.json new file mode 100644 index 0000000..fffe35c --- /dev/null +++ b/lib/test/mocks/get-response.json @@ -0,0 +1,355 @@ +{ + "_count": 10, + "_links": { + "item": [ + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/trewqasdfgyhujikolpyt", + "summary": { + "identifier": "trewqasdfgyhujikolpyt", + "insuredPeriod": "Jan 1, 2001 - Dec 31, 2001", + "underwritingYear": 2001, + "baseCompany": "Continuum Reinsurance", + "Insured": "Test" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/FP_CIAR02", + "summary": { + "identifier": "FP_CIAR02", + "insuredPeriod": "Jan 1, 2018 - Dec 31, 2018", + "underwritingYear": 2018, + "baseCompany": "SATYA_RE" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/GRE_PLB_141009_1", + "summary": { + "identifier": "GRE_PLB_141009_1", + "insuredPeriod": "Jan 1, 2009 - Dec 31, 2009", + "underwritingYear": 2009, + "baseCompany": "GRE Base" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/RLI0023-1-20.nov.2009-10:39:38", + "summary": { + "identifier": "RLI0023-1-20.nov.2009-10:39:38", + "insuredPeriod": "Jan 1, 2008 - Dec 31, 2008", + "underwritingYear": 2008, + "baseCompany": "Continuum Reinsurance", + "Insured": "Nestle" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/RLI0023-1-20.nov.2009-10:39:38", + "summary": { + "identifier": "RLI0023-1-20.nov.2009-10:39:38", + "insuredPeriod": "Jan 1, 2008 - Dec 31, 2008", + "underwritingYear": 2008, + "baseCompany": "Continuum Reinsurance", + "Insured": "Nestle" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/AM_10825_SQL3", + "summary": { + "identifier": "AM_10825_SQL3", + "insuredPeriod": "Jun 3, 2017 - Dec 31, 9999", + "underwritingYear": 2017, + "baseCompany": "GRE Base" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/POL55491", + "summary": { + "identifier": "POL55491", + "insuredPeriod": "Jan 1, 2007 - Jan 1, 2008", + "underwritingYear": 2007, + "baseCompany": "GRE Base", + "Insured": "pl_skra" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/CLDIFCURR2", + "summary": { + "identifier": "CLDIFCURR2", + "insuredPeriod": "Jan 1, 2021 - Dec 31, 2021", + "underwritingYear": 2021, + "baseCompany": "Continuum Reinsurance" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/LP_CG_09", + "summary": { + "identifier": "LP_CG_09", + "insuredPeriod": "Jan 1, 2018 - Dec 31, 2018", + "underwritingYear": 2018, + "baseCompany": "SATYA_RE" + } + }, + { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/CGU0028", + "summary": { + "identifier": "CGU0028", + "insuredPeriod": "Jul 1, 2006 - Nov 30, 2007", + "underwritingYear": 2006, + "baseCompany": "CGU AUTO PA / SCOB mandatory", + "Insured": "Norwegian Wood" + } + } + ], + "self": { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/" + }, + "first": { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/?_start=1" + }, + "next": { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/?_start=101" + }, + "prev": { + "href": "(not present)" + }, + "last": { + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/?_start=10701" + } + }, + "_options": { + "title": "Policy Businesses collection interactions", + "properties": { + "formerIdentifier": { + "type": "string", + "minLength": 0, + "maxLength": 50 + }, + "title": { + "type": "string", + "minLength": 0, + "maxLength": 60 + }, + "identifier": { + "type": "string", + "minLength": 1, + "maxLength": 36 + } + }, + "required": ["identifier"], + "links": [ + { + "rel": "search", + "title": "Search Policy Businesses collection content", + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/", + "method": "GET", + "mediaType": "application/vnd.hal+json", + "targetSchema": { + "$ref": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/schemas/policy/business/search" + }, + "schema": { + "type": "object", + "properties": { + "identifier": { + "type": "string", + "minLength": 1, + "maxLength": 36 + }, + "_count": { + "type": "integer", + "minimum": 0 + }, + "_start": { + "type": "integer", + "minimum": 1 + }, + "_num": { + "type": "integer", + "minimum": 1 + } + }, + "required": [] + } + }, + { + "rel": "create", + "title": "Create a Policy Business", + "href": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/policy/businesses/", + "method": "POST", + "mediaType": "application/vnd.hal+json", + "targetSchema": { + "$ref": "https://dxcdarcop001v.dk.emea.csc.com:9050/api/schemas/policy/business/create" + }, + "schema": { + "type": "object", + "properties": { + "formerIdentifier": { + "type": "string", + "minLength": 0, + "maxLength": 50 + }, + "title": { + "type": "string", + "minLength": 0, + "maxLength": 60 + }, + "identifier": { + "type": "string", + "minLength": 1, + "maxLength": 36 + }, + "typeOfBusiness": { + "type": "object", + "properties": { + "subclassNumber": { + "type": "integer" + }, + "externalCode": { + "type": "string", + "minLength": 1, + "maxLength": 50 + }, + "code": { + "type": "string", + "minLength": 1, + "maxLength": 15 + } + }, + "required": ["subclassNumber", "code"] + }, + "insuredPeriods": { + "type": "array", + "items": { + "type": "object", + "items": { + "endDate": { + "type": "string" + }, + "startDate": { + "type": "string" + }, + "insuredIdentifier": { + "type": "string" + }, + "insuredReference": { + "type": "string" + }, + "underwritingYear": { + "type": "integer" + }, + "insuredPeriodDateSpanString": { + "type": "string" + }, + "insuredName": { + "type": "string" + }, + "beginDateTime": { + "type": "string" + }, + "beginDate": { + "type": "string" + }, + "sections": { + "type": "array", + "items": { + "type": "object", + "items": { + "name": { + "type": "string" + }, + "informationalLimit1": { + "type": "integer" + }, + "informationalLimit2": { + "type": "integer" + }, + "totalGrossPremium": { + "type": "number" + }, + "numOfInsuredObjects": { + "type": "integer" + }, + "displayName": { + "type": "string" + }, + "attachmentPeriodDateSpanString": { + "type": "string" + }, + "externalIdentifier": { + "type": "string" + }, + "sharePercent": { + "type": "number" + }, + "mainLimit": { + "type": "integer" + }, + "scopeOfCoverVersion": { + "type": "integer" + }, + "includedCountries": { + "type": "array", + "items": { + "type": "object", + "properties": { + "name": { + "type": "string" + }, + "code": { + "type": "string" + }, + "isActive": { + "type": "boolean" + }, + "note": { + "type": "string" + }, + "isoAlpha3": { + "type": "string" + } + } + } + }, + "currency": { + "type": "object", + "properties": { + "isoAlpha": { + "type": "string", + "minLength": 1, + "maxLength": 3 + } + }, + "required": ["isoAlpha"] + }, + "includedRefData": { + "type": "array", + "items": { + "type": "object", + "properties": { + "subclassNumber": { + "type": "integer" + }, + "externalCode": { + "type": "string", + "minLength": 1, + "maxLength": 50 + }, + "code": { + "type": "string", + "minLength": 1, + "maxLength": 15 + } + }, + "required": ["subclassNumber", "code"] + } + } + } + } + } + } + } + } + }, + "required": ["identifier", "insuredPeriods", "typeOfBusiness"] + } + } + ] + } +}