Skip to content

Commit

Permalink
enums to objects
Browse files Browse the repository at this point in the history
  • Loading branch information
leoxs22 committed Jun 12, 2023
1 parent 8ddfb25 commit 11c9206
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 73 deletions.
50 changes: 20 additions & 30 deletions src/Findings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ import {
LOCATION,
INFO,
ALLOWED_INFO_IMPACT,
DEFAULT_INFO_IMPACT
DEFAULT_INFO_IMPACT,
FindingStatus,
Condition,
FINDING_STATUS,
CONDITIONS
} from './constants'
import {
createMdBlock,
Expand All @@ -55,20 +59,6 @@ export type FindingMetadata = {
resolution?: FindingStatus
}

export enum FindingStatus {
open = 'Open',
fixed = 'Fixed',
partiallyFixed = 'Partially Fixed',
acknowledged = 'Acknowledged',
deferred = 'Deferred'
}

export enum Condition {
ok = '✓',
warning = '⚠',
problem = 'X'
}

interface ArraySortCallback<TypeOne> {
(param1: TypeOne, param2: TypeOne): number
}
Expand Down Expand Up @@ -163,32 +153,32 @@ export const calculateCondition = (
remediation: FindingStatus,
totalRisk: string
): Condition => {
if (remediation === FindingStatus.fixed) {
return Condition.ok
if (remediation === FINDING_STATUS.fixed) {
return CONDITIONS.ok
}
if (totalRisk === HIGH || totalRisk === MEDIUM) {
if (remediation === FindingStatus.open) {
return Condition.problem
if (remediation === FINDING_STATUS.open) {
return CONDITIONS.problem
}
return Condition.warning
return CONDITIONS.warning
}
if (remediation === FindingStatus.partiallyFixed) {
return Condition.ok
if (remediation === FINDING_STATUS.partiallyFixed) {
return CONDITIONS.ok
}
return Condition.warning
return CONDITIONS.warning
}

const NEW_FINDING_MODEL = {
[ID]: createFindingId(),
[LIKELIHOOD_KEY]: HIGH,
[IMPACT_KEY]: HIGH,
[TITLE]: 'Untitled Finding',
[REMEDIATION]: FindingStatus.open,
[REMEDIATION]: FINDING_STATUS.open,
[LOCATION]: ''
}

export const parseFinding = (data: FindingMetadata) => {
data[REMEDIATION] = data[REMEDIATION] || FindingStatus.open
data[REMEDIATION] = data[REMEDIATION] || FINDING_STATUS.open
const { impact, likelihood, risk } = calculateTotalRisk(data)
const condition = calculateCondition(data[REMEDIATION], risk)
return sortFindingFields(
Expand Down Expand Up @@ -297,12 +287,12 @@ export const FINDING_LIST_TITLES = findingFields.reduce(
)

export const FINDING_RESUME_RISKS = [HIGH, MEDIUM, LOW]
export const FINDING_RESUME_FIELDS: string[] = Object.values(FindingStatus)
export const FINDING_RESUME_FIELDS: string[] = Object.values(FINDING_STATUS)
.map((s) => s.toString())
.concat([REPORTED])
export const MANDATORY_RESUME_FIELDS = [
FindingStatus.open.toString(),
FindingStatus.fixed.toString(),
FINDING_STATUS.open.toString(),
FINDING_STATUS.fixed.toString(),
REPORTED
]

Expand All @@ -327,8 +317,8 @@ export const getFindingResume = (findings: any[]) => {
[TOTAL]: total,
[REPORTED]: total,
...grouped,
[FIXED_PERCENT]: grouped[FindingStatus.fixed]
? `${Math.ceil((grouped[FindingStatus.fixed] * 100) / total)}%`
[FIXED_PERCENT]: grouped[FINDING_STATUS.fixed]
? `${Math.ceil((grouped[FINDING_STATUS.fixed] * 100) / total)}%`
: NONE
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,21 @@ export const CODE_MARK = '```'
export const TECH_BITS = 'tech-bits'
export const ALLOWED_INFO_IMPACT = [RECOMMENTATION, BUG, WARNING]
export const DEFAULT_INFO_IMPACT = RECOMMENTATION

export const FINDING_STATUS = {
open: 'Open',
fixed: 'Fixed',
partiallyFixed: 'Partially Fixed',
acknowledged: 'Acknowledged',
deferred: 'Deferred'
} as const

export type FindingStatus = (typeof FINDING_STATUS)[keyof typeof FINDING_STATUS]

export const CONDITIONS = {
ok: '✓',
warning: '⚠',
problem: 'X'
} as const

export type Condition = (typeof CONDITIONS)[keyof typeof CONDITIONS]
87 changes: 44 additions & 43 deletions src/test/findings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ import {
parseFindingId,
getFindingResume,
getFindingResumeData,
FindingStatus,
FINDING_MODEL,
sortFindingFields,
Condition,
isAllowedInfoImpact
} from '../Findings'
import {
Expand All @@ -33,7 +31,10 @@ import {
REPORTED,
RESOLUTION,
SORTED_FINDING_FIELDS,
DEFAULT_INFO_IMPACT
DEFAULT_INFO_IMPACT,
FINDING_STATUS,
CONDITION,
CONDITIONS
} from '../constants'
import { createMdBlock, isMdBlock, MdBlock, MdDoc, mdDocToMd } from '../mdModel'
import { arrayUnique } from '../utils'
Expand Down Expand Up @@ -85,8 +86,8 @@ describe('findings', () => {
expect(finding[LIKELIHOOD_KEY]).toBe(NONE)
expect(finding[IMPACT_KEY]).toBe(DEFAULT_INFO_IMPACT)
expect(finding[RISK_KEY]).toBe(INFO)
expect(finding[RESOLUTION]).toBe(FindingStatus.open)
expect(finding[STATUS]).toBe(Condition.warning)
expect(finding[RESOLUTION]).toBe(FINDING_STATUS.open)
expect(finding[STATUS]).toBe(CONDITIONS.warning)
})

it('should return finding default values for INFO findings', () => {
Expand All @@ -95,8 +96,8 @@ describe('findings', () => {
expect(finding[LIKELIHOOD_KEY]).toBe(NONE)
expect(finding[IMPACT_KEY]).toBe(DEFAULT_INFO_IMPACT)
expect(finding[RISK_KEY]).toBe(INFO)
expect(finding[RESOLUTION]).toBe(FindingStatus.open)
expect(finding[STATUS]).toBe(Condition.warning)
expect(finding[RESOLUTION]).toBe(FINDING_STATUS.open)
expect(finding[STATUS]).toBe(CONDITIONS.warning)
})
})

Expand Down Expand Up @@ -319,57 +320,57 @@ describe('findings', () => {

describe('getfindingResumeData', () => {
const data = [
[HIGH, HIGH, FindingStatus.fixed],
[MEDIUM, MEDIUM, FindingStatus.fixed],
[LOW, LOW, FindingStatus.fixed],
[HIGH, HIGH, FindingStatus.acknowledged],
[MEDIUM, MEDIUM, FindingStatus.acknowledged],
[LOW, LOW, FindingStatus.acknowledged],
[HIGH, HIGH, FindingStatus.deferred],
[MEDIUM, MEDIUM, FindingStatus.deferred],
[LOW, LOW, FindingStatus.deferred],
[HIGH, HIGH, FindingStatus.open],
[MEDIUM, MEDIUM, FindingStatus.open],
[LOW, LOW, FindingStatus.open],
[HIGH, HIGH, FindingStatus.partiallyFixed],
[MEDIUM, MEDIUM, FindingStatus.partiallyFixed],
[LOW, LOW, FindingStatus.partiallyFixed]
[HIGH, HIGH, FINDING_STATUS.fixed],
[MEDIUM, MEDIUM, FINDING_STATUS.fixed],
[LOW, LOW, FINDING_STATUS.fixed],
[HIGH, HIGH, FINDING_STATUS.acknowledged],
[MEDIUM, MEDIUM, FINDING_STATUS.acknowledged],
[LOW, LOW, FINDING_STATUS.acknowledged],
[HIGH, HIGH, FINDING_STATUS.deferred],
[MEDIUM, MEDIUM, FINDING_STATUS.deferred],
[LOW, LOW, FINDING_STATUS.deferred],
[HIGH, HIGH, FINDING_STATUS.open],
[MEDIUM, MEDIUM, FINDING_STATUS.open],
[LOW, LOW, FINDING_STATUS.open],
[HIGH, HIGH, FINDING_STATUS.partiallyFixed],
[MEDIUM, MEDIUM, FINDING_STATUS.partiallyFixed],
[LOW, LOW, FINDING_STATUS.partiallyFixed]
].map(([likelihood, impact, resolution]) => {
return { likelihood, impact, resolution }
})
const findings = data.map((f: any) => parseFinding(f))

const resumeData = getFindingResumeData(findings)
it(`should return [${FindingStatus.open}] findings`, () => {
expect(resumeData[FindingStatus.open]).toStrictEqual({
it(`should return [${FINDING_STATUS.open}] findings`, () => {
expect(resumeData[FINDING_STATUS.open]).toStrictEqual({
[HIGH]: 1,
[MEDIUM]: 1,
[LOW]: 1
})
})
it(`should return [${FindingStatus.acknowledged}] findings`, () => {
expect(resumeData[FindingStatus.acknowledged]).toStrictEqual({
it(`should return [${FINDING_STATUS.acknowledged}] findings`, () => {
expect(resumeData[FINDING_STATUS.acknowledged]).toStrictEqual({
[HIGH]: 1,
[MEDIUM]: 1,
[LOW]: 1
})
})
it(`should return [${FindingStatus.deferred}] findings`, () => {
expect(resumeData[FindingStatus.deferred]).toStrictEqual({
it(`should return [${FINDING_STATUS.deferred}] findings`, () => {
expect(resumeData[FINDING_STATUS.deferred]).toStrictEqual({
[HIGH]: 1,
[MEDIUM]: 1,
[LOW]: 1
})
})
it(`should return [${FindingStatus.fixed}] findings`, () => {
expect(resumeData[FindingStatus.fixed]).toStrictEqual({
it(`should return [${FINDING_STATUS.fixed}] findings`, () => {
expect(resumeData[FINDING_STATUS.fixed]).toStrictEqual({
[HIGH]: 1,
[MEDIUM]: 1,
[LOW]: 1
})
})
it(`should return [${FindingStatus.partiallyFixed}] findings`, () => {
expect(resumeData[FindingStatus.partiallyFixed]).toStrictEqual({
it(`should return [${FINDING_STATUS.partiallyFixed}] findings`, () => {
expect(resumeData[FINDING_STATUS.partiallyFixed]).toStrictEqual({
[HIGH]: 1,
[MEDIUM]: 1,
[LOW]: 1
Expand All @@ -385,36 +386,36 @@ describe('findings', () => {
})

describe('getfindingResumeDataWithCeroFields', () => {
const data = [[HIGH, HIGH, FindingStatus.partiallyFixed]].map(
const data = [[HIGH, HIGH, FINDING_STATUS.partiallyFixed]].map(
([likelihood, impact, resolution]) => {
return { likelihood, impact, resolution }
}
)
const findings = data.map((f: any) => parseFinding(f))

const resumeData = getFindingResumeData(findings)
it(`should return [${FindingStatus.open}] findings`, () => {
expect(resumeData[FindingStatus.open]).toStrictEqual({
it(`should return [${FINDING_STATUS.open}] findings`, () => {
expect(resumeData[FINDING_STATUS.open]).toStrictEqual({
[HIGH]: 0,
[MEDIUM]: 0,
[LOW]: 0
})
})
it(`should return no [${FindingStatus.acknowledged}] findings`, () => {
expect(resumeData[FindingStatus.acknowledged]).toBeUndefined()
it(`should return no [${FINDING_STATUS.acknowledged}] findings`, () => {
expect(resumeData[FINDING_STATUS.acknowledged]).toBeUndefined()
})
it(`should return no [${FindingStatus.deferred}] findings`, () => {
expect(resumeData[FindingStatus.deferred]).toBeUndefined()
it(`should return no [${FINDING_STATUS.deferred}] findings`, () => {
expect(resumeData[FINDING_STATUS.deferred]).toBeUndefined()
})
it(`should return [${FindingStatus.fixed}] findings`, () => {
expect(resumeData[FindingStatus.fixed]).toStrictEqual({
it(`should return [${FINDING_STATUS.fixed}] findings`, () => {
expect(resumeData[FINDING_STATUS.fixed]).toStrictEqual({
[HIGH]: 0,
[MEDIUM]: 0,
[LOW]: 0
})
})
it(`should return [${FindingStatus.partiallyFixed}] findings`, () => {
expect(resumeData[FindingStatus.partiallyFixed]).toStrictEqual({
it(`should return [${FINDING_STATUS.partiallyFixed}] findings`, () => {
expect(resumeData[FINDING_STATUS.partiallyFixed]).toStrictEqual({
[HIGH]: 1,
[MEDIUM]: 0,
[LOW]: 0
Expand Down

0 comments on commit 11c9206

Please sign in to comment.