Files
docuseal-tools/docuseal_signing/tests/test_submissions.py
Quentin Mondot c7f3a2dcfe
Some checks failed
tests / test (pull_request) Successful in 56s
pre-commit / pre-commit (pull_request) Failing after 1m34s
[IMP] docuseal_signing: pick the submitter role from the Docuseal roles
The submitter role was a free text field. It is now a dropdown listing
only the submitter roles of the Docuseal template
(docuseal.template.role), fetched with the Docuseal UUID of each role.

- The "Fetch from Docuseal" button, now above the role, syncs the roles
  (matched by UUID then name, in the Docuseal order, the first one
  selected by default) and the fields of the selected role only,
  keeping the existing mappings.
- Changing the role fetches the fields of the new role from Docuseal
  with an empty mapping, and warns that saving loses the current one.
- Signature requests only send the fields of the submitter role.
- Docuseal API errors and a missing token raise a clear UserError.
- Keep the Docuseal field name of new mapping lines on save
  (force_save on the read-only column).

Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 16:47:05 +01:00

134 lines
5.2 KiB
Python

from odoo.tests import TransactionCase
from .common import DocusealApiMockMixin
class TestCreateSubmissions(DocusealApiMockMixin, TransactionCase):
"""Payload sent to the Docuseal API by _create_submissions."""
@classmethod
def setUpClass(cls):
super().setUpClass()
def partner_field(fname):
return cls.env["ir.model.fields"].search([
("model", "=", "res.partner"), ("name", "=", fname),
], limit=1)
cls.template = cls.env["docuseal.template"].create({
"name": "Onboarding",
"template_id": 777,
})
cls.template.submitter_role_id = cls.env["docuseal.template.role"].create({
"template_id": cls.template.id,
"name": "Signer",
})
for docuseal_name, fname in [
("full_name", "name"),
("email", "email"),
("company_type", "company_type"),
("birth_date", "create_date"),
("country", "country_id"),
("categories", "category_id"),
("phone", "phone"),
]:
cls.env["docuseal.template.field"].create({
"template_id": cls.template.id,
"docuseal_field_name": docuseal_name,
"partner_field_id": partner_field(fname).id,
})
cls.country = cls.env["res.country"].search([], limit=1)
cls.categories = cls.env["res.partner.category"].create([
{"name": "Gold"}, {"name": "Silver"},
])
cls.partner = cls.env["res.partner"].create({
"name": "Camille Dupont",
"email": "camille@example.com",
"phone": False,
"country_id": cls.country.id,
"category_id": cls.categories.ids,
})
def test_submission_payload_conversion(self):
"""Field values are converted per type and locked when set."""
self._set_api_config()
with self._mock_api({"id": 55}):
results = self.template._create_submissions(self.partner)
self.assertEqual(results, [{"id": 55}])
request = self.requests[0]
self.assertEqual(
request["url"], "https://sign.example.com/api/submissions"
)
self.assertIn("test-token", request["headers"].values())
payload = request["payload"]
self.assertEqual(payload["template_id"], 777)
self.assertTrue(payload["send_email"])
self.assertEqual(len(payload["submitters"]), 1)
submitter = payload["submitters"][0]
self.assertEqual(submitter["name"], "Camille Dupont")
self.assertEqual(submitter["email"], "camille@example.com")
self.assertEqual(submitter["role"], "Signer")
fields = {field["name"]: field for field in submitter["fields"]}
self.assertEqual(
fields["full_name"]["default_value"], "Camille Dupont"
)
self.assertTrue(fields["full_name"]["readonly"])
# selection: human-readable label, not the raw key
self.assertEqual(fields["company_type"]["default_value"], "Individual")
self.assertTrue(fields["company_type"]["readonly"])
# many2one / many2many: display names joined with ", "
self.assertEqual(
fields["country"]["default_value"], self.country.display_name
)
self.assertEqual(
fields["categories"]["default_value"],
", ".join(self.categories.mapped("display_name")),
)
# datetime: dd/mm/yyyy
self.assertEqual(
fields["birth_date"]["default_value"],
self.partner.create_date.strftime("%d/%m/%Y"),
)
# empty value: sent as "" and left editable for the signer
self.assertEqual(fields["phone"]["default_value"], "")
self.assertFalse(fields["phone"]["readonly"])
def test_draft_submissions_do_not_send_email(self):
"""_create_draft_submissions creates pre-filled submissions
without asking Docuseal to email the signers."""
self._set_api_config()
with self._mock_api({"id": 56}):
self.template._create_draft_submissions(self.partner)
self.assertFalse(self.requests[0]["payload"]["send_email"])
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"})
with self._mock_api({"id": 57}):
self.template._create_submissions(self.partner | partner2)
self.assertEqual(len(self.requests), 2)
for request in self.requests:
self.assertEqual(len(request["payload"]["submitters"]), 1)
self.assertEqual(
self.requests[1]["payload"]["submitters"][0]["name"],
"Alex Martin",
)
def test_api_url_trailing_slash(self):
"""A configured API URL with a trailing slash does not produce
a double slash in the request URL."""
self._set_api_config(api_url="https://sign.example.com/")
with self._mock_api({"id": 58}):
self.template._create_submissions(self.partner)
self.assertEqual(
self.requests[0]["url"],
"https://sign.example.com/api/submissions",
)