test_error.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import json
  2. from unittest.mock import patch
  3. from odoo.tools import config, mute_logger
  4. from odoo.addons.test_http.controllers import CT_JSON
  5. from .test_common import TestHttpBase
  6. class TestHttpErrorHttp(TestHttpBase):
  7. @mute_logger('odoo.http') # UserError("Walter is AFK")
  8. def test_httperror0_exceptions_as_404(self):
  9. with self.subTest('Decorator/AccessError'):
  10. res = self.nodb_url_open('/test_http/hide_errors/decorator?error=AccessError')
  11. self.assertEqual(res.status_code, 404, "AccessError are configured to be hidden, they should be re-thrown as NotFound")
  12. self.assertNotIn("Wrong iris code", res.text, "The real AccessError message should be hidden.")
  13. with self.subTest('Decorator/UserError'):
  14. res = self.nodb_url_open('/test_http/hide_errors/decorator?error=UserError')
  15. self.assertEqual(res.status_code, 400, "UserError are not configured to be hidden, they should be kept as-is.")
  16. self.assertIn("Walter is AFK", res.text, "The real UserError message should be kept")
  17. with self.subTest('Context-Manager/AccessError'):
  18. res = self.nodb_url_open('/test_http/hide_errors/context-manager?error=AccessError')
  19. self.assertEqual(res.status_code, 404, "AccessError are configured to be hidden, they should be re-thrown as NotFound")
  20. self.assertNotIn("Wrong iris code", res.text, "The real AccessError message should be hidden.")
  21. with self.subTest('Context-Manager/UserError'):
  22. res = self.nodb_url_open('/test_http/hide_errors/context-manager?error=UserError')
  23. self.assertEqual(res.status_code, 400, "UserError are not configured to be hidden, they should be kept as-is.")
  24. self.assertIn("Walter is AFK", res.text, "The real UserError message should be kept")
  25. class TestHttpJsonError(TestHttpBase):
  26. jsonrpc_error_structure = {
  27. 'error': {
  28. 'code': ...,
  29. 'data': {
  30. 'arguments': ...,
  31. 'context': ...,
  32. 'debug': ...,
  33. 'message': ...,
  34. 'name': ...,
  35. },
  36. 'message': ...,
  37. },
  38. 'id': ...,
  39. 'jsonrpc': ...,
  40. }
  41. def assertIsErrorPayload(self, payload):
  42. self.assertEqual(
  43. set(payload),
  44. set(self.jsonrpc_error_structure),
  45. )
  46. self.assertEqual(
  47. set(payload['error']),
  48. set(self.jsonrpc_error_structure['error']),
  49. )
  50. self.assertEqual(
  51. set(payload['error']['data']),
  52. set(self.jsonrpc_error_structure['error']['data']),
  53. )
  54. @mute_logger('odoo.http')
  55. def test_errorjson0_value_error(self):
  56. res = self.db_url_open('/test_http/json_value_error',
  57. data=json.dumps({'jsonrpc': '2.0', 'id': 1234, 'params': {}}),
  58. headers=CT_JSON
  59. )
  60. res.raise_for_status()
  61. self.assertEqual(res.status_code, 200)
  62. self.assertEqual(res.headers.get('Content-Type', ''), 'application/json; charset=utf-8')
  63. payload = res.json()
  64. self.assertIsErrorPayload(payload)
  65. error_data = payload['error']['data']
  66. self.assertEqual(error_data['name'], 'builtins.ValueError')
  67. self.assertEqual(error_data['message'], 'Unknown destination')
  68. self.assertEqual(error_data['arguments'], ['Unknown destination'])
  69. self.assertEqual(error_data['context'], {})