From 223ffc62507cd8b2261c46254fb75e8178ae1a6a Mon Sep 17 00:00:00 2001 From: Abhishek Wagh Date: Tue, 12 Mar 2024 19:38:55 +0530 Subject: [PATCH] test cases for payment cash module --- g2p_payment_cash/__init__.py | 1 + g2p_payment_cash/tests/__init__.py | 1 + g2p_payment_cash/tests/test_entitlement.py | 86 +++++++++++++++++ .../tests/test_payment_manager.py | 92 +++++++++++++++++++ 4 files changed, 180 insertions(+) create mode 100644 g2p_payment_cash/tests/__init__.py create mode 100644 g2p_payment_cash/tests/test_entitlement.py create mode 100644 g2p_payment_cash/tests/test_payment_manager.py diff --git a/g2p_payment_cash/__init__.py b/g2p_payment_cash/__init__.py index 5d6d1635..2150ccf0 100644 --- a/g2p_payment_cash/__init__.py +++ b/g2p_payment_cash/__init__.py @@ -1,3 +1,4 @@ # Part of OpenG2P. See LICENSE file for full copyright and licensing details. from . import models +from . import tests diff --git a/g2p_payment_cash/tests/__init__.py b/g2p_payment_cash/tests/__init__.py new file mode 100644 index 00000000..5887c7e9 --- /dev/null +++ b/g2p_payment_cash/tests/__init__.py @@ -0,0 +1 @@ +from . import test_entitlement, test_payment_manager diff --git a/g2p_payment_cash/tests/test_entitlement.py b/g2p_payment_cash/tests/test_entitlement.py new file mode 100644 index 00000000..11c7e1ef --- /dev/null +++ b/g2p_payment_cash/tests/test_entitlement.py @@ -0,0 +1,86 @@ +from datetime import timedelta + +from odoo import fields +from odoo.tests import tagged +from odoo.tests.common import TransactionCase + +from odoo.addons.g2p_programs.models import constants + + +@tagged("post_install", "-at_install") +class TestG2PEntitlement(TransactionCase): + def setUp(self): + super().setUp() + self.program = self.env["g2p.program"].create({"name": "Test Program"}) + self.partner = self.env["res.partner"].create({"name": "Test Partner"}) + self.cycle = self.env["g2p.cycle"].create( + { + "name": "Test Cycle", + "program_id": self.program.id, + "start_date": fields.Datetime.now(), + "end_date": fields.Datetime.now() + timedelta(days=7), + } + ) + self.entitlement = self.env["g2p.entitlement"].create( + { + "program_id": self.program.id, + "partner_id": self.partner.id, + "cycle_id": self.cycle.id, + "state": "draft", + "initial_amount": 100.00, + } + ) + cash_pay_mgr_obj = "g2p.program.payment.manager.cash" + pay_file_config = self.env["g2p.payment.file.config"].create( + {"name": "Payment file config"} + ) + self.cash_mgr = self.env[cash_pay_mgr_obj].create( + { + "name": "Cash Manager", + "program_id": self.program.id, + "payment_file_config_ids": [(4, pay_file_config.id)], + } + ) + + def test_compute_show_payment_buttons_valid(self): + self.program.get_manager(constants.MANAGER_PAYMENT).unlink() + pay_manager = self.env["g2p.program.payment.manager"].create( + { + "program_id": self.program.id, + "manager_ref_id": self.cash_mgr, + } + ) + self.program.update({"payment_managers": [(4, pay_manager.id)]}) + self.entitlement.state = "approved" + self.entitlement._compute_show_payment_buttons() + self.assertTrue(self.entitlement.show_payment_prepare) + + def test_prepare_and_send_payment_cash_not_approved(self): + self.entitlement.state = "draft" + action = self.entitlement.prepare_and_send_payment_cash() + self.assertEqual(action["params"]["type"], "danger") + self.assertEqual(action["params"]["title"], "Payment") + self.assertEqual(action["params"]["message"], "Entitlement is not approved!") + + def test_prepare_and_send_payment_cash_already_paid(self): + self.entitlement.payment_status = "paid" + self.entitlement.state = "approved" + action = self.entitlement.prepare_and_send_payment_cash() + self.assertEqual(action["params"]["type"], "danger") + self.assertEqual(action["params"]["title"], "Payment") + self.assertEqual(action["params"]["message"], "Invalid operation!") + + def test_prepare_and_send_payment_cash_valid(self): + self.program.get_manager(constants.MANAGER_PAYMENT).unlink() + pay_manager = self.env["g2p.program.payment.manager"].create( + { + "program_id": self.program.id, + "manager_ref_id": self.cash_mgr, + } + ) + self.program.update({"payment_managers": [(4, pay_manager.id)]}) + self.entitlement.state = "approved" + action = self.entitlement.prepare_and_send_payment_cash() + self.assertEqual(action["params"]["type"], "success") + self.assertEqual(action["params"]["title"], "Payment") + self.assertEqual(action["params"]["message"], "Payment was issued and sent.") diff --git a/g2p_payment_cash/tests/test_payment_manager.py b/g2p_payment_cash/tests/test_payment_manager.py new file mode 100644 index 00000000..ff1b76c9 --- /dev/null +++ b/g2p_payment_cash/tests/test_payment_manager.py @@ -0,0 +1,92 @@ +from datetime import timedelta + +from odoo import fields +from odoo.tests import tagged +from odoo.tests.common import TransactionCase + + +@tagged("payment", "cash") +class TestG2PPaymentManagerCash(TransactionCase): + def setUp(self): + super().setUp() + self.program = self.env["g2p.program"].create({"name": "Test Program"}) + self.partner = self.env["res.partner"].create({"name": "Test Partner"}) + self.cycle = self.env["g2p.cycle"].create( + { + "name": "Test Cycle", + "program_id": self.program.id, + "start_date": fields.Datetime.now(), + "end_date": fields.Datetime.now() + timedelta(days=7), + } + ) + self.entitlement = self.env["g2p.entitlement"].create( + { + "program_id": self.program.id, + "partner_id": self.partner.id, + "cycle_id": self.cycle.id, + "state": "draft", + "initial_amount": 100.00, + } + ) + self.payment_manager = self.env["g2p.program.payment.manager.cash"].create( + { + "name": "Cash Payment Manager", + "program_id": self.program.id, + } + ) + + self.payment = self.env["g2p.payment"].create( + { + "name": "Test Payment", + "amount_issued": 100.0, + "cycle_id": self.cycle.id, + "state": "issued", + "entitlement_id": self.entitlement.id, + } + ) + + self.payment_batch = self.env["g2p.payment.batch"].create( + { + "program_id": self.program.id, + "cycle_id": self.cycle.id, + "payment_ids": [(4, self.payment.id)], + } + ) + + def test_selection_manager_ref_id(self): + selection = self.env["g2p.program.payment.manager"]._selection_manager_ref_id() + self.assertIn( + ("g2p.program.payment.manager.cash", "Cash Payment Manager"), selection + ) + + def test_crypto_key_set_creation(self): + crypto_key_set = self.env["g2p.crypto.key.set"].create( + { + "name": "Test Crypto Key Set", + "cash_payment_manager_id": self.payment_manager.id, + } + ) + self.assertEqual(crypto_key_set.cash_payment_manager_id, self.payment_manager) + + def test_send_payments(self): + result = self.payment_manager._send_payments([self.payment_batch]) + self.assertEqual(self.payment.state, "reconciled") + self.assertEqual(self.payment.status, "paid") + self.assertEqual(self.payment.amount_paid, 100.0) + + self.assertEqual(result["type"], "ir.actions.client") + self.assertEqual(result["tag"], "display_notification") + self.assertTrue(result["params"]["sticky"]) + self.assertEqual(result["params"]["type"], "success") + self.assertEqual( + result["params"]["next"]["type"], "ir.actions.act_window_close" + ) + + def test_send_payments_no_batches(self): + result = self.payment_manager._send_payments([]) + + self.assertEqual(result["type"], "ir.actions.client") + self.assertEqual(result["tag"], "display_notification") + self.assertTrue(result["params"]["sticky"]) + self.assertEqual(result["params"]["type"], "warning") + self.assertEqual(result["params"]["message"], "No payment batches to process.")