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

Introduce buttons to create users, groups and roles from the respective provider #890

Merged
merged 3 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
62 changes: 62 additions & 0 deletions src/Component/CreateAllGroupsButton/CreateAllGroupsButton.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';

import {
cleanup,
fireEvent,
render,
screen,
waitForElementToBeRemoved
} from '@testing-library/react';

import SHOGunAPIClient from '@terrestris/shogun-util/dist/service/SHOGunAPIClient';

import { CreateAllGroupsButton } from './CreateAllGroupsButton';
import GroupService from '@terrestris/shogun-util/dist/service/GroupService';
import Group from '@terrestris/shogun-util/dist/model/Group';

import type { PartialOmit } from '../../test-util';

const mockService: Partial<GroupService<Group>> = {
createAllFromProvider: jest.fn()
};

const mockSHOGunAPIClient: PartialOmit<SHOGunAPIClient, 'group'> = {
group: jest.fn().mockReturnValue(mockService)
};

jest.mock('../../Hooks/useSHOGunAPIClient', () => {
const originalModule = jest.requireActual('../../Hooks/useSHOGunAPIClient');
return {
__esModule: true,
...originalModule,
default: jest.fn(() => mockSHOGunAPIClient)
};
});

describe('<CreateAllUsersButton />', () => {
dnlkoch marked this conversation as resolved.
Show resolved Hide resolved

afterEach(cleanup);

it('can be rendered', () => {
const {
container
} = render(
<CreateAllGroupsButton />);

expect(container).toBeVisible();
});

it('calls the appropriate service method', async () => {
render(<CreateAllGroupsButton />);

const buttonElement = screen.getByText('CreateAllGroupsButton.title');

fireEvent.click(buttonElement);

expect(mockSHOGunAPIClient.group().createAllFromProvider).toHaveBeenCalled();

await waitForElementToBeRemoved(() => screen.queryByLabelText('loading'));

expect(screen.getByText('CreateAllGroupsButton.success')).toBeVisible();
});
});
81 changes: 81 additions & 0 deletions src/Component/CreateAllGroupsButton/CreateAllGroupsButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, {
useState
} from 'react';

import {
UsergroupAddOutlined
} from '@ant-design/icons';
import {
Button,
message,
Tooltip
} from 'antd';
import { ButtonProps } from 'antd/lib/button';

import { useTranslation } from 'react-i18next';

import useSHOGunAPIClient from '../../Hooks/useSHOGunAPIClient';

import Logger from '../../Logger';

export type CreateAllGroupsButtonProps = Omit<ButtonProps, 'onClick' | 'loading'> & {
onSuccess?: () => void;
onError?: (error: any) => void;
};

export const CreateAllGroupsButton: React.FC<CreateAllGroupsButtonProps> = ({
onSuccess,
onError,
...passThroughProps
}) => {

const [isLoading, setIsLoading] = useState<boolean>(false);

const [messageApi, contextHolder] = message.useMessage();

const client = useSHOGunAPIClient();

const {
t
} = useTranslation();

const onCreateGroupsClick = async () => {
setIsLoading(true);

try {
await client?.group().createAllFromProvider();

Check failure on line 46 in src/Component/CreateAllGroupsButton/CreateAllGroupsButton.tsx

View workflow job for this annotation

GitHub Actions / Lint, Typecheck and Test

Property 'createAllFromProvider' does not exist on type 'GroupService<Group<ProviderGroupDetails>, ProviderGroupDetails>'.

messageApi.success(t('CreateAllGroupsButton.success'));

onSuccess?.();
} catch (error) {
messageApi.error(t('CreateAllGroupsButton.error'));

Logger.error('Error while creating the groups: ', error);

onError?.(error);
} finally {
setIsLoading(false);
}
};

return (
<>
{contextHolder}
<Tooltip
title={t('CreateAllGroupsButton.tooltip')}
>
<Button
onClick={onCreateGroupsClick}
loading={isLoading}
icon={<UsergroupAddOutlined />}
{...passThroughProps}
>
{t('CreateAllGroupsButton.title')}
</Button>
</Tooltip>
</>
);
};

export default CreateAllGroupsButton;
62 changes: 62 additions & 0 deletions src/Component/CreateAllRolesButton/CreateAllRolesButton.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';

import {
cleanup,
fireEvent,
render,
screen,
waitForElementToBeRemoved
} from '@testing-library/react';

import SHOGunAPIClient from '@terrestris/shogun-util/dist/service/SHOGunAPIClient';

import { CreateAllRolesButton } from './CreateAllRolesButton';
import RoleService from '@terrestris/shogun-util/dist/service/RoleService';
import Role from '@terrestris/shogun-util/dist/model/Role';

import type { PartialOmit } from '../../test-util';

const mockService: Partial<RoleService<Role>> = {
createAllFromProvider: jest.fn()
};

const mockSHOGunAPIClient: PartialOmit<SHOGunAPIClient, 'role'> = {
role: jest.fn().mockReturnValue(mockService)
};

