[FIX] docuseal_signing: harden template copies, API errors and emails
- Do not copy the contextual action nor the submitter role with a template: a copy sharing the action would rewrite or delete the original's. - Make the template ID read-only once fetched from Docuseal: another Docuseal template needs a new entry. - Report an invalid API URL or token, an unexpected (non-JSON) response and a connection dropped mid-response as a readable UserError. - Refuse to email a signature request to contacts without a valid email address (email_normalize), before any call to Docuseal. - Stop logging the field values and the Docuseal response, which hold personal data and the signing links. - Prevent adding mapping lines by hand: they come from Docuseal only. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import http.client
|
||||
import io
|
||||
import json
|
||||
import urllib.error
|
||||
@@ -119,7 +120,10 @@ class TestCreateSubmissions(DocusealApiMockMixin, TransactionCase):
|
||||
def test_one_submission_per_partner(self):
|
||||
"""Each partner gets its own submission with a single submitter."""
|
||||
self._set_api_config()
|
||||
partner2 = self.env["res.partner"].create({"name": "Alex Martin"})
|
||||
partner2 = self.env["res.partner"].create({
|
||||
"name": "Alex Martin",
|
||||
"email": "alex@example.com",
|
||||
})
|
||||
with self._mock_api({"id": 57}):
|
||||
self.template._create_submissions(self.partner | partner2)
|
||||
|
||||
@@ -169,3 +173,78 @@ class TestCreateSubmissions(DocusealApiMockMixin, TransactionCase):
|
||||
side_effect=error,
|
||||
), self.assertRaisesRegex(UserError, "422: Role not found"):
|
||||
self.template._create_submissions(self.partner)
|
||||
|
||||
def test_partner_without_email_is_refused(self):
|
||||
"""No request is sent when a contact cannot receive the email,
|
||||
even if the other contacts can."""
|
||||
self._set_api_config()
|
||||
no_email = self.env["res.partner"].create({"name": "Sam Leroy"})
|
||||
with self._mock_api({"id": 62}), self.assertRaisesRegex(
|
||||
UserError, "Sam Leroy"
|
||||
):
|
||||
self.template._create_submissions(self.partner | no_email)
|
||||
self.assertFalse(self.requests)
|
||||
|
||||
# A draft is not emailed: no email address is required.
|
||||
with self._mock_api({"id": 63}):
|
||||
self.template._create_draft_submissions(no_email)
|
||||
self.assertEqual(len(self.requests), 1)
|
||||
|
||||
def _assert_user_error(self, regex, **urlopen_patch):
|
||||
with mock.patch.object(
|
||||
docuseal_template_module.urllib.request, "urlopen", **urlopen_patch
|
||||
), self.assertRaisesRegex(UserError, regex):
|
||||
self.template._create_submissions(self.partner)
|
||||
|
||||
def test_invalid_api_url_raises_user_error(self):
|
||||
"""An API URL without scheme is reported, not a traceback."""
|
||||
self._set_api_config(api_url="sign.example.com")
|
||||
with self.assertRaisesRegex(UserError, "is invalid"):
|
||||
self.template._create_submissions(self.partner)
|
||||
|
||||
def test_unexpected_response_raises_user_error(self):
|
||||
"""A non-JSON response (proxy, login page) is reported."""
|
||||
self._set_api_config()
|
||||
resp = mock.MagicMock()
|
||||
resp.read.return_value = b"<html>Login</html>"
|
||||
context = mock.MagicMock()
|
||||
context.__enter__.return_value = resp
|
||||
self._assert_user_error("unexpected response", return_value=context)
|
||||
|
||||
def test_dropped_connection_raises_user_error(self):
|
||||
"""A connection dropped mid-response is reported."""
|
||||
self._set_api_config()
|
||||
self._assert_user_error(
|
||||
"could not be reached",
|
||||
side_effect=http.client.RemoteDisconnected("closed"),
|
||||
)
|
||||
self._assert_user_error(
|
||||
"could not be reached",
|
||||
side_effect=http.client.IncompleteRead(b""),
|
||||
)
|
||||
|
||||
def test_malformed_email_is_refused(self):
|
||||
"""Malformed and multiple email addresses are refused too."""
|
||||
self._set_api_config()
|
||||
for email in (" ", "not-an-email", "a@example.com; b@example.com"):
|
||||
self.partner.email = email
|
||||
with self._mock_api({"id": 64}), self.assertRaisesRegex(
|
||||
UserError, "valid email"
|
||||
):
|
||||
self.template._create_submissions(self.partner)
|
||||
self.assertFalse(self.requests)
|
||||
|
||||
def test_invalid_token_raises_user_error(self):
|
||||
"""A token pasted with a line break is reported as a token problem,
|
||||
not as an invalid URL."""
|
||||
self._set_api_config()
|
||||
self.env["ir.config_parameter"].sudo().set_param(
|
||||
"docuseal.api_token", "abc\ndef"
|
||||
)
|
||||
with self.assertRaisesRegex(UserError, "API token"):
|
||||
self.template._create_submissions(self.partner)
|
||||
|
||||
def test_error_without_message_names_the_exception(self):
|
||||
"""An exception without message still explains what happened."""
|
||||
self._set_api_config()
|
||||
self._assert_user_error("TimeoutError", side_effect=TimeoutError())
|
||||
|
||||
Reference in New Issue
Block a user