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

File extension rename confirmation #7024

Merged
merged 6 commits into from
Apr 10, 2024
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
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
- Gave TAs read-only access to starter file information under assignment settings (#6996)
- Allow inactive groups in the submissions table to be toggled for display (#7000)
- Display error message for student-run tests when no test groups are runnable (#7003)
- Added a confirmation check while renaming a file with a different extension (#7024)

## [v2.4.8]
- Validate user-provided paths (#7025)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ class SubmissionFileUploadModal extends React.Component {

onSubmit = event => {
event.preventDefault();
if (this.state.newFiles.length === 1) {
const newFilename = this.state.renameTo;
const originalFilename = this.state.newFiles[0].name; // Assuming only one file is uploaded
const originalExtension = originalFilename.split(".").pop();
const newExtension = newFilename.split(".").pop();

if (originalExtension !== newExtension) {
const confirmChange = window.confirm(I18n.t("modals.file_upload.rename_warning"));
if (!confirmChange) {
// Prevent form submission if the user cancels the operation
return;
}
}
}
this.props.onSubmit(this.state.newFiles, undefined, this.state.unzip, this.state.renameTo);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,72 @@ describe("For the SubmissionFileUploadModal", () => {
expect(wrapper.find(".file-rename-textbox").props().disabled).toBeFalsy();
});
});
describe("The onSubmit method", () => {
// Mock onSubmit function
const mockOnSubmit = jest.fn();

beforeEach(() => {
wrapper = shallow(
<SubmissionFileUploadModal
onlyRequiredFiles={true}
requiredFiles={["q1.py", "q2.py", "q3.py", "part1/p1.py"]}
uploadTarget={null}
onSubmit={mockOnSubmit} // Pass the mocked onSubmit function
/>
);
});

it("should call onSubmit prop with correct arguments when extensions match", () => {
// Mock setState method
wrapper.setState({
newFiles: [{name: "file.py"}],
renameTo: "file.py",
});

// Simulate form submission
wrapper.instance().onSubmit({preventDefault: jest.fn()});

// Ensure onSubmit is called with expected arguments
expect(mockOnSubmit).toHaveBeenCalledWith([{name: "file.py"}], undefined, false, "file.py");
});

it("should not call onSubmit prop when extensions don't match and user cancels", () => {
// Mock window.confirm to return false
window.confirm = jest.fn(() => false);

// Mock setState method
wrapper.setState({
newFiles: [{name: "file1.py"}],
renameTo: "file2.txt",
});

// Simulate form submission
wrapper.instance().onSubmit({preventDefault: jest.fn()});

// Ensure onSubmit is not called
expect(mockOnSubmit).not.toHaveBeenCalled();
});

it("should call onSubmit prop with correct arguments when extensions don't match and user confirms", () => {
// Mock window.confirm to return true
window.confirm = jest.fn(() => true);

// Mock setState method
wrapper.setState({
newFiles: [{name: "file1.py"}],
renameTo: "file2.txt",
});

// Simulate form submission
wrapper.instance().onSubmit({preventDefault: jest.fn()});

// Ensure onSubmit is called with expected arguments
expect(mockOnSubmit).toHaveBeenCalledWith(
[{name: "file1.py"}],
undefined,
false,
"file2.txt"
);
});
});
});
3 changes: 3 additions & 0 deletions config/locales/views/modals/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
en:
modals:
file_upload:
rename_warning: |
Changing the file extension may render the file unusable.
Are you sure you want to proceed?
unzip: unzip all zip files in place