Debian Codegraph @

addons/l10n_id/tests/test_qris_transaction.py

raw symbols SCIP
1
2 from odoo.fields import Command
3 from odoo.addons.account.tests.common import AccountTestInvoicingCommon
4 from odoo.tests import tagged
5 from unittest.mock import patch
6
7
8 @tagged('post_install_l10n', 'post_install', '-at_install')
9 class TestQrisTransaction(AccountTestInvoicingCommon):
10 """ Testing the behaviours of QRIS Transaction """
11
12 @classmethod
13 @AccountTestInvoicingCommon.setup_chart_template('id')
14 def setUpClass(cls):
15 super().setUpClass()
16 cls.invoice = cls.env['account.move'].create({
17 'move_type': 'out_invoice',
18 'partner_id': cls.partner_a.id,
19 'invoice_date': '2019-05-01',
20 'date': '2019-05-01',
21 'invoice_line_ids': [
22 Command.create({'name': 'line1', 'price_unit': 110.0}),
23 ],
24 })
25 cls.qris_status_success = {
26 "status": "success",
27 "data": {
28 "qris_status": "paid",
29 "qris_payment_customername": "Zainal Arief",
30 "qris_payment_methodby": "Sakuku"
31 },
32 "qris_api_version_code": "2206091709"
33 }
34 cls.qris_status_fail = {
35 "status": "failed",
36 "data": {
37 "qris_status": "unpaid"
38 }
39 }
40
41 cls.acc_qris_id = cls.env['res.partner.bank'].create({
42 'acc_number': '123456789012345678',
43 'partner_id': cls.company_data['company'].partner_id.id,
44 'l10n_id_qris_api_key': 'apikey',
45 'l10n_id_qris_mid': 'mid',
46 })
47
48 # Utility method to help create QRIS transactions
49 def _create_sample_transaction(self, model, model_id, qris_id, amount, create_at, content):
50 return self.env['l10n_id.qris.transaction'].create({
51 'model': model,
52 'model_id': model_id,
53 'qris_invoice_id': qris_id,
54 'qris_amount': amount,
55 'qris_creation_datetime': create_at,
56 'qris_content': content,
57 })
58
59 def test_retrieve_backend_record(self):
60 """ Test the _get_record method to retrieve original record accordingly """
61 trx = self._create_sample_transaction(
62 "account.move", str(self.invoice.id), "11254", 11000, "2024-08-01", "qris_content_sample"
63 )
64 invoice = trx._get_record()
65 self.assertEqual(invoice, self.invoice)
66
67 def test_latest_transaction(self):
68 """ Test method _get_latest_transaction"""
69 self._create_sample_transaction(
70 "account.move", "1", "11254", 11000, "2024-08-01 03:00:00", "qris_content_sample"
71 )
72 self._create_sample_transaction(
73 "account.move", "1", "11254", 11000, "2024-08-01 03:00:15", "qris_content_sample_latest"
74 )
75
76 trx = self.env['l10n_id.qris.transaction']._get_latest_transaction('account.move', '1')
77 self.assertTrue(
78 trx['qris_amount'] == 11000 and trx['qris_content'] == "qris_content_sample_latest"
79 )
80
81 def test_l10n_id_get_qris_qr_statuses(self):
82 """ Test the method _l10n_id_get_qris_qr_statuses """
83
84 # Create QRIS transaction with 2 entries in invoice details
85 trx = self._create_sample_transaction(
86 "account.move", "1", "11253", 11000, "2024-08-01", "qris_content_sample"
87 )
88 trx |= self._create_sample_transaction(
89 "account.move", "1", "11254", 11000, "2024-08-01", "qris_content_sample"
90 )
91
92 # if QRIS returns success, _l10n_id_make_request should only be called once and
93 # status returned is {'paid': True, 'qr_statuses': [{self.qris_status_success}]}
94 # and check that the transaction is also paid
95 with patch(
96 'odoo.addons.l10n_id.models.res_bank._l10n_id_make_qris_request', return_value=self.qris_status_success
97 ) as patched:
98 res = trx._l10n_id_get_qris_qr_statuses()
99 patched.assert_called_once()
100 self.assertEqual(len(res['qr_statuses']), 1)
101 success_response = res['qr_statuses'][0]
102 self.assertTrue(res['paid'] and success_response['qris_payment_customername'] == 'Zainal Arief' and trx[0].paid)
103
104 # if QRIS returns fail for all, _l10n_id_make-request should be called twice and
105 # status returned is {'paid': False, 'qr_statuses': [{self.qris_status}, {self.qris_status_fail}]}
106 with patch(
107 'odoo.addons.l10n_id.models.res_bank._l10n_id_make_qris_request', return_value=self.qris_status_fail
108 ) as patched:
109 res = trx._l10n_id_get_qris_qr_statuses()
110 self.assertEqual(patched.call_count, 2)
111 self.assertEqual(len(res['qr_statuses']), 2)