from odoo.exceptions import AccessError, UserError from odoo.tests import TransactionCase, new_test_user from .common import DocusealApiMockMixin class TestServerAction(DocusealApiMockMixin, TransactionCase): """Lifecycle and execution of the generated contextual server action.""" def test_unlink_removes_server_action(self): """Deleting a template removes its generated server action, instead of leaving an orphaned action browsing a dead record.""" template = self.env["docuseal.template"].create({ "name": "Contract", "template_id": 123, }) template.action_create_server_action() action = template.server_action_id self.assertTrue(action.exists()) template.unlink() self.assertFalse(action.exists()) def test_create_server_action_is_idempotent(self): """Creating the contextual action twice updates the existing action instead of generating a duplicate.""" template = self.env["docuseal.template"].create({ "name": "Contract", "template_id": 456, }) template.action_create_server_action() first_action = template.server_action_id template.action_create_server_action() self.assertEqual(template.server_action_id, first_action) self.assertEqual( self.env["ir.actions.server"].search_count([ ("name", "=", "Request signature (Contract)"), ]), 1, ) def _run_action(self, template, partners, user): return template.server_action_id.with_user(user).with_context( active_model="res.partner", active_ids=partners.ids, active_id=partners[:1].id, ).run() def test_request_signature_is_restricted_to_administrators(self): """The contextual action is reserved to administrators, who can read the Docuseal templates; other users neither see nor run it.""" self._set_api_config() template = self.env["docuseal.template"].create({ "name": "Contract", "template_id": 789, }) template.action_create_server_action() self.assertEqual( template.server_action_id.groups_id, self.env.ref("base.group_system"), ) partner = self.env["res.partner"].create({ "name": "Camille Dupont", "email": "camille@example.com", }) user = new_test_user( self.env, login="docuseal_user", groups="base.group_user,base.group_partner_manager", ) with self._mock_api({"id": 60}), self.assertRaises(AccessError): self._run_action(template, partner, user) self.assertFalse(self.requests) with self._mock_api({"id": 60}): result = self._run_action(template, partner, self.env.user) self.assertEqual(len(self.requests), 1) self.assertEqual(self.requests[0]["payload"]["template_id"], 789) self.assertEqual(result["params"]["type"], "success") def test_request_signature_requires_a_single_contact(self): """Selecting several contacts is refused before any API call, so that no contact is emailed when the action fails.""" self._set_api_config() template = self.env["docuseal.template"].create({ "name": "Contract", "template_id": 790, }) template.action_create_server_action() partners = self.env["res.partner"].create([ {"name": "Camille Dupont"}, {"name": "Alex Martin"}, ]) with self._mock_api({"id": 61}), self.assertRaisesRegex( UserError, "single contact" ): self._run_action(template, partners, self.env.user) self.assertFalse(self.requests) def test_copy_does_not_share_action_or_role(self): """A duplicated template gets neither the contextual action nor the role of the original, so it cannot rewrite or delete them.""" template = self.env["docuseal.template"].create({ "name": "Contract", "template_id": 791, }) template.submitter_role_id = self.env["docuseal.template.role"].create({ "template_id": template.id, "name": "Signer", }) template.action_create_server_action() action = template.server_action_id duplicate = template.copy() self.assertFalse(duplicate.server_action_id) self.assertFalse(duplicate.submitter_role_id) duplicate.unlink() self.assertTrue(action.exists()) self.assertEqual(template.server_action_id, action)