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; }