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.

124 lines
4.2 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2016 Vauxoo - https://www.vauxoo.com/
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
  4. import json
  5. import requests
  6. from odoo.tests.common import HttpCase
  7. from odoo import api, exceptions, tools, models
  8. from odoo.tools.translate import _
  9. HOST = '127.0.0.1'
  10. PORT = tools.config['xmlrpc_port']
  11. class Webhook(models.Model):
  12. _inherit = 'webhook'
  13. @api.multi
  14. def run_wehook_test_get_foo(self):
  15. """
  16. This method is just to test webhook.
  17. This needs receive a json request with
  18. next json values: {'foo': 'bar'}
  19. If value is different will raise a error.
  20. """
  21. self.ensure_one()
  22. if self.env.request.jsonrequest['foo'] != 'bar':
  23. raise exceptions.ValidationError(_("Wrong value received"))
  24. class FakeHttpRequest(object):
  25. remote_address = None
  26. headers = {}
  27. class FakeRequest(object):
  28. def __init__(self, **args):
  29. self.httprequest = FakeHttpRequest()
  30. class TestWebhookPost(HttpCase):
  31. def setUp(self):
  32. super(TestWebhookPost, self).setUp()
  33. self.webhook = self.env['webhook']
  34. self.url_base = "http://%s:%s" % (HOST, PORT)
  35. self.url = self.get_webhook_url()
  36. def get_webhook_url(self, url='/webhook',
  37. webhook_name="wehook_test"):
  38. """
  39. :param string url: Full url of last url of webhook to use.
  40. If you use a full url will return url
  41. plus session_id
  42. default: /webhook
  43. :param string webhook_name: Name of webhook to process
  44. default: webhook_test
  45. :return: url with
  46. http://IP:PORT/webhook/webhook_name?session_id=###
  47. """
  48. webhook_name = webhook_name.replace('/', '')
  49. if url.startswith('/'):
  50. url = self.url_base + url + '/' + webhook_name
  51. url += '?session_id=' + self.session_id
  52. return url
  53. def post_webhook_event(self, event, url, data, remote_ip=None,
  54. headers=None, params=None):
  55. """
  56. :param string event String: Name of webhook event.
  57. :param string url: Full url of webhook services.
  58. :param dict data: Payload data of request.
  59. :param string remote_ip: Remote IP of webhook to set in
  60. test variable.
  61. :param dict headers: Request headers with main data.
  62. :param dict params: Extra values to send to webhook.
  63. """
  64. if headers is None:
  65. headers = {}
  66. if remote_ip is None:
  67. remote_ip = '127.0.0.1'
  68. headers.update({
  69. 'X-Webhook-Test-Event': event,
  70. 'X-Webhook-Test-Address': remote_ip,
  71. })
  72. headers.setdefault('accept', 'application/json')
  73. headers.setdefault('content-type', 'application/json')
  74. payload = json.dumps(data)
  75. response = requests.request(
  76. "POST", url, data=payload,
  77. headers=headers, params=params)
  78. return response.json()
  79. def test_webhook_ping(self):
  80. """
  81. Test to check that 'ping' generic method work fine!
  82. 'ping' event don't need to add it in inherit class.
  83. """
  84. json_response = self.post_webhook_event(
  85. 'ping', self.url, {})
  86. has_error = json_response.get('error', False)
  87. self.assertEqual(
  88. has_error, False, 'Error in webhook ping test!')
  89. def test_webhook_get_foo(self):
  90. """
  91. Test to check that 'get_foo' event from 'webhook_test'
  92. work fine!
  93. This event is defined in inherit method of test.
  94. """
  95. json_response = self.post_webhook_event(
  96. 'get_foo', self.url, {'foo': 'bar'})
  97. self.assertEqual(
  98. json_response.get('error', False), False,
  99. 'Error in webhook get foo test!.')
  100. def test_webhook_search_with_request(self):
  101. """Test to check that 'search_with_request' method works!"""
  102. fake_req = FakeRequest()
  103. fake_req.httprequest.headers['X-Webhook-Test-Address'] = '127.0.0.1'
  104. wh = self.webhook.search_with_request(fake_req)
  105. self.assertEqual(wh.id, self.env.ref('webhook.webhook_test').id,
  106. "Search webhook from request IP info is not working")