Skip to content

Commit 93aa2db

Browse files
committed
Update more integration tests to account for our optional _source
Changes here fall into one of two categories: * If the test was making an assertion on a value from _source, we simply null chain and continue to assert on a possibly undefined value. * If the test logic depends on _source being present, we first assert that presence, and exit the test early if absent.
1 parent 6c9bc3e commit 93aa2db

22 files changed

Lines changed: 431 additions & 393 deletions

x-pack/test/detection_engine_api_integration/security_and_spaces/tests/aliases.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export default ({ getService }: FtrProviderContext) => {
4646
await waitForSignalsToBePresent(supertest, 4, [id]);
4747
const signalsOpen = await getSignalsById(supertest, id);
4848
const hits = signalsOpen.hits.hits.map(
49-
(signal) => (signal._source.host_alias as HostAlias).name
49+
(signal) => (signal._source?.host_alias as HostAlias).name
5050
);
5151
expect(hits).to.eql(['host name 1', 'host name 2', 'host name 3', 'host name 4']);
5252
});
@@ -57,7 +57,7 @@ export default ({ getService }: FtrProviderContext) => {
5757
await waitForRuleSuccessOrStatus(supertest, id);
5858
await waitForSignalsToBePresent(supertest, 4, [id]);
5959
const signalsOpen = await getSignalsById(supertest, id);
60-
const hits = signalsOpen.hits.hits.map((signal) => (signal._source.host as HostAlias).name);
60+
const hits = signalsOpen.hits.hits.map((signal) => (signal._source?.host as HostAlias).name);
6161
expect(hits).to.eql(['host name 1', 'host name 2', 'host name 3', 'host name 4']);
6262
});
6363
});

