[FIX] docuseal_signing: restrict signature requests and report API errors
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m31s
tests / test (pull_request) Successful in 50s

- Reserve the generated "Request signature" action to administrators
  (base.group_system), who are the only ones able to read the Docuseal
  templates, instead of letting other users hit an AccessError.
- Request the signature of a single contact from the contextual action
  (_action_request_signature): with several contacts, a failure could
  leave some of them already emailed while the action reports an error.
  A notification confirms the request.
- Share the Docuseal API call in _docuseal_request: a missing token or a
  Docuseal error (with the explanation it sends) now raises a readable
  UserError when sending submissions too, not only when fetching.

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-09 17:03:54 +01:00
parent c7f3a2dcfe
commit 87653552b1
5 changed files with 210 additions and 43 deletions

View File

@@ -1,8 +1,11 @@
from odoo.tests import TransactionCase
from odoo.exceptions import AccessError, UserError
from odoo.tests import TransactionCase, new_test_user
from .common import DocusealApiMockMixin
class TestServerAction(TransactionCase):
"""Lifecycle of the generated contextual server action."""
class TestServerAction(DocusealApiMockMixin, TransactionCase):
"""Lifecycle and execution of the generated contextual server action."""
def test_unlink_removes_server_action(self):
"""Deleting a template removes its generated server action,
@@ -36,3 +39,62 @@ class TestServerAction(TransactionCase):
]),
1,
)
def _run_action(self, template, partners, user):
return template.server_action_id.with_user(user).with_context(
active_model="res.partner",
active_ids=partners.ids,
active_id=partners[:1].id,
).run()
def test_request_signature_is_restricted_to_administrators(self):
"""The contextual action is reserved to administrators, who can
read the Docuseal templates; other users neither see nor run it."""
self._set_api_config()
template = self.env["docuseal.template"].create({
"name": "Contract",
"template_id": 789,
})
template.action_create_server_action()
self.assertEqual(
template.server_action_id.groups_id,
self.env.ref("base.group_system"),
)
partner = self.env["res.partner"].create({
"name": "Camille Dupont",
"email": "camille@example.com",
})
user = new_test_user(
self.env,
login="docuseal_user",
groups="base.group_user,base.group_partner_manager",
)
with self._mock_api({"id": 60}), self.assertRaises(AccessError):
self._run_action(template, partner, user)
self.assertFalse(self.requests)
with self._mock_api({"id": 60}):
result = self._run_action(template, partner, self.env.user)
self.assertEqual(len(self.requests), 1)
self.assertEqual(self.requests[0]["payload"]["template_id"], 789)
self.assertEqual(result["params"]["type"], "success")
def test_request_signature_requires_a_single_contact(self):
"""Selecting several contacts is refused before any API call, so
that no contact is emailed when the action fails."""
self._set_api_config()
template = self.env["docuseal.template"].create({
"name": "Contract",
"template_id": 790,
})
template.action_create_server_action()
partners = self.env["res.partner"].create([
{"name": "Camille Dupont"}, {"name": "Alex Martin"},
])
with self._mock_api({"id": 61}), self.assertRaisesRegex(
UserError, "single contact"
):
self._run_action(template, partners, self.env.user)
self.assertFalse(self.requests)

View File

@@ -1,5 +1,15 @@
import io
import json
import urllib.error
from unittest import mock
from odoo.exceptions import UserError
from odoo.tests import TransactionCase
from odoo.addons.docuseal_signing.models import (
docuseal_template as docuseal_template_module,
)
from .common import DocusealApiMockMixin
@@ -131,3 +141,31 @@ class TestCreateSubmissions(DocusealApiMockMixin, TransactionCase):
self.requests[0]["url"],
"https://sign.example.com/api/submissions",
)
def test_missing_token_raises_user_error(self):
"""Without API token, the user is told to configure it instead of
getting a technical error."""
self._set_api_config()
self.env["ir.config_parameter"].sudo().set_param("docuseal.api_token", "")
with self._mock_api({"id": 59}), self.assertRaisesRegex(
UserError, "API token"
):
self.template._create_submissions(self.partner)
self.assertFalse(self.requests)
def test_docuseal_error_raises_user_error_with_detail(self):
"""A Docuseal rejection is reported with the explanation it sent."""
self._set_api_config()
error = urllib.error.HTTPError(
"https://sign.example.com/api/submissions",
422,
"Unprocessable Entity",
{},
io.BytesIO(json.dumps({"error": "Role not found"}).encode()),
)
with mock.patch.object(
docuseal_template_module.urllib.request,
"urlopen",
side_effect=error,
), self.assertRaisesRegex(UserError, "422: Role not found"):
self.template._create_submissions(self.partner)