-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #890 from terrestris/create-entities-from-provider…
…-endpoints Introduce buttons to create users, groups and roles from the respective provider
- Loading branch information
Showing
26 changed files
with
1,427 additions
and
552 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/Component/CreateAllGroupsButton/CreateAllGroupsButton.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('<CreateAllGroupsButton />', () => { | ||
|
||
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
81
src/Component/CreateAllGroupsButton/CreateAllGroupsButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
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
62
src/Component/CreateAllRolesButton/CreateAllRolesButton.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
src/Component/CreateAllRolesButton/CreateAllRolesButton.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
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; |
Oops, something went wrong.