x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_endpoint_exceptions.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export default ({ getService }: FtrProviderContext) => {
5959
await waitForRuleSuccessOrStatus(supertest, id);
6060
await waitForSignalsToBePresent(supertest, 4, [id]);
6161
const signalsOpen = await getSignalsById(supertest, id);
62-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host).sort();
62+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host).sort();
6363
expect(hits).to.eql([
6464
{
6565
os: { type: 'linux' },
@@ -82,7 +82,7 @@ export default ({ getService }: FtrProviderContext) => {
8282
await waitForRuleSuccessOrStatus(supertest, id);
8383
await waitForSignalsToBePresent(supertest, 4, [id]);
8484
const signalsOpen = await getSignalsById(supertest, id);
85-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host).sort();
85+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host).sort();
8686
expect(hits).to.eql([
8787
{
8888
os: { name: 'Linux' },
@@ -125,7 +125,7 @@ export default ({ getService }: FtrProviderContext) => {
125125
await waitForRuleSuccessOrStatus(supertest, id);
126126
await waitForSignalsToBePresent(supertest, 3, [id]);
127127
const signalsOpen = await getSignalsById(supertest, id);
128-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
128+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
129129
expect(hits).to.eql([
130130
{
131131
os: { name: 'Windows' },
@@ -162,7 +162,7 @@ export default ({ getService }: FtrProviderContext) => {
162162
await waitForRuleSuccessOrStatus(supertest, id);
163163
await waitForSignalsToBePresent(supertest, 3, [id]);
164164
const signalsOpen = await getSignalsById(supertest, id);
165-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
165+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
166166
expect(hits).to.eql([
167167
{
168168
os: { name: 'Windows' },
@@ -210,7 +210,7 @@ export default ({ getService }: FtrProviderContext) => {
210210
await waitForRuleSuccessOrStatus(supertest, id);
211211
await waitForSignalsToBePresent(supertest, 2, [id]);
212212
const signalsOpen = await getSignalsById(supertest, id);
213-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
213+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
214214
expect(hits).to.eql([
215215
{
216216
os: { name: 'Macos' },
@@ -255,7 +255,7 @@ export default ({ getService }: FtrProviderContext) => {
255255
await waitForRuleSuccessOrStatus(supertest, id);
256256
await waitForSignalsToBePresent(supertest, 2, [id]);
257257
const signalsOpen = await getSignalsById(supertest, id);
258-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
258+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
259259
expect(hits).to.eql([
260260
{
261261
os: { name: 'Macos' },
@@ -291,7 +291,7 @@ export default ({ getService }: FtrProviderContext) => {
291291
await waitForRuleSuccessOrStatus(supertest, id);
292292
await waitForSignalsToBePresent(supertest, 3, [id]);
293293
const signalsOpen = await getSignalsById(supertest, id);
294-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
294+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
295295
expect(hits).to.eql([
296296
{
297297
os: { type: 'windows' },
@@ -328,7 +328,7 @@ export default ({ getService }: FtrProviderContext) => {
328328
await waitForRuleSuccessOrStatus(supertest, id);
329329
await waitForSignalsToBePresent(supertest, 3, [id]);
330330
const signalsOpen = await getSignalsById(supertest, id);
331-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
331+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
332332
expect(hits).to.eql([
333333
{
334334
os: { type: 'windows' },
@@ -376,7 +376,7 @@ export default ({ getService }: FtrProviderContext) => {
376376
await waitForRuleSuccessOrStatus(supertest, id);
377377
await waitForSignalsToBePresent(supertest, 2, [id]);
378378
const signalsOpen = await getSignalsById(supertest, id);
379-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
379+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
380380
expect(hits).to.eql([
381381
{
382382
os: { type: 'macos' },
@@ -421,7 +421,7 @@ export default ({ getService }: FtrProviderContext) => {
421421
await waitForRuleSuccessOrStatus(supertest, id);
422422
await waitForSignalsToBePresent(supertest, 2, [id]);
423423
const signalsOpen = await getSignalsById(supertest, id);
424-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
424+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
425425
expect(hits).to.eql([
426426
{
427427
os: { type: 'macos' },
@@ -457,7 +457,7 @@ export default ({ getService }: FtrProviderContext) => {
457457
await waitForRuleSuccessOrStatus(supertest, id);
458458
await waitForSignalsToBePresent(supertest, 6, [id]);
459459
const signalsOpen = await getSignalsById(supertest, id);
460-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
460+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
461461
expect(hits).to.eql([
462462
{
463463
os: { type: 'windows' },
@@ -503,7 +503,7 @@ export default ({ getService }: FtrProviderContext) => {
503503
await waitForRuleSuccessOrStatus(supertest, id);
504504
await waitForSignalsToBePresent(supertest, 6, [id]);
505505
const signalsOpen = await getSignalsById(supertest, id);
506-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
506+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
507507
expect(hits).to.eql([
508508
{
509509
os: { type: 'windows' },
@@ -560,7 +560,7 @@ export default ({ getService }: FtrProviderContext) => {
560560
await waitForRuleSuccessOrStatus(supertest, id);
561561
await waitForSignalsToBePresent(supertest, 4, [id]);
562562
const signalsOpen = await getSignalsById(supertest, id);
563-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
563+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
564564
expect(hits).to.eql([
565565
{
566566
os: { type: 'macos' },
@@ -611,7 +611,7 @@ export default ({ getService }: FtrProviderContext) => {
611611
await waitForRuleSuccessOrStatus(supertest, id);
612612
await waitForSignalsToBePresent(supertest, 4, [id]);
613613
const signalsOpen = await getSignalsById(supertest, id);
614-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
614+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
615615
expect(hits).to.eql([
616616
{
617617
os: { type: 'macos' },
@@ -663,7 +663,7 @@ export default ({ getService }: FtrProviderContext) => {
663663
await waitForRuleSuccessOrStatus(supertest, id);
664664
await waitForSignalsToBePresent(supertest, 1, [id]);
665665
const signalsOpen = await getSignalsById(supertest, id);
666-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
666+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
667667
expect(hits).to.eql([
668668
{
669669
os: { type: 'macos' },
@@ -703,7 +703,7 @@ export default ({ getService }: FtrProviderContext) => {
703703
await waitForRuleSuccessOrStatus(supertest, id);
704704
await waitForSignalsToBePresent(supertest, 1, [id]);
705705
const signalsOpen = await getSignalsById(supertest, id);
706-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
706+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
707707
expect(hits).to.eql([
708708
{
709709
os: { type: 'macos' },
@@ -736,7 +736,7 @@ export default ({ getService }: FtrProviderContext) => {
736736
await waitForRuleSuccessOrStatus(supertest, id);
737737
await waitForSignalsToBePresent(supertest, 3, [id]);
738738
const signalsOpen = await getSignalsById(supertest, id);
739-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
739+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
740740
expect(hits).to.eql([
741741
{
742742
os: { type: 'linux' },
@@ -773,7 +773,7 @@ export default ({ getService }: FtrProviderContext) => {
773773
await waitForRuleSuccessOrStatus(supertest, id);
774774
await waitForSignalsToBePresent(supertest, 2, [id]);
775775
const signalsOpen = await getSignalsById(supertest, id);
776-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
776+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
777777
expect(hits).to.eql([
778778
{
779779
os: { type: 'macos' },
@@ -807,7 +807,7 @@ export default ({ getService }: FtrProviderContext) => {
807807
await waitForRuleSuccessOrStatus(supertest, id);
808808
await waitForSignalsToBePresent(supertest, 2, [id]);
809809
const signalsOpen = await getSignalsById(supertest, id);
810-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
810+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
811811
expect(hits).to.eql([
812812
{
813813
os: { type: 'macos' },
@@ -841,7 +841,7 @@ export default ({ getService }: FtrProviderContext) => {
841841
await waitForRuleSuccessOrStatus(supertest, id);
842842
await waitForSignalsToBePresent(supertest, 4, [id]);
843843
const signalsOpen = await getSignalsById(supertest, id);
844-
const hits = signalsOpen.hits.hits.map((hit) => hit._source.host);
844+
const hits = signalsOpen.hits.hits.map((hit) => hit._source?.host);
845845
expect(hits).to.eql([
846846
{
847847
os: { type: 'linux' },

x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_ml.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export default ({ getService }: FtrProviderContext) => {
152152
id: createdRule.id,
153153
rule_id: createdRule.rule_id,
154154
created_at: createdRule.created_at,
155-
updated_at: signal._source.signal.rule.updated_at,
155+
updated_at: signal._source?.signal.rule.updated_at,
156156
actions: [],
157157
interval: '5m',
158158
name: 'Test ML rule',

x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_signals_migrations.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
} from '../../../../plugins/security_solution/common/constants';
1414
import { ROLES } from '../../../../plugins/security_solution/common/test';
1515
import { SIGNALS_TEMPLATE_VERSION } from '../../../../plugins/security_solution/server/lib/detection_engine/routes/index/get_signals_template';
16+
import { Signal } from '../../../../plugins/security_solution/server/lib/detection_engine/signals/types';
1617
import { FtrProviderContext } from '../../common/ftr_provider_context';
1718
import {
1819
createSignalsIndex,
@@ -96,12 +97,11 @@ export default ({ getService }: FtrProviderContext): void => {
9697

9798
const [{ migration_index: newIndex }] = createResponses;
9899
await waitForIndexToPopulate(es, newIndex);
99-
const { body: migrationResults } = await es.search({ index: newIndex });
100+
const { body: migrationResults } = await es.search<{ signal: Signal }>({ index: newIndex });
100101

101102
expect(migrationResults.hits.hits).length(1);
102-
// @ts-expect-error _source has unknown type
103-
const migratedSignal = migrationResults.hits.hits[0]._source.signal;
104-
expect(migratedSignal._meta.version).to.equal(SIGNALS_TEMPLATE_VERSION);
103+
const migratedSignal = migrationResults.hits.hits[0]._source?.signal;
104+
expect(migratedSignal?._meta?.version).to.equal(SIGNALS_TEMPLATE_VERSION);
105105
});
106106

107107
it('specifying the signals alias itself is a bad request', async () => {

x-pack/test/detection_engine_api_integration/security_and_spaces/tests/create_threat_matching.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,12 @@ export default ({ getService }: FtrProviderContext) => {
153153
const signalsOpen = await getSignalsByIds(supertest, [id]);
154154
expect(signalsOpen.hits.hits.length).equal(10);
155155
const fullSource = signalsOpen.hits.hits.find(
156-
(signal) => signal._source.signal.parents[0].id === '7yJ-B2kBR346wHgnhlMn'
156+
(signal) => signal._source?.signal.parents[0].id === '7yJ-B2kBR346wHgnhlMn'
157157
);
158-
const fullSignal = fullSource!._source; // If this doesn't exist the test is going to fail anyway so using a bang operator here to get rid of ts error
158+
const fullSignal = fullSource?._source;
159+
if (!fullSignal) {
160+
return expect(fullSignal).to.be.ok();
161+
}
159162
expect(fullSignal).eql({
160163
'@timestamp': fullSignal['@timestamp'],
161164
agent: {
@@ -439,7 +442,7 @@ export default ({ getService }: FtrProviderContext) => {
439442
expect(signalsOpen.hits.hits.length).equal(2);
440443

441444
const { hits } = signalsOpen.hits;
442-
const threats = hits.map((hit) => hit._source.threat);
445+
const threats = hits.map((hit) => hit._source?.threat);
443446
expect(threats).to.eql([
444447
{
445448
indicator: [
@@ -544,7 +547,9 @@ export default ({ getService }: FtrProviderContext) => {
544547
expect(signalsOpen.hits.hits.length).equal(1);
545548

546549
const { hits } = signalsOpen.hits;
547-
const [threat] = hits.map((hit) => hit._source.threat) as Array<{ indicator: unknown[] }>;
550+
const [threat] = hits.map((hit) => hit._source?.threat) as Array<{
551+
indicator: unknown[];
552+
}>;
548553

549554
assertContains(threat.indicator, [
550555
{
@@ -644,7 +649,9 @@ export default ({ getService }: FtrProviderContext) => {
644649
expect(signalsOpen.hits.hits.length).equal(1);
645650

646651
const { hits } = signalsOpen.hits;
647-
const [threat] = hits.map((hit) => hit._source.threat) as Array<{ indicator: unknown[] }>;
652+
const [threat] = hits.map((hit) => hit._source?.threat) as Array<{
653+
indicator: unknown[];
654+
}>;
648655

649656
assertContains(threat.indicator, [
650657
{
@@ -779,7 +786,7 @@ export default ({ getService }: FtrProviderContext) => {
779786
expect(signalsOpen.hits.hits.length).equal(2);
780787

781788
const { hits } = signalsOpen.hits;
782-
const threats = hits.map((hit) => hit._source.threat) as Array<{ indicator: unknown[] }>;
789+
const threats = hits.map((hit) => hit._source?.threat) as Array<{ indicator: unknown[] }>;
783790

784791
assertContains(threats[0].indicator, [
785792
{

0 commit comments

Comments
 (0)