Skip to content

Commit a7f582e

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

2 files changed

Lines changed: 100 additions & 6 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: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
import base64
2+
import io
3+
4+
from PIL import Image
5+
6+
from odoo import exceptions
7+
18
# Copyright 2024 Tecnativa - Víctor Martínez
29
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
3-
410
from odoo.tests.common import tagged
511

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

0 commit comments

Comments
 (0)