|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | +import { faker } from '../src'; |
| 3 | +import { seededRuns } from './support/seededRuns'; |
| 4 | + |
| 5 | +const NON_SEEDED_BASED_RUN = 5; |
| 6 | + |
| 7 | +const functionNames = ['cve', 'cwe']; |
| 8 | + |
| 9 | +describe('security', () => { |
| 10 | + afterEach(() => { |
| 11 | + faker.locale = 'en'; |
| 12 | + }); |
| 13 | + |
| 14 | + for (const seed of seededRuns) { |
| 15 | + describe(`seed: ${seed}`, () => { |
| 16 | + for (const functionName of functionNames) { |
| 17 | + it(`${functionName}()`, () => { |
| 18 | + faker.seed(seed); |
| 19 | + |
| 20 | + const actual = faker.security[functionName](); |
| 21 | + expect(actual).toMatchSnapshot(); |
| 22 | + }); |
| 23 | + } |
| 24 | + }); |
| 25 | + } |
| 26 | + |
| 27 | + // Create and log-back the seed for debug purposes |
| 28 | + faker.seed(Math.ceil(Math.random() * 1_000_000_000)); |
| 29 | + |
| 30 | + describe(`random seeded tests for seed ${JSON.stringify( |
| 31 | + faker.seed() |
| 32 | + )}`, () => { |
| 33 | + for (let i = 1; i <= NON_SEEDED_BASED_RUN; i++) { |
| 34 | + describe('cve()', () => { |
| 35 | + it('should return a well formed string', () => { |
| 36 | + expect(faker.security.cve()).toMatch(/^CVE-[0-9]{4}-[0-9]{4}/); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe('cwe()', () => { |
| 41 | + it('should return a well formed string', () => { |
| 42 | + expect(faker.security.cwe()).toMatch(/^CWE-[0-9]{4}/); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('cvss()', () => { |
| 47 | + it('should return an object', () => { |
| 48 | + const cvss = faker.security.cvss(); |
| 49 | + expect(cvss).toBeTypeOf('object'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should return a numeric value', () => { |
| 53 | + expect(faker.security.cvss().score).toEqual(expect.any(Number)); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should return a well formed string', () => { |
| 57 | + expect(faker.security.cvss().vector).toMatch( |
| 58 | + /^CVSS:3.1\/AV:[NALP]\/AC:[LH]\/PR:[NLH]\/UI:[NR]\/S:[UC]\/C:[NLH]\/I:[NLH]\/A:[NLH]/ |
| 59 | + ); |
| 60 | + }); |
| 61 | + }); |
| 62 | + } |
| 63 | + }); |
| 64 | +}); |
0 commit comments