Files
docuseal-tools/docuseal_signing/tests/test_fetch_fields.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

226 lines
9.0 KiB
Python

import urllib.error
from unittest import mock
from odoo.exceptions import UserError
from odoo.tests import Form, TransactionCase
from odoo.addons.docuseal_signing.models import (
docuseal_template as docuseal_template_module,
)
from .common import DocusealApiMockMixin
class TestFetchFields(DocusealApiMockMixin, TransactionCase):
"""Roles and field mapping fetched from the Docuseal API."""
@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,
})
cls.template.submitter_role_id = cls.env["docuseal.template.role"].create({
"template_id": cls.template.id,
"name": "Signer",
"uuid": "uuid-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 setUp(self):
super().setUp()
self._set_api_config()
def _field_names(self):
self.template.invalidate_recordset(["field_ids", "role_ids"])
return self.template.field_ids.mapped("docuseal_field_name")
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", "uuid": "uuid-signer"}],
"fields": [{"name": "phone"}, {"name": "extra"}, {"name": ""}],
}
with self._mock_api(payload):
self.template.action_fetch_fields()
self.assertEqual(
self.requests[0]["url"], "https://sign.example.com/api/templates/999"
)
self.assertEqual(self._field_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 is only filled when still empty; the selected role is kept.
self.assertEqual(self.template.name, "Old name")
self.assertEqual(self.template.submitter_role_id.name, "Signer")
def test_fetch_fields_fills_empty_name_and_role(self):
"""Name and submitter role are set from the API when missing;
roles removed from Docuseal are dropped."""
self.template.name = False
self.template.submitter_role_id = False
payload = {
"name": "Contract",
"submitters": [{"name": "Counterparty", "uuid": "uuid-cp"}],
"fields": [],
}
with self._mock_api(payload):
self.template.action_fetch_fields()
self.assertEqual(self.template.name, "Contract")
self.assertEqual(self.template.submitter_role_id.name, "Counterparty")
self.assertEqual(self.template.role_ids.mapped("name"), ["Counterparty"])
self.assertFalse(self._field_names())
def test_fetch_fields_keeps_only_selected_role_fields(self):
"""Only the fields of the selected role are mapped."""
payload = {
"submitters": [
{"name": "Signer", "uuid": "uuid-signer"},
{"name": "Witness", "uuid": "uuid-witness"},
],
"fields": [
{"name": "phone", "submitter_uuid": "uuid-signer"},
{"name": "witness_name", "submitter_uuid": "uuid-witness"},
{"name": "city", "submitter_uuid": "uuid-signer"},
],
}
with self._mock_api(payload):
self.template.action_fetch_fields()
self.assertEqual(self._field_names(), ["phone", "city"])
self.assertEqual(
self.template.role_ids.mapped("name"), ["Signer", "Witness"]
)
def test_fetch_fields_syncs_roles(self):
"""Roles follow the Docuseal order, are matched by UUID (a rename
keeps the selection) and the selection falls back to the first
role when the selected one is removed."""
payload = {
"submitters": [
{"name": "Witness", "uuid": "uuid-witness"},
{"name": "Main signer", "uuid": "uuid-signer"},
{"name": ""},
],
"fields": [],
}
with self._mock_api(payload):
self.template.action_fetch_fields()
self.template.invalidate_recordset(["role_ids"])
self.assertEqual(
self.template.role_ids.mapped("name"), ["Witness", "Main signer"]
)
self.assertEqual(self.template.submitter_role_id.uuid, "uuid-signer")
# The selected role is removed from the Docuseal template.
payload["submitters"] = [
{"name": "Witness", "uuid": "uuid-witness"},
{"name": "Buyer", "uuid": "uuid-buyer"},
]
with self._mock_api(payload):
self.template.action_fetch_fields()
self.template.invalidate_recordset(["role_ids"])
self.assertEqual(
self.template.role_ids.mapped("name"), ["Witness", "Buyer"]
)
self.assertEqual(self.template.submitter_role_id.name, "Witness")
def test_change_role_loads_its_fields_without_mapping(self):
"""Changing the role in the form fetches the fields of that role
from Docuseal and clears the mapping."""
witness = self.env["docuseal.template.role"].create({
"template_id": self.template.id,
"name": "Witness",
"uuid": "uuid-witness",
})
payload = {
"submitters": [
{"name": "Signer", "uuid": "uuid-signer"},
{"name": "Witness", "uuid": "uuid-witness"},
],
"fields": [
{"name": "phone", "submitter_uuid": "uuid-signer"},
{"name": "witness_name", "submitter_uuid": "uuid-witness"},
{"name": "witness_city", "submitter_uuid": "uuid-witness"},
],
}
# The phone mapping of the current role is lost: warn the user.
with self._mock_api(payload), self.assertLogs(
"odoo.tests.form.onchange", level="WARNING"
) as logs:
with Form(self.template) as form:
form.submitter_role_id = witness
self.assertIn("Field mapping reset", logs.output[0])
self.assertEqual(self.template.submitter_role_id, witness)
self.assertEqual(self._field_names(), ["witness_name", "witness_city"])
self.assertFalse(self.template.field_ids.partner_field_id)
def test_change_role_without_mapping_does_not_warn(self):
"""No warning when there is no mapping to lose."""
self.template.field_ids.partner_field_id = False
witness = self.env["docuseal.template.role"].create({
"template_id": self.template.id,
"name": "Witness",
"uuid": "uuid-witness",
})
payload = {"submitters": [], "fields": [{"name": "witness_name"}]}
with self._mock_api(payload), self.assertNoLogs(
"odoo.tests.form.onchange", level="WARNING"
):
with Form(self.template) as form:
form.submitter_role_id = witness
self.assertEqual(self._field_names(), ["witness_name"])
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", "uuid": "uuid-signer"}],
"fields": [{"name": "a"}, {"name": "b"}, {"name": "c"}],
}
with self._mock_api(payload):
self.template.action_fetch_fields()
self.assertEqual(self._field_names(), ["a", "b", "c"])
# The user deletes the first mapping line.
self.template.field_ids[0].unlink()
with self._mock_api(payload):
self.template.action_fetch_fields()
self.assertEqual(self._field_names(), ["a", "b", "c"])
def test_fetch_errors_raise_user_error(self):
"""A missing token or a Docuseal error is reported to the user."""
with mock.patch.object(
docuseal_template_module.urllib.request,
"urlopen",
side_effect=urllib.error.HTTPError(
"https://sign.example.com", 401, "Unauthorized", {}, None
),
), self.assertRaisesRegex(UserError, "401"):
self.template.action_fetch_fields()
self.env["ir.config_parameter"].sudo().set_param("docuseal.api_token", "")
with self.assertRaises(UserError):
self.template.action_fetch_fields()