Skip to content

Commit e045226

Browse files
committed
Added tests for the scanText() method
1 parent afc0a02 commit e045226

File tree

7 files changed

+3241
-12
lines changed

7 files changed

+3241
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/coverage
12
/dist
23
/node_modules
34
tsconfig.tsbuildinfo

jest.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/** @type {import('ts-jest/dist/types').InitialOptionsTsJest} */
2+
module.exports = {
3+
preset: 'ts-jest',
4+
testEnvironment: 'node',
5+
};

package-lock.json

Lines changed: 3119 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,18 @@
1010
"scripts": {
1111
"build": "tsc",
1212
"prepare": "npm run build",
13-
"test": "echo \"Error: no test specified\" && exit 1"
13+
"test": "jest --coverage"
1414
},
1515
"keywords": [],
1616
"author": "Nagarjun Palavalli <nag@nightfall.ai>",
1717
"license": "ISC",
1818
"dependencies": {
19-
"axios": "^0.21.4",
20-
"typescript": "^4.4.3"
19+
"axios": "^0.21.4"
20+
},
21+
"devDependencies": {
22+
"@types/jest": "^27.0.2",
23+
"jest": "^27.3.1",
24+
"ts-jest": "^27.0.7",
25+
"typescript": "^4.4.4"
2126
}
2227
}

src/tests/mocks.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { Detector, ScanText } from "../types"
2+
3+
// Requests
4+
export const creditCardPayload = ['My credit card number is 4242424242424242']
5+
export const creditCardConfig: ScanText.RequestConfig = {
6+
detectionRules: [
7+
{
8+
name: 'Find credit cards',
9+
logicalOp: 'ANY',
10+
detectors: [
11+
{
12+
detectorType: Detector.Type.Nightfall,
13+
nightfallDetector: 'CREDIT_CARD_NUMBER',
14+
minNumFindings: 1,
15+
minConfidence: Detector.Confidence.Likely,
16+
displayName: 'Credit Card Number'
17+
}
18+
],
19+
}
20+
]
21+
}
22+
23+
// Responses
24+
export const errorResponse = {
25+
code: 40015,
26+
message: 'Invalid Request',
27+
description: "detectionRuleUUIDs and detectionRules can't both be empty",
28+
additionalData: {
29+
"InspectRequest.Config.DetectionRuleUUIDs": "at least one of (DetectionRuleUUIDs or DetectionRules) must have length greater than 0",
30+
"InspectRequest.Config.DetectionRules": "at least one of (DetectionRuleUUIDs or DetectionRules) must have length greater than 0"
31+
},
32+
}
33+
34+
export const scanTextResponse = {
35+
"findings": [
36+
[
37+
{
38+
"finding": "4242424242424242",
39+
"detector": {
40+
"name": "Credit Card Number",
41+
"uuid": "74c1815e-c0c3-4df5-8b1e-6cf98864a454"
42+
},
43+
"confidence": "LIKELY",
44+
"location": {
45+
"byteRange": {
46+
"start": 25,
47+
"end": 41
48+
},
49+
"codepointRange": {
50+
"start": 25,
51+
"end": 41
52+
}
53+
},
54+
"matchedDetectionRuleUUIDs": [],
55+
"matchedDetectionRules": [
56+
"Find credit cards"
57+
]
58+
}
59+
]
60+
],
61+
"redactedPayload": [
62+
""
63+
]
64+
}

src/tests/scanText.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Nightfall } from '../nightfall'
2+
import { creditCardConfig, creditCardPayload, errorResponse } from './mocks'
3+
4+
describe('should test the text scanning method', () => {
5+
// Set API key and other dependencies
6+
if (!process.env.NIGHTFALL_API_KEY) {
7+
throw new Error("NIGHTFALL_API_KEY environment variable is required")
8+
}
9+
10+
const apiKey = process.env.NIGHTFALL_API_KEY
11+
12+
// Run tests
13+
it('should create a new nightfall client and check if the scanText method exists', () => {
14+
const client = new Nightfall(apiKey)
15+
16+
expect(client).toBeDefined()
17+
expect(typeof client.scanText).toBe('function')
18+
})
19+
20+
it('should return an error if the request was configured incorrectly', async () => {
21+
const client = new Nightfall(apiKey)
22+
const scanTextSpy = jest.spyOn(client, 'scanText')
23+
24+
const response = await client.scanText(creditCardPayload, {})
25+
expect(scanTextSpy).toHaveBeenCalledWith(creditCardPayload, {})
26+
expect(response.data).toBeUndefined()
27+
expect(response.isError).toBe(true)
28+
const error = response.getError()
29+
expect(error).toEqual(errorResponse)
30+
})
31+
32+
it('should return findings', async () => {
33+
const client = new Nightfall(apiKey)
34+
const scanTextSpy = jest.spyOn(client, 'scanText')
35+
36+
const response = await client.scanText(creditCardPayload, creditCardConfig)
37+
expect(scanTextSpy).toHaveBeenCalledWith(creditCardPayload, creditCardConfig)
38+
expect(response.isError).toBe(false)
39+
expect(response.data).toBeDefined()
40+
expect(response.data).toHaveProperty('findings')
41+
expect(response.data?.findings).toHaveLength(1)
42+
})
43+
})

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
"sourceMap": true,
1515
"rootDir": "src"
1616
},
17-
"exclude": ["dist", "node_modules"]
17+
"exclude": ["dist", "node_modules", "src/tests"]
1818
}

0 commit comments

Comments
 (0)