Skip to content

Commit

Permalink
Merge pull request #50 from fingerprintjs/feature/unit-tests
Browse files Browse the repository at this point in the history
Feature/unit tests
  • Loading branch information
Sergey Shelomentsev authored May 3, 2023
2 parents b75c185 + 00cfe83 commit 5f8e172
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = {
'linebreak-style': ['error', 'unix'],
'prefer-const': 'error',
'prettier/prettier': 'error',
'@typescript-eslint/no-unused-vars': ['error'],
'@typescript-eslint/no-unused-vars': ['error', { argsIgnorePattern: '^_' }],
curly: [2, 'all'],
},
}
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ module.exports = {
collectCoverageFrom: [
'./proxy/**/**.ts',
'./management/**/**.ts',
'./proxy/handlers/status.ts',
'!**/index.ts',
'!**/errors/**',
'!**/config.ts',
'!**/env.ts',
'!**/handlers/**',
'!**/proxy/app.ts',
'!**/management/app.ts',
],
coverageReporters: ['lcov', 'json-summary', ['text', { file: 'coverage.txt', path: './' }]],
transform: {
Expand Down
16 changes: 16 additions & 0 deletions proxy/errors/integrarionError.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { IntegrationError } from './IntegrationError'

describe('check integration error result', () => {
it('check if error return valid body', async () => {
const json = {
vendor: 'Fingerprint Pro Azure Function',
message: 'Download failed',
path: 'fpjs/agent',
}

const message: IntegrationError = new IntegrationError(json.message, json.path)
const body = message.toBody()

expect(body).toBe(JSON.stringify(json))
})
})
162 changes: 162 additions & 0 deletions proxy/handlers/status.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { CustomerVariables } from '../customer-variables/CustomerVariables'
import { CustomerVariableType } from '../customer-variables/types'
import { EnvCustomerVariables } from '../customer-variables/EnvCustomerVariables'
import { handleStatus } from './status'
import { HttpRequest, Form, FormPart } from '@azure/functions'

const fp: FormPart = {
value: Buffer.from(''),
}
const form: Form = {
get: (_: string) => fp,
getAll: (_: string) => [fp],
has: (_: string) => true,
length: 0,
*[Symbol.iterator]() {},
}
const req: HttpRequest = {
method: 'GET',
url: 'https://fp.domain.com/fpjs/status',
headers: {},
query: {},
params: {},
user: null,
get: (x) => x,
parseFormBody: () => form,
}

function removeNonce(body: string): string {
const nonceParam = " nonce='"
const begin = body.indexOf(nonceParam)
const end = body.indexOf("'", begin + nonceParam.length) + 1

return body.replace(body.substring(begin, end), '')
}

describe('Handle status', () => {
it('returns correct status info in html if all variables are set', async () => {
const getHeaderCustomerVariables = (env: typeof process.env) =>
new CustomerVariables([new EnvCustomerVariables(env)])
const env = {
[CustomerVariableType.AgentDownloadPath]: 'qwertyt',
[CustomerVariableType.RoutePrefix]: 'dsgfkjdfs',
[CustomerVariableType.GetResultPath]: 'fdgvdsfgfds',
[CustomerVariableType.PreSharedSecret]: 'aadddddd',
}
const customerVariables = getHeaderCustomerVariables(env)

const result = await handleStatus({
httpRequest: req,
customerVariables: customerVariables,
})

expect(removeNonce(result.body)).toMatchInlineSnapshot(`
"
<html lang='en-US'>
<head>
<title>Fingerprint Pro Azure integration status</title>
<meta charset='utf-8'>
<style>
body, .env-info {
display: flex;
}
.env-info {
flex-direction: column;
}
body {
flex-direction: column;
align-items: center;
}
body > * {
margin-bottom: 1em;
}
</style>
</head>
<body>
<h1>Fingerprint Pro Azure integration status</h1>
<div>
Fingerprint Pro Azure Function App version: __azure_function_version__
</div>
<div>
✅ All environment variables are set
</div>
<span>
Please reach out our support via <a href='mailto:[email protected]'>[email protected]</a> if you have any issues
</span>
</body>
</html>
"
`)
})

it('returns correct status info in html if some variables are using default values', async () => {
const getHeaderCustomerVariables = (env: typeof process.env) =>
new CustomerVariables([new EnvCustomerVariables(env)])
const customerVariables = getHeaderCustomerVariables({})

const result = await handleStatus({
httpRequest: req,
customerVariables: customerVariables,
})

expect(removeNonce(result.body)).toMatchInlineSnapshot(`
"
<html lang='en-US'>
<head>
<title>Fingerprint Pro Azure integration status</title>
<meta charset='utf-8'>
<style>
body, .env-info {
display: flex;
}
.env-info {
flex-direction: column;
}
body {
flex-direction: column;
align-items: center;
}
body > * {
margin-bottom: 1em;
}
</style>
</head>
<body>
<h1>Fingerprint Pro Azure integration status</h1>
<div>
Fingerprint Pro Azure Function App version: __azure_function_version__
</div>
<div class='env-info'>
<div class='env-info-item'>
⚠️ <strong>fpjs_route_prefix </strong> is not defined and uses default value
</div>
<div class='env-info-item'>
⚠️ <strong>fpjs_get_result_path </strong> is not defined and uses default value
</div>
<div class='env-info-item'>
⚠️ <strong>fpjs_pre_shared_secret </strong> is not defined
</div>
<div class='env-info-item'>
⚠️ <strong>fpjs_agent_download_path </strong> is not defined and uses default value
</div>
</div>
<span>
Please reach out our support via <a href='mailto:[email protected]'>[email protected]</a> if you have any issues
</span>
</body>
</html>
"
`)
})
})
6 changes: 3 additions & 3 deletions proxy/handlers/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,16 @@ function renderHtml({ version, envInfo }: StatusInfo) {
body, .env-info {
display: flex;
}
.env-info {
flex-direction: column;
}
body {
flex-direction: column;
align-items: center;
}
body > * {
margin-bottom: 1em;
}
Expand Down

0 comments on commit 5f8e172

Please sign in to comment.