summaryrefslogtreecommitdiff
path: root/odoo/addons/test_new_api/tests/test_new_fields.py
diff options
context:
space:
mode:
authorJorge Pinna Puissant <jpp@odoo.com>2020-03-11 11:34:08 +0000
committerJorge Pinna Puissant <jpp@odoo.com>2020-03-18 11:51:27 +0000
commit4ce7406b1982c1ba9db7857de1ae9dc5858f584c (patch)
tree3dbc2c74ff57f2f3e943192c444b875db16ee198 /odoo/addons/test_new_api/tests/test_new_fields.py
parentc66e0557dfe8076c3f6347c8afb9b6851afb550c (diff)
[FIX] fields: XLSX files mistaken as SVG files
Before this commit, when a user try to import data with a XLSX file, the file was mistaken by an SVG file. This issue arises because a XLSX file from import isn't encoded in base64, and for testing if the file is an SVG file it will be decoded. base64.b64decode by default (when validate is False), will remove all characters that are in the base-64 alphabet from the input prior to decode. So in our case, when the non encoded XSLX file is force decoded the results starts, unluckily, with '<' and it's mistaken by an XML/SVG file. Note that, the XSLX file is wrongly tested because a XSLX file is just a ZIP file, and all the ZIP files start with PK\x03\x04, and P is the first byte of a base64 encoded XML file. Now, only files that were previously encoded into base64 are decoded to be tested. The validate = True parameter in base64.b64decode will raise a binascii.Error if there are a non-base64-alphabet characters in the input, this will allow us to know if the input was or wasn't base64 prior encoded. As base64.b64decode with validate = False, removed the non-base64-alphabet characters this allows to decode input files compatible with RFC 2045 (MIME). The files compatible with this standard will have a newline character (b'\n') after every 76 bytes of the output, and end with a newline. To keep backwards compatibility, we remove the newlines and the carriage return from the input before the decoding. opw-2194468 closes #36081 closes #31849 closes #33543 closes odoo/odoo#47906 X-original-commit: 65d709c9ab386d646f682c494cfb21cb06ec8034 Signed-off-by: Olivier Dony (odo) <odo@openerp.com> Signed-off-by: Jorge Pinna Puissant (jpp) <jpp@odoo.com> Co-authored-by: Xavier Morel <xmo@odoo.com>
Diffstat (limited to 'odoo/addons/test_new_api/tests/test_new_fields.py')
-rw-r--r--odoo/addons/test_new_api/tests/test_new_fields.py21
1 files changed, 21 insertions, 0 deletions
diff --git a/odoo/addons/test_new_api/tests/test_new_fields.py b/odoo/addons/test_new_api/tests/test_new_fields.py
index 3204b25bfe6..c27704a47d9 100644
--- a/odoo/addons/test_new_api/tests/test_new_fields.py
+++ b/odoo/addons/test_new_api/tests/test_new_fields.py
@@ -1453,6 +1453,27 @@ class TestFields(TransactionCaseWithUserDemo):
self.assertEqual(count(message), 1)
self.assertEqual(count(message1), 0)
+ def test_85_binary_guess_zip(self):
+ from odoo.addons.base.tests.test_mimetypes import ZIP
+ # Regular ZIP files can be uploaded by non-admin users
+ self.env['test_new_api.binary_svg'].with_user(
+ self.env.ref('base.user_demo'),
+ ).create({
+ 'name': 'Test without attachment',
+ 'image_wo_attachment': base64.b64decode(ZIP),
+ })
+
+ def test_86_text_base64_guess_svg(self):
+ from odoo.addons.base.tests.test_mimetypes import SVG
+ with self.assertRaises(UserError) as e:
+ self.env['test_new_api.binary_svg'].with_user(
+ self.env.ref('base.user_demo'),
+ ).create({
+ 'name': 'Test without attachment',
+ 'image_wo_attachment': SVG.decode("utf-8"),
+ })
+ self.assertEqual(e.exception.name, 'Only admins can upload SVG files.')
+
def test_90_binary_svg(self):
from odoo.addons.base.tests.test_mimetypes import SVG
# This should work without problems