-
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.
Implement normalization of ruleSource for API responses
- Loading branch information
Showing
3 changed files
with
92 additions
and
2 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
55 changes: 55 additions & 0 deletions
55
...ine/rule_management/logic/detection_rules_client/converters/normalize_rule_source.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,55 @@ | ||
/* | ||
* 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 { normalizeRuleSource } from './normalize_rule_source'; | ||
import type { BaseRuleParams } from '../../../../rule_schema'; | ||
|
||
describe('normalizeRuleSource', () => { | ||
it('should return rule_source of type `internal` when immutable is false and ruleSource is undefined', () => { | ||
const result = normalizeRuleSource({ | ||
immutable: false, | ||
ruleSource: undefined, | ||
}); | ||
expect(result).toEqual({ | ||
type: 'internal', | ||
}); | ||
}); | ||
|
||
it('should return rule_source of type `external` and `isCustomized: false` when immutable is true and ruleSource is undefined', () => { | ||
const result = normalizeRuleSource({ | ||
immutable: true, | ||
ruleSource: undefined, | ||
}); | ||
expect(result).toEqual({ | ||
type: 'external', | ||
is_customized: false, | ||
}); | ||
}); | ||
|
||
it('should return snake_case version of rule_source when ruleSource is present', () => { | ||
const externalRuleSource: BaseRuleParams['ruleSource'] = { | ||
type: 'external', | ||
isCustomized: true, | ||
}; | ||
const externalResult = normalizeRuleSource({ immutable: true, ruleSource: externalRuleSource }); | ||
expect(externalResult).toEqual({ | ||
type: externalRuleSource.type, | ||
is_customized: externalRuleSource.isCustomized, | ||
}); | ||
|
||
const internalRuleSource: BaseRuleParams['ruleSource'] = { | ||
type: 'internal', | ||
}; | ||
const internalResult = normalizeRuleSource({ | ||
immutable: false, | ||
ruleSource: internalRuleSource, | ||
}); | ||
expect(internalResult).toEqual({ | ||
type: internalRuleSource.type, | ||
}); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
...n_engine/rule_management/logic/detection_rules_client/converters/normalize_rule_source.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 { RuleSource } from '../../../../../../../common/api/detection_engine'; | ||
import { convertObjectKeysToSnakeCase } from '../../../../../../utils/object_case_converters'; | ||
import type { BaseRuleParams } from '../../../../rule_schema'; | ||
|
||
interface NormalizeRuleSourceParams { | ||
immutable: BaseRuleParams['immutable']; | ||
ruleSource: BaseRuleParams['ruleSource']; | ||
} | ||
export const normalizeRuleSource = ({ | ||
immutable, | ||
ruleSource, | ||
}: NormalizeRuleSourceParams): RuleSource => { | ||
if (!ruleSource) { | ||
const normalizedRuleSource = immutable | ||
? { | ||
type: 'external', | ||
isCustomized: false, | ||
} | ||
: { | ||
type: 'internal', | ||
}; | ||
|
||
return convertObjectKeysToSnakeCase(normalizedRuleSource) as RuleSource; | ||
} | ||
return convertObjectKeysToSnakeCase(ruleSource) as RuleSource; | ||
}; |