Skip to content

Commit

Permalink
update return value and add a unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Tianle Huang <[email protected]>
  • Loading branch information
tianleh committed Jan 3, 2024
1 parent b215211 commit 8dbe9d8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
26 changes: 26 additions & 0 deletions src/plugins/csp_configuration_provider/server/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,32 @@ describe('Provider', () => {
);
});

it('returns empty when value field is missing in opensearch response', async () => {
const cspRules = "frame-ancestors 'self'";

opensearchClient.search.mockImplementation(() => {
return opensearchClientMock.createSuccessTransportRequestPromise({
hits: {
hits: [
{
_source: {
value_typo: cspRules,
},
},
],
},
} as SearchResponse<any>);
});

const client = new OpenSearchCspClient(opensearchClient);
const getValue = await client.get(INDEX_NAME, INDEX_DOCUMENT_NAME);

expect(getValue).toBe('');
expect(opensearchClient.search).toBeCalledWith(
getSearchInput(INDEX_NAME, INDEX_DOCUMENT_NAME)
);
});

it('returns empty string when opensearch errors happen', async () => {
opensearchClient.search.mockImplementation(() => {
return opensearchClientMock.createErrorTransportRequestPromise(new Error(ERROR_MESSAGE));
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/csp_configuration_provider/server/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class OpenSearchCspClient implements CspClient {
body: query,
});

value = data?.body?.hits?.hits[0]?._source?.value;
value = data?.body?.hits?.hits[0]?._source?.value || '';
} catch (e) {
// eslint-disable-next-line no-console
console.error(
Expand Down

0 comments on commit 8dbe9d8

Please sign in to comment.