Skip to content

Commit

Permalink
feat: Add tests for recaptcha_failure and recaptcha_success
Browse files Browse the repository at this point in the history
Create a new class and set apply_recaptcha to true
Add teardown to not use apply_recaptcha in next tests
  • Loading branch information
kovalch committed Jul 2, 2024
1 parent e27cb2a commit e6bfac9
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
81 changes: 81 additions & 0 deletions ckanext/subscribe/tests/test_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import datetime

import ckan.plugins.toolkit as tk
import mock
from ckan import model
from ckan.plugins.toolkit import ValidationError
Expand Down Expand Up @@ -224,6 +225,86 @@ def test_dataset_and_group_at_same_time(self, send_request_email):
assert not send_request_email.called


# The reCAPTCHA tests
class TestRecaptchaOfSubscribeSignup(object):
def setup(self):
helpers.reset_db()
tk.config["ckanext.subscribe.apply_recaptcha"] = "true"

def teardown(self):
tk.config["ckanext.subscribe.apply_recaptcha"] = "false"

# mock the _verify_recaptcha function and test both
# successful and unsuccessful reCAPTCHA verification scenarios
@mock.patch("requests.post")
@mock.patch("ckanext.subscribe.email_verification.send_request_email")
@mock.patch("ckanext.subscribe.action._verify_recaptcha")
def test_verify_recaptcha_success(
self, mock_verify_recaptcha, send_request_email, mock_post
):
# Mocking the reCAPTCHA verification to return True
mock_verify_recaptcha.return_value = True
mock_post.return_value = mock.Mock(
status_code=200, json=lambda: {"success": True}
)

dataset = factories.Dataset()

# Calling the subscribe_signup action with a mock reCAPTCHA response
subscription = helpers.call_action(
"subscribe_signup",
{},
email="[email protected]",
dataset_id=dataset["id"],
g_recaptcha_response="test-recaptcha-response",
)

# Asserting that the email verification function was called once
send_request_email.assert_called_once()
eq(send_request_email.call_args[0][0].object_type, "dataset")
eq(send_request_email.call_args[0][0].object_id, dataset["id"])
eq(send_request_email.call_args[0][0].email, "[email protected]")

# Asserting that the subscription was created with the correct details
eq(subscription["object_type"], "dataset")
eq(subscription["object_id"], dataset["id"])
eq(subscription["email"], "[email protected]")
eq(subscription["verified"], False)
assert "verification_code" not in subscription

# Checking that the subscription object exists in the database
subscription_obj = model.Session.query(subscribe_model.Subscription).get(
subscription["id"]
)
assert subscription_obj

@mock.patch("ckanext.subscribe.email_verification.send_request_email")
@mock.patch("ckanext.subscribe.action._verify_recaptcha")
def test_verify_recaptcha_failure(self, mock_verify_recaptcha, send_request_email):
# Mocking the reCAPTCHA verification to return False
mock_verify_recaptcha.return_value = False

dataset = factories.Dataset()

# Attempting to call subscribe_signup action with an invalid reCAPTCHA
try:
helpers.call_action(
"subscribe_signup",
{},
email="[email protected]",
dataset_id=dataset["id"],
g_recaptcha_response="wrong_recaptcha",
)
except ValidationError as e:
# Asserting that the error is raised with the correct message
assert "Invalid reCAPTCHA. Please try again." in str(e.error_dict)

# Ensuring the email is not sent due to invalid reCAPTCHA
assert not send_request_email.called
else:
assert False, "ValidationError not raised"


class TestSubscribeVerify(object):
def setup(self):
helpers.reset_db()
Expand Down
2 changes: 1 addition & 1 deletion test.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ host = 0.0.0.0
port = 5000

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

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

0 comments on commit e6bfac9

Please sign in to comment.