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
4.1KB

  1. # Copyright 2015-2017 LasLabs Inc.
  2. # Copyright 2019 Simone Orsi - Camptocamp SA
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import mock
  5. from odoo.exceptions import ValidationError
  6. from odoo.tests.common import TransactionCase
  7. imp_model = "odoo.addons.website_form_recaptcha.models.website_form_recaptcha"
  8. imp_requests = "%s.requests" % imp_model
  9. class TestCaptcha(TransactionCase):
  10. def setUp(self):
  11. super(TestCaptcha, self).setUp()
  12. self.model_obj = self.env["website.form.recaptcha"]
  13. self.validate_vars = "T1", "T2"
  14. self.website = self.env["website"].get_current_website()
  15. self.website.recaptcha_key_site = "test-site"
  16. self.website.recaptcha_key_secret = "test-secret"
  17. @mock.patch(imp_requests)
  18. def test_post_with_proper_data(self, mk):
  19. mk.post.side_effect = StopIteration
  20. exp1, exp2 = self.validate_vars
  21. try:
  22. self.model_obj._validate_response(exp1, exp2, website=self.website)
  23. except StopIteration:
  24. pass
  25. mk.post.assert_called_once_with(
  26. self.model_obj.URL,
  27. data={
  28. "secret": self.website.recaptcha_key_secret,
  29. "response": exp1,
  30. "remoteip": exp2,
  31. },
  32. )
  33. @mock.patch(imp_requests)
  34. def test_valid(self, mk):
  35. expect = {"success": True}
  36. mk.post().json.return_value = expect
  37. self.assertTrue(
  38. self.model_obj._validate_response(*self.validate_vars, website=self.website)
  39. )
  40. @mock.patch(imp_requests)
  41. def test_known_error_raises(self, mk):
  42. expect = {"error-codes": ["missing-input-secret"]}
  43. mk.post().json.return_value = expect
  44. with self.assertRaises(ValidationError):
  45. self.model_obj._validate_response(*self.validate_vars, website=self.website)
  46. @mock.patch(imp_requests)
  47. def test_known_error_lookup(self, mk):
  48. expect = {"error-codes": ["missing-input-secret"]}
  49. mk.post().json.return_value = expect
  50. try:
  51. self.model_obj._validate_response(*self.validate_vars, website=self.website)
  52. except ValidationError as e:
  53. self.assertEqual(
  54. e.name, self.model_obj._get_error_message(expect["error-codes"][0])
  55. )
  56. @mock.patch(imp_requests)
  57. def test_unknown_error_lookup(self, mk):
  58. expect = {"error-codes": ["derp"]}
  59. mk.post().json.return_value = expect
  60. try:
  61. self.model_obj._validate_response(*self.validate_vars, website=self.website)
  62. except ValidationError as e:
  63. self.assertEqual(e.name, self.model_obj._get_error_message())
  64. @mock.patch(imp_requests)
  65. def test_no_success_raises(self, mk):
  66. expect = {"success": False}
  67. mk.post().json.return_value = expect
  68. with self.assertRaises(ValidationError):
  69. self.model_obj._validate_response(*self.validate_vars, website=self.website)
  70. def test_validate_request_no_value(self):
  71. request = object()
  72. req_values = {}
  73. with self.assertRaises(ValidationError) as err:
  74. self.model_obj._validate_request(request, req_values)
  75. self.assertEqual(err.exception.name, "The secret parameter is missing.")
  76. def test_validate_request_old_value(self):
  77. request = mock.MagicMock()
  78. request.g_recaptcha_response = "all good here"
  79. req_values = {}
  80. with mock.patch.object(type(self.model_obj), "_validate_response") as mocked:
  81. self.assertTrue(self.model_obj._validate_request(request, req_values))
  82. mocked.assert_not_called()
  83. def test_validate_request_validate_response(self):
  84. # Ensure that w/ proper conditions `validate_response is called`
  85. request = mock.MagicMock()
  86. request.g_recaptcha_response = None
  87. request.httprequest.environ = {}
  88. request.httprequest.remote_addr = "1.2.3.4"
  89. with mock.patch.object(type(self.model_obj), "_validate_response") as mocked:
  90. self.model_obj._validate_request(
  91. request, {self.model_obj.RESPONSE_ATTR: "validate_me"}
  92. )
  93. mocked.assert_called_with("validate_me", "1.2.3.4")
上海开阖软件有限公司 沪ICP备12045867号-1