You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.0 KiB

  1. # Copyright 2020 Coop IT Easy SCRL fs
  2. # Robin Keunen <robin@coopiteasy.be>
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  4. import datetime
  5. from unittest.mock import Mock, patch
  6. import requests
  7. from odoo.addons.easy_my_coop.tests.test_base import EMCBaseCase
  8. from .test_data import (
  9. SR_GET_RESULT,
  10. SR_SEARCH_RESULT,
  11. SR_VALIDATE_RESULT,
  12. dict_to_dump,
  13. )
  14. class EMCSRConnectorCase(EMCBaseCase):
  15. def setUp(self):
  16. super().setUp()
  17. self.backend = self.browse_ref(
  18. "easy_my_coop_connector.emc_backend_demo"
  19. )
  20. self.share_type_B_pt = self.browse_ref(
  21. "easy_my_coop.product_template_share_type_2_demo"
  22. )
  23. self.share_type_B_pp = self.share_type_B_pt.product_variant_id
  24. def test_search_requests(self):
  25. SubscriptionRequest = self.env["subscription.request"]
  26. SRBinding = self.env["emc.binding.subscription.request"]
  27. date_to = datetime.date.today()
  28. date_from = date_to - datetime.timedelta(days=1)
  29. with patch.object(requests, "get") as mock_get:
  30. mock_get.return_value = mock_response = Mock()
  31. mock_response.status_code = 200
  32. mock_response.content = dict_to_dump(SR_SEARCH_RESULT)
  33. SubscriptionRequest.fetch_subscription_requests(
  34. date_from=date_from, date_to=date_to
  35. )
  36. external_id = 1
  37. binding = SRBinding.search_binding(self.backend, external_id)
  38. self.assertTrue(
  39. bool(binding),
  40. "no binding created when searching subscription requests",
  41. )
  42. srequest = binding.internal_id
  43. self.assertEquals(srequest.name, "Manuel Dublues")
  44. self.assertEquals(
  45. srequest.share_product_id.id, self.share_type_B_pp.id
  46. )
  47. self.assertEquals(
  48. srequest.subscription_amount, self.share_type_B_pt.list_price * 3
  49. )
  50. with patch.object(requests, "get") as mock_get:
  51. mock_get.return_value = mock_response = Mock()
  52. mock_response.status_code = 200
  53. mock_response.content = dict_to_dump(SR_GET_RESULT)
  54. SubscriptionRequest.backend_read(external_id)
  55. self.assertEquals(srequest.name, "Robin Des Bois")
  56. def test_validate_request(self):
  57. srequest = self.browse_ref("easy_my_coop.subscription_request_1_demo")
  58. with patch.object(requests, "post") as mock_get:
  59. mock_get.return_value = mock_response = Mock()
  60. mock_response.status_code = 200
  61. mock_response.content = dict_to_dump(SR_VALIDATE_RESULT)
  62. srequest.validate_subscription_request()
  63. self.assertEquals(srequest.state, "done")
  64. # local invoice created
  65. self.assertTrue(len(srequest.capital_release_request) > 0)
  66. # local invoice linked to external invoice
  67. self.assertEquals(
  68. srequest.capital_release_request.binding_id.external_id,
  69. SR_VALIDATE_RESULT["id"],
  70. )
  71. # todo test 400