Debian Codegraph @

addons/web/static/tests/views/fields/binary_field.test.js

raw
1 import { after, expect, test } from "@odoo/hoot";
2 import { click, queryOne, queryValue, setInputFiles, waitFor } from "@odoo/hoot-dom";
3 import { Deferred, animationFrame } from "@odoo/hoot-mock";
4 import {
5 clickSave,
6 contains,
7 defineModels,
8 fields,
9 makeServerError,
10 models,
11 mountView,
12 onRpc,
13 pagerNext,
14 } from "@web/../tests/web_test_helpers";
15
16 import { toBase64Length } from "@web/core/utils/binary";
17 import { MAX_FILENAME_SIZE_BYTES } from "@web/views/fields/binary/binary_field";
18
19 const BINARY_FILE =
20 "R0lGODlhDAAMAKIFAF5LAP/zxAAAANyuAP/gaP///wAAAAAAACH5BAEAAAUALAAAAAAMAAwAAAMlWLPcGjDKFYi9lxKBOaGcF35DhWHamZUW0K4mAbiwWtuf0uxFAgA7";
21
22 class Partner extends models.Model {
23 _name = "res.partner";
24
25 foo = fields.Char({ default: "My little Foo Value" });
26 document = fields.Binary();
27 product_id = fields.Many2one({ relation: "product" });
28
29 _records = [{ foo: "coucou.txt", document: "coucou==\n" }];
30 }
31
32 class Product extends models.Model {
33 name = fields.Char();
34
35 _records = [
36 { id: 37, name: "xphone" },
37 { id: 41, name: "xpad" },
38 ];
39 }
40
41 defineModels([Partner, Product]);
42
43 onRpc("has_group", () => true);
44
45 test("BinaryField is correctly rendered (readonly)", async () => {
46 onRpc("/web/content", async (request) => {
47 expect.step("/web/content");
48
49 const body = await request.formData();
50 expect(body).toBeInstanceOf(FormData);
51 expect(body.get("field")).toBe("document", {
52 message: "we should download the field document",
53 });
54 expect(body.get("data")).toBe("coucou==\n", {
55 message: "we should download the correct data",
56 });
57
58 return new Blob([body.get("data")], { type: "text/plain" });
59 });
60
61 await mountView({
62 resModel: "res.partner",
63 resId: 1,
64 type: "form",
65 arch: `
66 <form edit="0">
67 <field name="document" filename="foo"/>
68 <field name="foo"/>
69 </form>
70 `,
71 });
72
73 expect(`.o_field_widget[name="document"] a > .fa-download`).toHaveCount(1, {
74 message: "the binary field should be rendered as a downloadable link in readonly",
75 });
76 expect(`.o_field_widget[name="document"]`).toHaveText("coucou.txt", {
77 message: "the binary field should display the name of the file in the link",
78 });
79 expect(`.o_field_char`).toHaveText("coucou.txt", {
80 message: "the filename field should have the file name as value",
81 });
82
83 // Testing the download button in the field
84 // We must avoid the browser to download the file effectively
85 const deferred = new Deferred();
86 const downloadOnClick = (ev) => {
87 const target = ev.target;
88 if (target.tagName === "A" && "download" in target.attributes) {
89 ev.preventDefault();
90 document.removeEventListener("click", downloadOnClick);
91 deferred.resolve();
92 }
93 };
94 document.addEventListener("click", downloadOnClick);
95 after(() => document.removeEventListener("click", downloadOnClick));
96
97 await contains(`.o_field_widget[name="document"] a`).click();
98 await deferred;
99 expect.verifySteps(["/web/content"]);
100 });
101
102 test("BinaryField is correctly rendered", async () => {
103 onRpc("/web/content", async (request) => {
104 expect.step("/web/content");
105
106 const body = await request.formData();
107 expect(body).toBeInstanceOf(FormData);
108 expect(body.get("field")).toBe("document", {
109 message: "we should download the field document",
110 });
111 expect(body.get("data")).toBe("coucou==\n", {
112 message: "we should download the correct data",
113 });
114
115 return new Blob([body.get("data")], { type: "text/plain" });
116 });
117
118 await mountView({
119 resModel: "res.partner",
120 resId: 1,
121 type: "form",
122 arch: `
123 <form>
124 <field name="document" filename="foo"/>
125 <field name="foo"/>
126 </form>
127 `,
128 });
129
130 expect(`.o_field_widget[name="document"] a > .fa-download`).toHaveCount(0, {
131 message: "the binary field should not be rendered as a downloadable link in edit",
132 });
133 expect(`.o_field_widget[name="document"].o_field_binary .o_input`).toHaveValue("coucou.txt", {
134 message: "the binary field should display the file name in the input edit mode",
135 });
136 expect(`.o_field_binary .o_clear_file_button`).toHaveCount(1, {
137 message: "there shoud be a button to clear the file",
138 });
139 expect(`.o_field_char input`).toHaveValue("coucou.txt", {
140 message: "the filename field should have the file name as value",
141 });
142
143 // Testing the download button in the field
144 // We must avoid the browser to download the file effectively
145 const deferred = new Deferred();
146 const downloadOnClick = (ev) => {
147 const target = ev.target;
148 if (target.tagName === "A" && "download" in target.attributes) {
149 ev.preventDefault();
150 document.removeEventListener("click", downloadOnClick);
151 deferred.resolve();
152 }
153 };
154 document.addEventListener("click", downloadOnClick);
155 after(() => document.removeEventListener("click", downloadOnClick));
156
157 await click(`.fa-download`);
158 await deferred;
159 expect.verifySteps(["/web/content"]);
160
161 await click(`.o_field_binary .o_clear_file_button`);
162 await animationFrame();
163 expect(`.o_field_binary input`).not.toBeVisible({ message: "the input should be hidden" });
164 expect(`.o_field_binary .o_select_file_button`).toHaveCount(1, {
165 message: "there should be a button to upload the file",
166 });
167 expect(`.o_field_char input`).toHaveValue("", {
168 message: "the filename field should be empty since we removed the file",
169 });
170
171 await clickSave();
172 expect(`.o_field_widget[name="document"] a > .fa-download`).toHaveCount(0, {
173 message:
174 "the binary field should not render as a downloadable link since we removed the file",
175 });
176 expect(`o_field_widget span`).toHaveCount(0, {
177 message:
178 "the binary field should not display a filename in the link since we removed the file",
179 });
180 });
181
182 test("BinaryField is correctly rendered (isDirty)", async () => {
183 await mountView({
184 resModel: "res.partner",
185 resId: 1,
186 type: "form",
187 arch: `
188 <form>
189 <field name="document" filename="foo"/>
190 <field name="foo"/>
191 </form>
192 `,
193 });
194
195 // Simulate a file upload
196 await click(`.o_select_file_button`);
197 await animationFrame();
198 const file = new File(["test"], "fake_file.txt", { type: "text/plain" });
199 await setInputFiles([file]);
200 await waitFor(`.o_form_button_save:visible`);
201 expect(`.o_field_widget[name="document"] .fa-download`).toHaveCount(0, {
202 message:
203 "the binary field should not be rendered as a downloadable since the record is dirty",
204 });
205
206 await clickSave();
207 expect(`.o_field_widget[name="document"] .fa-download`).toHaveCount(1, {
208 message:
209 "the binary field should render as a downloadable link since the record is not dirty",
210 });
211 });
212
213 test("file name field is not defined", async () => {
214 await mountView({
215 resModel: "res.partner",
216 resId: 1,
217 type: "form",
218 arch: `<form><field name="document" filename="foo"/></form>`,
219 });
220 expect(`.o_field_binary`).toHaveText("", {
221 message: "there should be no text since the name field is not in the view",
222 });
223 expect(`.o_field_binary .fa-download`).toBeVisible({
224 message: "download icon should be visible",
225 });
226 });
227
228 test("icons are displayed exactly once", async () => {
229 await mountView({
230 resModel: "res.partner",
231 resId: 1,
232 type: "form",
233 arch: `<form><field name="document" filename="foo"/></form>`,
234 });
235 expect(queryOne`.o_field_binary .o_select_file_button`).toBeVisible({
236 message: "only one select file icon should be visible",
237 });
238 expect(queryOne`.o_field_binary .o_download_file_button`).toBeVisible({
239 message: "only one download file icon should be visible",
240 });
241 expect(queryOne`.o_field_binary .o_clear_file_button`).toBeVisible({
242 message: "only one clear file icon should be visible",
243 });
244 });
245
246 test("input value is empty when clearing after uploading", async () => {
247 await mountView({
248 resModel: "res.partner",
249 resId: 1,
250 type: "form",
251 arch: `
252 <form>
253 <field name="document" filename="foo"/>
254 <field name="foo"/>
255 </form>
256 `,
257 });
258
259 await click(`.o_select_file_button`);
260 await animationFrame();
261 const file = new File(["test"], "fake_file.txt", { type: "text/plain" });
262 await setInputFiles([file]);
263 await waitFor(`.o_form_button_save:visible`);
264 expect(`.o_field_binary input[type=text]`).toHaveAttribute("readonly");
265 expect(`.o_field_binary input[type=text]`).toHaveValue("fake_file.txt");
266 expect(`.o_field_char input[type=text]`).toHaveValue("fake_file.txt");
267
268 await click(`.o_clear_file_button`);
269 await animationFrame();
270 expect(`.o_field_binary .o_input_file`).toHaveValue("");
271 expect(`.o_field_char input`).toHaveValue("");
272 });
273
274 test("option accepted_file_extensions", async () => {
275 await mountView({
276 resModel: "res.partner",
277 type: "form",
278 arch: `
279 <form>
280 <field name="document" widget="binary" options="{'accepted_file_extensions': '.dat,.bin'}"/>
281 </form>
282 `,
283 });
284 expect(`input.o_input_file`).toHaveAttribute("accept", ".dat,.bin", {
285 message: "the input should have the correct ``accept`` attribute",
286 });
287 });
288
289 test.tags("desktop");
290 test("readonly in create mode does not download", async () => {
291 onRpc("/web/content", () => {
292 expect.step("We shouldn't be getting the file.");
293 });
294
295 Partner._onChanges.product_id = (record) => {
296 record.document = "onchange==\n";
297 };
298 Partner._fields.document.readonly = true;
299
300 await mountView({
301 resModel: "res.partner",
302 type: "form",
303 arch: `
304 <form>
305 <field name="product_id"/>
306 <field name="document" filename="yooo"/>
307 </form>
308 `,
309 });
310
311 await click(`.o_field_many2one[name='product_id'] input`);
312 await animationFrame();
313 await click(`.o_field_many2one[name='product_id'] .dropdown-item`);
314 await animationFrame();
315 expect(`.o_field_widget[name="document"] a`).toHaveCount(0, {
316 message: "The link to download the binary should not be present",
317 });
318 expect(`.o_field_widget[name="document"] a > .fa-download`).toHaveCount(0, {
319 message: "The download icon should not be present",
320 });
321 expect.verifySteps([]);
322 });
323
324 test("BinaryField in list view (formatter)", async () => {
325 Partner._records[0]["document"] = BINARY_FILE;
326 await mountView({
327 resModel: "res.partner",
328 type: "list",
329 arch: `<list><field name="document"/></list>`,
330 });
331 expect(`.o_data_row .o_data_cell`).toHaveText("93.43 Bytes");
332 });
333
334 test("BinaryField in list view with filename", async () => {
335 Partner._records[0]["document"] = BINARY_FILE;
336 await mountView({
337 resModel: "res.partner",
338 type: "list",
339 arch: `
340 <list>
341 <field name="document" filename="foo" widget="binary"/>
342 <field name="foo"/>
343 </list>
344 `,
345 });
346 expect(`.o_data_row .o_data_cell`).toHaveText("coucou.txt");
347 });
348
349 test("new record has no download button", async () => {
350 Partner._fields.document.default = BINARY_FILE;
351 await mountView({
352 resModel: "res.partner",
353 type: "form",
354 arch: `<form><field name="document" filename="foo"/></form>`,
355 });
356 expect(`button.fa-download`).toHaveCount(0);
357 });
358
359 test("filename doesn't exceed 255 bytes", async () => {
360 const LARGE_BINARY_FILE = BINARY_FILE.repeat(5);
361 expect((LARGE_BINARY_FILE.length / 4) * 3).toBeGreaterThan(MAX_FILENAME_SIZE_BYTES, {
362 message:
363 "The initial binary file should be larger than max bytes that can represent the filename",
364 });
365
366 Partner._fields.document.default = LARGE_BINARY_FILE;
367 await mountView({
368 resModel: "res.partner",
369 type: "form",
370 arch: `<form><field name="document"/></form>`,
371 });
372 expect(queryValue(`.o_field_binary input[type=text]`)).toHaveLength(
373 toBase64Length(MAX_FILENAME_SIZE_BYTES),
374 {
375 message: "The filename shouldn't exceed the maximum size in bytes in base64",
376 }
377 );
378 });
379
380 test("filename is updated when using the pager", async () => {
381 Partner._records.push(
382 { id: 1, document: "abc", foo: "abc.txt" },
383 { id: 2, document: "def", foo: "def.txt" }
384 );
385 await mountView({
386 resModel: "res.partner",
387 resIds: [1, 2],
388 resId: 1,
389 type: "form",
390 arch: `
391 <form>
392 <field name="document" filename="foo"/>
393 <field name="foo"/>
394 </form>
395 `,
396 });
397 expect(`.o_field_binary input[type=text]`).toHaveValue("abc.txt", {
398 message: `displayed value should be "abc.txt"`,
399 });
400
401 await pagerNext();
402 expect(`.o_field_binary input[type=text]`).toHaveValue("def.txt", {
403 message: `displayed value should be "def.txt"`,
404 });
405 });
406
407 test("isUploading state should be set to false after upload", async () => {
408 expect.errors(1);
409
410 Partner._records.push({ id: 1 });
411 Partner._onChanges.document = (record) => {
412 if (record.document) {
413 throw makeServerError({ type: "ValidationError" });
414 }
415 };
416 await mountView({
417 resModel: "res.partner",
418 resId: 1,
419 type: "form",
420 arch: `<form><field name="document"/></form>`,
421 });
422
423 await click(`.o_select_file_button`);
424 await animationFrame();
425 const file = new File(["test"], "fake_file.txt", { type: "text/plain" });
426 await setInputFiles([file]);
427 await waitFor(`.o_form_button_save:visible`);
428 await animationFrame();
429 expect.verifyErrors([/RPC_ERROR/]);
430 expect(`.o_select_file_button`).toHaveText("Upload your file");
431 });
432
433 test("should accept file with allowed MIME type and reject others", async () => {
434 await mountView({
435 resModel: "res.partner",
436 resId: 1,
437 type: "form",
438 arch: `
439 <form>
440 <field name="document" filename="foo" widget="binary" options="{'allowed_mime_type' : 'application/pdf'}"/>
441 </form>
442 `,
443 });
444
445 await click(`.o_select_file_button`);
446 await animationFrame();
447 const pdfFile = new File(["test"], "fake_pdf.pdf", { type: "application/pdf" });
448 await setInputFiles([pdfFile]);
449
450 await waitFor(`.o_form_button_save:visible`);
451 expect(`.o_field_binary input[type=text]`).toHaveAttribute("readonly");
452 expect(`.o_field_binary input[type=text]`).toHaveValue("fake_pdf.pdf");
453
454 await click(`.o_clear_file_button`);
455 await animationFrame();
456
457 await click(`.o_select_file_button`);
458 await animationFrame();
459 const textFile = new File(["test"], "text_file.txt", { type: "text/plain" });
460 await setInputFiles([textFile]);
461 await animationFrame();
462
463 expect(".o_notification").toHaveCount(1);
464 expect(".o_notification_content").toHaveText(
465 "Oops! 'text_file.txt' didn’t upload since its format isn’t allowed."
466 );
467 expect(".o_notification_bar").toHaveClass("bg-danger");
468 });
469
470 test("doesn't crash if value is not a string", async () => {
471 class Dummy extends models.Model {
472 document = fields.Binary();
473 _applyComputesAndValidate() {}
474 }
475 defineModels([Dummy]);
476 Dummy._records.push({ id: 1, document: {} });
477 await mountView({
478 type: "form",
479 resModel: "dummy",
480 resId: 1,
481 arch: `
482 <form>
483 <field name="document"/>
484 </form>`,
485 });
486 expect(".o_field_binary input").toHaveValue("");
487 });