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.

108 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 json
  5. import requests
  6. from lxml import html
  7. import odoo
  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 = (
  35. self.demo_request_1.share_product_id.product_tmpl_id
  36. )
  37. date = Date.to_string(self.demo_request_1.date)
  38. self.demo_request_1_dict = {
  39. "id": self.demo_request_1.id,
  40. "name": "Manuel Dublues",
  41. "email": "manuel@demo.net",
  42. "date": date,
  43. "state": "draft",
  44. "ordered_parts": 3,
  45. "share_product": {
  46. "id": self.demo_share_product.id,
  47. "name": self.demo_share_product.name,
  48. },
  49. "address": {
  50. "street": "schaerbeekstraat",
  51. "zip_code": "1111",
  52. "city": "Brussels",
  53. "country": "BE",
  54. },
  55. "lang": "en_US",
  56. }
  57. def http_get(self, url, headers=None):
  58. headers = _add_api_key(headers)
  59. if url.startswith("/"):
  60. url = "http://{}:{}{}".format(HOST, PORT, url)
  61. return self.session.get(url, headers=headers)
  62. def http_get_content(self, route, headers=None):
  63. response = self.http_get(route, headers=headers)
  64. self.assertEquals(response.status_code, 200)
  65. content = response.content.decode("utf-8")
  66. return json.loads(content)
  67. def http_post(self, url, data, headers=None):
  68. headers = _add_api_key(headers)
  69. if url.startswith("/"):
  70. url = "http://{}:{}{}".format(HOST, PORT, url)
  71. return self.session.post(url, json=data, headers=headers)
  72. @staticmethod
  73. def html_doc(response):
  74. """Get an HTML LXML document."""
  75. return html.fromstring(response.content)
  76. def login(self, login, password):
  77. url = "/web/login"
  78. response = self.http_get(url)
  79. self.assertEquals(response.status_code, 200)
  80. doc = self.html_doc(response)
  81. token = doc.xpath("//input[@name='csrf_token']")[0].get("value")
  82. response = self.http_post(
  83. url=url,
  84. data={"login": login, "password": password, "csrf_token": token},
  85. )
  86. self.assertEquals(response.status_code, 200)
  87. return response