This repository has been archived by the owner on Feb 17, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from DarkGhostHunter/master
Added Laravel 7 support.
- Loading branch information
Showing
6 changed files
with
264 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
name: PHP Composer | ||
|
||
on: | ||
push: | ||
pull_request: | ||
|
||
jobs: | ||
test: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: true | ||
matrix: | ||
php: [7.4, 7.3, 7.2.15] | ||
laravel: [7.*, 6.*] | ||
dependency-version: [prefer-lowest, prefer-stable] | ||
include: | ||
- laravel: 7.* | ||
testbench: 5.* | ||
- laravel: 6.* | ||
testbench: ^4.1 | ||
|
||
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }} - ${{ matrix.dependency-version }} | ||
|
||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v1 | ||
|
||
- name: Setup PHP | ||
uses: shivammathur/setup-php@v1 | ||
with: | ||
php-version: ${{ matrix.php }} | ||
extensions: mbstring, intl | ||
coverage: xdebug | ||
|
||
- name: Cache dependencies | ||
uses: actions/cache@v1 | ||
with: | ||
path: ~/.composer/cache/files | ||
key: ${{ runner.os }}-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }} | ||
restore-keys: ${{ runner.os }}-laravel-${{ matrix.laravel }}-php-${{ matrix.php }}-composer- | ||
|
||
- name: Install dependencies | ||
run: | | ||
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-progress --no-update | ||
composer update --${{ matrix.dependency-version }} --prefer-dist --no-progress --no-suggest | ||
- name: Run Tests | ||
run: composer run-script test | ||
|
||
- name: Upload Coverage to Coveralls | ||
env: | ||
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
COVERALLS_SERVICE_NAME: github | ||
run: | | ||
rm -rf composer.* vendor/ | ||
composer require cedx/coveralls | ||
vendor/bin/coveralls build/logs/clover.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
<?php | ||
|
||
namespace DarkGhostHunter\Captchavel\Tests; | ||
|
||
use ReCaptcha\Response; | ||
use Illuminate\Support\Str; | ||
use ReCaptcha\RequestMethod; | ||
use Illuminate\Support\Carbon; | ||
use Orchestra\Testbench\TestCase; | ||
use DarkGhostHunter\Captchavel\ReCaptcha; | ||
use ReCaptcha\ReCaptcha as ReCaptchaFactory; | ||
use DarkGhostHunter\Captchavel\Http\Middleware\CheckRecaptcha; | ||
use DarkGhostHunter\Captchavel\Exceptions\FailedRecaptchaException; | ||
use DarkGhostHunter\Captchavel\Exceptions\InvalidRecaptchaException; | ||
|
||
class ThrottleRecaptchaTest extends TestCase | ||
{ | ||
|
||
protected function getPackageAliases($app) | ||
{ | ||
return [ | ||
'ReCaptcha' => 'DarkGhostHunter\Captchavel\Facades\ReCaptcha' | ||
]; | ||
} | ||
|
||
protected function getPackageProviders($app) | ||
{ | ||
return ['DarkGhostHunter\Captchavel\CaptchavelServiceProvider']; | ||
} | ||
|
||
/** | ||
* Define environment setup. | ||
* | ||
* @param \Illuminate\Foundation\Application $app | ||
* @return void | ||
*/ | ||
protected function getEnvironmentSetUp($app) | ||
{ | ||
$app['env'] = 'local'; | ||
|
||
$app->make('config')->set('captchavel.enable_local', true); | ||
$app->make('config')->set('captchavel.secret', 'test-secret'); | ||
$app->make('config')->set('captchavel.key', 'test-key'); | ||
|
||
$app->make('router')->get('test-get-with-middleware', function () { return 'true'; })->middleware('recaptcha'); | ||
$app->make('router')->get('test-get', function () { return 'true'; }); | ||
$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); | ||
$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 ($app) use ($mockRequester) { | ||
return $mockRequester; | ||
}); | ||
|
||
$this->post('test-post', [ | ||
'_recaptcha' => Str::random(356) | ||
])->assertOk(); | ||
} | ||
|
||
public function testFailsOnNonPostMethod() | ||
{ | ||
$this->app->make('router')->get('get-route', function () { return 'true'; })->middleware('recaptcha'); | ||
$this->app->make('router')->match(['head'], 'head-route', function () { return 'true'; })->middleware('recaptcha'); | ||
|
||
$response = $this->get('get-route'); | ||
|
||
$response->assertStatus(200); | ||
|
||
$response = $this->call('head', 'head-route'); | ||
|
||
$response->assertStatus(200); | ||
} | ||
|
||
public function testFailsInvalidToken() | ||
{ | ||
$response = $this->post('test-post', [ '_recaptcha' => ['not.string']]); | ||
|
||
$response->assertStatus(500); | ||
$this->assertInstanceOf(InvalidRecaptchaException::class, $response->exception); | ||
} | ||
|
||
public function testFailsInvalidRecaptcha() | ||
{ | ||
$mockRequester = \Mockery::mock(RequestMethod::class); | ||
$mockRequester->shouldReceive('submit')->andReturn(json_encode([ | ||
'success' => false, | ||
'score' => 0.8, | ||
'action' => '/testpost', | ||
'challenge_ts' => Carbon::now()->toIso8601ZuluString(), | ||
])); | ||
|
||
$this->app->when(ReCaptchaFactory::class) | ||
->needs(RequestMethod::class) | ||
->give(function ($app) use ($mockRequester) { | ||
return $mockRequester; | ||
}); | ||
|
||
$response = $this->post('test-post', [ '_recaptcha' => Str::random(356)]); | ||
|
||
$response->assertStatus(500); | ||
$this->assertInstanceOf(FailedRecaptchaException::class, $response->exception); | ||
} | ||
|
||
public function testMiddlewareAcceptsParameter() | ||
{ | ||
$mockReCaptchaFactory = \Mockery::mock(ReCaptchaFactory::class); | ||
$mockReCaptchaFactory->shouldReceive('setExpectedAction') | ||
->once() | ||
->andReturnSelf(); | ||
$mockReCaptchaFactory->shouldReceive('verify') | ||
->once() | ||
->andReturn(new Response(true, [], null, null, null, 1.0, null)); | ||
|
||
$this->app->when(CheckRecaptcha::class) | ||
->needs(ReCaptchaFactory::class) | ||
->give(function () use ($mockReCaptchaFactory) { | ||
return $mockReCaptchaFactory; | ||
}); | ||
|
||
$this->app->make('router') | ||
->post('test-post', function () { | ||
return 'reaches'; | ||
}) | ||
->middleware('recaptcha:0.9'); | ||
|
||
$this->post('test-post', [ '_recaptcha' => Str::random(356) ]) | ||
->assertOk(); | ||
} | ||
} |