Skip to content

Commit e6bfac9

Browse files
committed
feat: Add tests for recaptcha_failure and recaptcha_success
Create a new class and set apply_recaptcha to true Add teardown to not use apply_recaptcha in next tests
1 parent e27cb2a commit e6bfac9

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed

ckanext/subscribe/tests/test_action.py

+81
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import datetime
44

5+
import ckan.plugins.toolkit as tk
56
import mock
67
from ckan import model
78
from ckan.plugins.toolkit import ValidationError
@@ -224,6 +225,86 @@ def test_dataset_and_group_at_same_time(self, send_request_email):
224225
assert not send_request_email.called
225226

226227

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+
227308
class TestSubscribeVerify(object):
228309
def setup(self):
229310
helpers.reset_db()

test.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ host = 0.0.0.0
99
port = 5000
1010

1111
[app:main]
12-
use = config:/usr/lib/ckan/venv/src/ckan/test-core.ini
12+
use = config:../ckan/test-core.ini
1313

1414
# Insert any custom config settings to be used when running your extension's
1515
# tests here.

0 commit comments

Comments
 (0)