Skip to content

Commit

Permalink
Merge 403 and unsupported type errors into single error
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecote committed Apr 26, 2019
1 parent 38b402b commit 99aea10
Show file tree
Hide file tree
Showing 44 changed files with 564 additions and 619 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -313,75 +313,6 @@ describe('Flyout', () => {
});
});

describe('errors', () => {
const { importFile } = require('../../../../../lib/import_file');
const { resolveImportErrors } = require('../../../../../lib/resolve_import_errors');

it('should display unsupported type errors properly', async () => {
const component = shallowWithIntl(<Flyout.WrappedComponent {...defaultProps} />);

// Ensure all promises resolve
await Promise.resolve();
// Ensure the state changes are reflected
component.update();

importFile.mockImplementation(() => ({
success: false,
successCount: 0,
errors: [
{
id: '1',
type: 'wigwags',
title: 'My Title',
error: {
type: 'unsupported_type',
}
},
],
}));
resolveImportErrors.mockImplementation(() => ({
status: 'success',
importCount: 0,
failedImports: [
{
error: {
type: 'unsupported_type',
},
obj: {
id: '1',
type: 'wigwags',
title: 'My Title',
},
},
],
}));

component.setState({ file: mockFile, isLegacyFile: false });

// Go through the import flow
await component.instance().import();
component.update();

// Ensure all promises resolve
await Promise.resolve();

expect(component.state('status')).toBe('success');
expect(component.state('failedImports')).toEqual([
{
error: {
type: 'unsupported_type',
},
obj: {
id: '1',
type: 'wigwags',
title: 'My Title',
},
},
]);
expect(component.find('EuiFlyout EuiCallOut')).toMatchSnapshot();
});
});

describe('legacy conflicts', () => {
const { importLegacyFile } = require('../../../../../lib/import_legacy_file');
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,17 +602,6 @@ class FlyoutUI extends Component {
}
);
});
} else if (error.type === 'unsupported_type') {
return intl.formatMessage(
{
id: 'kbn.management.objects.objectsTable.flyout.importFailedUnsupportedType',
defaultMessage: '{type} [id={id}] unsupported type',
},
{
id: obj.id,
type: obj.type,
},
);
}
return getField(error, 'body.message', error.message || '');
}).join(' ')}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ describe('getSortedObjectsForExport()', () => {
const savedObjectsClient = {
errors: {} as any,
find: jest.fn(),
canFind: jest.fn(),
bulkGet: jest.fn(),
canBulkGet: jest.fn(),
create: jest.fn(),
bulkCreate: jest.fn(),
canBulkCreate: jest.fn(),
delete: jest.fn(),
get: jest.fn(),
update: jest.fn(),
};

afterEach(() => {
savedObjectsClient.find.mockReset();
savedObjectsClient.bulkGet.mockReset();
savedObjectsClient.create.mockReset();
savedObjectsClient.bulkCreate.mockReset();
savedObjectsClient.delete.mockReset();
savedObjectsClient.get.mockReset();
savedObjectsClient.update.mockReset();
beforeEach(() => {
jest.resetAllMocks();
savedObjectsClient.canBulkCreate.mockImplementation((types: string[]) =>
types.map(type => ({ type, can: true }))
);
savedObjectsClient.canBulkGet.mockImplementation((types: string[]) =>
types.map(type => ({ type, can: true }))
);
savedObjectsClient.canFind.mockImplementation((types: string[]) =>
types.map(type => ({ type, can: true }))
);
});

test('exports selected types and sorts them', async () => {
Expand Down Expand Up @@ -66,6 +72,7 @@ describe('getSortedObjectsForExport()', () => {
savedObjectsClient,
exportSizeLimit: 500,
types: ['index-pattern', 'search'],
supportedTypes: ['index-pattern', 'search'],
});
expect(response).toMatchInlineSnapshot(`
Array [
Expand Down Expand Up @@ -137,6 +144,7 @@ Array [
savedObjectsClient,
exportSizeLimit: 1,
types: ['index-pattern', 'search'],
supportedTypes: ['index-pattern', 'search'],
})
).rejects.toThrowErrorMatchingInlineSnapshot(`"Can't export more than 1 objects"`);
});
Expand Down Expand Up @@ -165,6 +173,7 @@ Array [
exportSizeLimit: 10000,
savedObjectsClient,
types: ['index-pattern', 'search'],
supportedTypes: ['index-pattern', 'search'],
objects: [
{
type: 'index-pattern',
Expand Down Expand Up @@ -249,6 +258,7 @@ Array [
exportSizeLimit: 10000,
savedObjectsClient,
types: ['index-pattern', 'search'],
supportedTypes: ['index-pattern', 'search'],
objects: [
{
type: 'search',
Expand Down Expand Up @@ -315,6 +325,7 @@ Array [
exportSizeLimit: 1,
savedObjectsClient,
types: ['index-pattern', 'search'],
supportedTypes: ['index-pattern', 'search'],
objects: [
{
type: 'index-pattern',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,49 @@ interface ExportObjectsOptions {
savedObjectsClient: SavedObjectsClient;
exportSizeLimit: number;
includeReferencesDeep?: boolean;
supportedTypes: string[];
}

function throwIfInvalidTypes(
action: string,
objectTypes: string[],
supportedTypes: string[],
authorizedTypes: Array<{ type: string; can: boolean }>
) {
const invalidTypes = new Set([
...objectTypes.filter(type => !supportedTypes.includes(type)),
...authorizedTypes.filter(resp => resp.can === false).map(resp => resp.type),
]);
if (invalidTypes.size) {
throw Boom.badRequest(
`Unable to ${action} ${Array.from(invalidTypes)
.sort()
.join(',')}`
);
}
}

async function fetchObjectsToExport({
objects,
types,
exportSizeLimit,
savedObjectsClient,
supportedTypes,
}: {
objects?: ObjectToExport[];
types?: string[];
exportSizeLimit: number;
savedObjectsClient: SavedObjectsClient;
supportedTypes: string[];
}) {
if (objects) {
const objectTypes = [...new Set(objects.map(obj => obj.type))];
throwIfInvalidTypes(
'bulk_get',
objectTypes,
supportedTypes,
await savedObjectsClient.canBulkGet(objectTypes)
);
if (objects.length > exportSizeLimit) {
throw Boom.badRequest(`Can't export more than ${exportSizeLimit} objects`);
}
Expand All @@ -61,6 +90,13 @@ async function fetchObjectsToExport({
}
return bulkGetResult.saved_objects;
}

throwIfInvalidTypes(
'find',
types || [],
supportedTypes,
await savedObjectsClient.canFind(types || [])
);
const findResponse = await savedObjectsClient.find({
type: types,
sortField: '_id',
Expand All @@ -79,12 +115,14 @@ export async function getSortedObjectsForExport({
savedObjectsClient,
exportSizeLimit,
includeReferencesDeep = false,
supportedTypes,
}: ExportObjectsOptions) {
const objectsToExport = await fetchObjectsToExport({
types,
objects,
savedObjectsClient,
exportSizeLimit,
supportedTypes,
});
return sortObjects(
includeReferencesDeep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ describe('injectNestedDependencies', () => {
const savedObjectsClient = {
errors: {} as any,
find: jest.fn(),
canFind: jest.fn(),
bulkGet: jest.fn(),
canBulkGet: jest.fn(),
create: jest.fn(),
bulkCreate: jest.fn(),
canBulkCreate: jest.fn(),
delete: jest.fn(),
get: jest.fn(),
update: jest.fn(),
Expand Down
Loading

0 comments on commit 99aea10

Please sign in to comment.