From 32fe3c3888b4c4e04c735ec0294943bb342119b5 Mon Sep 17 00:00:00 2001 From: DarkGhostHunter Date: Tue, 13 Aug 2019 12:44:23 -0400 Subject: [PATCH 1/2] Added test for helper --- tests/Middleware/CheckRecaptchaTest.php | 28 +++++++++++++++++++++++++ tests/RecaptchaResponseHolderTest.php | 1 - 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/Middleware/CheckRecaptchaTest.php b/tests/Middleware/CheckRecaptchaTest.php index 869c74c..c25150f 100644 --- a/tests/Middleware/CheckRecaptchaTest.php +++ b/tests/Middleware/CheckRecaptchaTest.php @@ -6,6 +6,7 @@ use DarkGhostHunter\Captchavel\Exceptions\InvalidCaptchavelMiddlewareMethod; use DarkGhostHunter\Captchavel\Exceptions\InvalidRecaptchaException; use DarkGhostHunter\Captchavel\Http\Middleware\CheckRecaptcha; +use DarkGhostHunter\Captchavel\ReCaptcha; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use Orchestra\Testbench\TestCase; @@ -47,6 +48,33 @@ protected function getEnvironmentSetUp($app) $app->make('router')->post('test-post', function () { return 'true'; })->middleware('recaptcha'); } + public function testSameRecaptchaInstance() + { + $mockRequester = \Mockery::mock(RequestMethod::class); + $mockRequester->shouldReceive('submit')->andReturn(json_encode([ + 'success' => true, + 'score' => 0.8, + 'action' => '/testpost', + 'challenge_ts' => Carbon::now()->toIso8601ZuluString(), + ])); + + $this->app->when(ReCaptchaFactory::class) + ->needs(RequestMethod::class) + ->give(function () use ($mockRequester) { + return $mockRequester; + }); + + $this->post('test-post', [ + '_recaptcha' => Str::random(356) + ])->assertOk(); + + $this->assertEquals(app(ReCaptcha::class), app('recaptcha')); + $this->assertEquals(app(ReCaptcha::class), recaptcha()); + $this->assertNotEquals(app(ReCaptcha::class), new ReCaptcha()); + $this->assertNotEquals(app('recaptcha'), new ReCaptcha()); + $this->assertNotEquals(recaptcha(), new ReCaptcha()); + } + public function testRequestWithCaptchaValidates() { $mockRequester = \Mockery::mock(RequestMethod::class); diff --git a/tests/RecaptchaResponseHolderTest.php b/tests/RecaptchaResponseHolderTest.php index 2686ad9..3ee7c1c 100644 --- a/tests/RecaptchaResponseHolderTest.php +++ b/tests/RecaptchaResponseHolderTest.php @@ -120,5 +120,4 @@ public function testExceptionOnSinceCheck() (new ReCaptcha())->since(); } - } From 0ab28287700e68b0c53a15fb82d424a0c20be077 Mon Sep 17 00:00:00 2001 From: DarkGhostHunter Date: Sun, 20 Oct 2019 00:04:04 -0300 Subject: [PATCH 2/2] * Added Laravel 6 compatibility. * Added some PHPDocs to some classes, mainly Exceptions. * Minor fix to the Transparent Recaptcha (uses the already resolved reCaptcha service instead of resolving it again). * Updated Travis CI build without PHP 7.1 --- .travis.yml | 10 ++++------ composer.json | 17 ++++++++--------- src/Exceptions/CaptchavelException.php | 3 +-- src/Exceptions/FailedRecaptchaException.php | 9 +++++++-- .../InvalidCaptchavelMiddlewareMethod.php | 5 +++++ src/Exceptions/InvalidRecaptchaException.php | 12 ++++++++++++ .../RecaptchaNotResolvedException.php | 5 +++++ src/Http/Middleware/TransparentRecaptcha.php | 9 +++++---- 8 files changed, 47 insertions(+), 23 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5486079..2f33387 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,6 @@ language: php php: - - 7.1 - 7.2 - 7.3 @@ -9,17 +8,16 @@ env: global: - CC_TEST_REPORTER_ID=3cb0149c1ae23214062407321730aa13a558d9e10aa7011e01282b5a46e7f100 -before_script: +install: - travis_retry composer self-update - travis_retry composer require php-coveralls/php-coveralls:2.1.* --dev - travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source - - curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter + - travis_retry curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter + +before_script: - chmod +x ./cc-test-reporter - ./cc-test-reporter before-build -script: - - ./vendor/bin/phpunit - after_script: - ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT diff --git a/composer.json b/composer.json index 913d0b8..095de0d 100644 --- a/composer.json +++ b/composer.json @@ -22,17 +22,16 @@ "php": "^7.1.3", "ext-json" : "*", "google/recaptcha": "^1.2", - "illuminate/config": "5.8.*", - "illuminate/container": "5.8.*", - "illuminate/http": "5.8.*", - "illuminate/routing": "5.8.*", - "illuminate/support": "5.8.*", - "illuminate/validation": "5.8.*", - "illuminate/view": "5.8.*" + "illuminate/config": "6.*", + "illuminate/container": "6.*", + "illuminate/http": "6.*", + "illuminate/routing": "6.*", + "illuminate/support": "6.*", + "illuminate/validation": "6.*", + "illuminate/view": "6.*" }, "require-dev": { - "orchestra/testbench": "3.8.*", - "phpunit/phpunit": "^7.0" + "orchestra/testbench": "4.*" }, "autoload": { "files": [ diff --git a/src/Exceptions/CaptchavelException.php b/src/Exceptions/CaptchavelException.php index aa3e48a..274f665 100644 --- a/src/Exceptions/CaptchavelException.php +++ b/src/Exceptions/CaptchavelException.php @@ -4,5 +4,4 @@ interface CaptchavelException { - -} \ No newline at end of file +} diff --git a/src/Exceptions/FailedRecaptchaException.php b/src/Exceptions/FailedRecaptchaException.php index 9ca1d40..8847615 100644 --- a/src/Exceptions/FailedRecaptchaException.php +++ b/src/Exceptions/FailedRecaptchaException.php @@ -6,10 +6,15 @@ class FailedRecaptchaException extends Exception implements CaptchavelException { - public function __construct(array $errorCodes) + /** + * Creates a new FailedRecaptchaException instance. + * + * @param array $errorCodes + */ + public function __construct(array $errorCodes = []) { $this->message = 'The Google reCAPTCHA library returned the following errors:' . - implode("\n- ", $errorCodes); + implode("\n- ", $errorCodes); parent::__construct(); } diff --git a/src/Exceptions/InvalidCaptchavelMiddlewareMethod.php b/src/Exceptions/InvalidCaptchavelMiddlewareMethod.php index 29cac7e..63e1603 100644 --- a/src/Exceptions/InvalidCaptchavelMiddlewareMethod.php +++ b/src/Exceptions/InvalidCaptchavelMiddlewareMethod.php @@ -6,5 +6,10 @@ class InvalidCaptchavelMiddlewareMethod extends Exception implements CaptchavelException { + /** + * The exception message + * + * @var string + */ protected $message = 'Captchavel does not work in GET routes.'; } \ No newline at end of file diff --git a/src/Exceptions/InvalidRecaptchaException.php b/src/Exceptions/InvalidRecaptchaException.php index 43e3098..246d330 100644 --- a/src/Exceptions/InvalidRecaptchaException.php +++ b/src/Exceptions/InvalidRecaptchaException.php @@ -7,8 +7,20 @@ class InvalidRecaptchaException extends Exception implements CaptchavelException { + /** + * The exception message + * + * @var string + */ protected $message = 'The reCAPTCHA token is empty'; + /** + * Creates a new InvalidRecaptchaException instance. + * + * @param null $token + * @param int $code + * @param \Throwable|null $previous + */ public function __construct($token = null, $code = 0, Throwable $previous = null) { if ($token !== null) { diff --git a/src/Exceptions/RecaptchaNotResolvedException.php b/src/Exceptions/RecaptchaNotResolvedException.php index fe4150d..a4d8195 100644 --- a/src/Exceptions/RecaptchaNotResolvedException.php +++ b/src/Exceptions/RecaptchaNotResolvedException.php @@ -6,5 +6,10 @@ class RecaptchaNotResolvedException extends Exception implements CaptchavelException { + /** + * The exception message + * + * @var string + */ protected $message = 'The reCAPTCHA has not been verified in this Request.'; } \ No newline at end of file diff --git a/src/Http/Middleware/TransparentRecaptcha.php b/src/Http/Middleware/TransparentRecaptcha.php index 0cc520e..b19ce2e 100644 --- a/src/Http/Middleware/TransparentRecaptcha.php +++ b/src/Http/Middleware/TransparentRecaptcha.php @@ -11,7 +11,7 @@ class TransparentRecaptcha extends CheckRecaptcha /** * Return if the Request has a valid reCAPTCHA token * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * @return bool * @throws \Throwable */ @@ -35,13 +35,14 @@ protected function hasValidRequest(Request $request) */ protected function resolve(Request $request, float $threshold) { - return app('recaptcha')->setResponse( - new Response(true, + return $this->reCaptcha->setResponse( + new Response( + true, [], null, now()->toIso8601ZuluString(), null, - $request->query->has('is_robot') || $request->input('is_robot') === true ? 0 : 1, + (int)$request->query->has('is_robot'), $this->sanitizeAction($request->getRequestUri())) ); }