noCAPTCHA: Don't validate captcha in testing environment

Hi,

This isn’t really an issue, just a suggestion in case anyone cares 😃

I’m trying to test my code with Laravel’s handy helpers and MailThief for intercepting e-mails… But getting past the captcha with an automated script is obviously not so easy…

To get around this, I read that some people just disable the captcha for testing. So to do this, I copied your validator and changed it like this:

    Validator::extend('nocaptcha', function($attribute, $value, $parameters, $validator) {
        unset($attribute);

        if (app()->environment('testing')) {
            return true;
        }

        return app('arcanedev.no-captcha')->verify($value, request()->getClientIp());
    });

So basically, I just check if the environment is set to testing, which is the default for Laravel, and if so I always let it pass.

I don’t know if this is something you would want to include in your package, perhaps with configurable environments that should be skipped or something…

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

Woops! At some point I removed the required validation rule and forgot to put it back… This complicates things… My tests worked because the captcha wasn’t required… doh!

So to make it work (for real) I had to create a custom sometimes rule… I added this to the Request class that holds my validation rules:

/**
 * Validate conditional rules.
 *
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function getValidatorInstance()
{
    $validator = parent::getValidatorInstance();

    $validator->sometimes('g-recaptcha-response', 'required|captcha', function ($input) {
        return ! app()->environment('testing');
    });

    return $validator;
}

So now it applies the required and captcha rules only if the environment isn’t testing… Of course now I need to add this to every Request class that needs a captcha.

Maybe you know a better approach 😉