UNPKG

auditjs

Version:

Audit dependencies to identify known vulnerabilities and maintenance problems

133 lines 6 kB
"use strict"; /* * Copyright 2019-Present Sonatype Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const TestHelper_1 = __importStar(require("../Tests/TestHelper")); const VulnerabilityExcluder_1 = require("./VulnerabilityExcluder"); const mock_fs_1 = __importDefault(require("mock-fs")); const jsonWithOnlyOneMatch = `{ "ignore": [ { "id": "test_id" } ] }`; const json = `{ "ignore": [ { "id": "test_id" }, { "id": "test_id2" } ] }`; const jsonWithNonApplicableId = `{ "ignore": [ { "id": "qwepquepoqwe" } ] }`; const nonSensicalJson = `{ "ignore": [ { ] }`; describe('VulnerabilityExcluder', () => { it('should filter vulnerabilities given a valid auditjs.json', () => __awaiter(void 0, void 0, void 0, function* () { var _a; (0, mock_fs_1.default)({ '/nonsensical': { 'auditjs.json': json, }, }); const results = yield (0, VulnerabilityExcluder_1.filterVulnerabilities)(TestHelper_1.ossIndexServerResults, '/nonsensical/auditjs.json'); (0, TestHelper_1.default)((_a = results[1].vulnerabilities) === null || _a === void 0 ? void 0 : _a.length).to.equal(0); mock_fs_1.default.restore(); })); it('should filter some vulnerabilities given a valid auditjs.json', () => __awaiter(void 0, void 0, void 0, function* () { var _b; (0, mock_fs_1.default)({ '/nonsensical': { 'auditjs.json': jsonWithOnlyOneMatch, }, }); const results = yield (0, VulnerabilityExcluder_1.filterVulnerabilities)(TestHelper_1.ossIndexServerResults, '/nonsensical/auditjs.json'); (0, TestHelper_1.default)((_b = results[1].vulnerabilities) === null || _b === void 0 ? void 0 : _b.length).to.equal(1); mock_fs_1.default.restore(); })); it('should not filter vulnerabilities given a valid auditjs.json with an id that does not match', () => __awaiter(void 0, void 0, void 0, function* () { var _c; (0, mock_fs_1.default)({ '/nonsensical': { 'auditjs.json': jsonWithNonApplicableId, }, }); const results = yield (0, VulnerabilityExcluder_1.filterVulnerabilities)(TestHelper_1.ossIndexServerResults, '/nonsensical/auditjs.json'); (0, TestHelper_1.default)((_c = results[1].vulnerabilities) === null || _c === void 0 ? void 0 : _c.length).to.equal(2); mock_fs_1.default.restore(); })); it('should just return the original results and not barf all over itself if the auditjs.json file is malformed', () => __awaiter(void 0, void 0, void 0, function* () { (0, mock_fs_1.default)({ '/nonsensical': { 'auditjs.json': nonSensicalJson, }, }); (0, TestHelper_1.default)((0, VulnerabilityExcluder_1.filterVulnerabilities)(TestHelper_1.ossIndexServerResults, '/nonsensical/auditjs.json')).to.eventually.be.rejected; mock_fs_1.default.restore(); })); it('should return original results if no auditjs.json exists ', () => __awaiter(void 0, void 0, void 0, function* () { (0, mock_fs_1.default)({ '/nonsensical': {} }); const results = yield (0, VulnerabilityExcluder_1.filterVulnerabilities)(TestHelper_1.ossIndexServerResults, '/nonsensical/auditjs.json'); (0, TestHelper_1.default)(results).to.deep.equal(TestHelper_1.ossIndexServerResults); mock_fs_1.default.restore(); })); }); //# sourceMappingURL=VulnerabilityExcluder.spec.js.map