|
2 | 2 |
|
3 | 3 | import datetime
|
4 | 4 |
|
| 5 | +import ckan.plugins.toolkit as tk |
5 | 6 | import mock
|
6 | 7 | from ckan import model
|
7 | 8 | from ckan.plugins.toolkit import ValidationError
|
@@ -224,6 +225,86 @@ def test_dataset_and_group_at_same_time(self, send_request_email):
|
224 | 225 | assert not send_request_email.called
|
225 | 226 |
|
226 | 227 |
|
| 228 | +# The reCAPTCHA tests |
| 229 | +class TestRecaptchaOfSubscribeSignup(object): |
| 230 | + def setup(self): |
| 231 | + helpers.reset_db() |
| 232 | + tk.config["ckanext.subscribe.apply_recaptcha"] = "true" |
| 233 | + |
| 234 | + def teardown(self): |
| 235 | + tk.config["ckanext.subscribe.apply_recaptcha"] = "false" |
| 236 | + |
| 237 | + # mock the _verify_recaptcha function and test both |
| 238 | + # successful and unsuccessful reCAPTCHA verification scenarios |
| 239 | + @mock.patch("requests.post") |
| 240 | + @mock.patch("ckanext.subscribe.email_verification.send_request_email") |
| 241 | + @mock.patch("ckanext.subscribe.action._verify_recaptcha") |
| 242 | + def test_verify_recaptcha_success( |
| 243 | + self, mock_verify_recaptcha, send_request_email, mock_post |
| 244 | + ): |
| 245 | + # Mocking the reCAPTCHA verification to return True |
| 246 | + mock_verify_recaptcha.return_value = True |
| 247 | + mock_post.return_value = mock.Mock( |
| 248 | + status_code=200, json=lambda: {"success": True} |
| 249 | + ) |
| 250 | + |
| 251 | + dataset = factories.Dataset() |
| 252 | + |
| 253 | + # Calling the subscribe_signup action with a mock reCAPTCHA response |
| 254 | + subscription = helpers.call_action( |
| 255 | + "subscribe_signup", |
| 256 | + {}, |
| 257 | + email="bob@example.com", |
| 258 | + dataset_id=dataset["id"], |
| 259 | + g_recaptcha_response="test-recaptcha-response", |
| 260 | + ) |
| 261 | + |
| 262 | + # Asserting that the email verification function was called once |
| 263 | + send_request_email.assert_called_once() |
| 264 | + eq(send_request_email.call_args[0][0].object_type, "dataset") |
| 265 | + eq(send_request_email.call_args[0][0].object_id, dataset["id"]) |
| 266 | + eq(send_request_email.call_args[0][0].email, "bob@example.com") |
| 267 | + |
| 268 | + # Asserting that the subscription was created with the correct details |
| 269 | + eq(subscription["object_type"], "dataset") |
| 270 | + eq(subscription["object_id"], dataset["id"]) |
| 271 | + eq(subscription["email"], "bob@example.com") |
| 272 | + eq(subscription["verified"], False) |
| 273 | + assert "verification_code" not in subscription |
| 274 | + |
| 275 | + # Checking that the subscription object exists in the database |
| 276 | + subscription_obj = model.Session.query(subscribe_model.Subscription).get( |
| 277 | + subscription["id"] |
| 278 | + ) |
| 279 | + assert subscription_obj |
| 280 | + |
| 281 | + @mock.patch("ckanext.subscribe.email_verification.send_request_email") |
| 282 | + @mock.patch("ckanext.subscribe.action._verify_recaptcha") |
| 283 | + def test_verify_recaptcha_failure(self, mock_verify_recaptcha, send_request_email): |
| 284 | + # Mocking the reCAPTCHA verification to return False |
| 285 | + mock_verify_recaptcha.return_value = False |
| 286 | + |
| 287 | + dataset = factories.Dataset() |
| 288 | + |
| 289 | + # Attempting to call subscribe_signup action with an invalid reCAPTCHA |
| 290 | + try: |
| 291 | + helpers.call_action( |
| 292 | + "subscribe_signup", |
| 293 | + {}, |
| 294 | + email="bob@example.com", |
| 295 | + dataset_id=dataset["id"], |
| 296 | + g_recaptcha_response="wrong_recaptcha", |
| 297 | + ) |
| 298 | + except ValidationError as e: |
| 299 | + # Asserting that the error is raised with the correct message |
| 300 | + assert "Invalid reCAPTCHA. Please try again." in str(e.error_dict) |
| 301 | + |
| 302 | + # Ensuring the email is not sent due to invalid reCAPTCHA |
| 303 | + assert not send_request_email.called |
| 304 | + else: |
| 305 | + assert False, "ValidationError not raised" |
| 306 | + |
| 307 | + |
227 | 308 | class TestSubscribeVerify(object):
|
228 | 309 | def setup(self):
|
229 | 310 | helpers.reset_db()
|
|
0 commit comments