Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Validation on Alert Assertion #1179

Merged
Merged
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
Loading