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.

101 lines
2.9 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.tests.common import TransactionCase
  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": 38},
  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": 38},
  48. "state": "draft",
  49. }
  50. def dict_to_dump(content):
  51. return json.dumps(content).encode("utf-8")
  52. class TestCase(TransactionCase):
  53. def setUp(self):
  54. super().setUp()
  55. self.backend = self.browse_ref(
  56. "easy_my_coop_connector.emc_backend_demo"
  57. )
  58. def test_search_requests(self):
  59. SubscriptionRequest = self.env["subscription.request"]
  60. SRBinding = self.env["emc.binding.subscription.request"]
  61. date_to = datetime.date.today()
  62. date_from = date_to - datetime.timedelta(days=1)
  63. with patch.object(requests, "get") as mock_get:
  64. mock_get.return_value = mock_response = Mock()
  65. mock_response.status_code = 200
  66. mock_response.content = dict_to_dump(SEARCH_RESULT)
  67. SubscriptionRequest.fetch_subscription_requests(
  68. date_from=date_from, date_to=date_to
  69. )
  70. external_id = 1
  71. binding = SRBinding.search_binding(self.backend, external_id)
  72. self.assertTrue(
  73. bool(binding),
  74. "no binding created when searching subscription requests",
  75. )
  76. srequest = binding.internal_id
  77. self.assertEquals(srequest.name, "Manuel Dublues")
  78. with patch.object(requests, "get") as mock_get:
  79. mock_get.return_value = mock_response = Mock()
  80. mock_response.status_code = 200
  81. mock_response.content = dict_to_dump(GET_RESULT)
  82. SubscriptionRequest.backend_read(external_id)
  83. self.assertEquals(srequest.name, "Robin Des Bois")