- Do not copy the contextual action nor the submitter role with a template: a copy sharing the action would rewrite or delete the original's. - Make the template ID read-only once fetched from Docuseal: another Docuseal template needs a new entry. - Report an invalid API URL or token, an unexpected (non-JSON) response and a connection dropped mid-response as a readable UserError. - Refuse to email a signature request to contacts without a valid email address (email_normalize), before any call to Docuseal. - Stop logging the field values and the Docuseal response, which hold personal data and the signing links. - Prevent adding mapping lines by hand: they come from Docuseal only. Assisted-By: Claude Opus 5.5 <noreply@anthropic.com>
238 lines
9.5 KiB
Python
238 lines
9.5 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()
|
|
|
|
def test_template_id_is_readonly_once_fetched(self):
|
|
"""The template ID can be set on a new entry, and becomes read-only
|
|
once roles have been fetched from Docuseal."""
|
|
form = Form(self.env["docuseal.template"])
|
|
form.template_id = 1000
|
|
new_template = form.save()
|
|
|
|
with Form(new_template) as form:
|
|
self.assertFalse(form._get_modifier("template_id", "readonly"))
|
|
with Form(self.template) as form:
|
|
self.assertTrue(form._get_modifier("template_id", "readonly"))
|