Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: permissions check for cross space reference editor [FUS-115] #1818

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as React from 'react';

import { FieldAppSDK } from '@contentful/app-sdk';
import '@testing-library/jest-dom/extend-expect';
import { fireEvent, render, screen } from '@testing-library/react';

import * as React from 'react';

import { useResource } from '../common/EntityStore';
import { useEditorPermissions } from '../common/useEditorPermissions';
import { MultipleResourceReferenceEditor } from './MultipleResourceReferenceEditor';
import { createFakeEntryResource, mockSdkForField } from './testHelpers/resourceEditorHelpers';

Expand All @@ -23,6 +24,12 @@ jest.mock('../common/EntityStore', () => {
};
});

jest.mock('../common/useEditorPermissions', () => {
return {
useEditorPermissions: jest.fn(),
};
});

jest.mock('react-intersection-observer', () => {
const module = jest.requireActual('react-intersection-observer');

Expand All @@ -49,6 +56,12 @@ const fieldDefinition = {
validations: [],
};

const mockedUseEditorPermissions = useEditorPermissions as jest.Mock;

beforeEach(() => {
mockedUseEditorPermissions.mockImplementation(() => ({ canLinkEntity: true }));
});

describe('Multiple resource editor', () => {
it('renders the action button when no value is set', async () => {
const sdk: FieldAppSDK = mockSdkForField(fieldDefinition);
Expand Down Expand Up @@ -76,6 +89,26 @@ describe('Multiple resource editor', () => {
});
});

it('hides the action button when insufficient permissions', async () => {
mockedUseEditorPermissions.mockImplementation(() => ({ canLinkEntity: false }));

const sdk: FieldAppSDK = mockSdkForField(fieldDefinition);
render(
<MultipleResourceReferenceEditor
getEntryRouteHref={() => ''}
isInitiallyDisabled={false}
sdk={sdk}
hasCardEditActions={true}
viewType="card"
// @ts-expect-error unused...
parameters={{}}
/>
);

const noPermission = await screen.findByText(/You don't have permission to view this content/);
expect(noPermission).toBeDefined();
});

it('renders custom actions when passed', async () => {
const sdk: FieldAppSDK = mockSdkForField(fieldDefinition);
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@ type ChildProps = {
type EditorProps = ReferenceEditorProps &
Omit<ChildProps, 'onSortStart' | 'onSortEnd' | 'onMove' | 'onRemoteItemAtIndex'> & {
children: (props: ReferenceEditorProps & ChildProps) => React.ReactElement;
apiUrl: string;
};

function ResourceEditor(props: EditorProps) {
const { setValue, items, apiUrl } = props;
const { setValue, items } = props;

const onSortStart = () => noop();
const onSortEnd = useCallback(
Expand All @@ -57,11 +56,9 @@ function ResourceEditor(props: EditorProps) {
[items, setValue]
);

const { dialogs, field } = props.sdk;
const linkActionsProps = useResourceLinkActions({
dialogs,
field,
apiUrl,
sdk: props.sdk,
parameters: props.parameters,
});

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import '@testing-library/jest-dom/extend-expect';
import { fireEvent, render, screen } from '@testing-library/react';

import { useResource } from '../common/EntityStore';
import { useEditorPermissions } from '../common/useEditorPermissions';
import { SingleResourceReferenceEditor } from './SingleResourceReferenceEditor';
import { createFakeEntryResource, mockSdkForField } from './testHelpers/resourceEditorHelpers';

Expand All @@ -31,6 +32,12 @@ jest.mock('react-intersection-observer', () => {
};
});

jest.mock('../common/useEditorPermissions', () => {
return {
useEditorPermissions: jest.fn(),
};
});

const fieldDefinition = {
type: 'ResourceLink',
id: 'foo',
Expand All @@ -44,6 +51,13 @@ const fieldDefinition = {
required: true,
validations: [],
};

const mockedUseEditorPermissions = useEditorPermissions as jest.Mock;

beforeEach(() => {
mockedUseEditorPermissions.mockImplementation(() => ({ canLinkEntity: true }));
});

describe('Single resource editor', () => {
it('renders the action button when no value is set', async () => {
const sdk: FieldAppSDK = mockSdkForField(fieldDefinition);
Expand Down Expand Up @@ -71,6 +85,25 @@ describe('Single resource editor', () => {
});
});

it('renders no the action button when permissions insufficient', async () => {
mockedUseEditorPermissions.mockImplementation(() => ({ canLinkEntity: false }));

const sdk: FieldAppSDK = mockSdkForField(fieldDefinition);
render(
<SingleResourceReferenceEditor
isInitiallyDisabled={false}
sdk={sdk}
hasCardEditActions={true}
viewType="card"
// @ts-expect-error unused...
parameters={{}}
/>
);

const noPermission = await screen.findByText(/You don't have permission to view this content/);
expect(noPermission).toBeDefined();
});

it('renders custom actions when passed', async () => {
const sdk: FieldAppSDK = mockSdkForField(fieldDefinition);
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,11 @@ import { useResourceLinkActions } from './useResourceLinkActions';
export function SingleResourceReferenceEditor(
props: ReferenceEditorProps & {
getEntryRouteHref: (entryRoute: EntryRoute) => string;
apiUrl: string;
}
) {
const { dialogs, field } = props.sdk;
const linkActionsProps = useResourceLinkActions({
dialogs,
field,
apiUrl: props.apiUrl,
sdk: props.sdk,
parameters: props.parameters,
});

return (
Expand Down
24 changes: 17 additions & 7 deletions packages/reference/src/resources/useResourceLinkActions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useMemo } from 'react';

import type { FieldAPI, FieldAppSDK } from '@contentful/app-sdk';
import type { FieldAPI } from '@contentful/app-sdk';
import type { ResourceLink } from 'contentful-management';

import { EditorPermissionsProps, useEditorPermissions } from '../common/useEditorPermissions';
import { LinkActionsProps } from '../components';

const getUpdatedValue = (
Expand All @@ -18,12 +19,14 @@ const getUpdatedValue = (
}
};

type ResourceLinkActionProps = Pick<EditorPermissionsProps, 'parameters' | 'sdk'>;

export function useResourceLinkActions({
dialogs,
field,
}: Pick<FieldAppSDK, 'field' | 'dialogs'> & {
apiUrl: string;
}): LinkActionsProps {
parameters,
sdk,
}: ResourceLinkActionProps): LinkActionsProps {
const { field, dialogs } = sdk;

const onLinkedExisting = useMemo(() => {
return (
links: ResourceLink<'Contentful:Entry'>[] | [ResourceLink<'Contentful:Entry'> | null]
Expand Down Expand Up @@ -58,6 +61,13 @@ export function useResourceLinkActions({
// @ts-expect-error wait for update of app-sdk version
}, [dialogs, field.allowedResources, multiple, onLinkedExisting]);

const { canLinkEntity } = useEditorPermissions({
entityType: 'Entry',
allContentTypes: [],
sdk,
parameters,
});

return {
onLinkExisting,
// @ts-expect-error
Expand All @@ -67,7 +77,7 @@ export function useResourceLinkActions({
contentTypes: [],
canCreateEntity: false,
canLinkMultiple: multiple,
canLinkEntity: true,
canLinkEntity,
isDisabled: false,
isEmpty: false,
isFull: false,
Expand Down
Loading