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

[ui-storagebrowser] adds change owner and group action #3973

Merged
merged 4 commits into from
Jan 28, 2025
Merged
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
Next Next commit
[ui-storagebrowser] adds change owner and group action
ramprasadagarwal committed Jan 27, 2025
commit 9ea68e261b48f71ab4b5d9f4c0caf7f331d68323
2 changes: 1 addition & 1 deletion apps/filebrowser/src/filebrowser/api.py
Original file line number Diff line number Diff line change
@@ -734,7 +734,7 @@ def chown(request):
path = request.POST.get('path')
user = request.POST.get("user")
group = request.POST.get("group")
recursive = request.POST.get('recursive', False)
recursive = coerce_bool(request.POST.get('recursive', False))

# TODO: Check if we need to explicitly handle encoding anywhere
request.fs.chown(path, user, group, recursive=recursive)
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to Cloudera, Inc. under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. Cloudera, Inc. licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

@use 'variables' as vars;

.antd.cuix {
.change-owner-group {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: we prefix classes with hue-

display: flex;
flex-direction: column;
flex: 1;
gap: 8px;

&__header-note {
padding: 8px;
background-color: vars.$fluidx-gray-200;
margin-bottom: 8px;
}

&__form {
display: flex;
flex-direction: column;
flex: 1;
gap: 16px;
}

&__entity {
display: flex;
flex-direction: column;
}

&__dropdown {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 8px;
}

&__checkbox {
display: flex;
gap: 8px;
}

&__label {
color: vars.$fluidx-gray-700;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,297 @@
// Licensed to Cloudera, Inc. under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. Cloudera, Inc. licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import React from 'react';
import { render, fireEvent, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
import userEvent from '@testing-library/user-event';
import ChangeOwnerAndGroupModal from './ChangeOwnerAndGroupModal';
import { StorageDirectoryTableData } from '../../../../../reactComponents/FileChooser/types';

const mockFiles: StorageDirectoryTableData[] = [
{
name: 'file1.txt',
size: '0 Byte',
type: 'file',
permission: 'rwxrwxrwx',
mtime: '2021-01-01 00:00:00',
path: 'test/path/file1.txt',
user: 'user1',
group: 'group1',
replication: 1
}
];

const mockSave = jest.fn();
jest.mock('../../../../../utils/hooks/useSaveData/useSaveData', () => ({
__esModule: true,
default: jest.fn(() => ({
save: mockSave,
loading: false
}))
}));

const mockOnSuccess = jest.fn();
const mockOnError = jest.fn();
const mockOnClose = jest.fn();

const users = ['user1', 'user2', 'user3'];
const groups = ['group1', 'group2', 'group3'];

describe('ChangeOwnerAndGroupModal Component', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should render correctly and show the modal', () => {
const { getByText } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

expect(getByText('Change Onwer / Group')).toBeInTheDocument();
expect(getByText('Submit')).toBeInTheDocument();
expect(getByText('Cancel')).toBeInTheDocument();
expect(getByText('User')).toBeInTheDocument();
expect(getByText('Group')).toBeInTheDocument();
expect(getByText('Recursive')).toBeInTheDocument();
expect(
getByText(
'Note: Only the Hadoop superuser, "{{superuser}}" or the HDFS supergroup, "{{supergroup}}" on this file system, may change the owner of a file.'
)
).toBeInTheDocument();
});

it('should show input fields for custom user when "Others" is selected', async () => {
const { getAllByRole, getByText, getByPlaceholderText } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

const [userSelect] = getAllByRole('combobox');

await userEvent.click(userSelect);
fireEvent.click(getByText('others'));
fireEvent.change(userSelect, { target: { value: 'others' } });

const userInput = getByPlaceholderText('Enter user');
expect(userInput).toBeInTheDocument();

fireEvent.change(userInput, { target: { value: 'customUser' } });
expect(userInput).toHaveValue('customUser');
});

it('should show input fields for custom group when "Others" is selected', async () => {
const { getAllByRole, getByText, getByPlaceholderText } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

const groupSelect = getAllByRole('combobox')[1];

await userEvent.click(groupSelect);
fireEvent.click(getByText('others'));
fireEvent.change(groupSelect, { target: { value: 'others' } });

const groupInput = getByPlaceholderText('Enter group');
expect(groupInput).toBeInTheDocument();

fireEvent.change(groupInput, { target: { value: 'customGroup' } });
expect(groupInput).toHaveValue('customGroup');
});

it('should toggle the recursive checkbox', () => {
const { getByRole } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

const recursiveCheckbox = getByRole('checkbox');
expect(recursiveCheckbox).not.toBeChecked();
fireEvent.click(recursiveCheckbox);
expect(recursiveCheckbox).toBeChecked();
fireEvent.click(recursiveCheckbox);
expect(recursiveCheckbox).not.toBeChecked();
});

it('should call handleChangeOwner when the form is submitted', async () => {
const { getByText } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

fireEvent.click(getByText('Submit'));

await waitFor(() => {
expect(mockSave).toHaveBeenCalledTimes(1);
expect(mockSave).toHaveBeenCalledWith(expect.any(FormData));
});
});

it('should call onSuccess when the request is successful', async () => {
mockSave.mockImplementationOnce(() => {
mockOnSuccess();
});

const { getByText } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

fireEvent.click(getByText('Submit'));

await waitFor(() => {
expect(mockOnSuccess).toHaveBeenCalledTimes(1);
});
});

it('should call onError when the request fails', async () => {
mockSave.mockImplementationOnce(() => {
mockOnError(new Error());
});

const { getByText } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

fireEvent.click(getByText('Submit'));

await waitFor(() => {
expect(mockOnError).toHaveBeenCalledTimes(1);
});
});

it('should call onClose when the modal is closed', () => {
const { getByText } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

fireEvent.click(getByText('Cancel'));
expect(mockOnClose).toHaveBeenCalledTimes(1);
});

it('should disable submit button when other option is selected and input not provided', async () => {
const { getAllByRole, getAllByText, getByPlaceholderText, getByRole } = render(
<ChangeOwnerAndGroupModal
isOpen={true}
superUser="hadoop-superuser"
superGroup="hdfs-supergroup"
users={users}
groups={groups}
files={mockFiles}
setLoading={jest.fn()}
onSuccess={mockOnSuccess}
onError={mockOnError}
onClose={mockOnClose}
/>
);

const [userSelect] = getAllByRole('combobox');

await userEvent.click(userSelect);
fireEvent.click(getAllByText('others')[0]);
fireEvent.change(userSelect, { target: { value: 'others' } });

const submitButton = getByRole('button', { name: /submit/i });
expect(submitButton).toBeDisabled();

const userInput = getByPlaceholderText('Enter user');
fireEvent.change(userInput, { target: { value: 'customUser' } });
expect(submitButton).toBeEnabled();
});
});
Loading