I'm using Rails 5 and have a form with a Google recaptcha. I use the Rails gem for this
gem "recaptcha", require: "recaptcha/rails"
So I have a simple form with
<%= form_for @comment, :html => {:class => "commentsForm"} do |f| %>
<div class="field">
<%= f.label :description, 'Your Comments' %><br>
<%= f.text_area :description %>
</div>
<%= recaptcha_tags %>
<div class="actions">
<%= submit_tag 'Submit', :class => 'btn-feedback' %>
</div>
<% end %>
and my controller to deal with this is
def create
@comment = Comment.new(params[:comment].permit(:description))
@comment.user = current_user
if verify_recaptcha(model: @comment)
but how do I write a unit test to test the controller method? I don't know how to "fake" the recaptcha submission. This is all I have
test "do submit comment" do
post comments_url, params: { comment: { description: "Some comments"} }
# Verify we got the proper response
assert_response :success
end
but this fails. How do I fake a recaptcha in a unit test?
Disadvantages of reCAPTCHA Visitors may even abandon the site as a result of the test. Some older reCAPTCHA tests can be fooled by bots.
Click as far away from the checkbox as possible while keeping your mouse cursor in the reCaptcha. You will then most likely fail it. ( it will just bring up the thing where you have to identify the pictures). The pictures are on there because like I said, bots can't process images and recognize things like cars.
This is the key to getting Invisible reCAPTCHA to challenge your human-ness. Right-click and choose "Inspect". Look for the "Toggle device toolbar" button, and click it. Now click "Add custom device", enter a name (e.g. "Robot") and set the User Agent String to "Googlebot/2.1", as shown below.
To invoke the invisible reCAPTCHA, you can either: Automatically bind the challenge to a button or. Programmatically bind the challenge to a button or. Programmatically invoke the challenge.
FOR V2 Only:
With the following test keys, you will always get No CAPTCHA and all verification requests will pass.
Site key: 6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI
Secret key: 6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe
Source: Google reCAPTCHA FAQs Page
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With