diff --git a/tests/__init__.py b/tests/__init__.py index 9487edd4702becdfb6aaf10afd21714b4fe3dd01_dGVzdHMvX19pbml0X18ucHk=..e9bb6a876a3bb8c2799ce7b134e541ca6e02122c_dGVzdHMvX19pbml0X18ucHk= 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -4,6 +4,7 @@ test_field, test_ir_model, test_mail_template, + test_qr_code, test_relation, test_switch, test_validate, diff --git a/tests/test_qr_code.py b/tests/test_qr_code.py new file mode 100644 index 0000000000000000000000000000000000000000..e9bb6a876a3bb8c2799ce7b134e541ca6e02122c_dGVzdHMvdGVzdF9xcl9jb2RlLnB5 --- /dev/null +++ b/tests/test_qr_code.py @@ -0,0 +1,62 @@ +############################################################################## +# +# Converter Odoo module +# Copyright © 2025 XCG Consulting <https://xcg-consulting.fr> +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +############################################################################## + +from unittest import skipUnless + +from odoo import tests # type: ignore[import-untyped] + +from .. import QRCodeDataURL, QRCodeFile, stringToQRCode + + +@skipUnless(tests.can_import("qrcode"), "qrcode module not available") +class TestQRCode(tests.TransactionCase): + def setUp(self): + super().setUp() + self.ready_mat = self.env["res.partner"].search( + [("name", "=", "Ready Mat")], limit=1 + ) + + def test_print_qr_code_1(self): + qr = stringToQRCode("https://google.com") + qr.print_ascii() + + def test_string_to_qr_code_2(self): + qr = stringToQRCode(self.ready_mat.website) + qr.print_ascii() + + def test_qr_code_file(self): + converter = QRCodeFile("website") + img = converter.odoo_to_message(self.ready_mat) + + self.assertEqual("image/png", img["mime-type"]) + self.assertRegex( + img["body"], + r"[A-Za-z0-9+/=]+$", + "The generated base64 body is incorrect", + ) + + def test_qr_code_data_url(self): + converter = QRCodeDataURL("website") + url = converter.odoo_to_message(self.ready_mat) + self.assertRegex( + url, + r"^data:image/png;base64,[A-Za-z0-9+/=]+$", + "The generated Data URL is not correctly formatted", + )