diff --git a/tests/analysis.spec.ts b/tests/analysis.spec.ts index c86111d4..696081be 100644 --- a/tests/analysis.spec.ts +++ b/tests/analysis.spec.ts @@ -4,9 +4,9 @@ import jsonschema from 'jsonschema'; import { analyzeFolders, extendAnalysis, analyzeBundle } from '../src/analysis'; import { uploadRemoteBundle } from '../src/bundles'; import { baseURL, sessionToken, source, TEST_TIMEOUT } from './constants/base'; -import { sampleProjectPath, bundleFilesFull, bundleExtender } from './constants/sample'; +import { sampleProjectPath, bundleFilesFull, bundleExtender, getReportReturn } from './constants/sample'; import { emitter } from '../src/emitter'; -import { AnalysisResponseProgress } from '../src/http'; +import { AnalysisResponseProgress, Result, UploadReportResponseDto, GetAnalysisErrorCodes } from '../src/http'; import { SupportedFiles } from '../src/interfaces/files.interface'; import { AnalysisSeverity, AnalysisContext } from '../src/interfaces/analysis-options.interface'; import * as sarifSchema from './sarif-schema-2.1.0.json'; @@ -349,17 +349,16 @@ describe('Functional test of analysis', () => { ); }); - // TODO: this test is being skipped for now since the /report flow hasn't been fully rolled out and it can't succeed for now - it.skip('should successfully analyze folder with the report option enabled', async () => { + it('should successfully analyze folder with the report option enabled', async () => { const mockReportBundle = jest.spyOn(report, 'reportBundle'); + mockReportBundle.mockReturnValueOnce(Promise.resolve(getReportReturn)); const bundle = await analyzeFolders({ connection: { baseURL, sessionToken, source }, - analysisOptions: { severity: AnalysisSeverity.info, prioritized: true, legacy: true }, + analysisOptions: { severity: AnalysisSeverity.info }, fileOptions: { paths: [sampleProjectPath], symlinksEnabled: false, - defaultFileIgnores: undefined, }, reportOptions: { enabled: true, @@ -368,9 +367,23 @@ describe('Functional test of analysis', () => { }); expect(mockReportBundle).toHaveBeenCalledTimes(1); + expect(bundle).toBeTruthy(); + }); - // TODO: check if bundle was successfully created - console.log(bundle); + it('should successfully analyze folder but not report with the report option disabled', async () => { + const mockReportBundle = jest.spyOn(report, 'reportBundle'); + + const bundle = await analyzeFolders({ + connection: { baseURL, sessionToken, source }, + analysisOptions: { severity: AnalysisSeverity.info }, + fileOptions: { + paths: [sampleProjectPath], + symlinksEnabled: false, + }, + }); + + expect(mockReportBundle).not.toHaveBeenCalled(); + expect(bundle).toBeTruthy(); }); }); }); diff --git a/tests/api.spec.ts b/tests/api.spec.ts index eba50862..7c6f302c 100644 --- a/tests/api.spec.ts +++ b/tests/api.spec.ts @@ -10,6 +10,7 @@ import { getAnalysis, AnalysisStatus, initReport, + getReport, } from '../src/http'; import { BundleFiles } from '../src/interfaces/files.interface'; import * as needle from '../src/needle'; @@ -382,49 +383,4 @@ describe('Requests to public API', () => { }, TEST_TIMEOUT, ); - - // TODO: this test is being skipped for now since the /report flow hasn't been fully rolled out and it can't succeed for now - it.skip('test successful report workflow', async () => { - // Create a bundle first - const files: BundleFiles = (await bundleFilesFull).reduce((r, d) => { - r[d.bundlePath] = pick(d, ['hash', 'content']); - return r; - }, {}); - - const bundleResponse = await createBundle({ - baseURL, - sessionToken, - source, - files, - }); - expect(bundleResponse.type).toEqual('success'); - if (bundleResponse.type === 'error') return; - const bundleHash = bundleResponse.value.bundleHash; - - const initReportResponse = await initReport({ - baseURL, - sessionToken, - source, - bundleHash: bundleHash, - report: { - enabled: true, - projectName: 'test-project', - }, - }); - - expect(initReportResponse.type).toEqual('success'); - - // TODO: getReport with reportId - // example options: - // { - // baseURL: 'http://localhost:11981', - // sessionToken: 'token 2bee9fd5-f9b6-48d7-a159-300216f99af4', - // source: 'snyk-cli', - // requestId: 'a00a3aba-eba4-4e17-ad0b-0cd7bf0cbd5c', - // reportId: '004355be-5424-4a2c-ad53-44841a83e185' - // } - - if (initReportResponse.type === 'error') return; - console.log(initReportResponse.value); - }); }); diff --git a/tests/constants/sample.ts b/tests/constants/sample.ts index 17190339..311fa7e4 100644 --- a/tests/constants/sample.ts +++ b/tests/constants/sample.ts @@ -3,6 +3,7 @@ import fs from 'fs'; import { FileInfo } from '../../src/interfaces/files.interface'; import { getFileInfo, notEmpty } from '../../src/files'; +import { ReportResult } from '../../src'; export const sampleProjectPath = path.resolve(__dirname, '../sample-repo'); export const supportedFiles = { @@ -88,3 +89,18 @@ export const bundleExtender: () => Promise<{ }, }; }; + +export const initReportReturn = { reportId: 'test-reportId' }; + +export const getReportReturn = { + status: 'COMPLETE', + uploadResult: { + projectId: 'test-projectId', + snapshotId: 'test-snapshotId', + reportUrl: 'test-reportUrl', + }, + analysisResult: { + type: 'sarif', + sarif: {}, + }, +} as ReportResult; diff --git a/tests/report.spec.ts b/tests/report.spec.ts index 31898a7d..e31afddd 100644 --- a/tests/report.spec.ts +++ b/tests/report.spec.ts @@ -1,22 +1,24 @@ import path from 'path'; - import { createBundleFromFolders } from '../src/bundles'; import { baseURL, sessionToken, source } from './constants/base'; import { reportBundle } from '../src/report'; -import { sampleProjectPath } from './constants/sample'; - -describe('Functional test for report creation', () => { - // TODO: this test is being skipped for now since the /report flow hasn't been fully rolled out and it can't succeed for now - it.skip('should report a bundle with correct parameters', async () => { - const paths: string[] = [path.join(sampleProjectPath)]; - - const baseConfig = { - baseURL, - sessionToken, - source, - paths, - }; +import * as http from '../src/http'; +import { sampleProjectPath, initReportReturn, getReportReturn } from './constants/sample'; + +jest.spyOn(http, 'initReport').mockReturnValue(Promise.resolve({ type: 'success', value: initReportReturn })); +jest.spyOn(http, 'getReport').mockReturnValue(Promise.resolve({ type: 'success', value: getReportReturn })); + +describe('Functional test for report', () => { + const paths: string[] = [path.join(sampleProjectPath)]; + + const baseConfig = { + baseURL, + sessionToken, + source, + paths, + }; + it('should report a bundle with correct parameters', async () => { const reportConfig = { enabled: true, projectName: 'test-project', @@ -35,9 +37,31 @@ describe('Functional test for report creation', () => { report: reportConfig, }); - // TODO: check result - console.log(result); + expect(result).not.toBeNull(); + expect(result.status).toBe('COMPLETE'); + expect(result).toHaveProperty('uploadResult'); + expect(result).toHaveProperty('analysisResult'); }); - // TODO: error handling test(s) + it('should fail report if no project name was given', async () => { + const reportConfig = { + enabled: true, + projectName: undefined, + }; + + const bundleResult = await createBundleFromFolders(baseConfig); + + expect(bundleResult).not.toBeNull(); + expect(bundleResult).toHaveProperty('bundleHash'); + const bundleHash = bundleResult?.bundleHash; + if (!bundleHash) return; + + expect(async () => { + await reportBundle({ + bundleHash, + ...baseConfig, + report: reportConfig, + }); + }).rejects.toThrowError('"project-name" must be provided for "report"'); + }); });