Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions changelogs/fragments/10908.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Trim collaboratorId when adding collaborators for workspace ([#10908](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/10908))
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ describe('WorkspaceCollaboratorInput', () => {
render(<WorkspaceCollaboratorInput {...defaultProps} error="error" />);
expect(screen.getByTestId('workspaceCollaboratorIdInput-0')).toBeInvalid();
});

it('trims whitespace from collaborator ID input', () => {
render(<WorkspaceCollaboratorInput {...defaultProps} />);
const input = screen.getByTestId('workspaceCollaboratorIdInput-0');
fireEvent.change(input, { target: { value: ' test ' } });
expect(defaultProps.onCollaboratorIdChange).toHaveBeenCalledWith('test', 0);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export const WorkspaceCollaboratorInput = ({
}: WorkspaceCollaboratorInputProps) => {
const handleCollaboratorIdChange = useCallback(
(e) => {
onCollaboratorIdChange(e.target.value, index);
onCollaboratorIdChange(e.target.value?.trim(), index);
},
[index, onCollaboratorIdChange]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,45 @@ describe('AddCollaboratorButton', () => {
]);
});

it('should trim collaborator IDs when adding collaborators', () => {
const mockOnAdd = jest.fn().mockImplementation(({ onAddCollaborators }) => {
onAddCollaborators([
{
accessLevel: 'readOnly',
collaboratorId: ' user-with-spaces ',
permissionType: 'user',
},
{
accessLevel: 'admin',
collaboratorId: '\tgroup-with-tabs\t',
permissionType: 'group',
},
]);
});
const displayedTypes = [
{
name: 'add user',
buttonLabel: 'add user',
onAdd: mockOnAdd,
id: 'user',
},
];
const { getByTestId, getByText } = render(
<AddCollaboratorButton {...mockProps} displayedTypes={displayedTypes} />
);
const button = getByTestId('add-collaborator-button');
fireEvent.click(button);
const addUserButton = getByText('add user');
fireEvent.click(addUserButton);

expect(mockProps.handleSubmitPermissionSettings).toHaveBeenCalledWith([
{ id: 0, modes: ['library_write', 'write'], type: 'user', userId: 'admin' },
{ group: 'group', id: 1, modes: ['library_read', 'read'], type: 'group' },
{ id: 2, modes: ['library_read', 'read'], type: 'user', userId: 'user-with-spaces' },
{ id: 3, modes: ['library_write', 'write'], type: 'group', group: 'group-with-tabs' },
]);
});

it('should throw DuplicateCollaboratorError with consistent details', async () => {
let errorCached: DuplicateCollaboratorError | undefined;
const mockOnAdd = jest.fn(async ({ onAddCollaborators }) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,23 @@ export const AddCollaboratorButton = ({

const onAddCollaborators = async (collaborators: WorkspaceCollaborator[]) => {
const uniqueCollaboratorIds = new Set();
const addedSettings = collaborators.map(({ permissionType, accessLevel, collaboratorId }) => ({
type: permissionType,
modes: accessLevelNameToWorkspacePermissionModesMap[accessLevel],
id: nextIdGenerator(),
...(permissionType === WorkspacePermissionItemType.User
? {
userId: collaboratorId,
}
: {
group: collaboratorId,
}),
collaboratorId,
})) as Array<WorkspacePermissionSetting & { collaboratorId: string }>;
const addedSettings = collaborators.map(({ permissionType, accessLevel, collaboratorId }) => {
const trimmedId = collaboratorId.trim();
return {
type: permissionType,
modes: accessLevelNameToWorkspacePermissionModesMap[accessLevel],
id: nextIdGenerator(),
...(permissionType === WorkspacePermissionItemType.User
? {
userId: trimmedId,
}
: {
group: trimmedId,
}),
collaboratorId: trimmedId,
};
}) as Array<WorkspacePermissionSetting & { collaboratorId: string }>;

const existingDuplicateSettings = addedSettings.filter((permissionSettingToAdd) =>
hasSameUserIdOrGroup(permissionSettings, permissionSettingToAdd)
);
Expand Down
Loading