UNPKG

auditjs

Version:

Audit dependencies to identify known vulnerabilities and maintenance problems

136 lines 8.37 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 Coordinates_1 = require("../Types/Coordinates"); const nock_1 = __importDefault(require("nock")); const IqRequestService_1 = require("./IqRequestService"); describe('IQRequestService', () => { it("should have it's third party API request rejected when the IQ Server is down", () => __awaiter(void 0, void 0, void 0, function* () { const internalId = '123456'; const stage = 'build'; (0, nock_1.default)('http://testlocation:8070') .post(`/api/v2/scan/applications/${internalId}/sources/auditjs?stageId=${stage}`) .replyWithError('you messed up!') .get(`/api/v2/applications?publicId=testapp`) .reply(404, TestHelper_1.applicationInternalIdResponse.body); const requestService = new IqRequestService_1.IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300, false); const coords = [new Coordinates_1.Coordinates('commander', '2.12.2', '@types')]; return (0, TestHelper_1.default)(requestService.submitToThirdPartyAPI(coords)).to.eventually.be.rejected; })); it('should respond with an error if the response for an ID is bad', () => __awaiter(void 0, void 0, void 0, function* () { const stage = 'build'; (0, nock_1.default)('http://testlocation:8070') .get(`/api/v2/applications?publicId=testapp`) .reply(TestHelper_1.applicationInternalIdResponse.statusCode, { thereisnoid: 'none' }); const requestService = new IqRequestService_1.IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300, false); const coords = [new Coordinates_1.Coordinates('commander', '2.12.2', '@types')]; return (0, TestHelper_1.default)(requestService.submitToThirdPartyAPI(coords)).to.eventually.be.rejectedWith('No valid ID on response from Nexus IQ, potentially check the public application ID you are using'); })); it("should have it's third party API request accepted when the IQ Server is up", () => __awaiter(void 0, void 0, void 0, function* () { const internalId = '4bb67dcfc86344e3a483832f8c496419'; const stage = 'build'; const response = { statusCode: 202, body: { statusUrl: 'api/v2/scan/applications/a20bc16e83944595a94c2e36c1cd228e/status/9cee2b6366fc4d328edc318eae46b2cb', }, }; (0, nock_1.default)('http://testlocation:8070') .post(`/api/v2/scan/applications/${internalId}/sources/auditjs?stageId=${stage}`) .reply(response.statusCode, response.body) .get(`/api/v2/applications?publicId=testapp`) .reply(TestHelper_1.applicationInternalIdResponse.statusCode, TestHelper_1.applicationInternalIdResponse.body); const requestService = new IqRequestService_1.IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300, false); const coords = [new Coordinates_1.Coordinates('commander', '2.12.2', '@types')]; return (0, TestHelper_1.default)(requestService.submitToThirdPartyAPI(coords)).to.eventually.equal('api/v2/scan/applications/a20bc16e83944595a94c2e36c1cd228e/status/9cee2b6366fc4d328edc318eae46b2cb'); })); it("should have it's third party API request rejected when IQ Server is up but API gives bad response", () => __awaiter(void 0, void 0, void 0, function* () { const internalId = '4bb67dcfc86344e3a483832f8c496419'; const stage = 'build'; const response = { statusCode: 202, body: { statusUrl: 'api/v2/scan/applications/a20bc16e83944595a94c2e36c1cd228e/status/9cee2b6366fc4d328edc318eae46b2cb', }, }; (0, nock_1.default)('http://testlocation:8070') .post(`/api/v2/scan/applications/${internalId}/sources/auditjs?stageId=${stage}`) .reply(404, response.body) .get(`/api/v2/applications?publicId=testapp`) .reply(TestHelper_1.applicationInternalIdResponse.statusCode, TestHelper_1.applicationInternalIdResponse.body); const requestService = new IqRequestService_1.IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300, false); const coords = [new Coordinates_1.Coordinates('commander', '2.12.2', '@types')]; return (0, TestHelper_1.default)(requestService.submitToThirdPartyAPI(coords)).to.eventually.be.rejectedWith('Unable to submit to Third Party API'); })); it('should have return a proper result when polling IQ Server and the request is eventually valid', () => __awaiter(void 0, void 0, void 0, function* () { const response = { statusCode: 200, body: { policyAction: 'None', reportHtmlUrl: 'http://localhost:8070/ui/links/application/test-app/report/95c4c14e', isError: false, }, }; const stage = 'build'; (0, nock_1.default)('http://testlocation:8070') .get(`/api/v2/scan/applications/a20bc16e83944595a94c2e36c1cd228e/status/9cee2b6366fc4d328edc318eae46b2cb`) .reply(response.statusCode, response.body); const requestService = new IqRequestService_1.IqRequestService('admin', 'admin123', 'http://testlocation:8070', 'testapp', stage, 300, false); requestService.asyncPollForResults('api/v2/scan/applications/a20bc16e83944595a94c2e36c1cd228e/status/9cee2b6366fc4d328edc318eae46b2cb', () => { return false; }, (x) => { return (0, TestHelper_1.default)(x.reportHtmlUrl).to.equal('http://localhost:8070/ui/links/application/test-app/report/95c4c14e'); }); })); }); //# sourceMappingURL=IqRequestService.spec.js.map