From 432d3554cbf3113d44102df01cd84ffa771eb233 Mon Sep 17 00:00:00 2001 From: Souvik Mukhopadhyay Date: Tue, 12 Nov 2024 09:23:18 +0530 Subject: [PATCH] secret-scan: Add test coverage fix #521 --- .github/workflows/validate-secret-scan.yaml | 1 + codecov.yml | 9 + packages/secret-scan/.mocharc.json | 5 - packages/secret-scan/jest.config.ts | 20 ++ packages/secret-scan/package.json | 4 +- packages/secret-scan/src/test/secret.test.ts | 209 ++++++++++++------- packages/secret-scan/tsconfig.json | 2 +- packages/secret-scan/tsconfig.spec.json | 14 ++ 8 files changed, 175 insertions(+), 89 deletions(-) delete mode 100644 packages/secret-scan/.mocharc.json create mode 100644 packages/secret-scan/jest.config.ts create mode 100644 packages/secret-scan/tsconfig.spec.json diff --git a/.github/workflows/validate-secret-scan.yaml b/.github/workflows/validate-secret-scan.yaml index e9505b6b..dacc5ce0 100644 --- a/.github/workflows/validate-secret-scan.yaml +++ b/.github/workflows/validate-secret-scan.yaml @@ -66,3 +66,4 @@ jobs: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} with: flags: secret-scan + files: /coverage/packages/secret-scan/coverage-final.json diff --git a/codecov.yml b/codecov.yml index f3f97c65..07957f0b 100644 --- a/codecov.yml +++ b/codecov.yml @@ -52,3 +52,12 @@ flag_management: target: 80% - type: patch target: 98% + - name: secret-scan + paths: + - packages/secret-scan/ + carryforward: true + statuses: + - type: project + target: 80% + - type: patch + target: 98% \ No newline at end of file diff --git a/packages/secret-scan/.mocharc.json b/packages/secret-scan/.mocharc.json deleted file mode 100644 index f2b52bdf..00000000 --- a/packages/secret-scan/.mocharc.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "require": "tsx", - "spec": "src/test/**/*.ts", - "extension": ["ts"] -} \ No newline at end of file diff --git a/packages/secret-scan/jest.config.ts b/packages/secret-scan/jest.config.ts new file mode 100644 index 00000000..b488b080 --- /dev/null +++ b/packages/secret-scan/jest.config.ts @@ -0,0 +1,20 @@ +import type { Config } from 'jest' + +const config: Config = { + displayName: 'secret scan', + testMatch: ['**/*.test.ts'], + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }] + }, + moduleFileExtensions: ['ts', 'js', 'html'], + preset: 'ts-jest', + testEnvironment: 'node', + moduleNameMapper: { + '^@/(.*)$': '/src/$1' + }, + coverageDirectory: '../../coverage/packages/secret-scan', + coverageReporters: ['json'], + collectCoverage: true +} + +export default config \ No newline at end of file diff --git a/packages/secret-scan/package.json b/packages/secret-scan/package.json index 6d5d0583..a4243e7b 100644 --- a/packages/secret-scan/package.json +++ b/packages/secret-scan/package.json @@ -6,7 +6,7 @@ "scripts": { "genKey": "tsx src/generateKey.ts", "build": "tsup", - "test": "mocha", + "test": "jest", "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", "lint": "eslint \"{src,tests}/**/*.ts\" --fix" }, @@ -17,12 +17,10 @@ ".": "./dist/index.js" }, "dependencies": { - "mocha": "^10.6.0", "randexp": "^0.5.3", "tsx": "^4.16.2" }, "devDependencies": { - "@types/mocha": "^10.0.7", "@types/node": "^20.14.10", "eslint-config-standard-with-typescript": "^43.0.1", "jest": "^29.5.0", diff --git a/packages/secret-scan/src/test/secret.test.ts b/packages/secret-scan/src/test/secret.test.ts index e04fb1e3..ae01fba9 100644 --- a/packages/secret-scan/src/test/secret.test.ts +++ b/packages/secret-scan/src/test/secret.test.ts @@ -1,5 +1,3 @@ -import { describe, it } from 'mocha' -import assert = require('assert') import { algolia, alibaba, @@ -71,237 +69,288 @@ import { import type { TestCase } from '@/types' import secretDetector from '@/index' -const testcaseTitleTemplate = (title: string) => `should detect ${title}` +const testcaseTitleTemplate = (title: string): string => + `should detect ${title}` -function testSecret(testcase: TestCase[]) { - testcase.forEach(({ input, expected }, index) => { +function testSecret(testcases: TestCase[]): void { + testcases.forEach(({ input, expected }, index) => { const result = secretDetector.detect(input) - assert.equal( - result.found, - expected, - `(i=${index}) For Input: ${input}, expected ${expected} but got ${result.found}` - ) + expect(result.found).toBe(expected) }) } describe('Detect Secrets from string', () => { - it(testcaseTitleTemplate('Private keys'), () => { + test(testcaseTitleTemplate('Private keys'), () => { testSecret(private_key.testcases) }) - it(testcaseTitleTemplate('OpenAI keys'), () => { + test(testcaseTitleTemplate('OpenAI keys'), () => { testSecret(openAI.testcases) }) - it(testcaseTitleTemplate('PyPi keys'), () => { + test(testcaseTitleTemplate('PyPi keys'), () => { testSecret(pypi.testcases) }) - it(testcaseTitleTemplate('Sendgrid keys'), () => { + test(testcaseTitleTemplate('Sendgrid keys'), () => { testSecret(sendgrid.testcases) }) - it(testcaseTitleTemplate('Square OAuth keys'), () => { + test(testcaseTitleTemplate('Square OAuth keys'), () => { testSecret(square_OAuth.testcases) }) - it(testcaseTitleTemplate('Stripe keys'), () => { + test(testcaseTitleTemplate('Stripe keys'), () => { testSecret(stripe.testcases) }) - it(testcaseTitleTemplate('Telegram token'), () => { + test(testcaseTitleTemplate('Telegram token'), () => { testSecret(telegram_token.testcases) }) - it(testcaseTitleTemplate('Twilio keys'), () => { + test(testcaseTitleTemplate('Twilio keys'), () => { testSecret(twilio.testcases) }) - it(testcaseTitleTemplate('NPM keys'), () => { + test(testcaseTitleTemplate('NPM keys'), () => { testSecret(npm.testcases) }) - it(testcaseTitleTemplate('Mailchimp keys'), () => { + test(testcaseTitleTemplate('Mailchimp keys'), () => { testSecret(mailchimp.testcases) }) - // ! not working for seperated by dots, saying true to all - // it(testcaseTitleTemplate("JWT Key"), () => { + // Uncomment and add logic if this functionality is restored + // test(testcaseTitleTemplate("JWT Key"), () => { // testSecret(jwt.testcases); // }); - it(testcaseTitleTemplate('Public IP'), () => { + test(testcaseTitleTemplate('Public IP'), () => { testSecret(ip_public.testcases) }) - it(testcaseTitleTemplate('Github Token'), () => { + test(testcaseTitleTemplate('Github Token'), () => { testSecret(github.testcases) }) - it(testcaseTitleTemplate('Discord Token'), () => { + test(testcaseTitleTemplate('Discord Token'), () => { testSecret(discord.testcases) }) - it(testcaseTitleTemplate('Artifactory Token'), () => { + + test(testcaseTitleTemplate('Artifactory Token'), () => { testSecret(artifactory.testcases) }) - it(testcaseTitleTemplate('AWS Token'), () => { + + test(testcaseTitleTemplate('AWS Token'), () => { testSecret(aws.testcases) }) - it(testcaseTitleTemplate('Algolia Token'), () => { + + test(testcaseTitleTemplate('Algolia Token'), () => { testSecret(algolia.testcases) }) - it(testcaseTitleTemplate('Alibaba Key'), () => { + + test(testcaseTitleTemplate('Alibaba Key'), () => { testSecret(alibaba.testcases) }) - it(testcaseTitleTemplate('Adafruit Key'), () => { + + test(testcaseTitleTemplate('Adafruit Key'), () => { testSecret(adafruit.testcases) }) - it(testcaseTitleTemplate('Adobe Key'), () => { + + test(testcaseTitleTemplate('Adobe Key'), () => { testSecret(adobe.testcases) }) - it(testcaseTitleTemplate('Age Key'), () => { + + test(testcaseTitleTemplate('Age Key'), () => { testSecret(age.testcases) }) - it(testcaseTitleTemplate('Airtable Key'), () => { + + test(testcaseTitleTemplate('Airtable Key'), () => { testSecret(airtable.testcases) }) - it(testcaseTitleTemplate('Asana Key'), () => { + + test(testcaseTitleTemplate('Asana Key'), () => { testSecret(asana.testcases) }) - it(testcaseTitleTemplate('Atlassian Key'), () => { + + test(testcaseTitleTemplate('Atlassian Key'), () => { testSecret(atlassian.testcases) }) - it(testcaseTitleTemplate('Authress Key'), () => { + + test(testcaseTitleTemplate('Authress Key'), () => { testSecret(authress.testcases) }) - it(testcaseTitleTemplate('Beamer Key'), () => { + + test(testcaseTitleTemplate('Beamer Key'), () => { testSecret(beamer.testcases) }) - it(testcaseTitleTemplate('Bitbucket Key'), () => { + + test(testcaseTitleTemplate('Bitbucket Key'), () => { testSecret(bitbucket.testcases) }) - it(testcaseTitleTemplate('Bittrex Key'), () => { + + test(testcaseTitleTemplate('Bittrex Key'), () => { testSecret(bittrex.testcases) }) - it(testcaseTitleTemplate('Clojars Key'), () => { + + test(testcaseTitleTemplate('Clojars Key'), () => { testSecret(clojars.testcases) }) - it(testcaseTitleTemplate('Codecov Key'), () => { + test(testcaseTitleTemplate('Codecov Key'), () => { testSecret(codecov.testcases) }) - it(testcaseTitleTemplate('Coinbase Key'), () => { + + test(testcaseTitleTemplate('Coinbase Key'), () => { testSecret(coinbase.testcases) }) - it(testcaseTitleTemplate('Confluent Key'), () => { + + test(testcaseTitleTemplate('Confluent Key'), () => { testSecret(confluent.testcases) }) - it(testcaseTitleTemplate('Contentful Key'), () => { + + test(testcaseTitleTemplate('Contentful Key'), () => { testSecret(contentful.testcases) }) - it(testcaseTitleTemplate('Databricks Key'), () => { + + test(testcaseTitleTemplate('Databricks Key'), () => { testSecret(databricks.testcases) }) - it(testcaseTitleTemplate('Datadog Key'), () => { + + test(testcaseTitleTemplate('Datadog Key'), () => { testSecret(datadog.testcases) }) - it(testcaseTitleTemplate('Defined Networking Key'), () => { + + test(testcaseTitleTemplate('Defined Networking Key'), () => { testSecret(definednetworking.testcases) }) - it(testcaseTitleTemplate('Digital Ocean Key'), () => { + + test(testcaseTitleTemplate('Digital Ocean Key'), () => { testSecret(digitalocean.testcases) }) - it(testcaseTitleTemplate('Doppler Key'), () => { + + test(testcaseTitleTemplate('Doppler Key'), () => { testSecret(doppler.testcases) }) - it(testcaseTitleTemplate('Dropbox Key'), () => { + + test(testcaseTitleTemplate('Dropbox Key'), () => { testSecret(dropbox.testcases) }) - it(testcaseTitleTemplate('Duffel Key'), () => { + + test(testcaseTitleTemplate('Duffel Key'), () => { testSecret(duffel.testcases) }) - it(testcaseTitleTemplate('Dynatrace Key'), () => { + + test(testcaseTitleTemplate('Dynatrace Key'), () => { testSecret(dynatrace.testcases) }) - it(testcaseTitleTemplate('Easypost Key'), () => { + + test(testcaseTitleTemplate('Easypost Key'), () => { testSecret(easypost.testcases) }) - it(testcaseTitleTemplate('Facebook Key'), () => { + + test(testcaseTitleTemplate('Facebook Key'), () => { testSecret(facebook.testcases) }) - it(testcaseTitleTemplate('Flutterwave Key'), () => { + + test(testcaseTitleTemplate('Flutterwave Key'), () => { testSecret(flutterwave.testcases) }) - it(testcaseTitleTemplate('Frameio Key'), () => { + + test(testcaseTitleTemplate('Frameio Key'), () => { testSecret(frameio.testcases) }) - it(testcaseTitleTemplate('Gitlab Key'), () => { + + test(testcaseTitleTemplate('Gitlab Key'), () => { testSecret(gitlab.testcases) }) - it(testcaseTitleTemplate('Grafana Key'), () => { + + test(testcaseTitleTemplate('Grafana Key'), () => { testSecret(grafana.testcases) }) - it(testcaseTitleTemplate('Harness Key'), () => { + + test(testcaseTitleTemplate('Harness Key'), () => { testSecret(harness.testcases) }) - it(testcaseTitleTemplate('Hashicorp Key'), () => { + + test(testcaseTitleTemplate('Hashicorp Key'), () => { testSecret(hashicorp.testcases) }) - it(testcaseTitleTemplate('Heroku Key'), () => { + + test(testcaseTitleTemplate('Heroku Key'), () => { testSecret(heroku.testcases) }) - it(testcaseTitleTemplate('Hubspot Key'), () => { + + test(testcaseTitleTemplate('Hubspot Key'), () => { testSecret(hubspot.testcases) }) - it(testcaseTitleTemplate('Huggingface Key'), () => { + + test(testcaseTitleTemplate('Huggingface Key'), () => { testSecret(huggingface.testcases) }) - it(testcaseTitleTemplate('Infracost Key'), () => { + + test(testcaseTitleTemplate('Infracost Key'), () => { testSecret(infracost.testcases) }) - it(testcaseTitleTemplate('Intra42 Key'), () => { + + test(testcaseTitleTemplate('Intra42 Key'), () => { testSecret(intra42.testcases) }) - // it(testcaseTitleTemplate('Kubernetes Key'), () => { - // testSecret(kubernetes.testcases) + + // Uncomment if needed in the future + // test(testcaseTitleTemplate('Kubernetes Key'), () => { + // testSecret(kubernetes.testcases); // }); - it(testcaseTitleTemplate('Linear Key'), () => { + + test(testcaseTitleTemplate('Linear Key'), () => { testSecret(linear.testcases) }) - it(testcaseTitleTemplate('Lob Key'), () => { + + test(testcaseTitleTemplate('Lob Key'), () => { testSecret(lob.testcases) }) - it(testcaseTitleTemplate('Planetscale Key'), () => { + + test(testcaseTitleTemplate('Planetscale Key'), () => { testSecret(planetscale.testcases) }) - it(testcaseTitleTemplate('Postman Key'), () => { + + test(testcaseTitleTemplate('Postman Key'), () => { testSecret(postman.testcases) }) - it(testcaseTitleTemplate('Prefect Key'), () => { + + test(testcaseTitleTemplate('Prefect Key'), () => { testSecret(prefect.testcases) }) - it(testcaseTitleTemplate('Pulumi Key'), () => { + + test(testcaseTitleTemplate('Pulumi Key'), () => { testSecret(pulumi.testcases) }) - it(testcaseTitleTemplate('Readme Key'), () => { + + test(testcaseTitleTemplate('Readme Key'), () => { testSecret(readme.testcases) }) - it(testcaseTitleTemplate('Rubygems Key'), () => { + + test(testcaseTitleTemplate('Rubygems Key'), () => { testSecret(rubygems.testcases) }) - it(testcaseTitleTemplate('Scalingo Key'), () => { + + test(testcaseTitleTemplate('Scalingo Key'), () => { testSecret(scalingo.testcases) }) - it(testcaseTitleTemplate('Sendinblue Key'), () => { + + test(testcaseTitleTemplate('Sendinblue Key'), () => { testSecret(sendinblue.testcases) }) - it(testcaseTitleTemplate('Shippo Key'), () => { + + test(testcaseTitleTemplate('Shippo Key'), () => { testSecret(shippo.testcases) }) - it(testcaseTitleTemplate('Shopify Key'), () => { + + test(testcaseTitleTemplate('Shopify Key'), () => { testSecret(shopify.testcases) }) - it(testcaseTitleTemplate('Sidekiq Key'), () => { + + test(testcaseTitleTemplate('Sidekiq Key'), () => { testSecret(sidekiq.testcases) }) }) diff --git a/packages/secret-scan/tsconfig.json b/packages/secret-scan/tsconfig.json index c110615b..e5ad3943 100644 --- a/packages/secret-scan/tsconfig.json +++ b/packages/secret-scan/tsconfig.json @@ -1,4 +1,5 @@ { + "extends": "../tsconfig/base.json", "compilerOptions": { "module": "commonjs", "declaration": true, @@ -7,7 +8,6 @@ "experimentalDecorators": true, "allowSyntheticDefaultImports": true, "target": "ES2021", - "sourceMap": true, "outDir": "./dist", "baseUrl": ".", "paths": { diff --git a/packages/secret-scan/tsconfig.spec.json b/packages/secret-scan/tsconfig.spec.json new file mode 100644 index 00000000..ab41d837 --- /dev/null +++ b/packages/secret-scan/tsconfig.spec.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "dist/out-tsc", + "module": "ES2022", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] + } \ No newline at end of file