Skip to content

Commit

Permalink
Merge branch 'master' into task-manager/update-scheduled-task
Browse files Browse the repository at this point in the history
  • Loading branch information
gmmorris committed Nov 21, 2019
2 parents ed78933 + 46e0f9f commit 0abcf92
Show file tree
Hide file tree
Showing 44 changed files with 917 additions and 751 deletions.
50 changes: 0 additions & 50 deletions src/core/server/ui_settings/create_objects_client_stub.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export const createOrUpgradeSavedConfigMock = jest.fn();
jest.doMock('./create_or_upgrade_saved_config', () => ({
createOrUpgradeSavedConfig: createOrUpgradeSavedConfigMock,
}));
Original file line number Diff line number Diff line change
Expand Up @@ -17,34 +17,35 @@
* under the License.
*/

import sinon from 'sinon';
import Chance from 'chance';

import { SavedObjectsErrorHelpers } from '../../saved_objects';

import { savedObjectsClientMock } from '../../saved_objects/service/saved_objects_client.mock';
import { loggingServiceMock } from '../../logging/logging_service.mock';
import * as getUpgradeableConfigNS from './get_upgradeable_config';
import { getUpgradeableConfigMock } from './get_upgradeable_config.test.mock';

import { createOrUpgradeSavedConfig } from './create_or_upgrade_saved_config';

const chance = new Chance();
describe('uiSettings/createOrUpgradeSavedConfig', function() {
const sandbox = sinon.createSandbox();
afterEach(() => sandbox.restore());
afterEach(() => jest.resetAllMocks());

const version = '4.0.1';
const prevVersion = '4.0.0';
const buildNum = chance.integer({ min: 1000, max: 5000 });

function setup() {
const logger = loggingServiceMock.create();
const getUpgradeableConfig = sandbox.stub(getUpgradeableConfigNS, 'getUpgradeableConfig');
const savedObjectsClient = {
create: sinon.stub().callsFake(async (type, attributes, options = {}) => ({
type,
id: options.id,
version: 'foo',
})),
} as any; // mute until we have savedObjects mocks
const getUpgradeableConfig = getUpgradeableConfigMock;
const savedObjectsClient = savedObjectsClientMock.create();
savedObjectsClient.create.mockImplementation(
async (type, attributes, options = {}) =>
({
type,
id: options.id,
version: 'foo',
} as any)
);

async function run(options = {}) {
const resp = await createOrUpgradeSavedConfig({
Expand All @@ -56,8 +57,8 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {
...options,
});

sinon.assert.calledOnce(getUpgradeableConfig);
sinon.assert.alwaysCalledWith(getUpgradeableConfig, { savedObjectsClient, version });
expect(getUpgradeableConfigMock).toHaveBeenCalledTimes(1);
expect(getUpgradeableConfig).toHaveBeenCalledWith({ savedObjectsClient, version });

return resp;
}
Expand All @@ -78,9 +79,8 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {

await run();

sinon.assert.calledOnce(savedObjectsClient.create);
sinon.assert.calledWithExactly(
savedObjectsClient.create,
expect(savedObjectsClient.create).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.create).toHaveBeenCalledWith(
'config',
{
buildNum,
Expand All @@ -103,7 +103,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {
[chance.word()]: chance.sentence(),
};

getUpgradeableConfig.resolves({
getUpgradeableConfig.mockResolvedValue({
id: prevVersion,
attributes: savedAttributes,
type: '',
Expand All @@ -112,10 +112,9 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {

await run();

sinon.assert.calledOnce(getUpgradeableConfig);
sinon.assert.calledOnce(savedObjectsClient.create);
sinon.assert.calledWithExactly(
savedObjectsClient.create,
expect(getUpgradeableConfig).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.create).toHaveBeenCalledTimes(1);
expect(savedObjectsClient.create).toHaveBeenCalledWith(
'config',
{
...savedAttributes,
Expand All @@ -130,7 +129,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {
it('should log a message for upgrades', async () => {
const { getUpgradeableConfig, logger, run } = setup();

getUpgradeableConfig.resolves({
getUpgradeableConfig.mockResolvedValue({
id: prevVersion,
attributes: { buildNum: buildNum - 100 },
type: '',
Expand All @@ -154,16 +153,14 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {
it('does not log when upgrade fails', async () => {
const { getUpgradeableConfig, logger, run, savedObjectsClient } = setup();

getUpgradeableConfig.resolves({
getUpgradeableConfig.mockResolvedValue({
id: prevVersion,
attributes: { buildNum: buildNum - 100 },
type: '',
references: [],
});

savedObjectsClient.create.callsFake(async () => {
throw new Error('foo');
});
savedObjectsClient.create.mockRejectedValue(new Error('foo'));

try {
await run();
Expand All @@ -181,9 +178,7 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {
it('throws write errors', async () => {
const { run, savedObjectsClient } = setup();
const error = new Error('foo');
savedObjectsClient.create.callsFake(async () => {
throw error;
});
savedObjectsClient.create.mockRejectedValue(error);

await expect(run({ handleWriteErrors: false })).rejects.toThrowError(error);
});
Expand All @@ -192,15 +187,17 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {
it('returns undefined for ConflictError', async () => {
const { run, savedObjectsClient } = setup();
const error = new Error('foo');
savedObjectsClient.create.throws(SavedObjectsErrorHelpers.decorateConflictError(error));
savedObjectsClient.create.mockRejectedValue(
SavedObjectsErrorHelpers.decorateConflictError(error)
);

expect(await run({ handleWriteErrors: true })).toBe(undefined);
});

it('returns config attributes for NotAuthorizedError', async () => {
const { run, savedObjectsClient } = setup();
const error = new Error('foo');
savedObjectsClient.create.throws(
savedObjectsClient.create.mockRejectedValue(
SavedObjectsErrorHelpers.decorateNotAuthorizedError(error)
);

Expand All @@ -212,7 +209,9 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {
it('returns config attributes for ForbiddenError', async () => {
const { run, savedObjectsClient } = setup();
const error = new Error('foo');
savedObjectsClient.create.throws(SavedObjectsErrorHelpers.decorateForbiddenError(error));
savedObjectsClient.create.mockRejectedValue(
SavedObjectsErrorHelpers.decorateForbiddenError(error)
);

expect(await run({ handleWriteErrors: true })).toEqual({
buildNum,
Expand All @@ -222,15 +221,17 @@ describe('uiSettings/createOrUpgradeSavedConfig', function() {
it('throws error for other SavedObjects exceptions', async () => {
const { run, savedObjectsClient } = setup();
const error = new Error('foo');
savedObjectsClient.create.throws(SavedObjectsErrorHelpers.decorateGeneralError(error));
savedObjectsClient.create.mockRejectedValue(
SavedObjectsErrorHelpers.decorateGeneralError(error)
);

await expect(run({ handleWriteErrors: true })).rejects.toThrowError(error);
});

it('throws error for all other exceptions', async () => {
const { run, savedObjectsClient } = setup();
const error = new Error('foo');
savedObjectsClient.create.throws(error);
savedObjectsClient.create.mockRejectedValue(error);

await expect(run({ handleWriteErrors: true })).rejects.toThrowError(error);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
* under the License.
*/

import sinon from 'sinon';

export function assertSinonMatch(value: any, match: any) {
const stub = sinon.stub();
stub(value);
sinon.assert.calledWithExactly(stub, match);
}
export const getUpgradeableConfigMock = jest.fn();
jest.doMock('./get_upgradeable_config', () => ({
getUpgradeableConfig: getUpgradeableConfigMock,
}));
Loading

0 comments on commit 0abcf92

Please sign in to comment.