Skip to content

Commit

Permalink
tests: fix Jest mocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninjeneer committed Apr 11, 2023
1 parent 4583700 commit bbbb2fd
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 33 deletions.
5 changes: 2 additions & 3 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
"recommendations": [
"dbaeumer.vscode-eslint",
"visualstudioexptteam.vscodeintellicode",
"ms-vscode.vscode-typescript-next",
"orta.vscode-jest",
"christian-kohler.npm-intellisense",
"hbenl.vscode-test-explorer"
"hbenl.vscode-test-explorer",
"ms-vscode.vscode-typescript-next"
]
}
8 changes: 8 additions & 0 deletions .vscode/server.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"folders": [
{
"path": ".."
}
],
"settings": {}
}
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
transform: {
"^.+\\.(t|j)sx?$": ["@swc/jest"],
},
silent: true
};
4 changes: 2 additions & 2 deletions src/models/probe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ export type SupabaseProbeResult = {
resultId: string;
}

export type ProbeResult = {
export type ProbeResult<T = any> = {
context: {
timestampStart: number;
timestampStop: number;
probeUid: string;
probeName: string;
};
result: any;
result: T;
}
19 changes: 19 additions & 0 deletions src/models/results/nmap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export type NmapResults = NmapResult[]
export type NmapResult = {
id: string;
sourceIdentifier: string;
published: string;
lastModified: string;
vulnStatus: string;
descriptions: Description[];
metrics: Metrics;
references: any[];
}

type Metrics = {
}

type Description = {
lang: string;
value: string;
}
20 changes: 18 additions & 2 deletions src/services/reports/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ProbeResult } from "../../models/probe"
import { NmapResult, NmapResults } from "../../models/results/nmap"
import { PROBE_NAMES } from "../../utils"

type Parser = (result: ProbeResult) => any
Expand All @@ -7,8 +8,23 @@ const noParser = (result: ProbeResult) => {
return result
}

const nmapParser = (result: ProbeResult) => {
return result
export const nmapParser = (probeResult: ProbeResult<NmapResults>): ProbeResult<NmapResults> => {
const descriptionIgnorePatterns = [
/\*\* REJECT \*\* DO NOT USE THIS CANDIDATE NUMBER/
]

const finalResults = []
for (const res of probeResult.result) {
if (descriptionIgnorePatterns.some((pattern) => res.descriptions.map((d) => d.value).some((description) => pattern.test(description)))) {
console.log(`Ignoring ${res.id}`)
continue
}
finalResults.push(res)
}
return {
...probeResult,
result: finalResults
}
}

const dummyParser = (result: ProbeResult) => {
Expand Down
40 changes: 40 additions & 0 deletions test/parsers/nmapParser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { ProbeResult } from "../../src/models/probe"
import { NmapResult, NmapResults } from "../../src/models/results/nmap"
import { nmapParser } from "../../src/services/reports/parser"

describe('Nmap Parser tests', () => {
it('should remove the REJECT CVEs', () => {
const res = {
result: [
{
descriptions: [
{ lang: 'en', value: '** REJECT ** DO NOT USE THIS CANDIDATE NUMBER bla bla bla' }
]
} as NmapResult
]
} as ProbeResult<NmapResults>
expect(nmapParser(res).result).toHaveLength(0)

const res2 = {
result: [
{
descriptions: [
{ lang: 'en', value: '** REJECT ** DO NOT USE THIS CANDIDATE NUMBER bla bla bla' }
]
} as NmapResult,
{
descriptions: [
{ lang: 'en', value: 'A good description' }
]
} as NmapResult,
{
descriptions: [
{ lang: 'en', value: '** REJECT ** DO NOT USE THIS CANDIDATE NUMBER bla bla bla' }
]
} as NmapResult
]
} as ProbeResult<NmapResults>
expect(nmapParser(res2).result).toHaveLength(1)
expect(nmapParser(res2).result[0].descriptions[0].value).toBe('A good description')
})
})
37 changes: 11 additions & 26 deletions test/service/scanService.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
import IScanService from "../../src/services/scan/interfaces/scanServiceInterface"
import ScanService from "../../src/services/scan/scanService";
import { ScanStatus } from "../../src/services/scan/types/startData";
import { createScanRequest, CreateScanRequest } from "../../src/services/requests/validators/scanRequest";
import AwsSqsQueue from "../../src/storage/messagequeue/awsSqsQueue";
import SupabaseStorage from "../../src/storage/scans/supabaseStorage";
import { ScanStatus } from '../../src/models/scan'
import { requestScan } from "../../src/services/requests/scanService";
import { saveScanStartData, saveProbesStartData } from "../../src/storage/scan.storage";
import { publishProbeRequest } from "../../src/storage/awsSqsQueue";

jest.mock('../../src/storage/scans/supabaseStorage.ts')
jest.mock('../../src/storage/messagequeue/awsSqsQueue.ts')

describe('Scan Service Tests', () => {
let scanService: IScanService;
let supabaseStorage: SupabaseStorage;
let awsSqsQueue: AwsSqsQueue;
jest.mock('../../src/storage/scan.storage')
jest.mock('../../src/storage/awsSqsQueue')

let spySaveScanStartData: jest.SpyInstance;
let spySaveProbeStartData: jest.SpyInstance;
let spyPublishProbeRequest: jest.SpyInstance;

beforeEach(() => {
supabaseStorage = new SupabaseStorage();
awsSqsQueue = new AwsSqsQueue();
scanService = new ScanService(supabaseStorage, awsSqsQueue);
spySaveScanStartData = jest.spyOn(supabaseStorage, 'saveScanStartData');
spySaveProbeStartData = jest.spyOn(supabaseStorage, 'saveProbesStartData');
spyPublishProbeRequest = jest.spyOn(awsSqsQueue, 'publishProbeRequest');
})
describe('Scan Service Tests', () => {

it('should request a scan', async () => {
const scanRequest: CreateScanRequest = {
Expand All @@ -34,23 +19,23 @@ describe('Scan Service Tests', () => {
{ name: 'probe-nmap', settings: {} }
]
}
const response = await scanService.requestScan(scanRequest);
const response = await requestScan(scanRequest);
expect(response.scanId).toBeDefined()
expect(spySaveScanStartData).toHaveBeenCalledWith({
expect(saveScanStartData).toHaveBeenCalledWith({
id: response.scanId,
status: ScanStatus.PENDING,
notification: false,
target: scanRequest.target,
periodicity: '* * * * *'
});
expect(spySaveProbeStartData).toHaveBeenCalledWith([
expect(saveProbesStartData).toHaveBeenCalledWith([
{
id: expect.any(String),
status: ScanStatus.PENDING,
scanId: response.scanId
}
]);
expect(spyPublishProbeRequest).toHaveBeenCalledWith([{
expect(publishProbeRequest).toHaveBeenCalledWith([{
context: {
id: expect.any(String),
name: 'probe-nmap',
Expand Down

0 comments on commit bbbb2fd

Please sign in to comment.