Skip to content

Commit 6e20de2

Browse files
committed
[MIG] web_pwa_customize: Migration to 19.0
1 parent c2c8938 commit 6e20de2

2 files changed

Lines changed: 89 additions & 5 deletions

File tree

web_pwa_customize/models/res_config_settings.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from PIL import Image
88

9-
from odoo import _, api, exceptions, fields, models
9+
from odoo import api, exceptions, fields, models
1010
from odoo.tools.mimetypes import guess_mimetype
1111

1212

@@ -97,7 +97,7 @@ def set_values(self):
9797
# Fail if icon provided is larger than 2mb
9898
if sys.getsizeof(self.pwa_icon) > 2196608:
9999
raise exceptions.UserError(
100-
_("You can't upload a file with more than 2 MB.")
100+
self.env._("You can't upload a file with more than 2 MB.")
101101
)
102102
# Confirm if the pwa_icon binary content is an SVG or PNG
103103
# and process accordingly
@@ -109,8 +109,10 @@ def set_values(self):
109109
"image/svg"
110110
) and not pwa_icon_mimetype.startswith("image/png"):
111111
raise exceptions.UserError(
112-
_("You can only upload SVG or PNG files. Found: %s.")
113-
% pwa_icon_mimetype
112+
self.env._(
113+
"You can only upload SVG or PNG files. Found: %s.",
114+
pwa_icon_mimetype,
115+
)
114116
)
115117
# Delete all previous records if we are writting new ones
116118
if pwa_icon_ir_attachments:
@@ -121,7 +123,7 @@ def set_values(self):
121123
# Fail if provided PNG is smaller than 512x512
122124
if self._unpack_icon(self.pwa_icon).size < (512, 512):
123125
raise exceptions.UserError(
124-
_("You can only upload PNG files bigger than 512x512")
126+
self.env._("You can only upload PNG files bigger than 512x512")
125127
)
126128
for size in [
127129
(128, 128),

web_pwa_customize/tests/test_web_pwa_customize.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Copyright 2024 Tecnativa - Víctor Martínez
22
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
33

4+
import base64
5+
import io
6+
7+
from PIL import Image
8+
9+
from odoo import exceptions
410
from odoo.tests.common import tagged
511

612
from odoo.addons.base.tests.common import HttpCaseWithUserDemo
@@ -24,3 +30,79 @@ def test_webmanifest_customize(self):
2430
self.assertEqual(data["short_name"], "SHORT-NAME")
2531
self.assertEqual(data["background_color"], "#2E69B5")
2632
self.assertEqual(data["theme_color"], "#2E69B4")
33+
34+
def test_pwa_settings_processing(self):
35+
"""Test the settings logic and icon attachment generation"""
36+
# 1. Test PNG (valid 512x512)
37+
img = Image.new("RGB", (512, 512), color="red")
38+
img_byte_arr = io.BytesIO()
39+
img.save(img_byte_arr, format="PNG")
40+
icon_base64 = base64.b64encode(img_byte_arr.getvalue())
41+
config = self.env["res.config.settings"].create(
42+
{
43+
"pwa_short_name": "New Name",
44+
"pwa_icon": icon_base64,
45+
}
46+
)
47+
config.execute()
48+
# Check manifest endpoint with icons
49+
response = self.url_open("/web/manifest.webmanifest")
50+
data = response.json()
51+
self.assertIn("icons", data)
52+
self.assertGreaterEqual(len(data["icons"]), 6)
53+
# 2. Test SVG (valid)
54+
svg_content = (
55+
'<svg width="100" height="100">'
56+
'<circle cx="50" cy="50" r="40" stroke="green" '
57+
'stroke-width="4" fill="yellow" />'
58+
"</svg>"
59+
)
60+
svg_base64 = base64.b64encode(svg_content.encode("utf-8"))
61+
config.write({"pwa_icon": svg_base64})
62+
config.execute()
63+
response = self.url_open("/web/manifest.webmanifest")
64+
data = response.json()
65+
self.assertEqual(len(data["icons"]), 1)
66+
self.assertEqual(data["icons"][0]["type"], "image/svg+xml")
67+
# 3. Test Clearing Icons
68+
config.write({"pwa_icon": False})
69+
config.execute()
70+
attachments = self.env["ir.attachment"].search(
71+
[("url", "like", "/web_pwa_customize/icon")]
72+
)
73+
self.assertEqual(len(attachments), 0)
74+
# 4. Test PNG too small (< 512x512)
75+
small_img = Image.new("RGB", (100, 100), color="blue")
76+
small_img_byte_arr = io.BytesIO()
77+
small_img.save(small_img_byte_arr, format="PNG")
78+
small_icon_base64 = base64.b64encode(small_img_byte_arr.getvalue())
79+
with self.assertRaises(exceptions.UserError):
80+
config.write({"pwa_icon": small_icon_base64})
81+
config.execute()
82+
# 5. Test File too large (> 2 MB)
83+
large_content = b"0" * (2 * 1024 * 1024 + 1024) # Slightly over 2MB
84+
large_base64 = base64.b64encode(large_content)
85+
with self.assertRaises(exceptions.UserError):
86+
config.write({"pwa_icon": large_base64})
87+
config.execute()
88+
# 6. Test Invalid Image type (gif)
89+
gif_img = Image.new("RGB", (512, 512), color="green")
90+
gif_byte_arr = io.BytesIO()
91+
gif_img.save(gif_byte_arr, format="GIF")
92+
gif_base64 = base64.b64encode(gif_byte_arr.getvalue())
93+
with self.assertRaises(exceptions.UserError):
94+
config.write({"pwa_icon": gif_base64})
95+
config.execute()
96+
97+
def test_default_get_colors(self):
98+
"""Test default colors are set in res.config.settings"""
99+
# Clear ICP to test defaults
100+
icp = self.env["ir.config_parameter"].sudo()
101+
icp.set_param("pwa.manifest.background_color", False)
102+
icp.set_param("pwa.manifest.theme_color", False)
103+
104+
res = self.env["res.config.settings"].default_get(
105+
["pwa_background_color", "pwa_theme_color"]
106+
)
107+
self.assertEqual(res.get("pwa_background_color"), "#714B67")
108+
self.assertEqual(res.get("pwa_theme_color"), "#714B67")

0 commit comments

Comments
 (0)