Skip to content

Commit

Permalink
Merge pull request #175 from snyk/fix/ZEN-537/add-report-tests
Browse files Browse the repository at this point in the history
fix: add report tests [ZEN-537]
  • Loading branch information
patricia-v committed Mar 10, 2023
2 parents 2fbd29c + 64672ce commit 5ac244d
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 70 deletions.
29 changes: 21 additions & 8 deletions tests/analysis.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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();
});
});
});
46 changes: 1 addition & 45 deletions tests/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
16 changes: 16 additions & 0 deletions tests/constants/sample.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
58 changes: 41 additions & 17 deletions tests/report.spec.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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"');
});
});

0 comments on commit 5ac244d

Please sign in to comment.