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.

155 lines
4.7 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. import json
  6. from unittest.mock import Mock, patch
  7. import requests
  8. from odoo.addons.easy_my_coop.tests.test_base import EMCBaseCase
  9. NOT_FOUND_ERROR = {"name": "Not Found", "code": 404}
  10. FORBIDDEN_ERROR = {"name": "Forbidden", "code": 403}
  11. SERVER_ERROR = {"name": "Server Error", "code": 500}
  12. NO_RESULT = {"count": 0, "rows": []}
  13. SEARCH_RESULT = {
  14. "count": 1,
  15. "rows": [
  16. {
  17. "id": 1,
  18. "date": "2020-05-14",
  19. "email": "manuel@demo.net",
  20. "address": {
  21. "city": "Brussels",
  22. "street": "schaerbeekstraat",
  23. "zip_code": "1111",
  24. "country": "BE",
  25. },
  26. "lang": "en_US",
  27. "ordered_parts": 3,
  28. "name": "Manuel Dublues",
  29. "share_product": {"name": "Part B - Worker", "id": 31},
  30. "state": "draft",
  31. }
  32. ],
  33. }
  34. GET_RESULT = {
  35. "id": 1,
  36. "name": "Robin Des Bois",
  37. "date": "2020-05-14",
  38. "email": "manuel@demo.net",
  39. "address": {
  40. "city": "Brussels",
  41. "street": "schaerbeekstraat",
  42. "zip_code": "1111",
  43. "country": "BE",
  44. },
  45. "lang": "en_US",
  46. "ordered_parts": 3,
  47. "share_product": {"name": "Part B - Worker", "id": 31},
  48. "state": "draft",
  49. }
  50. VALIDATE_RESULT = {
  51. "id": 9999,
  52. "number": "SUBJ/2020/001",
  53. "date_due": "2020-08-12",
  54. "state": "open",
  55. "date_invoice": "2020-08-12",
  56. "date": "2020-08-12",
  57. "type": "out_invoice",
  58. "subscription_request": {"name": "Manuel Dublues", "id": 1},
  59. "partner": {"name": "Manuel Dublues", "id": 1},
  60. "invoice_lines": [
  61. {
  62. "price_unit": 25.0,
  63. "quantity": 3.0,
  64. "account": {"name": "Product Sales", "id": 2},
  65. "name": "Part B - Worker",
  66. "product": {"name": "Part B - Worker", "id": 2},
  67. }
  68. ],
  69. "journal": {"name": "Subscription Journal", "id": 1},
  70. "account": {"name": "Cooperators", "id": 1},
  71. }
  72. def dict_to_dump(content):
  73. return json.dumps(content).encode("utf-8")
  74. class EMCConnectorCase(EMCBaseCase):
  75. def setUp(self):
  76. super().setUp()
  77. self.backend = self.browse_ref(
  78. "easy_my_coop_connector.emc_backend_demo"
  79. )
  80. self.share_type_B_pt = self.browse_ref(
  81. "easy_my_coop.product_template_share_type_2_demo"
  82. )
  83. self.share_type_B_pp = self.share_type_B_pt.product_variant_id
  84. def test_search_requests(self):
  85. SubscriptionRequest = self.env["subscription.request"]
  86. SRBinding = self.env["emc.binding.subscription.request"]
  87. date_to = datetime.date.today()
  88. date_from = date_to - datetime.timedelta(days=1)
  89. with patch.object(requests, "get") as mock_get:
  90. mock_get.return_value = mock_response = Mock()
  91. mock_response.status_code = 200
  92. mock_response.content = dict_to_dump(SEARCH_RESULT)
  93. SubscriptionRequest.fetch_subscription_requests(
  94. date_from=date_from, date_to=date_to
  95. )
  96. external_id = 1
  97. binding = SRBinding.search_binding(self.backend, external_id)
  98. self.assertTrue(
  99. bool(binding),
  100. "no binding created when searching subscription requests",
  101. )
  102. srequest = binding.internal_id
  103. self.assertEquals(srequest.name, "Manuel Dublues")
  104. self.assertEquals(
  105. srequest.share_product_id.id, self.share_type_B_pp.id
  106. )
  107. self.assertEquals(
  108. srequest.subscription_amount, self.share_type_B_pt.list_price * 3
  109. )
  110. with patch.object(requests, "get") as mock_get:
  111. mock_get.return_value = mock_response = Mock()
  112. mock_response.status_code = 200
  113. mock_response.content = dict_to_dump(GET_RESULT)
  114. SubscriptionRequest.backend_read(external_id)
  115. self.assertEquals(srequest.name, "Robin Des Bois")
  116. def test_validate_request(self):
  117. srequest = self.browse_ref("easy_my_coop.subscription_request_1_demo")
  118. with patch.object(requests, "post") as mock_get:
  119. mock_get.return_value = mock_response = Mock()
  120. mock_response.status_code = 200
  121. mock_response.content = dict_to_dump(VALIDATE_RESULT)
  122. srequest.validate_subscription_request()
  123. self.assertEquals(srequest.state, "done")
  124. # local invoice created
  125. self.assertTrue(len(srequest.capital_release_request) > 0)
  126. # local invoice linked to external invoice
  127. self.assertEquals(
  128. srequest.capital_release_request.binding_id.external_id,
  129. VALIDATE_RESULT["id"],
  130. )
  131. # todo test 400