Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I fake a recaptcha in a unit test?

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?

like image 655
satish Avatar asked Feb 27 '18 17:02

satish


People also ask

Can reCAPTCHA be fooled?

Disadvantages of reCAPTCHA Visitors may even abandon the site as a result of the test. Some older reCAPTCHA tests can be fooled by bots.

How do I force a reCAPTCHA to fail?

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.

How do I simulate bots for reCAPTCHA?

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.

How do you trigger a reCAPTCHA?

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.


1 Answers

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

like image 128
Rishabh Sairawat Avatar answered Nov 15 '22 17:11

Rishabh Sairawat