Newer
Older
##############################################################################
#
# Redner Odoo module
aronabencherif.diatta@xcg.africa
committed
# Copyright © 2023, 2024 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/>.
#
##############################################################################
aronabencherif.diatta@xcg.africa
committed
from odoo.exceptions import ValidationError
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
from .common import TestCommon
TEMPLATE_URL = "https://test-redner-url/api/v1/template/test-account"
class Test(TestCommon):
"""Test redner template management & their HTTP calls."""
@mock.patch("requests.sessions.Session.put")
@mock.patch("requests.sessions.Session.post")
def test_http_calls(self, requests_post_mock, requests_put_mock):
"""Test HTTP calls while managing redner templates from Odoo."""
# Our mocks return success codes.
success_mock = mock.Mock(
status_code=200, json=lambda: {"name": "test-redner-id"}
)
requests_post_mock.return_value = success_mock
requests_put_mock.return_value = success_mock
# Create a template. Default "language" value needed, see ::create.
values = self.env["redner.template"].default_get(["language"])
values.update({"name": "test-name", "body": "test-body"})
redner_template = self.env["redner.template"].create(values)
self.assertEqual(redner_template.redner_id, "test-redner-id")
requests_put_mock.assert_not_called()
requests_post_mock.assert_called_once_with(
TEMPLATE_URL,
json={
"name": "test-name",
"language": "mustache",
"body": "test-body",
"produces": "text/html",
"body-format": "text",
"locale": "fr_FR",
"version": fields.Datetime.to_string(redner_template.create_date),
},
headers={"Rednerd-API-Key": "test-api-key"},
timeout=20,
)
requests_post_mock.reset_mock()
# Update template.
redner_template.name = "test-name-2"
requests_post_mock.assert_not_called()
requests_put_mock.assert_called_once_with(
TEMPLATE_URL + "/test-redner-id",
json={
"name": "test-name-2",
"language": "mustache",
"body": "test-body",
"produces": "text/html",
"body-format": "text",
"locale": "fr_FR",
"version": fields.Datetime.to_string(redner_template.write_date),
},
headers={"Rednerd-API-Key": "test-api-key"},
timeout=20,
)
requests_put_mock.reset_mock()
aronabencherif.diatta@xcg.africa
committed
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
def test_synchronization_with_redner(self):
redner_template = self._create_redner_template_without_redner()
self.assertFalse(
redner_template.use_redner, "Value of 'Use redner' has changed"
)
self.assertFalse(
redner_template.redner_id, "Template is created without redner"
)
self.assertFalse(
redner_template.detected_keywords, "Template is created without keywords"
)
# Join a template with redner
redner_template.synchronize_template_with_redner()
self.assertTrue(
redner_template.use_redner, "Value of 'Use redner' equals True after join"
)
self.assertTrue(redner_template.redner_id, "Template is updated with redner")
self.assertFalse(
redner_template.detected_keywords, "Template is updated with keywords"
)
with self.assertRaises(ValidationError), self.cr.savepoint():
# Template has already been synced with redner
redner_template.synchronize_template_with_redner()