``action_fetch_fields`` wiped the whole ``field_ids`` mapping on every fetch, dropping the res.partner fields configured by the administrator. Reconcile instead: keep existing mappings untouched, add fields new to the Docuseal template and drop the ones removed from it. Covered by unit tests running against a mocked Docuseal API. Assisted-By: GLM-5.3-Flash
90 lines
3.2 KiB
Python
90 lines
3.2 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."""
|
|
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"})
|
|
|
|
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)
|