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.

204 lines
7.2 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 json
  5. from datetime import timedelta
  6. from werkzeug.exceptions import BadRequest
  7. import odoo
  8. from odoo.fields import Date
  9. from odoo.addons.base_rest.controllers.main import _PseudoCollection
  10. from odoo.addons.component.core import WorkContext
  11. from .common import BaseEMCRestCase
  12. class TestSRController(BaseEMCRestCase):
  13. def setUp(self):
  14. super().setUp()
  15. collection = _PseudoCollection("emc.services", self.env)
  16. emc_services_env = WorkContext(
  17. model_name="rest.service.registration", collection=collection
  18. )
  19. self.sr_service = emc_services_env.component(
  20. usage="subscription-request"
  21. )
  22. self.demo_request_1 = self.browse_ref(
  23. "easy_my_coop.subscription_request_1_demo"
  24. )
  25. self.demo_request_2 = self.browse_ref(
  26. "easy_my_coop.subscription_request_waiting_demo"
  27. )
  28. self.demo_share_product = (
  29. self.demo_request_1.share_product_id.product_tmpl_id
  30. )
  31. date = Date.to_string(self.demo_request_1.date)
  32. self.demo_request_1_dict = {
  33. "id": self.demo_request_1.get_api_external_id(),
  34. "name": "Manuel Dublues",
  35. "email": "manuel@demo.net",
  36. "date": date,
  37. "state": "draft",
  38. "ordered_parts": 3,
  39. "share_product": {
  40. "id": self.demo_share_product.get_api_external_id(),
  41. "name": self.demo_share_product.name,
  42. },
  43. "address": {
  44. "street": "schaerbeekstraat",
  45. "zip_code": "1111",
  46. "city": "Brussels",
  47. "country": "BE",
  48. },
  49. "lang": "en_US",
  50. "capital_release_request": [],
  51. }
  52. def test_service(self):
  53. # kept as example
  54. # useful if you need to change data in database and check db type
  55. result = self.sr_service.get(self.demo_request_1.get_api_external_id())
  56. self.assertEquals(self.demo_request_1_dict, result)
  57. all_sr = self.sr_service.search()
  58. self.assertTrue(all_sr)
  59. sr_date = self.demo_request_1.date
  60. date_from = Date.to_string(sr_date - timedelta(days=1))
  61. date_to = Date.to_string(sr_date + timedelta(days=1))
  62. date_sr = self.sr_service.search(date_from=date_from, date_to=date_to)
  63. self.assertTrue(date_sr)
  64. def test_route_get(self):
  65. external_id = self.demo_request_1.get_api_external_id()
  66. route = "/api/subscription-request/%s" % external_id
  67. content = self.http_get_content(route)
  68. self.assertEquals(self.demo_request_1_dict, content)
  69. @odoo.tools.mute_logger("odoo.addons.base_rest.http")
  70. def test_route_get_returns_not_found(self):
  71. route = "/api/subscription-request/%s" % "99999"
  72. response = self.http_get(route)
  73. self.assertEquals(response.status_code, 404)
  74. def test_route_get_string_returns_method_not_allowed(self):
  75. route = "/api/subscription-request/%s" % "abc"
  76. response = self.http_get(route)
  77. self.assertEquals(response.status_code, 405)
  78. def test_route_search_all(self):
  79. route = "/api/subscription-request"
  80. content = self.http_get_content(route)
  81. self.assertIn(self.demo_request_1_dict, content["rows"])
  82. def test_route_search_by_date(self):
  83. sr_date = self.demo_request_1.date
  84. date_from = Date.to_string(sr_date - timedelta(days=1))
  85. date_to = Date.to_string(sr_date + timedelta(days=1))
  86. route = "/api/subscription-request?date_from=%s" % date_from
  87. content = self.http_get_content(route)
  88. self.assertIn(self.demo_request_1_dict, content["rows"])
  89. route = "/api/subscription-request?date_to=%s" % date_to
  90. content = self.http_get_content(route)
  91. self.assertIn(self.demo_request_1_dict, content["rows"])
  92. route = "/api/subscription-request?date_from={}&date_to={}".format(
  93. date_from, date_to
  94. )
  95. content = self.http_get_content(route)
  96. self.assertIn(self.demo_request_1_dict, content["rows"])
  97. route = "/api/subscription-request?date_from=%s" % "2300-01-01"
  98. content = self.http_get_content(route)
  99. self.assertEquals(content["count"], 0)
  100. route = "/api/subscription-request?date_to=%s" % "1900-01-01"
  101. content = self.http_get_content(route)
  102. self.assertEquals(content["count"], 0)
  103. @odoo.tools.mute_logger("odoo.addons.base_rest.http")
  104. def test_route_search_acd_date_returns_bad_request(self):
  105. route = "/api/subscription-request?date_from=%s" % "20200101"
  106. response = self.http_get(route)
  107. self.assertEquals(response.status_code, 400)
  108. def test_route_create(self):
  109. url = "/api/subscription-request"
  110. data = {
  111. "name": "Lisa des Danses",
  112. "email": "lisa@desdanses.be",
  113. "ordered_parts": 3,
  114. "share_product": self.demo_share_product.id,
  115. "address": {
  116. "street": "schaerbeekstraat",
  117. "zip_code": "1111",
  118. "city": "Brussels",
  119. "country": "BE",
  120. },
  121. "lang": "en_US",
  122. }
  123. response = self.http_post(url, data=data)
  124. self.assertEquals(response.status_code, 200)
  125. content = json.loads(response.content.decode("utf-8"))
  126. content.pop("id") # can't know id in advance
  127. expected = {
  128. **data,
  129. **{
  130. "date": Date.to_string(Date.today()),
  131. "state": "draft",
  132. "share_product": {
  133. "id": self.demo_share_product.get_api_external_id(),
  134. "name": self.demo_share_product.name,
  135. },
  136. "capital_release_request": [],
  137. },
  138. }
  139. self.assertEquals(expected, content)
  140. def test_route_update(self):
  141. url = (
  142. "/api/subscription-request/%s"
  143. % self.demo_request_1.get_api_external_id()
  144. )
  145. data = {"state": "done"}
  146. response = self.http_post(url, data=data)
  147. self.assertEquals(response.status_code, 200)
  148. content = json.loads(response.content.decode("utf-8"))
  149. expected = self.demo_request_1_dict
  150. expected["state"] = "done"
  151. self.assertEquals(expected, content)
  152. def test_route_validate(self):
  153. url = (
  154. "/api/subscription-request/%s/validate"
  155. % self.demo_request_1.get_api_external_id()
  156. )
  157. response = self.http_post(url, data={})
  158. self.assertEquals(response.status_code, 200)
  159. content = json.loads(response.content.decode("utf-8"))
  160. state = content.get("state")
  161. self.assertEquals(state, "done")
  162. def test_service_validate_draft_request(self):
  163. self.sr_service.validate(self.demo_request_1.get_api_external_id())
  164. self.assertEquals(self.demo_request_1.state, "done")
  165. self.assertTrue(len(self.demo_request_1.capital_release_request) > 0)
  166. def test_service_validate_done_request(self):
  167. with self.assertRaises(BadRequest):
  168. self.sr_service.validate(self.demo_request_2.get_api_external_id())