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

Fix: Android - Error attempting to download an attachment #25742

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 src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,6 +1198,7 @@ const CONST = {
TIME_STARTS_01: /^01:\d{2} [AP]M$/,
TIME_FORMAT: /^\d{2}:\d{2} [AP]M$/,
DATE_TIME_FORMAT: /^\d{2}-\d{2} \d{2}:\d{2} [AP]M$/,
ILLEGAL_FILENAME_CHARACTERS: /\/|<|>|\*|"|:|\?|\\|\|/g,
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider using the list operator [] for single characters in Regex, as it's more concise and doesn't require as many escape sequences:

// Instead of:
ILLEGAL_FILENAME_CHARACTERS: /\/|<|>|\*|"|:|\?|\\|\|/g,

// Use:
ILLEGAL_FILENAME_CHARACTERS: /[\/<>*":?\\|]/g,

This makes the code cleaner and aligns with common practices for handling individual characters.

},

PRONOUNS: {
Expand Down
2 changes: 2 additions & 0 deletions src/libs/fileDownload/FileUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ function cleanFileName(fileName) {
function appendTimeToFileName(fileName) {
const file = splitExtensionFromFileName(fileName);
let newFileName = `${file.fileName}-${DateUtils.getDBTime()}`;
// Replace illegal characters before trying to download the attachment.
newFileName = newFileName.replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_');
if (file.fileExtension) {
newFileName += `.${file.fileExtension}`;
}
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/FileUtilsTest.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import CONST from '../../src/CONST';
import DateUtils from '../../src/libs/DateUtils';
import * as FileUtils from '../../src/libs/fileDownload/FileUtils';

Expand Down Expand Up @@ -26,13 +27,13 @@ describe('FileUtils', () => {
it('should append current time to the end of the file name', () => {
const actualFileName = FileUtils.appendTimeToFileName('image.jpg');
const expectedFileName = `image-${DateUtils.getDBTime()}.jpg`;
expect(actualFileName).toEqual(expectedFileName);
expect(actualFileName).toEqual(expectedFileName.replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_'));
Comment on lines 29 to +30
Copy link
Contributor

Choose a reason for hiding this comment

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

I noticed that the test is using part of the same logic as the implementation, specifically the .replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_'). This means it's essentially testing the implementation against itself, which can mask errors and lead to false positives.

A more robust approach would be to test against a static expectation, as shown below:

        it('should append current time to the end of the file name', () => {
            const fixedTime = '2023-08-25 13:56:12.119';
            jest.spyOn(DateUtils, 'getDBTime').mockReturnValue(fixedTime);

            const actualFileName = FileUtils.appendTimeToFileName('image.jpg');
            const expectedFileName = 'image-2023-08-25 13_56_12.119.jpg'; // Static expectation

            expect(actualFileName).toEqual(expectedFileName);
        });

In this revised version, the expected file name is hardcoded, and a mock ensures that the time appended to the file name is known and fixed. This makes the test more robust and reliable.

By removing the .replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_'), we ensure that if someone modifies the Regex in a way that introduces an error, the test has a chance to catch it. This change aligns the test more closely with the principle of testing the expected outcome rather than replicating the implementation logic.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for suggestions. Agree to use static values in automated test. I also had this in mind but we used dynamic value - DateUtils.getDBTime() already before this PR, I hadn't raised this concern.

});

it('should append current time to the end of the file name without extension', () => {
const actualFileName = FileUtils.appendTimeToFileName('image');
const expectedFileName = `image-${DateUtils.getDBTime()}`;
expect(actualFileName).toEqual(expectedFileName);
expect(actualFileName).toEqual(expectedFileName.replace(CONST.REGEX.ILLEGAL_FILENAME_CHARACTERS, '_'));
});
});
});
Loading