-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_web_pwa_oca_controller.py
124 lines (104 loc) · 4.83 KB
/
test_web_pwa_oca_controller.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright 2020 João Marques
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
import base64
import json
import odoo.tests
from odoo import exceptions
from odoo.modules.module import get_resource_path
class TestUi(odoo.tests.HttpCase):
def setUp(self):
super().setUp()
self.user = self.env.ref("base.user_admin")
self.res_config_settings_obj = (
self.env["res.config.settings"].with_user(self.user).create({})
)
def test_manifest_valid_json(self):
# Call the manifest controller
manifest_data = self.url_open("/web_pwa_oca/manifest.webmanifest")
# should be valid json
manifest_content_str = manifest_data.content.decode("utf-8")
json.loads(manifest_content_str)
def test_manifest_correct_paramenters(self):
# Set PWA parameters in settings
self.res_config_settings_obj.pwa_name = "Test PWA"
self.res_config_settings_obj.pwa_short_name = "Test"
# icon should remain the default one
self.res_config_settings_obj.pwa_icon = False
self.res_config_settings_obj.set_values()
# Call the manifest controller
manifest_data = self.url_open("/web_pwa_oca/manifest.webmanifest")
manifest_content_str = manifest_data.content.decode("utf-8")
manifest_content = json.loads(manifest_content_str)
self.assertEqual(manifest_content["name"], "Test PWA")
self.assertEqual(manifest_content["short_name"], "Test")
# icon should remain the default one
self.assertEqual(
manifest_content["icons"][0]["src"],
"/web_pwa_oca/static/img/icons/icon-128x128.png",
)
self.assertEqual(manifest_content["icons"][0]["sizes"], "128x128")
self.assertTrue(manifest_content["icons"][0]["type"].startswith("image/png"))
def test_manifest_logo_upload(self):
with open(
"%s/static/img/icons/odoo_logo.svg" % get_resource_path("web_pwa_oca"), "rb"
) as fi:
icon_to_send = base64.b64encode(fi.read())
# Set PWA icon in settings
self.res_config_settings_obj.pwa_icon = icon_to_send
self.res_config_settings_obj.set_values()
# Call the manifest controller
manifest_data = self.url_open("/web_pwa_oca/manifest.webmanifest")
manifest_content_str = manifest_data.content.decode("utf-8")
manifest_content = json.loads(manifest_content_str)
self.assertEqual(manifest_content["icons"][0]["src"], "/web_pwa_oca/icon.svg")
self.assertTrue(manifest_content["icons"][0]["type"].startswith("image/svg"))
self.assertEqual(
manifest_content["icons"][0]["sizes"],
"128x128 144x144 152x152 192x192 256x256 512x512",
)
# Get the icon and compare it
icon_data = self.url_open("/web_pwa_oca/icon.svg")
icon_data_bytes = base64.b64encode(icon_data.content)
self.assertEqual(icon_data_bytes, icon_to_send)
def test_png_logo_upload(self):
with open(
"%s/static/img/icons/icon-512x512.png" % get_resource_path("web_pwa_oca"),
"rb",
) as fi:
icon_to_send = base64.b64encode(fi.read())
# Set PWA icon in settings
self.res_config_settings_obj.pwa_icon = icon_to_send
self.res_config_settings_obj.set_values()
# Call the manifest controller
manifest_data = self.url_open("/web_pwa_oca/manifest.webmanifest")
manifest_content_str = manifest_data.content.decode("utf-8")
manifest_content = json.loads(manifest_content_str)
expected_vals = {
"src": "/web_pwa_oca/icon512x512.png",
"sizes": "512x512",
"type": "image/png",
}
self.assertTrue(expected_vals in manifest_content["icons"])
def test_manifest_logo_upload_big(self):
# Set PWA icon in settings
with self.assertRaises(exceptions.UserError):
# Image with more than 2MB
self.res_config_settings_obj.pwa_icon = b"a" * 3000000
self.res_config_settings_obj.set_values()
def test_manifest_logo_upload_extension(self):
with self.assertRaises(exceptions.UserError):
# Image that is not SVG or PNG
self.res_config_settings_obj.pwa_icon = b"a" * 1000
self.res_config_settings_obj.set_values()
def test_manifest_logo_upload_small(self):
icon_to_send = None
with open(
"%s/static/img/icons/icon-128x128.png" % get_resource_path("web_pwa_oca"),
"rb",
) as fi:
icon_to_send = base64.b64encode(fi.read())
# Set PWA icon in settings
with self.assertRaises(exceptions.UserError):
# Image smaller than 512X512
self.res_config_settings_obj.pwa_icon = icon_to_send
self.res_config_settings_obj.set_values()