Skip to content

Commit

Permalink
[Cases] Decode responses from the user actions service (elastic#157971)
Browse files Browse the repository at this point in the history
## Summary

This PR decodes the response returned by the user actions service.

Depends on: elastic#157858,
elastic#156622

### Checklist

Delete any items that are not applicable to this PR.

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

### For maintainers

- [x] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and delanni committed May 25, 2023
1 parent 9e37b92 commit 36e400d
Show file tree
Hide file tree
Showing 10 changed files with 637 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,13 @@ const CaseUserActionDeprecatedResponseRt = rt.intersection([
/**
* This includes the comment_id but not the action_id or case_id
*/
const UserActionAttributes = rt.intersection([CaseUserActionBasicRt, CaseUserActionInjectedIdsRt]);
export const UserActionAttributesRt = rt.intersection([
CaseUserActionBasicRt,
CaseUserActionInjectedIdsRt,
]);

const UserActionRt = rt.intersection([
UserActionAttributes,
UserActionAttributesRt,
rt.type({
id: rt.string,
version: rt.string,
Expand All @@ -93,7 +96,7 @@ export type CaseUserActionsDeprecatedResponse = rt.TypeOf<
typeof CaseUserActionsDeprecatedResponseRt
>;
export type CaseUserActionDeprecatedResponse = rt.TypeOf<typeof CaseUserActionDeprecatedResponseRt>;
export type UserActionAttributes = rt.TypeOf<typeof UserActionAttributes>;
export type UserActionAttributes = rt.TypeOf<typeof UserActionAttributesRt>;

/**
* This defines the high level category for the user action. Whether the user add, removed, updated something
Expand Down
3 changes: 3 additions & 0 deletions x-pack/plugins/cases/server/common/types/user_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import type { SavedObject } from '@kbn/core/server';
import type { UserActionAttributes } from '../../../common/api';
import { UserActionAttributesRt } from '../../../common/api';
import type { User } from './user';

interface UserActionCommonPersistedAttributes {
Expand All @@ -21,5 +22,7 @@ export interface UserActionPersistedAttributes extends UserActionCommonPersisted
payload: Record<string, unknown>;
}

export const UserActionTransformedAttributesRt = UserActionAttributesRt;

export type UserActionTransformedAttributes = UserActionAttributes;
export type UserActionSavedObjectTransformed = SavedObject<UserActionTransformedAttributes>;
20 changes: 10 additions & 10 deletions x-pack/plugins/cases/server/services/cases/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1863,7 +1863,7 @@ describe('CasesService', () => {
});
});

describe('Encoding responses', () => {
describe('Decoding responses', () => {
const caseTransformedAttributesProps = CaseTransformedAttributesRt.types.reduce(
(acc, type) => ({ ...acc, ...type.props }),
{}
Expand All @@ -1883,7 +1883,7 @@ describe('CasesService', () => {
);

describe('getCase', () => {
it('encodes correctly', async () => {
it('decodes correctly', async () => {
unsecuredSavedObjectsClient.get.mockResolvedValue(createCaseSavedObjectResponse());

await expect(service.getCase({ id: 'a' })).resolves.not.toThrow();
Expand Down Expand Up @@ -1913,7 +1913,7 @@ describe('CasesService', () => {
});

describe('getResolveCase', () => {
it('encodes correctly', async () => {
it('decodes correctly', async () => {
unsecuredSavedObjectsClient.resolve.mockResolvedValue({
saved_object: createCaseSavedObjectResponse(),
outcome: 'exactMatch',
Expand Down Expand Up @@ -1952,7 +1952,7 @@ describe('CasesService', () => {
});

describe('getCases', () => {
it('encodes correctly', async () => {
it('decodes correctly', async () => {
unsecuredSavedObjectsClient.bulkGet.mockResolvedValue({
saved_objects: [
createCaseSavedObjectResponse({ caseId: '1' }),
Expand All @@ -1963,7 +1963,7 @@ describe('CasesService', () => {
await expect(service.getCases({ caseIds: ['a', 'b'] })).resolves.not.toThrow();
});

it('do not encodes errors', async () => {
it('do not decodes errors', async () => {
const errorSO = {
...omit(createCaseSavedObjectResponse({ caseId: '2' }), 'attributes'),
error: {
Expand Down Expand Up @@ -2078,7 +2078,7 @@ describe('CasesService', () => {
});

describe('findCases', () => {
it('encodes correctly', async () => {
it('decodes correctly', async () => {
const findMockReturn = createSOFindResponse([
createFindSO({ caseId: '1' }),
createFindSO({ caseId: '2' }),
Expand Down Expand Up @@ -2123,7 +2123,7 @@ describe('CasesService', () => {
});

describe('post', () => {
it('encodes correctly', async () => {
it('decodes correctly', async () => {
unsecuredSavedObjectsClient.create.mockResolvedValue(createCaseSavedObjectResponse());

await expect(
Expand Down Expand Up @@ -2166,7 +2166,7 @@ describe('CasesService', () => {
});

describe('patchCase', () => {
it('encodes correctly', async () => {
it('decodes correctly', async () => {
unsecuredSavedObjectsClient.update.mockResolvedValue(createUpdateSOResponse());

await expect(
Expand Down Expand Up @@ -2199,7 +2199,7 @@ describe('CasesService', () => {
});

describe('patchCases', () => {
it('encodes correctly', async () => {
it('decodes correctly', async () => {
unsecuredSavedObjectsClient.bulkUpdate.mockResolvedValue({
saved_objects: [
createCaseSavedObjectResponse({ caseId: '1' }),
Expand All @@ -2222,7 +2222,7 @@ describe('CasesService', () => {
).resolves.not.toThrow();
});

it('do not encodes errors', async () => {
it('do not decodes errors', async () => {
const errorSO = {
...omit(createCaseSavedObjectResponse({ caseId: '2' }), 'attributes'),
error: {
Expand Down
Loading

0 comments on commit 36e400d

Please sign in to comment.