Skip to content

Commit

Permalink
[FTR] move supertestWithoutAuth service to kbn-ftr-common-functional-…
Browse files Browse the repository at this point in the history
…services (elastic#189613)

## Summary

While working on elastic#188737 I had to move `supertestWithoutAuth` into
`kbn-ftr-common-functional-services` package. This change seems to be
bigger than initially planned.

Moving it to the separate PR with following changes:
- move FTR `SupertestWithoutAuthProvider` service to package
- remove "duplicates" in favour of service from package
- update service type where needed
  • Loading branch information
dmlemeshko committed Aug 5, 2024
1 parent 32b7873 commit 477d07f
Show file tree
Hide file tree
Showing 44 changed files with 315 additions and 352 deletions.
3 changes: 3 additions & 0 deletions packages/kbn-ftr-common-functional-services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ export type EsArchiver = ProvidedType<typeof EsArchiverProvider>;
import { EsProvider } from './services/es';
export type Es = ProvidedType<typeof EsProvider>;

import { SupertestWithoutAuthProvider } from './services/supertest_without_auth';
export type SupertestWithoutAuthProviderType = ProvidedType<typeof SupertestWithoutAuthProvider>;

export type { FtrProviderContext } from './services/ftr_provider_context';
2 changes: 2 additions & 0 deletions packages/kbn-ftr-common-functional-services/services/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { EsArchiverProvider } from './es_archiver';
import { EsProvider } from './es';
import { KibanaServerProvider } from './kibana_server';
import { RetryService } from './retry';
import { SupertestWithoutAuthProvider } from './supertest_without_auth';

export const services = {
es: EsProvider,
kibanaServer: KibanaServerProvider,
esArchiver: EsArchiverProvider,
retry: RetryService,
supertestWithoutAuth: SupertestWithoutAuthProvider,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/

import { format as formatUrl } from 'url';
import supertest from 'supertest';
import { FtrProviderContext } from './ftr_provider_context';

/**
* Returns supertest.SuperTest<supertest.Test> instance that will not persist cookie between API requests.
* If you need to pass certificate, do the following:
* await supertestWithoutAuth
* .get('/abc')
* .ca(CA_CERT)
*/
export function SupertestWithoutAuthProvider({ getService }: FtrProviderContext) {
const config = getService('config');
const kbnUrl = formatUrl({
...config.get('servers.kibana'),
auth: false,
});

return supertest(kbnUrl);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import { ES_TEST_INDEX_NAME } from '@kbn/alerting-api-integration-helpers';
import { AlertsFilter } from '@kbn/alerting-plugin/common/rule';
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
import { Space, User } from '../types';
import { ObjectRemover } from './object_remover';
import { getUrlPrefix } from './space_test_utils';
Expand All @@ -15,7 +16,7 @@ import { getTestRuleData } from './get_test_rule_data';
export interface AlertUtilsOpts {
user?: User;
space: Space;
supertestWithoutAuth: any;
supertestWithoutAuth: SupertestWithoutAuthProviderType;
indexRecordActionId?: string;
objectRemover?: ObjectRemover;
}
Expand Down Expand Up @@ -57,7 +58,7 @@ export class AlertUtils {
private referenceCounter = 1;
private readonly user?: User;
private readonly space: Space;
private readonly supertestWithoutAuth: any;
private readonly supertestWithoutAuth: SupertestWithoutAuthProviderType;
private readonly indexRecordActionId?: string;
private readonly objectRemover?: ObjectRemover;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import expect from '@kbn/expect';
import { Agent as SuperTestAgent } from 'supertest';
import { chunk, omit } from 'lodash';
import { v4 as uuidv4 } from 'uuid';
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
import { SuperuserAtSpace1, UserAtSpaceScenarios } from '../../../scenarios';
import { getUrlPrefix, getTestRuleData, ObjectRemover } from '../../../../common/lib';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
Expand All @@ -17,7 +18,7 @@ const findTestUtils = (
describeType: 'internal' | 'public',
objectRemover: ObjectRemover,
supertest: SuperTestAgent,
supertestWithoutAuth: any
supertestWithoutAuth: SupertestWithoutAuthProviderType
) => {
describe(describeType, () => {
afterEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import expect from '@kbn/expect';
import { Agent as SuperTestAgent } from 'supertest';
import { chunk, omit } from 'lodash';
import { v4 as uuidv4 } from 'uuid';
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
import { UserAtSpaceScenarios } from '../../../scenarios';
import { getUrlPrefix, getTestRuleData, ObjectRemover } from '../../../../common/lib';
import { FtrProviderContext } from '../../../../common/ftr_provider_context';
Expand All @@ -17,7 +18,7 @@ const findTestUtils = (
describeType: 'internal' | 'public',
objectRemover: ObjectRemover,
supertest: SuperTestAgent,
supertestWithoutAuth: any
supertestWithoutAuth: SupertestWithoutAuthProviderType
) => {
describe(describeType, () => {
afterEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import expect from '@kbn/expect';
import { Agent as SuperTestAgent } from 'supertest';
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
import { SuperuserAtSpace1, UserAtSpaceScenarios } from '../../../scenarios';
import {
getUrlPrefix,
Expand All @@ -20,7 +21,7 @@ const getTestUtils = (
describeType: 'internal' | 'public',
objectRemover: ObjectRemover,
supertest: SuperTestAgent,
supertestWithoutAuth: any
supertestWithoutAuth: SupertestWithoutAuthProviderType
) => {
describe(describeType, () => {
afterEach(() => objectRemover.removeAll());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/

import expect from '@kbn/expect';
import { SuperTest } from 'supertest';
import { CSV_QUOTE_VALUES_SETTING } from '@kbn/share-plugin/common/constants';
import {
ELASTIC_HTTP_VERSION_HEADER,
Expand All @@ -15,7 +14,7 @@ import {
import { FtrProviderContext } from '../../../ftr_provider_context';

export default function featureControlsTests({ getService }: FtrProviderContext) {
const supertest: SuperTest<any> = getService('supertestWithoutAuth');
const supertestWithoutAuth = getService('supertestWithoutAuth');
const security = getService('security');
const spaces = getService('spaces');
const deployment = getService('deployment');
Expand Down Expand Up @@ -55,7 +54,7 @@ export default function featureControlsTests({ getService }: FtrProviderContext)
async function saveAdvancedSetting(username: string, password: string, spaceId?: string) {
const basePath = spaceId ? `/s/${spaceId}` : '';

return await supertest
return await supertestWithoutAuth
.post(`${basePath}/internal/kibana/settings`)
.auth(username, password)
.set('kbn-xsrf', 'foo')
Expand All @@ -67,7 +66,7 @@ export default function featureControlsTests({ getService }: FtrProviderContext)
async function saveTelemetrySetting(username: string, password: string, spaceId?: string) {
const basePath = spaceId ? `/s/${spaceId}` : '';

return await supertest
return await supertestWithoutAuth
.post(`${basePath}/internal/telemetry/optIn`)
.auth(username, password)
.set('kbn-xsrf', 'foo')
Expand Down
3 changes: 0 additions & 3 deletions x-pack/test/api_integration/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { services as commonServices } from '../../common/services';

// @ts-ignore not ts yet
import { EsSupertestWithoutAuthProvider } from './es_supertest_without_auth';
// @ts-ignore not ts yet
import { SupertestWithoutAuthProvider } from './supertest_without_auth';

import { UsageAPIProvider } from './usage_api';

Expand All @@ -36,7 +34,6 @@ export const services = {
dataViewApi: DataViewApiProvider,
esSupertestWithoutAuth: EsSupertestWithoutAuthProvider,
infraOpsSourceConfiguration: InfraOpsSourceConfigurationProvider,
supertestWithoutAuth: SupertestWithoutAuthProvider,
usageAPI: UsageAPIProvider,
ml: MachineLearningProvider,
ingestManager: IngestManagerProvider,
Expand Down
26 changes: 0 additions & 26 deletions x-pack/test/api_integration/services/supertest_without_auth.js

This file was deleted.

4 changes: 2 additions & 2 deletions x-pack/test/common/services/bsearch_secure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

import expect from '@kbn/expect';
import request from 'superagent';
import type SuperTest from 'supertest';
import type { IEsSearchResponse } from '@kbn/search-types';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import { BFETCH_ROUTE_VERSION_LATEST } from '@kbn/bfetch-plugin/common';
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
import { FtrService } from '../ftr_provider_context';

const parseBfetchResponse = (resp: request.Response): Array<Record<string, any>> => {
Expand All @@ -28,7 +28,7 @@ const getSpaceUrlPrefix = (spaceId?: string): string => {
};

interface SendOptions {
supertestWithoutAuth: SuperTest.Agent;
supertestWithoutAuth: SupertestWithoutAuthProviderType;
auth: { username: string; password: string };
referer?: string;
kibanaVersion?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
*/

import { services as commonServices } from '../common/services';
import { services as apiIntegrationServices } from '../api_integration/services';

export const services = {
...commonServices,
supertestWithoutAuth: apiIntegrationServices.supertestWithoutAuth,
};
5 changes: 3 additions & 2 deletions x-pack/test/fleet_api_integration/privileges_helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
* 2.0.
*/

import supertest, { type SuperTest } from 'supertest';
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
import supertest from 'supertest';

interface PrivilegeTestScenario {
user: {
Expand All @@ -26,7 +27,7 @@ interface PrivilegeTestRoute {

export function runPrivilegeTests(
routes: PrivilegeTestRoute[],
supertestWithoutAuth: SuperTest<supertest.Test>
supertestWithoutAuth: SupertestWithoutAuthProviderType
) {
for (const route of routes) {
describe(`${route.method} ${route.path}`, () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,10 @@
* 2.0.
*/

import { services as kibanaApiIntegrationServices } from '@kbn/test-suites-src/api_integration/services';
import { services as kibanaFunctionalServices } from '@kbn/test-suites-src/functional/services';
import { services as commonServices } from '../../../common/services';
import { services as apiIntegrationServices } from '../../../api_integration/services';

export const services = {
...commonServices,
esSupertestWithoutAuth: apiIntegrationServices.esSupertestWithoutAuth,
supertest: kibanaApiIntegrationServices.supertest,
supertestWithoutAuth: apiIntegrationServices.supertestWithoutAuth,
retry: kibanaApiIntegrationServices.retry,
esArchiver: kibanaFunctionalServices.esArchiver,
kibanaServer: kibanaFunctionalServices.kibanaServer,
};
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import type { Client } from '@elastic/elasticsearch';
import type { ToolingLog } from '@kbn/tooling-log';
import querystring from 'querystring';
import { KbnClient } from '@kbn/test';
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
import { routeWithNamespace, waitFor } from '../../../../common/utils/security_solution';

export const getAssetCriticalityIndex = (namespace?: string) =>
Expand Down Expand Up @@ -202,7 +203,7 @@ export const assetCriticalityRouteHelpersFactory = (
});

export const assetCriticalityRouteHelpersFactoryNoAuth = (
supertestWithoutAuth: SuperTest.Agent,
supertestWithoutAuth: SupertestWithoutAuthProviderType,
namespace?: string
) => ({
privilegesForUser: async ({ username, password }: { username: string; password: string }) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import { MappingTypeMapping } from '@elastic/elasticsearch/lib/api/types';
import { removeLegacyTransforms } from '@kbn/security-solution-plugin/server/lib/entity_analytics/utils/transforms';
import { EntityRiskScoreRecord } from '@kbn/security-solution-plugin/common/api/entity_analytics/common';
import { SupertestWithoutAuthProviderType } from '@kbn/ftr-common-functional-services';
import {
createRule,
waitForAlertsToBePresent,
Expand Down Expand Up @@ -579,7 +580,7 @@ interface Credentials {
}

export const riskEngineRouteHelpersFactoryNoAuth = (
supertestWithoutAuth: SuperTest.Agent,
supertestWithoutAuth: SupertestWithoutAuthProviderType,
namespace?: string
) => ({
privilegesForUser: async ({ username, password }: Credentials) =>
Expand Down
1 change: 0 additions & 1 deletion x-pack/test/spaces_api_integration/common/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,4 @@ import { services as apiIntegrationServices } from '../../api_integration/servic
export const services = {
...commonServices,
usageAPI: apiIntegrationServices.usageAPI,
supertestWithoutAuth: apiIntegrationServices.supertestWithoutAuth,
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
SavedObjectsImportFailure,
SavedObjectsImportAmbiguousConflictError,
} from '@kbn/core/server';
import { SuperTest } from 'supertest';
import { getAggregatedSpaceData, getUrlPrefix } from '../lib/space_test_utils';
import { DescribeFn, TestDefinitionAuthentication } from '../lib/types';
import { getTestDataLoader, SPACE_1, SPACE_2 } from '../../../common/lib/test_data_loader';
Expand Down Expand Up @@ -98,7 +99,9 @@ interface Aggs extends estypes.AggregationsMultiBucketAggregateBase {
}
export function copyToSpaceTestSuiteFactory(context: FtrProviderContext) {
const testDataLoader = getTestDataLoader(context);
const supertestWithoutAuth = context.getService('supertestWithoutAuth');
const supertestWithoutAuth = context.getService(
'supertestWithoutAuth'
) as unknown as SuperTest<any>;
const es = context.getService('es');

const collectSpaceContents = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import expect from '@kbn/expect';
import { SavedObject } from '@kbn/core/server';
import { DEFAULT_SPACE_ID } from '@kbn/spaces-plugin/common/constants';
import { CopyResponse } from '@kbn/spaces-plugin/server/lib/copy_to_spaces';
import { SuperTest } from 'supertest';
import { getUrlPrefix } from '../lib/space_test_utils';
import { DescribeFn, TestDefinitionAuthentication } from '../lib/types';
import type { FtrProviderContext } from '../ftr_provider_context';
Expand Down Expand Up @@ -69,7 +70,9 @@ const getDestinationSpace = (originSpaceId?: string) => {
export function resolveCopyToSpaceConflictsSuite(context: FtrProviderContext) {
const testDataLoader = getTestDataLoader(context);
const supertestWithAuth = context.getService('supertest');
const supertestWithoutAuth = context.getService('supertestWithoutAuth');
const supertestWithoutAuth = context.getService(
'supertestWithoutAuth'
) as unknown as SuperTest<any>;

const getVisualizationAtSpace = async (spaceId: string): Promise<SavedObject<any>> => {
return supertestWithAuth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { SuperTest } from 'supertest';
import { AUTHENTICATION } from '../../common/lib/authentication';
import { SPACES } from '../../common/lib/spaces';
import { createTestSuiteFactory } from '../../common/suites/create';
Expand All @@ -22,7 +23,7 @@ export default function createSpacesOnlySuite({ getService }: FtrProviderContext
expectConflictResponse,
expectRbacForbiddenResponse,
expectSolutionSpecifiedResult,
} = createTestSuiteFactory(esArchiver, supertestWithoutAuth);
} = createTestSuiteFactory(esArchiver, supertestWithoutAuth as unknown as SuperTest<any>);

describe('create', () => {
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* 2.0.
*/

import { SuperTest } from 'supertest';
import { AUTHENTICATION } from '../../common/lib/authentication';
import { SPACES } from '../../common/lib/spaces';
import { deleteTestSuiteFactory } from '../../common/suites/delete';
Expand All @@ -22,7 +23,7 @@ export default function deleteSpaceTestSuite({ getService }: FtrProviderContext)
expectEmptyResult,
expectNotFound,
expectReservedSpaceResult,
} = deleteTestSuiteFactory(es, esArchiver, supertestWithoutAuth);
} = deleteTestSuiteFactory(es, esArchiver, supertestWithoutAuth as unknown as SuperTest<any>);

describe('delete', () => {
[
Expand Down
Loading

0 comments on commit 477d07f

Please sign in to comment.