Fields are now ordered as returned by the Docuseal API: a sequence field (reorderable with drag & drop) is written by ``action_fetch_fields`` according to the API field order, so deleting a mapping line and re-fetching restores the original order instead of appending it at the end. Also refresh the French catalog (new Sequence field, new help texts, stale smart_search entries dropped). Covered by a unit test reproducing the delete-then-refetch scenario. Assisted-By: GLM-5.3-Flash
133 lines
4.8 KiB
Python
133 lines
4.8 KiB
Python
import json
|
|
from unittest import mock
|
|
|
|
from odoo.addons.docuseal_signing.models import (
|
|
docuseal_template as docuseal_template_module,
|
|
)
|
|
from odoo.tests import TransactionCase
|
|
|
|
|
|
class TestFetchFields(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 _mock_urlopen(self, payload):
|
|
resp = mock.MagicMock()
|
|
resp.read.return_value = json.dumps(payload).encode()
|
|
context_manager = mock.MagicMock()
|
|
context_manager.__enter__.return_value = resp
|
|
context_manager.__exit__.return_value = False
|
|
return context_manager
|
|
|
|
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 mock.patch.object(
|
|
docuseal_template_module.urllib.request,
|
|
"urlopen",
|
|
return_value=self._mock_urlopen(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 mock.patch.object(
|
|
docuseal_template_module.urllib.request,
|
|
"urlopen",
|
|
return_value=self._mock_urlopen(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 mock.patch.object(
|
|
docuseal_template_module.urllib.request,
|
|
"urlopen",
|
|
return_value=self._mock_urlopen(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 mock.patch.object(
|
|
docuseal_template_module.urllib.request,
|
|
"urlopen",
|
|
return_value=self._mock_urlopen(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"],
|
|
)
|