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

Commit

Permalink
Updated tests and removed redundant closures.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Dec 21, 2021
1 parent fcbe710 commit 39aeb29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 89 deletions.
23 changes: 5 additions & 18 deletions tests/Http/Middleware/ScoreMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ function (): void {

public function test_fakes_response_if_authenticated_in_guard(): void
{
$this->app['router']->post('v3/guarded', function (ReCaptchaResponse $response) {
return $response;
})->middleware(ReCaptcha::score()->except('web')->toString());
$this->app['router']->post('v3/guarded', [__CLASS__, 'returnSameResponse'])->middleware(ReCaptcha::score()->except('web')->toString());

$this->actingAs(User::make(), 'web');

Expand Down Expand Up @@ -164,9 +162,7 @@ public function test_uses_custom_input(): void
$this->fulfilledResponse(['success' => true, 'score' => 0.7, 'foo' => 'bar'])
);

$this->app['router']->post('test', function (ReCaptchaResponse $response) {
return $response;
})->middleware('recaptcha.score:null,null,foo');
$this->app['router']->post('test', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,null,foo');

$this->post('test', ['foo' => 'token'])
->assertOk()
Expand Down Expand Up @@ -343,9 +339,7 @@ public function test_no_error_if_no_action(): void
$this->fulfilledResponse(['success' => true, 'action' => 'foo', 'apk_package_name' => null])
);

$this->app['router']->post('test', function (ReCaptchaResponse $response) {
return $response;
})->middleware('recaptcha.score:null,null');
$this->app['router']->post('test', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,null');

$this->post('test', [Captchavel::INPUT => 'token'])->assertOk();
}
Expand All @@ -360,9 +354,7 @@ public function test_no_error_if_action_same(): void
$this->fulfilledResponse(['success' => true, 'action' => 'foo', 'apk_package_name' => null])
);

$this->app['router']->post('test', function (ReCaptchaResponse $response) {
return $response;
})->middleware('recaptcha.score:null,foo');
$this->app['router']->post('test', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,foo');

$this->post('test', [Captchavel::INPUT => 'token'])->assertOk();
}
Expand All @@ -382,12 +374,7 @@ public function test_exception_if_action_not_equal(): void
)
);

$this->app['router']->post(
'test',
function (ReCaptchaResponse $response) {
return $response;
}
)->middleware('recaptcha.score:null,bar');
$this->app['router']->post('test', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,bar');

$this->post('test', [Captchavel::INPUT => 'token'])
->assertSessionHasErrors(Captchavel::INPUT, trans('captchavel::validation.match'))
Expand Down
95 changes: 24 additions & 71 deletions tests/Http/Middleware/UsesRoutesWithMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,85 +3,38 @@
namespace Tests\Http\Middleware;

use DarkGhostHunter\Captchavel\Http\ReCaptchaResponse;
use function app;

trait UsesRoutesWithMiddleware
{
protected function createsRoutes()
{
config(['captchavel.enable' => true]);

$this->app['router']->post('v3/default', function (ReCaptchaResponse $response) {
return $response;
})->middleware('recaptcha.score');

$this->app['router']->post('v3/threshold_1', function (ReCaptchaResponse $response) {
return $response;
})->middleware('recaptcha.score:1.0');

$this->app['router']->post('v3/threshold_0', function (ReCaptchaResponse $response) {
return $response;
})->middleware('recaptcha.score:0');

$this->app['router']->post('v3/action_foo', function (ReCaptchaResponse $response) {
return $response;
})->middleware('recaptcha.score:null,foo');

$this->app['router']->post('v3/input_bar', function (ReCaptchaResponse $response) {
return $response;
})->middleware('recaptcha.score:null,null,bar');

$this->app['router']->post('v2/checkbox', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:checkbox');

$this->app['router']->post('v2/checkbox/input_bar', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:checkbox,null,bar');

$this->app['router']->post('v2/checkbox/remember', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:checkbox,10');

$this->app['router']->post('v2/invisible', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:invisible');

$this->app['router']->post('v2/invisible/input_bar', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:invisible,null,bar');

$this->app['router']->post('v2/invisible/remember', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:invisible,10');

$this->app['router']->post('v2/android', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:android');
$this->app['router']->post('v3/default', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score');
$this->app['router']->post('v3/threshold_1', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:1.0');
$this->app['router']->post('v3/threshold_0', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:0');
$this->app['router']->post('v3/action_foo', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,foo');
$this->app['router']->post('v3/input_bar', [__CLASS__, 'returnSameResponse'])->middleware('recaptcha.score:null,null,bar');

$this->app['router']->post('v2/checkbox', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:checkbox');
$this->app['router']->post('v2/checkbox/input_bar', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:checkbox,null,bar');
$this->app['router']->post('v2/checkbox/remember', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:checkbox,10');
$this->app['router']->post('v2/invisible', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:invisible');
$this->app['router']->post('v2/invisible/input_bar', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:invisible,null,bar');
$this->app['router']->post('v2/invisible/remember', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:invisible,10');
$this->app['router']->post('v2/android', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:android');
$this->app['router']->post('v2/android/input_bar', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:android,null,bar');
$this->app['router']->post('v2/android/remember', [__CLASS__, 'returnResponseIfExists'])->middleware('recaptcha:android,10');
}

$this->app['router']->post('v2/android/input_bar', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:android,null,bar');
public static function returnSameResponse(ReCaptchaResponse $response): ReCaptchaResponse
{
return $response;
}

$this->app['router']->post('v2/android/remember', function () {
if (app()->has(ReCaptchaResponse::class)) {
return app(ReCaptchaResponse::class);
}
})->middleware('recaptcha:android,10');
public static function returnResponseIfExists(): ?ReCaptchaResponse
{
return app()->has(ReCaptchaResponse::class) ? app(ReCaptchaResponse::class) : null;
}
}

0 comments on commit 39aeb29

Please sign in to comment.