Files
docuseal-tools/docuseal_signing/tests/test_fetch_fields.py
Quentin Mondot f9a8358e95
Some checks failed
pre-commit / pre-commit (pull_request) Failing after 1m28s
tests / test (pull_request) Successful in 51s
[IMP] docuseal_signing: add unit tests for API payload and server action
Cover with a mocked Docuseal API (shared ``DocusealApiMockMixin``
capturing every request):

- field value conversion matrix in ``_create_submissions``: char,
  selection label, many2one/many2many display names, date formatting,
  empty value left editable while set values are locked
- payload structure and one-submission-per-partner behavior
- draft submissions created with ``send_email`` disabled
- API URL trailing slash stripped
- server action creation is idempotent

Assisted-By: GLM-5.3-Flash
2026-11-09 15:39:26 +01:00

105 lines
3.9 KiB
Python

from odoo.tests import TransactionCase
from .common import DocusealApiMockMixin
class TestFetchFields(DocusealApiMockMixin, TransactionCase):
"""Reconciliation of the field mapping on action_fetch_fields."""
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.phone_field = cls.env["ir.model.fields"].search(
[("model", "=", "res.partner"), ("name", "=", "phone")], limit=1
)
cls.template = cls.env["docuseal.template"].create({
"name": "Old name",
"template_id": 999,
"submitter_role": "Signer",
})
cls.env["docuseal.template.field"].create({
"template_id": cls.template.id,
"docuseal_field_name": "phone",
"partner_field_id": cls.phone_field.id,
})
cls.env["docuseal.template.field"].create({
"template_id": cls.template.id,
"docuseal_field_name": "obsolete",
})
def test_fetch_fields_reconciles_mapping(self):
"""Existing mappings keep their partner field; removed Docuseal
fields are dropped and new ones are added, in the order returned
by the Docuseal API."""
payload = {
"name": "Contract",
"submitters": [{"name": "Signer"}],
"fields": [{"name": "phone"}, {"name": "extra"}, {"name": ""}],
}
with self._mock_api(payload):
self.template.action_fetch_fields()
names = {
field.docuseal_field_name for field in self.template.field_ids
}
self.assertEqual(names, {"phone", "extra"})
# The mapping order follows the Docuseal API field order.
self.assertEqual(
self.template.field_ids.mapped("docuseal_field_name"),
["phone", "extra"],
)
phone_mapping = self.template.field_ids.filtered(
lambda field: field.docuseal_field_name == "phone"
)
self.assertEqual(phone_mapping.partner_field_id, self.phone_field)
# Name and submitter role are only filled when still empty.
self.assertEqual(self.template.name, "Old name")
self.assertEqual(self.template.submitter_role, "Signer")
def test_fetch_fields_fills_empty_name_and_role(self):
"""Name and submitter role are set from the API when missing."""
self.template.name = False
self.template.submitter_role = False
payload = {
"name": "Contract",
"submitters": [{"name": "Counterparty"}],
"fields": [],
}
with self._mock_api(payload):
self.template.action_fetch_fields()
self.assertEqual(self.template.name, "Contract")
self.assertEqual(self.template.submitter_role, "Counterparty")
self.assertFalse(self.template.field_ids)
def test_fetch_fields_restores_order_after_manual_delete(self):
"""Deleting the first mapped field then re-fetching from the
Docuseal API re-establishes the original field order."""
payload = {
"submitters": [{"name": "Signer"}],
"fields": [{"name": "a"}, {"name": "b"}, {"name": "c"}],
}
with self._mock_api(payload):
self.template.action_fetch_fields()
self.assertEqual(
self.template.field_ids.mapped("docuseal_field_name"),
["a", "b", "c"],
)
# The user deletes the first mapping line.
self.template.field_ids[0].unlink()
# Re-fetching must restore the Docuseal order. Invalidate the
# one2many cache to read like a fresh request would (the web
# client always re-reads after the action).
with self._mock_api(payload):
self.template.action_fetch_fields()
self.template.invalidate_recordset(["field_ids"])
self.assertEqual(
self.template.field_ids.mapped("docuseal_field_name"),
["a", "b", "c"],
)