-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some tests for our risk score route
Testing/documenting management/handling of parameters, mostly.
- Loading branch information
Showing
6 changed files
with
195 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
x-pack/plugins/security_solution/server/lib/risk_engine/risk_score_service.mock.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import type { RiskScoreService } from './risk_score_service'; | ||
import type { RiskScore } from './types'; | ||
|
||
const createRiskScoreMock = (overrides: Partial<RiskScore> = {}): RiskScore => ({ | ||
'@timestamp': '2023-02-15T00:15:19.231Z', | ||
identifierField: 'host.name', | ||
identifierValue: 'hostname', | ||
level: 'High', | ||
totalScore: 149, | ||
totalScoreNormalized: 85.332, | ||
alertsScore: 85, | ||
otherScore: 0, | ||
notes: [], | ||
riskiestInputs: [], | ||
...overrides, | ||
}); | ||
|
||
const createRiskScoreServiceMock = (): jest.Mocked<RiskScoreService> => ({ | ||
getScores: jest.fn(), | ||
}); | ||
|
||
export const riskScoreServiceMock = { | ||
create: createRiskScoreServiceMock, | ||
createRiskScore: createRiskScoreMock, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
154 changes: 154 additions & 0 deletions
154
x-pack/plugins/security_solution/server/lib/risk_engine/routes/risk_score_route.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { loggerMock } from '@kbn/logging-mocks'; | ||
|
||
import { RISK_SCORES_URL } from '../../../../common/constants'; | ||
import { | ||
serverMock, | ||
requestContextMock, | ||
requestMock, | ||
} from '../../detection_engine/routes/__mocks__'; | ||
import { riskScoreService } from '../risk_score_service'; | ||
import { riskScoreServiceMock } from '../risk_score_service.mock'; | ||
import { riskScoringRoute } from './risk_scoring_route'; | ||
|
||
jest.mock('../risk_score_service'); | ||
|
||
describe('GET risk_engine/scores route', () => { | ||
let server: ReturnType<typeof serverMock.create>; | ||
let { clients, context } = requestContextMock.createTools(); | ||
let logger: ReturnType<typeof loggerMock.create>; | ||
let mockRiskScoreService: ReturnType<typeof riskScoreServiceMock.create>; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
|
||
server = serverMock.create(); | ||
logger = loggerMock.create(); | ||
({ clients, context } = requestContextMock.createTools()); | ||
mockRiskScoreService = riskScoreServiceMock.create(); | ||
|
||
clients.appClient.getAlertsIndex.mockReturnValue('default-alerts-index'); | ||
(riskScoreService as jest.Mock).mockReturnValue(mockRiskScoreService); | ||
|
||
riskScoringRoute(server.router, logger); | ||
}); | ||
|
||
const buildRequest = (body: object = {}) => | ||
requestMock.create({ | ||
method: 'get', | ||
path: RISK_SCORES_URL, | ||
body, | ||
}); | ||
|
||
describe('parameters', () => { | ||
describe('index / dataview', () => { | ||
it('defaults to scoring the alerts index if no dataview is provided', async () => { | ||
const request = buildRequest(); | ||
|
||
const response = await server.inject(request, requestContextMock.convertContext(context)); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(mockRiskScoreService.getScores).toHaveBeenCalledWith( | ||
expect.objectContaining({ index: 'default-alerts-index' }) | ||
); | ||
}); | ||
|
||
it('respects the provided dataview', async () => { | ||
const request = buildRequest({ data_view_id: 'custom-dataview-id' }); | ||
|
||
// mock call to get dataview title | ||
clients.savedObjectsClient.get.mockResolvedValueOnce({ | ||
id: '', | ||
type: '', | ||
references: [], | ||
attributes: { title: 'custom-dataview-index' }, | ||
}); | ||
const response = await server.inject(request, requestContextMock.convertContext(context)); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(mockRiskScoreService.getScores).toHaveBeenCalledWith( | ||
expect.objectContaining({ index: 'custom-dataview-index' }) | ||
); | ||
}); | ||
|
||
it('defaults to the alerts index if dataview is not found', async () => { | ||
const request = buildRequest({ data_view_id: 'custom-dataview-id' }); | ||
|
||
const response = await server.inject(request, requestContextMock.convertContext(context)); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(mockRiskScoreService.getScores).toHaveBeenCalledWith( | ||
expect.objectContaining({ index: 'default-alerts-index' }) | ||
); | ||
}); | ||
}); | ||
|
||
describe('date range', () => { | ||
it('defaults to the last 15 days of data', async () => { | ||
const request = buildRequest(); | ||
const response = await server.inject(request, requestContextMock.convertContext(context)); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(mockRiskScoreService.getScores).toHaveBeenCalledWith( | ||
expect.objectContaining({ range: { start: 'now-15d', end: 'now' } }) | ||
); | ||
}); | ||
|
||
it('respects the provided range if provided', async () => { | ||
const request = buildRequest({ range: { start: 'now-30d', end: 'now-20d' } }); | ||
const response = await server.inject(request, requestContextMock.convertContext(context)); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(mockRiskScoreService.getScores).toHaveBeenCalledWith( | ||
expect.objectContaining({ range: { start: 'now-30d', end: 'now-20d' } }) | ||
); | ||
}); | ||
|
||
it.todo('rejects if date range is invalid'); | ||
}); | ||
|
||
describe('data filter', () => { | ||
it('respects the provided filter if provided', async () => { | ||
const request = buildRequest({ | ||
filter: { | ||
bool: { | ||
filter: [ | ||
{ | ||
ids: { | ||
values: '1', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}); | ||
const response = await server.inject(request, requestContextMock.convertContext(context)); | ||
|
||
expect(response.status).toEqual(200); | ||
expect(mockRiskScoreService.getScores).toHaveBeenCalledWith( | ||
expect.objectContaining({ | ||
filter: { | ||
bool: { | ||
filter: [ | ||
{ | ||
ids: { | ||
values: '1', | ||
}, | ||
}, | ||
], | ||
}, | ||
}, | ||
}) | ||
); | ||
}); | ||
|
||
it.todo('rejects if filter is invalid'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters