Skip to content

Commit

Permalink
Fix: Validation on Alert Assertion (#1179)
Browse files Browse the repository at this point in the history
* test: add tests

* fix: fix alert validation that did not work

* refactor: move probe request validation to joi

* fix: set default alert for Symon

* fix: handle empty alert assertion

* fix: validate legacy alerts

* refactor: move legacy alert tranformation to Joi
  • Loading branch information
haricnugraha authored Nov 15, 2023
1 parent 1e5cf52 commit a9a2df4
Show file tree
Hide file tree
Showing 2 changed files with 319 additions and 301 deletions.
57 changes: 57 additions & 0 deletions src/components/config/validation/validator/probe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import type { Probe } from '../../../../interfaces/probe'

import { FAILED_REQUEST_ASSERTION } from '../../../../looper'
import { validateProbes } from './probe'
import { resetContext, setContext } from '../../../../../src/context'
import type { MonikaFlags } from '../../../../../src/flag'

describe('Probe validation', () => {
describe('Probe sanitization', () => {
Expand Down Expand Up @@ -156,5 +158,60 @@ describe('Probe validation', () => {
expect(result[0].alerts[0].assertion).eq('')
expect(result[0].alerts[0].message).eq(FAILED_REQUEST_ASSERTION.message)
})

it('should throws an error if alert assertion is invalid', () => {
// arrange
const probe = {
id: 'Example',
requests: [{ url: 'https://example.com' }],
alerts: [{ assertion: 'response.time > 1000 ms' }],
} as unknown as Probe

// act & assert
expect(validateProbes([probe])).to.eventually.throw()
})

it('should set the alerts empty if the probe if alert assertion is invalid in Symon mode', async () => {
// arrange
setContext({
flags: {
symonKey: 'bDF8j',
symonUrl: 'https://example.com',
} as MonikaFlags,
})
const probes = [
{
id: 'Example',
requests: [{ url: 'https://example.com' }],
alerts: [{ assertion: 'response.time > 1000 ms' }],
},
{
id: 'Example 2',
requests: [{ url: 'https://example.com' }],
alerts: [{ assertion: 'response.time > 1000' }],
},
{
id: 'Example 3',
requests: [{ url: 'https://example.com' }],
alerts: [{ assertion: 'response.status == 200' }],
},
] as unknown as Probe[]

// act
const validatedProbes = await validateProbes(probes)

// assert
expect(
validatedProbes.find(({ id }) => id === 'Example')?.alerts
).deep.eq([''])
expect(
validatedProbes.find(({ id }) => id === 'Example 2')?.alerts
).deep.eq([{ assertion: 'response.time > 1000' }])
expect(
validatedProbes.find(({ id }) => id === 'Example 3')?.alerts
).deep.eq([{ assertion: 'response.status == 200' }])

resetContext()
})
})
})
Loading

0 comments on commit a9a2df4

Please sign in to comment.