Files
docuseal-tools/docuseal_signing/tests/test_server_action.py
Quentin Mondot 87653552b1
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m31s
tests / test (pull_request) Successful in 50s
[FIX] docuseal_signing: restrict signature requests and report API errors
- 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>
2026-11-09 17:04:23 +01:00

101 lines
3.8 KiB
Python

from odoo.exceptions import AccessError, UserError
from odoo.tests import TransactionCase, new_test_user
from .common import DocusealApiMockMixin
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,
instead of leaving an orphaned action browsing a dead record."""
template = self.env["docuseal.template"].create({
"name": "Contract",
"template_id": 123,
})
template.action_create_server_action()
action = template.server_action_id
self.assertTrue(action.exists())
template.unlink()
self.assertFalse(action.exists())
def test_create_server_action_is_idempotent(self):
"""Creating the contextual action twice updates the existing
action instead of generating a duplicate."""
template = self.env["docuseal.template"].create({
"name": "Contract",
"template_id": 456,
})
template.action_create_server_action()
first_action = template.server_action_id
template.action_create_server_action()
self.assertEqual(template.server_action_id, first_action)
self.assertEqual(
self.env["ir.actions.server"].search_count([
("name", "=", "Request signature (Contract)"),
]),
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)