-
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.
- Loading branch information
1 parent
eec0fc1
commit 2464efd
Showing
12 changed files
with
293 additions
and
11 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
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
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
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
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
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
76 changes: 76 additions & 0 deletions
76
...-objects/core-saved-objects-api-server-internal/src/lib/apis/remove_references_to.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,76 @@ | ||
/* | ||
* 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 { apiContextMock, ApiExecutionContextMock } from '../../mocks'; | ||
import { createType } from '../../test_helpers/repository.test.common'; | ||
import { performRemoveReferencesTo } from './remove_references_to'; | ||
|
||
const fooType = createType('foo', {}); | ||
const barType = createType('bar', {}); | ||
|
||
describe('performRemoveReferencesTo', () => { | ||
const namespace = 'some_ns'; | ||
const indices = ['.kib_1', '.kib_2']; | ||
let apiExecutionContext: ApiExecutionContextMock; | ||
|
||
beforeEach(() => { | ||
apiExecutionContext = apiContextMock.create(); | ||
apiExecutionContext.registry.registerType(fooType); | ||
apiExecutionContext.registry.registerType(barType); | ||
|
||
apiExecutionContext.helpers.common.getCurrentNamespace.mockImplementation( | ||
(space) => space ?? 'default' | ||
); | ||
apiExecutionContext.helpers.common.getIndicesForTypes.mockReturnValue(indices); | ||
}); | ||
|
||
describe('with all extensions enabled', () => { | ||
it('calls getCurrentNamespace with the correct parameters', async () => { | ||
await performRemoveReferencesTo( | ||
{ type: 'foo', id: 'id', options: { namespace } }, | ||
apiExecutionContext | ||
); | ||
|
||
const commonHelper = apiExecutionContext.helpers.common; | ||
expect(commonHelper.getCurrentNamespace).toHaveBeenCalledTimes(1); | ||
expect(commonHelper.getCurrentNamespace).toHaveBeenLastCalledWith(namespace); | ||
}); | ||
|
||
it('calls authorizeRemoveReferences with the correct parameters', async () => { | ||
await performRemoveReferencesTo( | ||
{ type: 'foo', id: 'id', options: { namespace } }, | ||
apiExecutionContext | ||
); | ||
|
||
const securityExt = apiExecutionContext.extensions.securityExtension!; | ||
expect(securityExt.authorizeRemoveReferences).toHaveBeenCalledTimes(1); | ||
expect(securityExt.authorizeRemoveReferences).toHaveBeenLastCalledWith({ | ||
namespace, | ||
object: { type: 'foo', id: 'id' }, | ||
}); | ||
}); | ||
|
||
it('calls client.updateByQuery with the correct parameters', async () => { | ||
await performRemoveReferencesTo( | ||
{ type: 'foo', id: 'id', options: { namespace, refresh: false } }, | ||
apiExecutionContext | ||
); | ||
|
||
const client = apiExecutionContext.client; | ||
expect(client.updateByQuery).toHaveBeenCalledTimes(1); | ||
expect(client.updateByQuery).toHaveBeenLastCalledWith( | ||
{ | ||
refresh: false, | ||
index: indices, | ||
body: expect.any(Object), | ||
}, | ||
{ ignore: [404], meta: true } | ||
); | ||
}); | ||
}); | ||
}); |
47 changes: 47 additions & 0 deletions
47
...s/core/saved-objects/core-saved-objects-api-server-internal/src/mocks/api_context.mock.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,47 @@ | ||
/* | ||
* 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 { loggerMock, MockedLogger } from '@kbn/logging-mocks'; | ||
import { | ||
elasticsearchClientMock, | ||
ElasticsearchClientMock, | ||
} from '@kbn/core-elasticsearch-client-server-mocks'; | ||
import { SavedObjectTypeRegistry } from '@kbn/core-saved-objects-base-server-internal'; | ||
import { serializerMock } from '@kbn/core-saved-objects-base-server-mocks'; | ||
import type { ApiExecutionContext } from '../lib/apis/types'; | ||
import { apiHelperMocks, RepositoryHelpersMock } from './api_helpers.mocks'; | ||
import { savedObjectsExtensionsMock } from './saved_objects_extensions.mock'; | ||
import { createMigratorMock, KibanaMigratorMock } from './migrator.mock'; | ||
|
||
export type ApiExecutionContextMock = Pick<ApiExecutionContext, 'allowedTypes' | 'mappings'> & { | ||
registry: SavedObjectTypeRegistry; | ||
helpers: RepositoryHelpersMock; | ||
extensions: ReturnType<typeof savedObjectsExtensionsMock.create>; | ||
client: ElasticsearchClientMock; | ||
serializer: ReturnType<typeof serializerMock.create>; | ||
migrator: KibanaMigratorMock; | ||
logger: MockedLogger; | ||
}; | ||
|
||
const createApiExecutionContextMock = (): ApiExecutionContextMock => { | ||
return { | ||
registry: new SavedObjectTypeRegistry(), | ||
helpers: apiHelperMocks.create(), | ||
extensions: savedObjectsExtensionsMock.create(), | ||
client: elasticsearchClientMock.createElasticsearchClient(), | ||
serializer: serializerMock.create(), | ||
migrator: createMigratorMock(), | ||
logger: loggerMock.create(), | ||
allowedTypes: ['foo', 'bar'], | ||
mappings: { properties: { mockMappings: { type: 'text' } } }, | ||
}; | ||
}; | ||
|
||
export const apiContextMock = { | ||
create: createApiExecutionContextMock, | ||
}; |
110 changes: 110 additions & 0 deletions
110
.../core/saved-objects/core-saved-objects-api-server-internal/src/mocks/api_helpers.mocks.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,110 @@ | ||
/* | ||
* 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 type { PublicMethodsOf } from '@kbn/utility-types'; | ||
import type { | ||
CommonHelper, | ||
EncryptionHelper, | ||
ValidationHelper, | ||
PreflightCheckHelper, | ||
SerializerHelper, | ||
} from '../lib/apis/helpers'; | ||
|
||
export type CommonHelperMock = jest.Mocked<PublicMethodsOf<CommonHelper>>; | ||
|
||
const createCommonHelperMock = (): CommonHelperMock => { | ||
const mock: CommonHelperMock = { | ||
createPointInTimeFinder: jest.fn(), | ||
getIndexForType: jest.fn(), | ||
getIndicesForTypes: jest.fn(), | ||
getCurrentNamespace: jest.fn(), | ||
getValidId: jest.fn(), | ||
}; | ||
|
||
mock.getIndexForType.mockReturnValue('.kibana_mock'); | ||
mock.getIndicesForTypes.mockReturnValue(['.kibana_mock']); | ||
mock.getCurrentNamespace.mockImplementation((space) => space ?? 'default'); | ||
mock.getValidId.mockReturnValue('valid-id'); | ||
|
||
return mock; | ||
}; | ||
|
||
export type EncryptionHelperMock = jest.Mocked<PublicMethodsOf<EncryptionHelper>>; | ||
|
||
const createEncryptionHelperMock = (): EncryptionHelperMock => { | ||
const mock: EncryptionHelperMock = { | ||
optionallyEncryptAttributes: jest.fn(), | ||
optionallyDecryptAndRedactSingleResult: jest.fn(), | ||
optionallyDecryptAndRedactBulkResult: jest.fn(), | ||
}; | ||
|
||
return mock; | ||
}; | ||
|
||
export type ValidationHelperMock = jest.Mocked<PublicMethodsOf<ValidationHelper>>; | ||
|
||
const createValidationHelperMock = (): ValidationHelperMock => { | ||
const mock: ValidationHelperMock = { | ||
validateInitialNamespaces: jest.fn(), | ||
validateObjectNamespaces: jest.fn(), | ||
validateObjectForCreate: jest.fn(), | ||
validateOriginId: jest.fn(), | ||
}; | ||
|
||
return mock; | ||
}; | ||
|
||
export type SerializerHelperMock = jest.Mocked<PublicMethodsOf<SerializerHelper>>; | ||
|
||
const createSerializerHelperMock = (): SerializerHelperMock => { | ||
const mock: SerializerHelperMock = { | ||
rawToSavedObject: jest.fn(), | ||
}; | ||
|
||
return mock; | ||
}; | ||
|
||
export type PreflightCheckHelperMock = jest.Mocked<PublicMethodsOf<PreflightCheckHelper>>; | ||
|
||
const createPreflightCheckHelperMock = (): PreflightCheckHelperMock => { | ||
const mock: PreflightCheckHelperMock = { | ||
preflightCheckForCreate: jest.fn(), | ||
preflightCheckForBulkDelete: jest.fn(), | ||
preflightCheckNamespaces: jest.fn(), | ||
preflightCheckForUpsertAliasConflict: jest.fn(), | ||
}; | ||
|
||
return mock; | ||
}; | ||
|
||
export interface RepositoryHelpersMock { | ||
common: CommonHelperMock; | ||
encryption: EncryptionHelperMock; | ||
validation: ValidationHelperMock; | ||
preflight: PreflightCheckHelperMock; | ||
serializer: SerializerHelperMock; | ||
} | ||
|
||
const createRepositoryHelpersMock = (): RepositoryHelpersMock => { | ||
return { | ||
common: createCommonHelperMock(), | ||
encryption: createEncryptionHelperMock(), | ||
validation: createValidationHelperMock(), | ||
preflight: createPreflightCheckHelperMock(), | ||
serializer: createSerializerHelperMock(), | ||
}; | ||
}; | ||
|
||
export const apiHelperMocks = { | ||
create: createRepositoryHelpersMock, | ||
createCommonHelper: createCommonHelperMock, | ||
createEncryptionHelper: createEncryptionHelperMock, | ||
createValidationHelper: createValidationHelperMock, | ||
createSerializerHelper: createSerializerHelperMock, | ||
createPreflightCheckHelper: createPreflightCheckHelperMock, | ||
}; |
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
22 changes: 22 additions & 0 deletions
22
...ages/core/saved-objects/core-saved-objects-api-server-internal/src/mocks/migrator.mock.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,22 @@ | ||
/* | ||
* 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 type { IKibanaMigrator } from '@kbn/core-saved-objects-base-server-internal'; | ||
|
||
export type KibanaMigratorMock = jest.Mocked<IKibanaMigrator>; | ||
|
||
export const createMigratorMock = (kibanaVersion: string = '8.0.0'): KibanaMigratorMock => { | ||
return { | ||
kibanaVersion, | ||
runMigrations: jest.fn(), | ||
prepareMigrations: jest.fn(), | ||
getStatus$: jest.fn(), | ||
getActiveMappings: jest.fn(), | ||
migrateDocument: jest.fn(), | ||
}; | ||
}; |
Oops, something went wrong.