From 40bb5ab8bb7864c26e88ee8fcc723ec684469d0c Mon Sep 17 00:00:00 2001 From: DarkGhostHunter Date: Wed, 3 Apr 2019 19:52:09 -0300 Subject: [PATCH 1/5] Minor typos [skip ci] --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 941eebf..aa3d009 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,7 @@ CAPTCHAVEL_MODE=auto #### `auto` -The `auto` option leverages the frontend work to you. Just add the `data-recaptcha="true"` attribute to the forms where you want to check for reCAPTCHA. +The `auto` option leverages the frontend work from you. Just add the `data-recaptcha="true"` attribute to the forms where you want to check for reCAPTCHA. ```blade
@@ -237,7 +237,9 @@ The `auto` option leverages the frontend work to you. Just add the `data-recaptc
``` -Captchavel will inject the Google reCAPTCHA v3 as a deferred script in the head before `` tag, in every response (except JSON, AJAX or anything non-HTML), so it can have more analytics about how users interact with your site. +Captchavel will inject the Google reCAPTCHA v3 as a deferred script before `` tag, in every response (except JSON, AJAX or anything non-HTML), so it can have more analytics about how users interact with your site. + +To override the script that gets injected, take a look in the [editing the script view](#editing-the-script-view) section. #### `manual` @@ -248,8 +250,6 @@ Since the frontend won't have nothing injected, this mode it gives you freedom t * manually include the `recaptcha-inject` middleware only in the routes you want, * or include the `recaptcha::script` blade template in your layouts you want. -Take a look in the [editing the script view](#editing-the-script-view) section. - > The manual mode is very handy if your responses have a lot of data and want better performance, because the middleware won't look into the responses. ### Enable on Local Environment From 39e470bb657b551298ee4a62d52861884244ed2b Mon Sep 17 00:00:00 2001 From: DarkGhostHunter Date: Wed, 3 Apr 2019 19:59:05 -0300 Subject: [PATCH 2/5] Minor rephrasing. [skip ci] --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aa3d009..6158176 100644 --- a/README.md +++ b/README.md @@ -298,7 +298,7 @@ Route::post('{product]/review') The Google reCAPTCHA library underneath allows to make the request to the reCAPTCHA servers using a custom "Request Method". The `request_method` key accepts the Class you want to instance. -The default `null` value is enough for any normal application, but you're free to, for example, create your own logic or use the classes included in the [ReCaptcha package](https://github.com/google/recaptcha/tree/master/src/ReCaptcha/RequestMethod) (that this package requires). You can mimic this next example, were we will use Guzzle. +The default `null` value is enough for any normal application, but you're free to, for example, create your own logic or use the classes included in the [ReCaptcha package](https://github.com/google/recaptcha/tree/master/src/ReCaptcha/RequestMethod) (that this package requires). ```php Date: Sat, 6 Apr 2019 20:07:00 -0300 Subject: [PATCH 3/5] Fixed injection on renderable exception, that shouldn't happen. --- src/Http/Middleware/InjectRecaptchaScript.php | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Http/Middleware/InjectRecaptchaScript.php b/src/Http/Middleware/InjectRecaptchaScript.php index 9c676c8..3d91ec9 100644 --- a/src/Http/Middleware/InjectRecaptchaScript.php +++ b/src/Http/Middleware/InjectRecaptchaScript.php @@ -27,8 +27,8 @@ class InjectRecaptchaScript /** * InjectRecaptchaScript constructor. * - * @param \Illuminate\Contracts\Config\Repository $config - * @param \Illuminate\Contracts\View\Factory $view + * @param \Illuminate\Contracts\Config\Repository $config + * @param \Illuminate\Contracts\View\Factory $view */ public function __construct(Config $config, View $view) { @@ -39,8 +39,8 @@ public function __construct(Config $config, View $view) /** * Handle an incoming request. * - * @param \Illuminate\Http\Request $request - * @param \Closure $next + * @param \Illuminate\Http\Request $request + * @param \Closure $next * @return mixed * @throws \Throwable * @throws \\Symfony\Component\HttpKernel\Exception\HttpException @@ -65,13 +65,17 @@ public function handle($request, Closure $next) */ protected function isHtml(Request $request, $response) { - return $response instanceof Response && $request->acceptsHtml() && !$request->ajax() && !$request->pjax(); + return $response instanceof Response + && $request->acceptsHtml() + && ! $request->ajax() + && ! $request->pjax() + && ! $response->exception; } /** * Injects the front-end Scripts * - * @param \Illuminate\Http\Response $response + * @param \Illuminate\Http\Response $response * @return \Illuminate\Http\Response */ protected function injectScript(Response $response) @@ -79,7 +83,7 @@ protected function injectScript(Response $response) // To inject the script automatically, we will do it before the ending // head tag. If it's not found, the response may not be valid HTML, // so we will bail out returning the original untouched content. - if (!$endHeadPosition = stripos($content = $response->content(), '')) { + if (! $endHeadPosition = stripos($content = $response->content(), '')) { return $response; }; From 5b864adbaa0f3c81a0c31eeb0bfac99a1e8e5779 Mon Sep 17 00:00:00 2001 From: DarkGhostHunter Date: Sat, 6 Apr 2019 20:10:13 -0300 Subject: [PATCH 4/5] Added Tests for non-Injection on responses triggered by exceptions. --- .../Middleware/InjectRecaptchaScriptTest.php | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/tests/Middleware/InjectRecaptchaScriptTest.php b/tests/Middleware/InjectRecaptchaScriptTest.php index db4ff8f..951a401 100644 --- a/tests/Middleware/InjectRecaptchaScriptTest.php +++ b/tests/Middleware/InjectRecaptchaScriptTest.php @@ -2,19 +2,7 @@ namespace DarkGhostHunter\Captchavel\Tests; -use DarkGhostHunter\Captchavel\Exceptions\FailedRecaptchaException; -use DarkGhostHunter\Captchavel\Exceptions\InvalidCaptchavelMiddlewareMethod; -use DarkGhostHunter\Captchavel\Exceptions\InvalidRecaptchaException; -use DarkGhostHunter\Captchavel\Http\Middleware\CheckRecaptcha; -use Illuminate\Contracts\Http\Kernel; -use Illuminate\Http\Request; -use Illuminate\Support\Carbon; -use Illuminate\Support\Str; use Orchestra\Testbench\TestCase; -use ReCaptcha\ReCaptcha; -use ReCaptcha\RequestMethod; -use ReCaptcha\RequestParameters; -use ReCaptcha\Response; class InjectRecaptchaScriptTest extends TestCase { @@ -109,4 +97,12 @@ public function testDoesntInjectsOnAjax() ->assertDontSee('api.js?render=test-key&onload=captchavelCallback'); } + public function testDoesntInjectsOnException() + { + $response = $this->get('route-doesnt-exists-will-trigger-exception'); + + $response->assertDontSee('Start Captchavel Script') + ->assertDontSee('api.js?render=test-key&onload=captchavelCallback'); + } + } From 009cbf504310bb8ad99d46201c5c465c7811151f Mon Sep 17 00:00:00 2001 From: DarkGhostHunter Date: Tue, 9 Apr 2019 13:12:12 -0400 Subject: [PATCH 5/5] Added more test to Transparent Middleware registration --- src/CaptchavelServiceProvider.php | 2 +- tests/ServiceProviderTest.php | 18 +++++++++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/CaptchavelServiceProvider.php b/src/CaptchavelServiceProvider.php index 229490c..8054986 100644 --- a/src/CaptchavelServiceProvider.php +++ b/src/CaptchavelServiceProvider.php @@ -91,7 +91,7 @@ protected function bootMiddleware() protected function shouldEnableMiddleware() { return $this->app->environment('production') - || $this->app->environment('local') && $this->app->make('config')->get('captchavel.enable_local'); + || ($this->app->environment('local') && $this->app->make('config')->get('captchavel.enable_local')); } /** diff --git a/tests/ServiceProviderTest.php b/tests/ServiceProviderTest.php index 6f27744..eaf341e 100644 --- a/tests/ServiceProviderTest.php +++ b/tests/ServiceProviderTest.php @@ -132,10 +132,26 @@ public function testDoesntRegisterInjectMiddlewareOnNonAuto() ); } + public function testRegisterMiddlewareOnLocalTrue() + { + $this->app['env'] = 'local'; + $this->app['config']->set('captchavel.enable_local', true); + + /** @var CaptchavelServiceProvider $provider */ + $provider = $this->app->make(CaptchavelServiceProvider::class, ['app' => $this->app]); + + $provider->boot(); + + /** @var \Illuminate\Routing\Router $router */ + $router = $this->app->make('router'); + + $this->assertEquals(CheckRecaptcha::class, $router->getMiddleware()['recaptcha']); + } + public function testPublishesConfigFile() { $this->artisan('vendor:publish', [ - '--provider' => 'DarkGhostHunter\Captchavel\CaptchavelServiceProvider' + '--provider' => CaptchavelServiceProvider::class ]); $this->assertFileExists(config_path('captchavel.php'));