Skip to content

Commit

Permalink
[8.x] [Security Solution][Investigations] - replace supertest in api …
Browse files Browse the repository at this point in the history
…integration tests + unskip notes tests (#196774) (#197907)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Security Solution][Investigations] - replace supertest in api
integration tests + unskip notes tests
(#196774)](#196774)

<!--- Backport version: 8.9.8 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Philippe
Oberti","email":"philippe.oberti@elastic.co"},"sourceCommit":{"committedDate":"2024-10-18T16:12:57Z","message":"[Security
Solution][Investigations] - replace supertest in api integration tests +
unskip notes tests
(#196774)","sha":"09fe7bd64a345e9e2ab7c81644cc7510bc6e1f6a","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","backport:skip","v9.0.0","Team:Threat
Hunting:Investigations"],"number":196774,"url":"https://github.com/elastic/kibana/pull/196774","mergeCommit":{"message":"[Security
Solution][Investigations] - replace supertest in api integration tests +
unskip notes tests
(#196774)","sha":"09fe7bd64a345e9e2ab7c81644cc7510bc6e1f6a"}},"sourceBranch":"main","suggestedTargetBranches":[],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","labelRegex":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/196774","number":196774,"mergeCommit":{"message":"[Security
Solution][Investigations] - replace supertest in api integration tests +
unskip notes tests
(#196774)","sha":"09fe7bd64a345e9e2ab7c81644cc7510bc6e1f6a"}}]}]
BACKPORT-->
  • Loading branch information
PhilippeOberti authored Oct 25, 2024
1 parent 6412c34 commit b015b62
Show file tree
Hide file tree
Showing 14 changed files with 241 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
*/

import expect from '@kbn/expect';
import { TIMELINE_DRAFT_URL } from '@kbn/security-solution-plugin/common/constants';
import TestAgent from 'supertest/lib/agent';
import { FtrProviderContextWithSpaces } from '../../../../ftr_provider_context_with_spaces';

import { FtrProviderContext } from '../../../../../api_integration/ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const kibanaServer = getService('kibanaServer');
const supertest = getService('supertest');
export default function ({ getService }: FtrProviderContextWithSpaces) {
const utils = getService('securitySolutionUtils');
let supertest: TestAgent;

describe('Draft timeline - Saved Objects', () => {
before(() => kibanaServer.savedObjects.cleanStandardList());
after(() => kibanaServer.savedObjects.cleanStandardList());
before(async () => (supertest = await utils.createSuperTest()));

describe('Clean draft timelines', () => {
it('returns a draft timeline if none exists', async () => {
const response = await supertest
.post('/api/timeline/_draft')
.post(TIMELINE_DRAFT_URL)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31')
.send({
Expand All @@ -36,7 +36,7 @@ export default function ({ getService }: FtrProviderContext) {

it('returns a draft timeline template if none exists', async () => {
const response = await supertest
.post('/api/timeline/_draft')
.post(TIMELINE_DRAFT_URL)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31')
.send({
Expand All @@ -60,7 +60,7 @@ export default function ({ getService }: FtrProviderContext) {

it('returns a cleaned draft timeline if another one already exists', async () => {
const response = await supertest
.post('/api/timeline/_draft')
.post(TIMELINE_DRAFT_URL)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31')
.send({
Expand Down Expand Up @@ -114,7 +114,7 @@ export default function ({ getService }: FtrProviderContext) {
expect(noteIds).to.have.length(1, 'should have one note');

const cleanDraftTimelineRequest = await supertest
.post('/api/timeline/_draft')
.post(TIMELINE_DRAFT_URL)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31')
.send({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,26 @@

import expect from '@kbn/expect';
import { v4 as uuidv4 } from 'uuid';
import { Note } from '@kbn/security-solution-plugin/common/api/timeline';
import { createNote, deleteAllNotes } from './helpers';
import { FtrProviderContext } from '../../../../../api_integration/ftr_provider_context';

export default function ({ getService }: FtrProviderContext) {
const kibanaServer = getService('kibanaServer');
const supertest = getService('supertest');

// Failing: See https://github.com/elastic/kibana/issues/196492
describe.skip('Note - Saved Objects', () => {
const es = getService('es');

before(() => kibanaServer.savedObjects.cleanStandardList());
after(() => kibanaServer.savedObjects.cleanStandardList());
import { GetNotesResult, Note } from '@kbn/security-solution-plugin/common/api/timeline';
import { NOTE_URL } from '@kbn/security-solution-plugin/common/constants';
import TestAgent from 'supertest/lib/agent';
import { FtrProviderContextWithSpaces } from '../../../../ftr_provider_context_with_spaces';
import { createNote, deleteNotes } from '../../utils/notes';

export default function ({ getService }: FtrProviderContextWithSpaces) {
const utils = getService('securitySolutionUtils');
let supertest: TestAgent;

describe('Note - Saved Objects', () => {
before(async () => {
supertest = await utils.createSuperTest();
});

describe('create a note', () => {
it('should return a timelineId, noteId and version', async () => {
const myNote = 'world test';
const response = await supertest
.patch('/api/note')
.patch(NOTE_URL)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31')
.send({
Expand All @@ -44,10 +44,10 @@ export default function ({ getService }: FtrProviderContext) {
expect(version).to.not.be.empty();
});

it('if noteId exist update note and return existing noteId and new version', async () => {
it('should update note and return existing noteId and new version if noteId exist', async () => {
const myNote = 'world test';
const response = await supertest
.patch('/api/note')
.patch(NOTE_URL)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31')
.send({
Expand All @@ -61,7 +61,7 @@ export default function ({ getService }: FtrProviderContext) {

const myNewNote = 'new world test';
const responseToTest = await supertest
.patch('/api/note')
.patch(NOTE_URL)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31')
.send({
Expand All @@ -78,7 +78,7 @@ export default function ({ getService }: FtrProviderContext) {

describe('get notes', () => {
beforeEach(async () => {
await deleteAllNotes(es);
await deleteNotes(supertest);
});

const eventId1 = uuidv4();
Expand Down Expand Up @@ -117,11 +117,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get(`/api/note?documentIds=${eventId1}`)
.get(`${NOTE_URL}?documentIds=${eventId1}`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(2);
notes.forEach((note: Note) => expect(note.eventId).to.be(eventId1));
Expand Down Expand Up @@ -172,11 +171,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get(`/api/note?documentIds=${eventId1}&documentIds=${eventId2}`)
.get(`${NOTE_URL}?documentIds=${eventId1}&documentIds=${eventId2}`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(4);
notes.forEach((note: Note) => {
Expand Down Expand Up @@ -214,11 +212,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get(`/api/note?savedObjectIds=${timelineId1}`)
.get(`${NOTE_URL}?savedObjectIds=${timelineId1}`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(2);
notes.forEach((note: Note) => expect(note.timelineId).to.be(timelineId1));
Expand Down Expand Up @@ -269,11 +266,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get(`/api/note?savedObjectIds=${timelineId1}&savedObjectIds=${timelineId2}`)
.get(`${NOTE_URL}?savedObjectIds=${timelineId1}&savedObjectIds=${timelineId2}`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(4);
notes.forEach((note: Note) => {
Expand All @@ -298,48 +294,29 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note')
.get(NOTE_URL)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount } = response.body;
const { totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(4);
});

it('should retrieve notes considering perPage query parameter', async () => {
it('should retrieve notes considering perPage and page query parameters', async () => {
await Promise.all([
createNote(supertest, { text: 'first note' }),
createNote(supertest, { text: 'second note' }),
createNote(supertest, { text: 'third note' }),
]);

const response = await supertest
.get('/api/note?perPage=1')
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;

expect(totalCount).to.be(3);
expect(notes.length).to.be(1);
});

it('should retrieve considering page query parameter', async () => {
await createNote(supertest, { text: 'first note' });
await createNote(supertest, { text: 'second note' });
await createNote(supertest, { text: 'third note' });

const response = await supertest
.get('/api/note?perPage=1&page=2')
.get(`${NOTE_URL}?perPage=1`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(3);
expect(notes.length).to.be(1);
expect(notes[0].note).to.be('second note');
});

it('should retrieve considering search query parameter', async () => {
Expand All @@ -358,11 +335,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note?search=event')
.get(`${NOTE_URL}?search=event`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount } = response.body;
const { totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(2);
});
Expand All @@ -376,11 +352,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note?sortField=eventId')
.get(`${NOTE_URL}?sortField=eventId`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(3);
expect(notes[0].eventId).to.be('1');
Expand All @@ -396,33 +371,33 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note?sortField=eventId&sortOrder=desc')
.get(`${NOTE_URL}?sortField=eventId&sortOrder=desc`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(3);
expect(notes[0].eventId).to.be('3');
expect(notes[1].eventId).to.be('2');
expect(notes[2].eventId).to.be('1');
});

// TODO figure out why this test is failing on CI but not locally
it.skip('should retrieve all notes that have been created by a specific user', async () => {
await Promise.all([
createNote(supertest, { text: 'first note' }),
createNote(supertest, { text: 'second note' }),
]);

const response = await supertest
.get('/api/note?userFilter=elastic')
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount } = response.body;

expect(totalCount).to.be(2);
// skipped https://github.com/elastic/kibana/issues/196896
describe('@skipInServerless', () => {
it('should retrieve all notes that have been created by a specific user', async () => {
await Promise.all([
createNote(supertest, { text: 'first note' }),
createNote(supertest, { text: 'second note' }),
]);

const response = await supertest
.get(`${NOTE_URL}?userFilter=elastic`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');
const { totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(2);
});
});

it('should return nothing if no notes have been created by that user', async () => {
Expand All @@ -432,11 +407,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note?userFilter=user1')
.get(`${NOTE_URL}?userFilter=user1`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount } = response.body;
const { totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(0);
});
Expand All @@ -457,11 +431,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note?associatedFilter=document_only')
.get(`${NOTE_URL}?associatedFilter=document_only`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(1);
expect(notes[0].eventId).to.be(eventId1);
Expand All @@ -483,11 +456,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note?associatedFilter=saved_object_only')
.get(`${NOTE_URL}?associatedFilter=saved_object_only`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(1);
expect(notes[0].timelineId).to.be(timelineId1);
Expand All @@ -509,11 +481,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note?associatedFilter=document_and_saved_object')
.get(`${NOTE_URL}?associatedFilter=document_and_saved_object`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(1);
expect(notes[0].eventId).to.be(eventId1);
Expand All @@ -536,11 +507,10 @@ export default function ({ getService }: FtrProviderContext) {
]);

const response = await supertest
.get('/api/note?associatedFilter=orphan')
.get(`${NOTE_URL}?associatedFilter=orphan`)
.set('kbn-xsrf', 'true')
.set('elastic-api-version', '2023-10-31');

const { totalCount, notes } = response.body;
const { notes, totalCount } = response.body as GetNotesResult;

expect(totalCount).to.be(1);
expect(notes[0].eventId).to.be('');
Expand Down
Loading

0 comments on commit b015b62

Please sign in to comment.