Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { ApiClientFixture } from '@kbn/scout';
import { apiTest, tags, type RoleApiCredentials } from '@kbn/scout';
import { expect } from '@kbn/scout/api';
import { COMMON_HEADERS, SERVICE_PATH, SERVICE_KEY } from '../../fixtures/constants';
Expand All @@ -16,17 +17,38 @@ apiTest.describe(
{ tag: tags.deploymentAgnostic },
() => {
let adminApiCredentials: RoleApiCredentials;
const testSpaceId = `default-data-view-${Date.now()}-${Math.random().toString(36).slice(2)}`;

const newId = () => `default-id-${Date.now()}-${Math.random()}`;
const defaultPath = `${SERVICE_PATH}/default`;
const serviceKeyId = `${SERVICE_KEY}_id`;

apiTest.beforeAll(async ({ requestAuth }) => {
const scopedPath = (path: string) => `s/${testSpaceId}/${path}`;

function expectDefaultDataView(apiClient: ApiClientFixture, defaultId: string) {
return expect
.poll(async () => {
const getResponse = await apiClient.get(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
},
responseType: 'json',
});
return getResponse.body[serviceKeyId];
})
.toBe(defaultId);
}

apiTest.beforeAll(async ({ apiServices, requestAuth }) => {
adminApiCredentials = await requestAuth.getApiKey('admin');
await apiServices.spaces.create({
id: testSpaceId,
name: testSpaceId,
});
});

apiTest.afterEach(async ({ apiClient }) => {
await apiClient.post(defaultPath, {
await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -39,10 +61,14 @@ apiTest.describe(
});
});

apiTest.afterAll(async ({ apiServices }) => {
await apiServices.spaces.delete(testSpaceId);
});

apiTest('can set default data view', async ({ apiClient }) => {
const defaultId = newId();

const setResponse = await apiClient.post(defaultPath, {
const setResponse = await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -57,22 +83,13 @@ apiTest.describe(
expect(setResponse).toHaveStatusCode(200);
expect(setResponse.body.acknowledged).toBe(true);

const getResponse = await apiClient.get(defaultPath, {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
},
responseType: 'json',
});

expect(getResponse).toHaveStatusCode(200);
expect(getResponse.body[serviceKeyId]).toBe(defaultId);
await expectDefaultDataView(apiClient, defaultId);
});

apiTest('does not override existing default without force flag', async ({ apiClient }) => {
const defaultId = newId();

await apiClient.post(defaultPath, {
await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -84,7 +101,7 @@ apiTest.describe(
},
});

const overrideResponse = await apiClient.post(defaultPath, {
const overrideResponse = await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -97,22 +114,13 @@ apiTest.describe(

expect(overrideResponse).toHaveStatusCode(200);

const getResponse = await apiClient.get(defaultPath, {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
},
responseType: 'json',
});

expect(getResponse).toHaveStatusCode(200);
expect(getResponse.body[serviceKeyId]).toBe(defaultId);
await expectDefaultDataView(apiClient, defaultId);
});

apiTest('can clear default data view with force flag', async ({ apiClient }) => {
const defaultId = newId();

await apiClient.post(defaultPath, {
await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -124,7 +132,7 @@ apiTest.describe(
},
});

const clearResponse = await apiClient.post(defaultPath, {
const clearResponse = await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -138,16 +146,7 @@ apiTest.describe(

expect(clearResponse).toHaveStatusCode(200);

const getResponse = await apiClient.get(defaultPath, {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
},
responseType: 'json',
});

expect(getResponse).toHaveStatusCode(200);
expect(getResponse.body[serviceKeyId]).toBe('');
await expectDefaultDataView(apiClient, '');
});
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import type { ApiClientFixture } from '@kbn/scout';
import { apiTest, tags, type RoleApiCredentials } from '@kbn/scout';
import { expect } from '@kbn/scout/api';
import { COMMON_HEADERS, SERVICE_PATH_LEGACY, SERVICE_KEY_LEGACY } from '../../fixtures/constants';
Expand All @@ -16,17 +17,40 @@ apiTest.describe(
{ tag: tags.deploymentAgnostic },
() => {
let adminApiCredentials: RoleApiCredentials;
const testSpaceId = `default-index-pattern-${Date.now()}-${Math.random()
.toString(36)
.slice(2)}`;

const newId = () => `default-id-${Date.now()}-${Math.random()}`;
const defaultPath = `${SERVICE_PATH_LEGACY}/default`;
const serviceKeyId = `${SERVICE_KEY_LEGACY}_id`;

apiTest.beforeAll(async ({ requestAuth }) => {
const scopedPath = (path: string) => `s/${testSpaceId}/${path}`;

function expectDefaultIndexPattern(apiClient: ApiClientFixture, defaultId: string) {
return expect
.poll(async () => {
const getResponse = await apiClient.get(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
},
responseType: 'json',
});
return getResponse.body[serviceKeyId];
})
.toBe(defaultId);
}

apiTest.beforeAll(async ({ apiServices, requestAuth }) => {
adminApiCredentials = await requestAuth.getApiKey('admin');
await apiServices.spaces.create({
id: testSpaceId,
name: testSpaceId,
});
});

apiTest.afterEach(async ({ apiClient }) => {
await apiClient.post(defaultPath, {
await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -39,10 +63,14 @@ apiTest.describe(
});
});

apiTest.afterAll(async ({ apiServices }) => {
await apiServices.spaces.delete(testSpaceId);
});

apiTest('can set default index pattern', async ({ apiClient }) => {
const defaultId = newId();

const setResponse = await apiClient.post(defaultPath, {
const setResponse = await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -57,22 +85,14 @@ apiTest.describe(
expect(setResponse).toHaveStatusCode(200);
expect(setResponse.body.acknowledged).toBe(true);

const getResponse = await apiClient.get(defaultPath, {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
},
responseType: 'json',
});

expect(getResponse).toHaveStatusCode(200);
expect(getResponse.body[serviceKeyId]).toBe(defaultId);
await expectDefaultIndexPattern(apiClient, defaultId);
});

apiTest('does not override existing default without force flag', async ({ apiClient }) => {
const defaultId = newId();

await apiClient.post(defaultPath, {
// Set a default index pattern
await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -83,8 +103,10 @@ apiTest.describe(
force: true,
},
});
await expectDefaultIndexPattern(apiClient, defaultId);

const overrideResponse = await apiClient.post(defaultPath, {
// Try to override the default index pattern without the force flag
const overrideResponse = await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -94,25 +116,17 @@ apiTest.describe(
[serviceKeyId]: newId(),
},
});

expect(overrideResponse).toHaveStatusCode(200);

const getResponse = await apiClient.get(defaultPath, {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
},
responseType: 'json',
});

expect(getResponse).toHaveStatusCode(200);
expect(getResponse.body[serviceKeyId]).toBe(defaultId);
// Verify that the default index pattern is still the same
await expectDefaultIndexPattern(apiClient, defaultId);
});

apiTest('can clear default index pattern with force flag', async ({ apiClient }) => {
const defaultId = newId();

await apiClient.post(defaultPath, {
// Set a default index pattern
await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -123,8 +137,10 @@ apiTest.describe(
force: true,
},
});
await expectDefaultIndexPattern(apiClient, defaultId);

const clearResponse = await apiClient.post(defaultPath, {
// Clear the default index pattern
const clearResponse = await apiClient.post(scopedPath(defaultPath), {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
Expand All @@ -135,19 +151,10 @@ apiTest.describe(
force: true,
},
});

expect(clearResponse).toHaveStatusCode(200);

const getResponse = await apiClient.get(defaultPath, {
headers: {
...COMMON_HEADERS,
...adminApiCredentials.apiKeyHeader,
},
responseType: 'json',
});

expect(getResponse).toHaveStatusCode(200);
expect(getResponse.body[serviceKeyId]).toBe('');
// Verify that the default index pattern is cleared
await expectDefaultIndexPattern(apiClient, '');
});
}
);
Loading