Skip to content

Commit

Permalink
Allow turnstile to pass when disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
njoguamos committed Feb 11, 2024
1 parent ab28a69 commit 8468a6c
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Rules/TurnstileRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TurnstileRule implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! (new Turnstile())->validate(token: $value)) {
if (config(key: 'turnstile.enabled') && ! (new Turnstile())->validate(token: $value)) {
$fail(trans(key: 'turnstile::turnstile.failed'));
}
}
Expand Down
11 changes: 6 additions & 5 deletions tests/Http/MiddleWare/TurnstileMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use NjoguAmos\Turnstile\Http\Middleware\TurnstileMiddleware;
use Symfony\Component\HttpFoundation\Response;

test(description: 'it skips the middleware when disabled', closure: function () {
it(description: 'skips the middleware when disabled', closure: function () {
config()->set(key: 'turnstile.secretkey', value: null);
config()->set(key: 'turnstile.enabled', value: false);

$response = (new TurnstileMiddleware())
Expand All @@ -16,7 +17,7 @@
expect(value: $response->isRedirect())->toBeFalse();
});

test(description: 'it redirects back the request if the token is invalid', closure: function () {
it(description: 'redirects back the request if the token is invalid', closure: function () {
setSecretKey(type: 'invalid');

$response = (new TurnstileMiddleware())
Expand All @@ -34,7 +35,7 @@
->toBe(expected: trans(key: 'turnstile::turnstile.failed'));
});

test(description: 'it redirects back the request if the token is already spent', closure: function () {
it(description: 'redirects back the request if the token is already spent', closure: function () {
setSecretKey(type: 'spent');

$response = (new TurnstileMiddleware())
Expand All @@ -52,7 +53,7 @@
->toBe(expected: trans(key: 'turnstile::turnstile.failed'));
});

test(description: 'it allows the request to pass when the token is valid', closure: function () {
it(description: 'allows the request to pass when the token is valid', closure: function () {
setSecretKey(type: 'valid');

$response = (new TurnstileMiddleware())
Expand All @@ -70,7 +71,7 @@
->and(value: $response->isSuccessful())->toBeTrue();
});

test(description: 'it redirects back the request if the token is null', closure: function () {
it(description: 'redirects back the request if the token is null', closure: function () {
setSecretKey(type: 'valid');

$response = (new TurnstileMiddleware())
Expand Down
7 changes: 7 additions & 0 deletions tests/Rules/TurnstileRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,10 @@

expect(value:$this->rule)->not->toPassWith('DUMMY.TOKEN');
});

it(description: 'passes when turnstile is disable', closure: function () {
config()->set('turnstile.secretkey', null);
config()->set('turnstile.enabled', false);

expect(value:$this->rule)->toPassWith('');
});

0 comments on commit 8468a6c

Please sign in to comment.