-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix recaptcha tests #14
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import datetime | ||
|
||
import ckan.plugins.toolkit as tk | ||
import mock | ||
from ckan import model | ||
from ckan.plugins.toolkit import ValidationError | ||
|
@@ -223,102 +224,85 @@ def test_dataset_and_group_at_same_time(self, send_request_email): | |
|
||
assert not send_request_email.called | ||
|
||
# Adding the reCAPTCHA tests | ||
@mock.patch("requests.post") | ||
def test_verify_recaptcha_success(self, mock_post): | ||
mock_post.return_value = mock.Mock(status_code=200) | ||
mock_post.return_value.json.return_value = {"success": True} | ||
|
||
response = helpers.call_action( | ||
"subscribe_signup", | ||
{}, | ||
email="[email protected]", | ||
dataset_id=factories.Dataset()["id"], | ||
__extras={"g-recaptcha-response": "test-recaptcha-response"}, | ||
) | ||
assert "email" in response | ||
|
||
@mock.patch("requests.post") | ||
def test_verify_recaptcha_failure(self, mock_post): | ||
mock_post.return_value = mock.Mock(status_code=200) | ||
mock_post.return_value.json.return_value = {"success": False} | ||
# The reCAPTCHA tests | ||
class TestRecaptchaOfSubscribeSignup(object): | ||
def setup(self): | ||
helpers.reset_db() | ||
tk.config["ckanext.subscribe.apply_recaptcha"] = "true" | ||
|
||
with assert_raises(ValidationError): | ||
helpers.call_action( | ||
"subscribe_signup", | ||
{}, | ||
email="[email protected]", | ||
dataset_id=factories.Dataset()["id"], | ||
__extras={"g-recaptcha-response": "test-recaptcha-response"}, | ||
) | ||
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") | ||
def test_recaptcha_frontend_form(self, mock_post, send_request_email): | ||
mock_post.return_value = mock.Mock(status_code=200) | ||
mock_post.return_value.json.return_value = {"success": True} | ||
@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"], | ||
__extras={"g-recaptcha-response": "test-recaptcha-response"}, | ||
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 | ||
subscription_obj = model.Session.query(Subscription).get(subscription["id"]) | ||
|
||
# Checking that the subscription object exists in the database | ||
subscription_obj = model.Session.query(subscribe_model.Subscription).get( | ||
subscription["id"] | ||
) | ||
assert subscription_obj | ||
|
||
# Verify that 'g-recaptcha-response' was passed in __extras | ||
extras = subscription["__extras"] | ||
assert "g-recaptcha-response" in extras | ||
eq(extras["g-recaptcha-response"], "test-recaptcha-response") | ||
|
||
|
||
@mock.patch("requests.post") | ||
@mock.patch("ckanext.subscribe.email_verification.send_request_email") | ||
@mock.patch("ckan.plugins.toolkit.request") | ||
def test_recaptcha_backend_form(self, mock_request, mock_post, send_request_email): | ||
mock_post.return_value = mock.Mock(status_code=200) | ||
mock_post.return_value.json.return_value = {"success": True} | ||
|
||
# Mock the request parameters to include g-recaptcha-response | ||
mock_request.params = {"g-recaptcha-response": "test-recaptcha-response"} | ||
|
||
dataset = factories.Dataset() | ||
|
||
subscription = helpers.call_action( | ||
"subscribe_signup", | ||
{}, | ||
email="[email protected]", | ||
dataset_id=dataset["id"], | ||
) | ||
|
||
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]") | ||
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 | ||
subscription_obj = model.Session.query(Subscription).get(subscription["id"]) | ||
assert subscription_obj | ||
|
||
# Verify that 'g-recaptcha-response' was passed in request params | ||
eq(mock_request.params.get("g-recaptcha-response"), "test-recaptcha-response") | ||
@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): | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for switching this back, I forgot all about it :)