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.

104 lines
3.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 requests
  5. import json
  6. import odoo
  7. from lxml import html
  8. from odoo.fields import Date
  9. from odoo.addons.base_rest.tests.common import BaseRestCase
  10. HOST = "127.0.0.1"
  11. PORT = odoo.tools.config["http_port"]
  12. def _add_api_key(headers):
  13. key_dict = {"API-KEY": "api-key"}
  14. if headers:
  15. headers.update(key_dict)
  16. else:
  17. headers = key_dict
  18. return headers
  19. class BaseEMCRestCase(BaseRestCase):
  20. @classmethod
  21. def setUpClass(cls, *args, **kwargs):
  22. super().setUpClass(*args, **kwargs)
  23. cls.AuthApiKey = cls.env["auth.api.key"]
  24. emc_manager = cls.env.ref("easy_my_coop.res_users_manager_emc_demo")
  25. cls.api_key_test = cls.AuthApiKey.create(
  26. {"name": "test-key", "key": "api-key", "user_id": emc_manager.id}
  27. )
  28. def setUp(self):
  29. super().setUp()
  30. self.session = requests.Session()
  31. self.demo_request_1 = self.browse_ref(
  32. "easy_my_coop.subscription_request_1_demo"
  33. )
  34. self.demo_share_product = self.demo_request_1.share_product_id
  35. date = Date.to_string(self.demo_request_1.date)
  36. self.demo_request_1_dict = {
  37. "id": self.demo_request_1.id,
  38. "name": "Manuel Dublues",
  39. "email": "manuel@demo.net",
  40. "date": date,
  41. "state": "draft",
  42. "ordered_parts": 3,
  43. "share_product": {
  44. "id": self.demo_share_product.id,
  45. "name": self.demo_share_product.name,
  46. },
  47. "address": {
  48. "street": "schaerbeekstraat",
  49. "zip_code": "1111",
  50. "city": "Brussels",
  51. "country": "BE",
  52. },
  53. "lang": "en_US",
  54. }
  55. def http_get(self, url, headers=None):
  56. headers = _add_api_key(headers)
  57. if url.startswith("/"):
  58. url = "http://%s:%s%s" % (HOST, PORT, url)
  59. return self.session.get(url, headers=headers)
  60. def http_get_content(self, route, headers=None):
  61. response = self.http_get(route, headers=headers)
  62. self.assertEquals(response.status_code, 200)
  63. content = response.content.decode("utf-8")
  64. return json.loads(content)
  65. def http_post(self, url, data, headers=None):
  66. headers = _add_api_key(headers)
  67. if url.startswith("/"):
  68. url = "http://%s:%s%s" % (HOST, PORT, url)
  69. return self.session.post(url, json=data, headers=headers)
  70. @staticmethod
  71. def html_doc(response):
  72. """Get an HTML LXML document."""
  73. return html.fromstring(response.content)
  74. def login(self, login, password):
  75. url = "/web/login"
  76. response = self.http_get(url)
  77. self.assertEquals(response.status_code, 200)
  78. doc = self.html_doc(response)
  79. token = doc.xpath("//input[@name='csrf_token']")[0].get("value")
  80. response = self.http_post(
  81. url=url,
  82. data={"login": login, "password": password, "csrf_token": token},
  83. )
  84. self.assertEquals(response.status_code, 200)
  85. return response