- 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>
123 lines
4.6 KiB
Python
123 lines
4.6 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)
|
|
|
|
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)
|