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)
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 theRequest
class that holds my validation rules:So now it applies the
required
andcaptcha
rules only if the environment isn’ttesting
… Of course now I need to add this to everyRequest
class that needs a captcha.Maybe you know a better approach 😉