[FIX] docuseal_signing: harden template copies, API errors and emails
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m31s
tests / test (pull_request) Successful in 52s

- 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:
2026-11-09 17:35:19 +01:00
parent 87653552b1
commit fddc2e5c4b
7 changed files with 238 additions and 27 deletions

View File

@@ -223,3 +223,15 @@ class TestFetchFields(DocusealApiMockMixin, TransactionCase):
self.env["ir.config_parameter"].sudo().set_param("docuseal.api_token", "")
with self.assertRaises(UserError):
self.template.action_fetch_fields()
def test_template_id_is_readonly_once_fetched(self):
"""The template ID can be set on a new entry, and becomes read-only
once roles have been fetched from Docuseal."""
form = Form(self.env["docuseal.template"])
form.template_id = 1000
new_template = form.save()
with Form(new_template) as form:
self.assertFalse(form._get_modifier("template_id", "readonly"))
with Form(self.template) as form:
self.assertTrue(form._get_modifier("template_id", "readonly"))

View File

@@ -98,3 +98,25 @@ class TestServerAction(DocusealApiMockMixin, TransactionCase):
):
self._run_action(template, partners, self.env.user)
self.assertFalse(self.requests)
def test_copy_does_not_share_action_or_role(self):
"""A duplicated template gets neither the contextual action nor
the role of the original, so it cannot rewrite or delete them."""
template = self.env["docuseal.template"].create({
"name": "Contract",
"template_id": 791,
})
template.submitter_role_id = self.env["docuseal.template.role"].create({
"template_id": template.id,
"name": "Signer",
})
template.action_create_server_action()
action = template.server_action_id
duplicate = template.copy()
self.assertFalse(duplicate.server_action_id)
self.assertFalse(duplicate.submitter_role_id)
duplicate.unlink()
self.assertTrue(action.exists())
self.assertEqual(template.server_action_id, action)

View File

@@ -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())