From 3edecd2aada621197ca8a2bfdc15064d9dbb928e Mon Sep 17 00:00:00 2001 From: "Antoine Prieels (ANP)" Date: Thu, 13 Dec 2018 14:55:19 +0000 Subject: [PATCH] [IMP] snailmail_account: Add tests to make sure that all default templates can be sent with Pingen Add tests to make sure that, with sample data, the format of the invoices is accepted by Pingen. --- addons/snailmail_account/__init__.py | 1 + addons/snailmail_account/tests/__init__.py | 1 + .../tests/test_pingen_send.py | 96 +++++++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 addons/snailmail_account/tests/__init__.py create mode 100644 addons/snailmail_account/tests/test_pingen_send.py diff --git a/addons/snailmail_account/__init__.py b/addons/snailmail_account/__init__.py index 35e7c9600c5..d22fb1f7f36 100644 --- a/addons/snailmail_account/__init__.py +++ b/addons/snailmail_account/__init__.py @@ -2,3 +2,4 @@ from . import models from . import wizard +from . import tests diff --git a/addons/snailmail_account/tests/__init__.py b/addons/snailmail_account/tests/__init__.py new file mode 100644 index 00000000000..b81c3a2d614 --- /dev/null +++ b/addons/snailmail_account/tests/__init__.py @@ -0,0 +1 @@ +from . import test_pingen_send diff --git a/addons/snailmail_account/tests/test_pingen_send.py b/addons/snailmail_account/tests/test_pingen_send.py new file mode 100644 index 00000000000..ce73f388274 --- /dev/null +++ b/addons/snailmail_account/tests/test_pingen_send.py @@ -0,0 +1,96 @@ + +import requests +import json +import base64 + +from odoo.addons.account.tests.account_test_classes import AccountingTestCase +from odoo.tests import tagged + +@tagged('post_install', '-at_install', '-standard', 'external') +class TestPingenSend(AccountingTestCase): + + def setUp(self): + super(TestPingenSend, self).setUp() + self.pingen_url = "https://stage-api.pingen.com/document/upload/token/30fc3947dbea4792eb12548b41ec8117/" + self.sample_invoice = self.create_invoice() + self.sample_invoice.partner_id.vat = "BE000000000" + self.letter = self.env['snailmail.letter'].create({ + 'partner_id': self.sample_invoice.partner_id.id, + 'model': 'account.invoice', + 'res_id': self.sample_invoice.id, + 'user_id': self.env.user.id, + 'company_id': self.sample_invoice.company_id.id, + 'report_template': self.env.ref('account.account_invoices').id + }) + self.data = { + 'data': json.dumps({ + 'speed': 1, + 'color': 2, + 'duplex': 0, + 'send': True, + }) + } + + def create_invoice(self): + """ Create a sample invoice """ + currency = self.env.ref('base.EUR') + partner_agrolait = self.env.ref("base.res_partner_2") + product = self.env.ref("product.product_product_4") + + account_receivable = self.env['account.account'].create({ + 'code': 'TESTPINGEN1', + 'name': 'Test Receivable Account', + 'user_type_id': self.env.ref('account.data_account_type_receivable').id, + 'reconcile': True + }) + account_income = self.env['account.account'].create({ + 'code': 'TESTPINGEN2', + 'name': 'Test Account', + 'user_type_id': self.env.ref('account.data_account_type_direct_costs').id + }) + + invoice = self.env['account.invoice'].create({ + 'partner_id': partner_agrolait.id, + 'currency_id': currency.id, + 'name': 'invoice to client', + 'account_id': account_receivable.id, + 'type': 'out_invoice', + 'date_invoice': '2018-12-11', + }) + + self.env['account.invoice.line'].create({ + 'product_id': product.id, + 'quantity': 1, + 'price_unit': 42, + 'invoice_id': invoice.id, + 'name': 'something', + 'account_id': account_income.id, + }) + + invoice.action_invoice_open() + + return invoice + + def render_and_send(self, report_name): + self.sample_invoice.company_id.external_report_layout_id = self.env.ref('web.' + report_name) + self.letter.attachment_id = False + attachment_id = self.letter.with_context(force_report_rendering=True)._fetch_attachment() + + files = { + 'file': ('pingen_test_%s.pdf' % report_name, base64.b64decode(attachment_id.datas), 'application/pdf'), + } + + response = requests.post(self.pingen_url, data=self.data, files=files) + + try: + response.raise_for_status() + except: + return False + + return True + + def test_pingen_send_invoice(self): + self.assertTrue(self.render_and_send('external_layout_standard')) + self.assertTrue(self.render_and_send('external_layout_background')) + self.assertTrue(self.render_and_send('external_layout_boxed')) + self.assertTrue(self.render_and_send('external_layout_clean'))