jest.mock('../../Hooks/useSHOGunAPIClient', () => {
const originalModule = jest.requireActual('../../Hooks/useSHOGunAPIClient');
return {
__esModule: true,
...originalModule,
default: jest.fn(() => mockSHOGunAPIClient)
};
});

describe('<CreateAllRolesButton />', () => {

afterEach(cleanup);

it('can be rendered', () => {
const {
container
} = render(
<CreateAllRolesButton />);

expect(container).toBeVisible();
});

it('calls the appropriate service method', async () => {
render(<CreateAllRolesButton />);

const buttonElement = screen.getByText('CreateAllRolesButton.title');

fireEvent.click(buttonElement);

expect(mockSHOGunAPIClient.role().createAllFromProvider).toHaveBeenCalled();

await waitForElementToBeRemoved(() => screen.queryByLabelText('loading'));

expect(screen.getByText('CreateAllRolesButton.success')).toBeVisible();
});
});
81 changes: 81 additions & 0 deletions src/Component/CreateAllRolesButton/CreateAllRolesButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import React, {
useState
} from 'react';

import {
TagOutlined
} from '@ant-design/icons';
import {
Button,
message,
Tooltip
} from 'antd';
import { ButtonProps } from 'antd/lib/button';

import { useTranslation } from 'react-i18next';

import useSHOGunAPIClient from '../../Hooks/useSHOGunAPIClient';

import Logger from '../../Logger';

export type CreateAllRolesButtonProps = Omit<ButtonProps, 'onClick' | 'loading'> & {
onSuccess?: () => void;
onError?: (error: any) => void;
};

export const CreateAllRolesButton: React.FC<CreateAllRolesButtonProps> = ({
onSuccess,
onError,
...passThroughProps
}) => {

const [isLoading, setIsLoading] = useState<boolean>(false);

const [messageApi, contextHolder] = message.useMessage();

const client = useSHOGunAPIClient();

const {
t
} = useTranslation();

const onCreateRolesClick = async () => {
setIsLoading(true);

try {
await client?.role().createAllFromProvider();

Check failure on line 46 in src/Component/CreateAllRolesButton/CreateAllRolesButton.tsx

View workflow job for this annotation

GitHub Actions / Lint, Typecheck and Test

Property 'createAllFromProvider' does not exist on type 'RoleService<Role<ProviderRoleDetails>, ProviderRoleDetails>'.

messageApi.success(t('CreateAllRolesButton.success'));

onSuccess?.();
} catch (error) {
messageApi.error(t('CreateAllRolesButton.error'));

Logger.error('Error while creating the roles: ', error);

onError?.(error);
} finally {
setIsLoading(false);
}
};

return (
<>
{contextHolder}
<Tooltip
title={t('CreateAllRolesButton.tooltip')}
>
<Button
onClick={onCreateRolesClick}
loading={isLoading}
icon={<TagOutlined />}
{...passThroughProps}
>
{t('CreateAllRolesButton.title')}
</Button>
</Tooltip>
</>
);
};

export default CreateAllRolesButton;
62 changes: 62 additions & 0 deletions src/Component/CreateAllUsersButton/CreateAllUsersButton.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import React from 'react';

import {
cleanup,
fireEvent,
render,
screen,
waitForElementToBeRemoved
} from '@testing-library/react';

import SHOGunAPIClient from '@terrestris/shogun-util/dist/service/SHOGunAPIClient';

import { CreateAllUsersButton } from './CreateAllUsersButton';
import UserService from '@terrestris/shogun-util/dist/service/UserService';
import User from '@terrestris/shogun-util/dist/model/User';

import type { PartialOmit } from '../../test-util';

const mockService: Partial<UserService<User>> = {
createAllFromProvider: jest.fn()
};

const mockSHOGunAPIClient: PartialOmit<SHOGunAPIClient, 'user'> = {
user: jest.fn().mockReturnValue(mockService)
};

jest.mock('../../Hooks/useSHOGunAPIClient', () => {
const originalModule = jest.requireActual('../../Hooks/useSHOGunAPIClient');
return {
__esModule: true,
...originalModule,
default: jest.fn(() => mockSHOGunAPIClient)
};
});

describe('<CreateAllUsersButton />', () => {

afterEach(cleanup);

it('can be rendered', () => {
const {
container
} = render(
<CreateAllUsersButton />);

expect(container).toBeVisible();
});

it('calls the appropriate service method', async () => {
render(<CreateAllUsersButton />);

const buttonElement = screen.getByText('CreateAllUsersButton.title');

fireEvent.click(buttonElement);

expect(mockSHOGunAPIClient.user().createAllFromProvider).toHaveBeenCalled();

await waitForElementToBeRemoved(() => screen.queryByLabelText('loading'));

expect(screen.getByText('CreateAllUsersButton.success')).toBeVisible();
});
});
Loading
Loading