From 6e27c43c58dd323c74dfe218f6c6b5a7ee9ddce5 Mon Sep 17 00:00:00 2001 From: "Julien Carion (juca)" Date: Tue, 12 Dec 2023 11:42:28 +0000 Subject: [PATCH] [FIX] web, project: avoid crash on file upload in portal This commit fixes a crash that would happen on file upload in the project sharing portal chatter. This crash was caused by the file size check in the uploadFiles method of the FileInput component due to the fact that the PortalFileInput calls this method once per file instead of once for all files. The fix therefore ensures that the file size check will be correctly applied in the case of a single file parameter instead of a files array. Steps to reproduce: - Access the portal (with the /my route) - Go to Projects - Open the AGR - S00064 - Sales Order project - Open any task in the kanban view - Send an attachment in the chatter - Before the fix, a traceback occurs on file upload Crash introduced by https://github.com/odoo/odoo/pull/138203/commits/82b4395b2f12e1c86d2bd5c4acd47c321be071d8 opw-3628996 closes odoo/odoo#146214 X-original-commit: 8e2aef9ab607b06eb4667859204cfd7186b3f060 Signed-off-by: Xavier Bol (xbo) --- addons/project/__manifest__.py | 1 + .../static/tests/portal_components_tests.js | 76 +++++++++++++++++++ .../static/src/core/file_input/file_input.js | 4 +- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 addons/project/static/tests/portal_components_tests.js diff --git a/addons/project/__manifest__.py b/addons/project/__manifest__.py index ab64551b14f..6d630f43beb 100644 --- a/addons/project/__manifest__.py +++ b/addons/project/__manifest__.py @@ -81,6 +81,7 @@ 'project/static/src/js/portal_rating.js', ], 'web.qunit_suite_tests': [ + 'project/static/src/project_sharing/components/portal_file_input/portal_file_input.js', 'project/static/tests/**/*.js', ], 'web.assets_tests': [ diff --git a/addons/project/static/tests/portal_components_tests.js b/addons/project/static/tests/portal_components_tests.js new file mode 100644 index 00000000000..f447dfadc85 --- /dev/null +++ b/addons/project/static/tests/portal_components_tests.js @@ -0,0 +1,76 @@ +/** @odoo-module **/ + +import { makeTestEnv } from "@web/../tests/helpers/mock_env"; +import { + editInput, + getFixture, + mount, + patchWithCleanup, +} from "@web/../tests/helpers/utils"; +import { PortalFileInput } from "@project/project_sharing/components/portal_file_input/portal_file_input"; +import { registry } from "@web/core/registry"; +import { session } from "@web/session"; +import { makeFakeLocalizationService } from "@web/../tests/helpers/mock_services"; + +const serviceRegistry = registry.category("services"); + +let target; + +// ----------------------------------------------------------------------------- +// Helpers +// ----------------------------------------------------------------------------- + +async function createFileInput({ mockPost, mockAdd, props }) { + serviceRegistry.add("notification", { + start: () => ({ + add: mockAdd || (() => {}), + }), + }); + serviceRegistry.add("http", { + start: () => ({ + post: mockPost || (() => {}), + }), + }); + const env = await makeTestEnv(); + await mount(PortalFileInput, target, { env, props }); +} + +// ----------------------------------------------------------------------------- +// Tests +// ----------------------------------------------------------------------------- + +QUnit.module("Project", ({ beforeEach }) => { + beforeEach(() => { + patchWithCleanup(odoo, { csrf_token: "dummy" }); + + target = getFixture(); + }); + + QUnit.module("PortalComponents"); + + QUnit.test("uploading a file that is too heavy in portal will send a notification", async (assert) => { + serviceRegistry.add("localization", makeFakeLocalizationService()); + patchWithCleanup(session, { max_file_upload_size: 2 }); + await createFileInput({ + props: { + onUpload(files) { + assert.deepEqual(files, [null]); + }, + }, + mockAdd: (message) => { + assert.step("notification"); + assert.strictEqual( + message, + "The selected file (4B) is over the maximum allowed file size (2B)." + ); + }, + }); + + const file = new File(["test"], "fake_file.txt", { type: "text/plain" }); + await editInput(target, ".o_file_input input", file); + assert.verifySteps( + ["notification"], + "Only the notification will be triggered and the file won't be uploaded." + ); + }); +}); diff --git a/addons/web/static/src/core/file_input/file_input.js b/addons/web/static/src/core/file_input/file_input.js index c2a9badf00c..941cd64e2db 100644 --- a/addons/web/static/src/core/file_input/file_input.js +++ b/addons/web/static/src/core/file_input/file_input.js @@ -56,8 +56,8 @@ export class FileInput extends Component { } async uploadFiles(params) { - if (params.ufile.length) { - const fileSize = params.ufile[0].size; + if ((params.ufile && params.ufile.length) || params.file) { + const fileSize = (params.ufile && params.ufile[0].size) || params.file.size; if (!checkFileSize(fileSize, this.notification)) { return null; }