Skip to content

Commit bce1a4d

Browse files
committed
Add tests
1 parent f463ce6 commit bce1a4d

9 files changed

Lines changed: 242 additions & 69 deletions

File tree

x-pack/plugins/actions/server/builtin_action_types/jira/service.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,19 @@ describe('Jira service', () => {
583583
'[Action][Jira]: Unable to get capabilities. Error: An error has occurred. Reason: Could not get capabilities'
584584
);
585585
});
586+
587+
test('it should throw an auth error', async () => {
588+
requestMock.mockImplementation(() => {
589+
const error: ResponseError = new Error('An error has occurred');
590+
// @ts-ignore this can happen!
591+
error.response = { data: 'Unauthorized' };
592+
throw error;
593+
});
594+
595+
await expect(service.getCapabilities()).rejects.toThrow(
596+
'[Action][Jira]: Unable to get capabilities. Error: An error has occurred. Reason: Unauthorized'
597+
);
598+
});
586599
});
587600

588601
describe('getIssueTypes', () => {

x-pack/plugins/actions/server/builtin_action_types/jira/service.ts

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ export const createExternalService = (
102102
return fields;
103103
};
104104

105-
const createErrorMessage = (errorResponse: ResponseError | null | undefined): string => {
105+
const createErrorMessage = (errorResponse: ResponseError | string | null | undefined): string => {
106106
if (errorResponse == null) {
107107
return '';
108108
}
109+
if (typeof errorResponse === 'string') {
110+
// Jira error.response.data can be string!!
111+
return errorResponse;
112+
}
109113

110114
const { errorMessages, errors } = errorResponse;
111115

@@ -181,11 +185,7 @@ export const createExternalService = (
181185
i18n.NAME,
182186
`Unable to get incident with id ${id}. Error: ${
183187
error.message
184-
} Reason: ${createErrorMessage(
185-
error.response?.data ?? error.response?.statusText
186-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
187-
: null
188-
)}`
188+
} Reason: ${createErrorMessage(error.response?.data)}`
189189
)
190190
);
191191
}
@@ -238,9 +238,7 @@ export const createExternalService = (
238238
getErrorMessage(
239239
i18n.NAME,
240240
`Unable to create incident. Error: ${error.message}. Reason: ${createErrorMessage(
241-
error.response?.data ?? error.response?.statusText
242-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
243-
: null
241+
error.response?.data
244242
)}`
245243
)
246244
);
@@ -282,11 +280,7 @@ export const createExternalService = (
282280
i18n.NAME,
283281
`Unable to update incident with id ${incidentId}. Error: ${
284282
error.message
285-
}. Reason: ${createErrorMessage(
286-
error.response?.data ?? error.response?.statusText
287-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
288-
: null
289-
)}`
283+
}. Reason: ${createErrorMessage(error.response?.data)}`
290284
)
291285
);
292286
}
@@ -317,11 +311,7 @@ export const createExternalService = (
317311
i18n.NAME,
318312
`Unable to create comment at incident with id ${incidentId}. Error: ${
319313
error.message
320-
}. Reason: ${createErrorMessage(
321-
error.response?.data ?? error.response?.statusText
322-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
323-
: null
324-
)}`
314+
}. Reason: ${createErrorMessage(error.response?.data)}`
325315
)
326316
);
327317
}
@@ -343,9 +333,7 @@ export const createExternalService = (
343333
getErrorMessage(
344334
i18n.NAME,
345335
`Unable to get capabilities. Error: ${error.message}. Reason: ${createErrorMessage(
346-
error.response?.data ?? error.response?.statusText
347-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
348-
: null
336+
error.response?.data
349337
)}`
350338
)
351339
);
@@ -384,9 +372,7 @@ export const createExternalService = (
384372
getErrorMessage(
385373
i18n.NAME,
386374
`Unable to get issue types. Error: ${error.message}. Reason: ${createErrorMessage(
387-
error.response?.data ?? error.response?.statusText
388-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
389-
: null
375+
error.response?.data
390376
)}`
391377
)
392378
);
@@ -431,9 +417,7 @@ export const createExternalService = (
431417
getErrorMessage(
432418
i18n.NAME,
433419
`Unable to get fields. Error: ${error.message}. Reason: ${createErrorMessage(
434-
error.response?.data ?? error.response?.statusText
435-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
436-
: null
420+
error.response?.data
437421
)}`
438422
)
439423
);
@@ -484,9 +468,7 @@ export const createExternalService = (
484468
getErrorMessage(
485469
i18n.NAME,
486470
`Unable to get issues. Error: ${error.message}. Reason: ${createErrorMessage(
487-
error.response?.data ?? error.response?.statusText
488-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
489-
: null
471+
error.response?.data
490472
)}`
491473
)
492474
);
@@ -510,9 +492,7 @@ export const createExternalService = (
510492
getErrorMessage(
511493
i18n.NAME,
512494
`Unable to get issue with id ${id}. Error: ${error.message}. Reason: ${createErrorMessage(
513-
error.response?.data ?? error.response?.statusText
514-
? { errors: { ok: error.response?.statusText }, errorMessages: null }
515-
: null
495+
error.response?.data
516496
)}`
517497
)
518498
);

x-pack/plugins/case/server/routes/api/cases/configure/get_configure.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { initGetCaseConfigure } from './get_configure';
1919
import { CASE_CONFIGURE_URL } from '../../../../../common/constants';
2020
import { mappings } from '../../../../client/configure/mock';
2121
import { ConnectorTypes } from '../../../../../common/api/connectors';
22+
import { CaseClient } from '../../../../client';
2223

2324
describe('GET configuration', () => {
2425
let routeHandler: RequestHandler<any, any, any>;
@@ -43,6 +44,7 @@ describe('GET configuration', () => {
4344
expect(res.status).toEqual(200);
4445
expect(res.payload).toEqual({
4546
...mockCaseConfigure[0].attributes,
47+
error: null,
4648
mappings: mappings[ConnectorTypes.jira],
4749
version: mockCaseConfigure[0].version,
4850
});
@@ -77,6 +79,7 @@ describe('GET configuration', () => {
7779
email: 'testemail@elastic.co',
7880
username: 'elastic',
7981
},
82+
error: null,
8083
mappings: mappings[ConnectorTypes.jira],
8184
updated_at: '2020-04-09T09:43:51.778Z',
8285
updated_by: {
@@ -122,4 +125,41 @@ describe('GET configuration', () => {
122125
expect(res.status).toEqual(404);
123126
expect(res.payload.isBoom).toEqual(true);
124127
});
128+
129+
it('returns an error when mappings request throws', async () => {
130+
const req = httpServerMock.createKibanaRequest({
131+
path: CASE_CONFIGURE_URL,
132+
method: 'get',
133+
});
134+
135+
const context = await createRouteContext(
136+
createMockSavedObjectsRepository({
137+
caseConfigureSavedObject: mockCaseConfigure,
138+
caseMappingsSavedObject: [],
139+
})
140+
);
141+
const mockGetMappings = jest.fn().mockImplementation(() => {
142+
throw new Error();
143+
});
144+
const mockThrowContext = {
145+
...context,
146+
case: {
147+
...context.case,
148+
getCaseClient: () =>
149+
({
150+
...context?.case?.getCaseClient(),
151+
getMappings: mockGetMappings,
152+
} as CaseClient),
153+
},
154+
};
155+
156+
const res = await routeHandler(mockThrowContext, req, kibanaResponseFactory);
157+
expect(res.status).toEqual(200);
158+
expect(res.payload).toEqual({
159+
...mockCaseConfigure[0].attributes,
160+
error: 'Error connecting to My connector 3 instance',
161+
mappings: [],
162+
version: mockCaseConfigure[0].version,
163+
});
164+
});
125165
});

x-pack/plugins/case/server/routes/api/cases/configure/patch_configure.test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { mockCaseConfigure } from '../../__fixtures__/mock_saved_objects';
1818
import { initPatchCaseConfigure } from './patch_configure';
1919
import { CASE_CONFIGURE_URL } from '../../../../../common/constants';
2020
import { ConnectorTypes } from '../../../../../common/api/connectors';
21+
import { CaseClient } from '../../../../client';
2122

2223
describe('PATCH configuration', () => {
2324
let routeHandler: RequestHandler<any, any, any>;
@@ -135,6 +136,53 @@ describe('PATCH configuration', () => {
135136
);
136137
});
137138

139+
it('patch configuration with error message for getMappings throw', async () => {
140+
const req = httpServerMock.createKibanaRequest({
141+
path: CASE_CONFIGURE_URL,
142+
method: 'patch',
143+
body: {
144+
closure_type: 'close-by-pushing',
145+
connector: {
146+
id: 'connector-new',
147+
name: 'New connector',
148+
type: '.jira',
149+
fields: null,
150+
},
151+
version: mockCaseConfigure[0].version,
152+
},
153+
});
154+
155+
const context = await createRouteContext(
156+
createMockSavedObjectsRepository({
157+
caseConfigureSavedObject: mockCaseConfigure,
158+
caseMappingsSavedObject: [],
159+
})
160+
);
161+
const mockGetMappings = jest.fn().mockImplementation(() => {
162+
throw new Error();
163+
});
164+
const mockThrowContext = {
165+
...context,
166+
case: {
167+
...context.case,
168+
getCaseClient: () =>
169+
({
170+
...context?.case?.getCaseClient(),
171+
getMappings: mockGetMappings,
172+
} as CaseClient),
173+
},
174+
};
175+
176+
const res = await routeHandler(mockThrowContext, req, kibanaResponseFactory);
177+
178+
expect(res.status).toEqual(200);
179+
expect(res.payload).toEqual(
180+
expect.objectContaining({
181+
mappings: [],
182+
error: 'Error connecting to New connector instance',
183+
})
184+
);
185+
});
138186
it('throw error when configuration have not being created', async () => {
139187
const req = httpServerMock.createKibanaRequest({
140188
path: CASE_CONFIGURE_URL,

x-pack/plugins/case/server/routes/api/cases/configure/post_configure.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import { initPostCaseConfigure } from './post_configure';
1919
import { newConfiguration } from '../../__mocks__/request_responses';
2020
import { CASE_CONFIGURE_URL } from '../../../../../common/constants';
2121
import { ConnectorTypes } from '../../../../../common/api/connectors';
22+
import { CaseClient } from '../../../../client';
2223

2324
describe('POST configuration', () => {
2425
let routeHandler: RequestHandler<any, any, any>;
@@ -64,6 +65,44 @@ describe('POST configuration', () => {
6465
})
6566
);
6667
});
68+
it('create configuration with error message for getMappings throw', async () => {
69+
const req = httpServerMock.createKibanaRequest({
70+
path: CASE_CONFIGURE_URL,
71+
method: 'post',
72+
body: newConfiguration,
73+
});
74+
75+
const context = await createRouteContext(
76+
createMockSavedObjectsRepository({
77+
caseConfigureSavedObject: mockCaseConfigure,
78+
caseMappingsSavedObject: [],
79+
})
80+
);
81+
const mockGetMappings = jest.fn().mockImplementation(() => {
82+
throw new Error();
83+
});
84+
const mockThrowContext = {
85+
...context,
86+
case: {
87+
...context.case,
88+
getCaseClient: () =>
89+
({
90+
...context?.case?.getCaseClient(),
91+
getMappings: mockGetMappings,
92+
} as CaseClient),
93+
},
94+
};
95+
96+
const res = await routeHandler(mockThrowContext, req, kibanaResponseFactory);
97+
98+
expect(res.status).toEqual(200);
99+
expect(res.payload).toEqual(
100+
expect.objectContaining({
101+
mappings: [],
102+
error: 'Error connecting to My connector 2 instance',
103+
})
104+
);
105+
});
67106

68107
it('create configuration without authentication', async () => {
69108
routeHandler = await createRoute(initPostCaseConfigure, 'post', true);

x-pack/plugins/security_solution/public/cases/components/configure_cases/mapping.test.tsx

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55
*/
66

77
import React from 'react';
8-
import { mount, ReactWrapper } from 'enzyme';
8+
import { mount } from 'enzyme';
99

1010
import { TestProviders } from '../../../common/mock';
1111
import { Mapping, MappingProps } from './mapping';
1212
import { mappings } from './__mock__';
1313

1414
describe('Mapping', () => {
15-
let wrapper: ReactWrapper;
16-
const setEditFlyoutVisibility = jest.fn();
1715
const props: MappingProps = {
1816
connectorActionTypeId: '.servicenow',
1917
isLoading: false,
@@ -22,39 +20,27 @@ describe('Mapping', () => {
2220

2321
beforeEach(() => {
2422
jest.clearAllMocks();
25-
wrapper = mount(<Mapping {...props} />, { wrappingComponent: TestProviders });
2623
});
27-
28-
afterEach(() => {
29-
wrapper.unmount();
24+
test('it shows mapping form group', () => {
25+
const wrapper = mount(<Mapping {...props} />, { wrappingComponent: TestProviders });
26+
expect(wrapper.find('[data-test-subj="static-mappings"]').first().exists()).toBe(true);
3027
});
31-
describe('Common', () => {
32-
test('it shows mapping form group', () => {
33-
expect(wrapper.find('[data-test-subj="static-mappings"]').first().exists()).toBe(true);
34-
});
35-
36-
test('correctly maps fields', () => {
37-
expect(wrapper.find('[data-test-subj="field-mapping-source"] code').first().text()).toBe(
38-
'title'
39-
);
40-
expect(wrapper.find('[data-test-subj="field-mapping-target"] code').first().text()).toBe(
41-
'short_description'
42-
);
43-
});
44-
// skipping until next PR
45-
test.skip('it shows the update button', () => {
46-
expect(
47-
wrapper.find('[data-test-subj="case-mappings-update-connector-button"]').first().exists()
48-
).toBe(true);
49-
});
5028

51-
test.skip('it triggers update flyout', () => {
52-
expect(setEditFlyoutVisibility).not.toHaveBeenCalled();
53-
wrapper
54-
.find('button[data-test-subj="case-mappings-update-connector-button"]')
55-
.first()
56-
.simulate('click');
57-
expect(setEditFlyoutVisibility).toHaveBeenCalled();
29+
test('correctly maps fields', () => {
30+
const wrapper = mount(<Mapping {...props} />, { wrappingComponent: TestProviders });
31+
expect(wrapper.find('[data-test-subj="field-mapping-source"] code').first().text()).toBe(
32+
'title'
33+
);
34+
expect(wrapper.find('[data-test-subj="field-mapping-target"] code').first().text()).toBe(
35+
'short_description'
36+
);
37+
});
38+
test('displays connection warning when isLoading: false and mappings: []', () => {
39+
const wrapper = mount(<Mapping {...{ ...props, mappings: [] }} />, {
40+
wrappingComponent: TestProviders,
5841
});
42+
expect(wrapper.find('[data-test-subj="field-mapping-desc"]').first().text()).toBe(
43+
'Field mappings require an established connection to ServiceNow. Please check your connection credentials.'
44+
);
5945
});
6046
});

0 commit comments

Comments
 (0)