test_pingen_send.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import requests
  2. import json
  3. import base64
  4. import logging
  5. from odoo.tests.common import HttpCase
  6. from odoo.tests import tagged
  7. _logger = logging.getLogger(__name__)
  8. @tagged('post_install', '-at_install', '-standard', 'external')
  9. class TestPingenSend(HttpCase):
  10. def setUp(self):
  11. super(TestPingenSend, self).setUp()
  12. self.pingen_url = "https://stage-api.pingen.com/document/upload/token/30fc3947dbea4792eb12548b41ec8117/"
  13. self.sample_invoice = self.create_invoice()
  14. self.sample_invoice.partner_id.vat = "BE000000000"
  15. self.letter = self.env['snailmail.letter'].create({
  16. 'partner_id': self.sample_invoice.partner_id.id,
  17. 'model': 'account.move',
  18. 'res_id': self.sample_invoice.id,
  19. 'user_id': self.env.user.id,
  20. 'company_id': self.sample_invoice.company_id.id,
  21. 'report_template': self.env.ref('account.account_invoices').id
  22. })
  23. self.data = {
  24. 'data': json.dumps({
  25. 'speed': 1,
  26. 'color': 1,
  27. 'duplex': 0,
  28. 'send': True,
  29. })
  30. }
  31. def create_invoice(self):
  32. """ Create a sample invoice """
  33. invoice = self.env['account.move'].with_context(default_move_type='out_invoice').create({
  34. 'move_type': 'out_invoice',
  35. 'partner_id': self.env.ref("base.res_partner_2").id,
  36. 'currency_id': self.env.ref('base.EUR').id,
  37. 'invoice_date': '2018-12-11',
  38. 'invoice_line_ids': [(0, 0, {
  39. 'product_id': self.env.ref("product.product_product_4").id,
  40. 'quantity': 1,
  41. 'price_unit': 42,
  42. })],
  43. })
  44. invoice.action_post()
  45. return invoice
  46. def render_and_send(self, report_name):
  47. self.sample_invoice.company_id.external_report_layout_id = self.env.ref('web.' + report_name)
  48. self.letter.attachment_id = False
  49. attachment_id = self.letter.with_context(force_report_rendering=True)._fetch_attachment()
  50. files = {
  51. 'file': ('pingen_test_%s.pdf' % report_name, base64.b64decode(attachment_id.datas), 'application/pdf'),
  52. }
  53. response = requests.post(self.pingen_url, data=self.data, files=files)
  54. if 400 <= response.status_code <= 599 or response.json()['error']:
  55. msg = "%(code)s %(side)s Error: %(reason)s for url: %(url)s\n%(body)s" % {
  56. 'code': response.status_code,
  57. 'side': r"%s",
  58. 'reason': response.reason,
  59. 'url': self.pingen_url,
  60. 'body': response.text}
  61. if response.status_code <= 499 or response.json()['error']:
  62. raise requests.HTTPError(msg % "Client")
  63. else:
  64. _logger.warning(msg % "Server")
  65. def test_pingen_send_invoice(self):
  66. self.render_and_send('external_layout_standard')
  67. self.render_and_send('external_layout_striped')
  68. self.render_and_send('external_layout_boxed')
  69. self.render_and_send('external_layout_bold')