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.

231 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. import requests
  6. from lxml import html
  7. import odoo
  8. from odoo.addons.base_rest.tests.common import BaseRestCase
  9. HOST = "127.0.0.1"
  10. PORT = odoo.tools.config["http_port"]
  11. class BaseEMCRestCase(BaseRestCase):
  12. @classmethod
  13. def setUpClass(cls, *args, **kwargs):
  14. super().setUpClass(*args, **kwargs)
  15. cls.AuthApiKey = cls.env["auth.api.key"]
  16. cls.api_key_test = cls.env.ref(
  17. "easy_my_coop_api.auth_api_key_manager_emc_demo"
  18. )
  19. cls._chart_template_create()
  20. cls._add_chart_of_accounts()
  21. cls._journals_setup()
  22. def setUp(self):
  23. super().setUp()
  24. self.session = requests.Session()
  25. @classmethod
  26. def _chart_template_create(cls):
  27. transfer_account_id = cls.env["account.account.template"].create(
  28. {
  29. "code": "000",
  30. "name": "Liquidity Transfers",
  31. "reconcile": True,
  32. "user_type_id": cls.env.ref(
  33. "account.data_account_type_current_assets"
  34. ).id,
  35. }
  36. )
  37. cls.chart = cls.env["account.chart.template"].create(
  38. {
  39. "name": "Test COA",
  40. "code_digits": 4,
  41. "bank_account_code_prefix": 1014,
  42. "cash_account_code_prefix": 1014,
  43. "currency_id": cls.env.ref("base.USD").id,
  44. "transfer_account_code_prefix": "000",
  45. }
  46. )
  47. transfer_account_id.update({"chart_template_id": cls.chart.id})
  48. cls.env["ir.model.data"].create(
  49. {
  50. "res_id": transfer_account_id.id,
  51. "model": transfer_account_id._name,
  52. "name": "Liquidity Transfers",
  53. }
  54. )
  55. act = cls.env["account.account.template"].create(
  56. {
  57. "code": "001",
  58. "name": "Expenses",
  59. "user_type_id": cls.env.ref(
  60. "account.data_account_type_expenses"
  61. ).id,
  62. "chart_template_id": cls.chart.id,
  63. "reconcile": True,
  64. }
  65. )
  66. cls.env["ir.model.data"].create(
  67. {"res_id": act.id, "model": act._name, "name": "expenses"}
  68. )
  69. act = cls.env["account.account.template"].create(
  70. {
  71. "code": "002",
  72. "name": "Product Sales",
  73. "user_type_id": cls.env.ref(
  74. "account.data_account_type_revenue"
  75. ).id,
  76. "chart_template_id": cls.chart.id,
  77. "reconcile": True,
  78. }
  79. )
  80. cls.env["ir.model.data"].create(
  81. {"res_id": act.id, "model": act._name, "name": "sales"}
  82. )
  83. act = cls.env["account.account.template"].create(
  84. {
  85. "code": "003",
  86. "name": "Account Receivable",
  87. "user_type_id": cls.env.ref(
  88. "account.data_account_type_receivable"
  89. ).id,
  90. "chart_template_id": cls.chart.id,
  91. "reconcile": True,
  92. }
  93. )
  94. cls.env["ir.model.data"].create(
  95. {"res_id": act.id, "model": act._name, "name": "receivable"}
  96. )
  97. act = cls.env["account.account.template"].create(
  98. {
  99. "code": "004",
  100. "name": "Account Payable",
  101. "user_type_id": cls.env.ref(
  102. "account.data_account_type_payable"
  103. ).id,
  104. "chart_template_id": cls.chart.id,
  105. "reconcile": True,
  106. }
  107. )
  108. cls.env["ir.model.data"].create(
  109. {"res_id": act.id, "model": act._name, "name": "payable"}
  110. )
  111. @classmethod
  112. def _add_chart_of_accounts(cls):
  113. cls.company = cls.env.user.company_id
  114. cls.chart.try_loading_for_current_company()
  115. cls.revenue = cls.env["account.account"].search(
  116. [
  117. (
  118. "user_type_id",
  119. "=",
  120. cls.env.ref("account.data_account_type_revenue").id,
  121. )
  122. ],
  123. limit=1,
  124. )
  125. cls.expense = cls.env["account.account"].search(
  126. [
  127. (
  128. "user_type_id",
  129. "=",
  130. cls.env.ref("account.data_account_type_expenses").id,
  131. )
  132. ],
  133. limit=1,
  134. )
  135. cls.receivable = cls.env["account.account"].search(
  136. [
  137. (
  138. "user_type_id",
  139. "=",
  140. cls.env.ref("account.data_account_type_receivable").id,
  141. )
  142. ],
  143. limit=1,
  144. )
  145. cls.payable = cls.env["account.account"].search(
  146. [
  147. (
  148. "user_type_id",
  149. "=",
  150. cls.env.ref("account.data_account_type_payable").id,
  151. )
  152. ],
  153. limit=1,
  154. )
  155. cls.equity_account = cls.env.ref("easy_my_coop.account_equity_demo")
  156. cls.cooperator_account = cls.env.ref(
  157. "easy_my_coop.account_cooperator_demo"
  158. )
  159. return True
  160. @classmethod
  161. def _journals_setup(cls):
  162. cls.subscription_journal = cls.env.ref(
  163. "easy_my_coop.subscription_journal"
  164. )
  165. cls.subscription_journal.write(
  166. {
  167. "default_debit_account_id": cls.equity_account.id,
  168. "default_credit_account_id": cls.equity_account.id,
  169. }
  170. )
  171. return True
  172. def _add_api_key(self, headers):
  173. key_dict = {"API-KEY": self.api_key_test.key}
  174. if headers:
  175. headers.update(key_dict)
  176. else:
  177. headers = key_dict
  178. return headers
  179. def http_get(self, url, headers=None):
  180. headers = self._add_api_key(headers)
  181. if url.startswith("/"):
  182. url = "http://{}:{}{}".format(HOST, PORT, url)
  183. return self.session.get(url, headers=headers)
  184. def http_get_content(self, route, headers=None):
  185. response = self.http_get(route, headers=headers)
  186. self.assertEquals(response.status_code, 200)
  187. content = response.content.decode("utf-8")
  188. return json.loads(content)
  189. def http_post(self, url, data, headers=None):
  190. headers = self._add_api_key(headers)
  191. if url.startswith("/"):
  192. url = "http://{}:{}{}".format(HOST, PORT, url)
  193. return self.session.post(url, json=data, headers=headers)
  194. @staticmethod
  195. def html_doc(response):
  196. """Get an HTML LXML document."""
  197. return html.fromstring(response.content)
  198. def login(self, login, password):
  199. url = "/web/login"
  200. response = self.http_get(url)
  201. self.assertEquals(response.status_code, 200)
  202. doc = self.html_doc(response)
  203. token = doc.xpath("//input[@name='csrf_token']")[0].get("value")
  204. response = self.http_post(
  205. url=url,
  206. data={"login": login, "password": password, "csrf_token": token},
  207. )
  208. self.assertEquals(response.status_code, 200)
  209. return response