Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Commit

Permalink
Merge pull request #27 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixed macros and threshold calculation.
  • Loading branch information
DarkGhostHunter authored Jun 2, 2020
2 parents 21ac0fe + 99286ed commit cb78abc
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ Here is the full array of [reCAPTCHA credentials](#set-up) to use depending on t

## Testing with Captchavel

When unit testing your application, this package [automatically fakes reCAPTCHA responses](#fake-responses) by setting.
When unit testing your application, this package [automatically fakes reCAPTCHA responses](#fake-responses).

> When mocking requests, there is no need to add any reCAPTCHA token or secrets in your tests.
Expand Down Expand Up @@ -280,7 +280,7 @@ $this->post('login', [

Alternatively, `fakeScore()` method that will fake any score you set.

> Fake responses don't come with action, hostnames or APK package names.
> Fake responses don't come with actions, hostnames or APK package names.
### Events

Expand Down
4 changes: 4 additions & 0 deletions src/CaptchavelServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DarkGhostHunter\Captchavel;

use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Config\Repository;
Expand Down Expand Up @@ -43,5 +44,8 @@ public function boot(Router $router, Repository $config)

$router->aliasMiddleware('recaptcha.v2', VerifyReCaptchaV2::class);
$router->aliasMiddleware('recaptcha.v3', VerifyReCaptchaV3::class);

Request::macro('isRobot', [RequestMacro::class, 'isRobot']);
Request::macro('isHuman', [RequestMacro::class, 'isHuman']);
}
}
2 changes: 1 addition & 1 deletion src/Http/ReCaptchaResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function isHuman()
throw new LogicException('This is not a reCAPTCHA v3 response, or the score is absent.');
}

return $this->threshold >= $this->score;
return $this->score >= $this->threshold;
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/RequestMacro.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace DarkGhostHunter\Captchavel;

use DarkGhostHunter\Captchavel\Http\ReCaptchaResponse;

class RequestMacro
{
/**
* Check if the reCAPTCHA response is equal or above threshold score.
*
* @return bool
*/
public static function isHuman()
{
return app(ReCaptchaResponse::class)->isHuman();
}

/**
* Check if the reCAPTCHA response is below threshold score.
*
* @return bool
*/
public static function isRobot()
{
return ! static::isHuman();
}
}
56 changes: 51 additions & 5 deletions tests/Http/Middleware/ScoreMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Tests\RegistersPackage;
use Illuminate\Http\Request;
use Orchestra\Testbench\TestCase;
use Illuminate\Http\Client\Factory;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Event;
use DarkGhostHunter\Captchavel\Captchavel;
use GuzzleHttp\Psr7\Response as GuzzleResponse;
use DarkGhostHunter\Captchavel\Http\ReCaptchaResponse;
use DarkGhostHunter\Captchavel\Events\ReCaptchaResponseReceived;

Expand Down Expand Up @@ -50,11 +53,11 @@ public function test_validates_if_real()
->andReturnSelf();
$mock->shouldReceive('retrieve')
->with('token', '127.0.0.1')
->andReturn(new ReCaptchaResponse([
'success' => true,
'score' => 0.5,
'foo' => 'bar'
]));
->andReturn(new ReCaptchaResponse([
'success' => true,
'score' => 0.5,
'foo' => 'bar'
]));

$this->post('v3/default', [
Captchavel::INPUT => 'token'
Expand Down Expand Up @@ -484,4 +487,47 @@ public function test_exception_if_action_not_equal()
Captchavel::INPUT => 'token'
])->assertJsonValidationErrors('action');
}

public function test_checks_for_human_score()
{
config(['captchavel.credentials.v3.secret' => 'secret']);
config(['captchavel.fake' => false]);

$mock = $this->mock(Factory::class);

$mock->shouldReceive('asForm')->withNoArgs()->times(4)->andReturnSelf();
$mock->shouldReceive('withOptions')->with(['version' => 2.0])->times(4)->andReturnSelf();
$mock->shouldReceive('post')
->with(Captchavel::RECAPTCHA_ENDPOINT, [
'secret' => 'secret',
'response' => 'token',
'remoteip' => '127.0.0.1',
])
->times(4)
->andReturn(new Response(new GuzzleResponse(200, ['Content-type' => 'application/json'], json_encode([
'success' => true,
'score' => 0.5,
]))));

Route::post('human_human', function (Request $request) {
return $request->isHuman() ? 'true' : 'false';
})->middleware('recaptcha.v3:0.7');

Route::post('human_robot', function (Request $request) {
return $request->isRobot() ? 'true' : 'false';
})->middleware('recaptcha.v3:0.7');

Route::post('robot_human', function (Request $request) {
return $request->isHuman() ? 'true' : 'false';
})->middleware('recaptcha.v3:0.3');

Route::post('robot_robot', function (Request $request) {
return $request->isRobot() ? 'true' : 'false';
})->middleware('recaptcha.v3:0.3');

$this->post('human_human', [Captchavel::INPUT => 'token'])->assertSee('false');
$this->post('human_robot', [Captchavel::INPUT => 'token'])->assertSee('true');
$this->post('robot_human', [Captchavel::INPUT => 'token'])->assertSee('true');
$this->post('robot_robot', [Captchavel::INPUT => 'token'])->assertSee('false');
}
}

0 comments on commit cb78abc

Please sign in to comment.