thinhbuzz / laravel-google-captcha

Google captcha for Laravel 5, Laravel 6, Laravel 7, Laravel 8, Laravel 9 and Laravel 10, support multiple captcha on page
MIT License
200 stars 32 forks source link

Testing inside Laravel Dusk #51

Open divix1988 opened 6 months ago

divix1988 commented 6 months ago

I am running Dusk automation tests and none of this seem to work:

   public function createApplication(): Application
    {
        ...
        $app['validator']->extend('captcha', function() {
            fwrite(STDOUT, print_r("Bypassing captcha validation and returning explicit true!\n", TRUE));
            return true;
        });

        CaptchaFacade::shouldReceive('verify')->andReturn(true);
        CaptchaFacade::shouldReceive('display')
            ->andReturn('<input type="hidden" name="g-recaptcha-response" value="1" />');

        return $app;
    }

The error message is Failed to validate the captcha. How to sort it? image

thinhbuzz commented 6 months ago

What method do you use to display captcha in the template? by the way can you reproduce it with a sample repo?

divix1988 commented 6 months ago

@thinhbuzz I use a simple usage of: {!! app('captcha')->display() !!} to display captcha. Dusk is an automative test so it can be repped with any code, it's not related to environment.

I found this "partial" solution:

class AppServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        if (isset($_SERVER['DB_HOST_DUSK']) && $_SERVER['DB_HOST_DUSK'] === 'localhost') {
            $this->app['validator']->extend('captcha', function() {
                _log("Bypassing captcha validation for Dusk automation tests!");
                return true;
            });
        }
    }

The above works but it required injecting test specific code into app codebase, which I am not a big fan. It would be great if plugin would detect some kind of ENV flag, which Dusk has and can define inside: .env.dusk to bypass captcha checking. That's what I've done above.

thinhbuzz commented 6 months ago

Sorry for the late reply. Can you create a sample repository of the problem