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

Commit

Permalink
Fixed dangling null parameters on Middleware helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkGhostHunter committed Nov 15, 2021
1 parent 0dd3495 commit 3436a57
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 29 deletions.
42 changes: 35 additions & 7 deletions src/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use DarkGhostHunter\Captchavel\Http\Middleware\VerifyReCaptchaV2;
use DarkGhostHunter\Captchavel\Http\Middleware\VerifyReCaptchaV3;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use LogicException;
use Stringable;

use function config;
use function implode;
use function max;
use function rtrim;

use const DEBUG_BACKTRACE_IGNORE_ARGS;

Expand All @@ -28,7 +28,7 @@ class ReCaptcha implements Stringable
*/
public function __construct(
protected string $version,
protected string $input = Captchavel::INPUT,
protected string $input = 'null',
protected string $threshold = 'null',
protected string $action = 'null',
protected string $remember = 'null',
Expand Down Expand Up @@ -121,6 +121,16 @@ public function remember(int $minutes = null): static
return $this;
}

/**
* Checking for a "remember" on this route and stores the key forever.
*
* @return $this
*/
public function rememberForever(): static
{
return $this->remember(0);
}

/**
* Bypass checking for a "remember" on this route.
*
Expand Down Expand Up @@ -197,10 +207,28 @@ public function toString(): string
*/
public function __toString(): string
{
$string = $this->version === Captchavel::SCORE
? VerifyReCaptchaV3::SIGNATURE . ':' . implode(',', [$this->threshold, $this->action])
: VerifyReCaptchaV2::SIGNATURE . ':' . implode(',', [$this->version, $this->remember]);
$declaration = $this->getBaseParameters()
->reverse()
->skipUntil(static function (string $parameter): bool {
return $parameter !== 'null';
})
->reverse()
->implode(',');

return Str::replaceFirst(',', ':', $declaration);
}

return rtrim($string . ',' . implode(',', [$this->input, implode(',', $this->guards)]), ',');
/**
* Returns the parameters as a collection.
*
* @return \Illuminate\Support\Collection
*/
protected function getBaseParameters(): Collection
{
return Collection::make(
$this->version === Captchavel::SCORE
? [VerifyReCaptchaV3::SIGNATURE, $this->threshold, $this->action]
: [VerifyReCaptchaV2::SIGNATURE, $this->version, $this->remember]
)->push($this->input, ...$this->guards);
}
}
70 changes: 48 additions & 22 deletions tests/ReCaptchaMiddlewareHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,52 @@ class ReCaptchaMiddlewareHelperTest extends TestCase

public function test_creates_full_recaptcha_v2_checkbox_string(): void
{
static::assertEquals('recaptcha:checkbox,null,g-recaptcha-response', (string) ReCaptcha::checkbox());
static::assertEquals('recaptcha:checkbox', (string) ReCaptcha::checkbox());
static::assertEquals('recaptcha:checkbox,null,foo', (string) ReCaptcha::checkbox()->input('foo'));
static::assertEquals('recaptcha:checkbox,null,g-recaptcha-response,bar', (string) ReCaptcha::checkbox()->except('bar'));
static::assertEquals('recaptcha:checkbox,10,g-recaptcha-response,bar', (string) ReCaptcha::checkbox()->except('bar')->remember());
static::assertEquals('recaptcha:checkbox,20,g-recaptcha-response,bar', (string) ReCaptcha::checkbox()->except('bar')->remember(20));
static::assertEquals('recaptcha:checkbox,false,g-recaptcha-response,bar', (string) ReCaptcha::checkbox()->except('bar')->dontRemember());
static::assertEquals('recaptcha:checkbox,null,null,bar', (string) ReCaptcha::checkbox()->except('bar'));
static::assertEquals('recaptcha:checkbox,10,null,bar', (string) ReCaptcha::checkbox()->except('bar')->remember());
static::assertEquals('recaptcha:checkbox,20,null,bar', (string) ReCaptcha::checkbox()->except('bar')->remember(20));
static::assertEquals('recaptcha:checkbox,0,null,bar', (string) ReCaptcha::checkbox()->except('bar')->rememberForever());
static::assertEquals('recaptcha:checkbox,false,null,bar', (string) ReCaptcha::checkbox()->except('bar')->dontRemember());
static::assertEquals('recaptcha:checkbox,false,foo,bar', (string) ReCaptcha::checkbox()->input('foo')->except('bar')->dontRemember());
}

public function test_creates_full_recaptcha_v2_invisible_string(): void
{
static::assertEquals('recaptcha:invisible,null,g-recaptcha-response', (string) ReCaptcha::invisible());
static::assertEquals('recaptcha:invisible', (string) ReCaptcha::invisible());
static::assertEquals('recaptcha:invisible,null,foo', (string) ReCaptcha::invisible()->input('foo'));
static::assertEquals('recaptcha:invisible,null,g-recaptcha-response,bar', (string) ReCaptcha::invisible()->except('bar'));
static::assertEquals('recaptcha:invisible,10,g-recaptcha-response,bar', (string) ReCaptcha::invisible()->except('bar')->remember());
static::assertEquals('recaptcha:invisible,20,g-recaptcha-response,bar', (string) ReCaptcha::invisible()->except('bar')->remember(20));
static::assertEquals('recaptcha:invisible,false,g-recaptcha-response,bar', (string) ReCaptcha::invisible()->except('bar')->dontRemember());
static::assertEquals('recaptcha:invisible,null,null,bar', (string) ReCaptcha::invisible()->except('bar'));
static::assertEquals('recaptcha:invisible,10,null,bar', (string) ReCaptcha::invisible()->except('bar')->remember());
static::assertEquals('recaptcha:invisible,20,null,bar', (string) ReCaptcha::invisible()->except('bar')->remember(20));
static::assertEquals('recaptcha:invisible,0,null,bar', (string) ReCaptcha::invisible()->except('bar')->rememberForever());
static::assertEquals('recaptcha:invisible,false,null,bar', (string) ReCaptcha::invisible()->except('bar')->dontRemember());
static::assertEquals('recaptcha:invisible,false,foo,bar', (string) ReCaptcha::invisible()->input('foo')->except('bar')->dontRemember());
}

public function test_creates_full_recaptcha_v2_android_string(): void
{
static::assertEquals('recaptcha:android,null,g-recaptcha-response', (string) ReCaptcha::android());
static::assertEquals('recaptcha:android', (string) ReCaptcha::android());
static::assertEquals('recaptcha:android,null,foo', (string) ReCaptcha::android()->input('foo'));
static::assertEquals('recaptcha:android,null,g-recaptcha-response,bar', (string) ReCaptcha::android()->except('bar'));
static::assertEquals('recaptcha:android,10,g-recaptcha-response,bar', (string) ReCaptcha::android()->except('bar')->remember());
static::assertEquals('recaptcha:android,20,g-recaptcha-response,bar', (string) ReCaptcha::android()->except('bar')->remember(20));
static::assertEquals('recaptcha:android,false,g-recaptcha-response,bar', (string) ReCaptcha::android()->except('bar')->dontRemember());
static::assertEquals('recaptcha:android,null,null,bar', (string) ReCaptcha::android()->except('bar'));
static::assertEquals('recaptcha:android,10,null,bar', (string) ReCaptcha::android()->except('bar')->remember());
static::assertEquals('recaptcha:android,20,null,bar', (string) ReCaptcha::android()->except('bar')->remember(20));
static::assertEquals('recaptcha:android,0,null,bar', (string) ReCaptcha::android()->except('bar')->rememberForever());
static::assertEquals('recaptcha:android,false,null,bar', (string) ReCaptcha::android()->except('bar')->dontRemember());
static::assertEquals('recaptcha:android,false,foo,bar', (string) ReCaptcha::android()->input('foo')->except('bar')->dontRemember());
}

public function test_exception_if_using_remember_on_v3(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('You cannot set [remember] for a [score] middleware.');
ReCaptcha::score()->remember();
}

public function test_exception_if_using_dont_remember_on_v3(): void
{
$this->expectException(LogicException::class);
$this->expectExceptionMessage('You cannot set [dontRemember] for a [score] middleware.');
ReCaptcha::score()->dontRemember();
}

public function test_exception_if_using_v3_methods_on_v2_checkbox(): void
Expand All @@ -66,7 +86,7 @@ public function test_exception_if_using_v3_methods_on_v2_android(): void
public function test_creates_full_recaptcha_v3_score_string()
{
static::assertSame(
'recaptcha.score:0.5,null,g-recaptcha-response',
'recaptcha.score:0.5',
(string) ReCaptcha::score()
);

Expand All @@ -84,38 +104,44 @@ public function test_creates_full_recaptcha_v3_score_string()
public function test_uses_threshold_from_config()
{
static::assertSame(
'recaptcha.score:0.5,null,g-recaptcha-response',
'recaptcha.score:0.5',
(string) ReCaptcha::score()
);

config(['captchavel.threshold' => 0.1]);

static::assertSame(
'recaptcha.score:0.1,null,g-recaptcha-response',
'recaptcha.score:0.1',
(string) ReCaptcha::score()
);
}

public function test_normalizes_threshold(): void
{
static::assertSame(
'recaptcha.score:1.0,null,g-recaptcha-response',
'recaptcha.score:1.0',
(string) ReCaptcha::score(1.7)
);

static::assertSame(
'recaptcha.score:0.0,null,g-recaptcha-response',
'recaptcha.score:0.0',
(string) ReCaptcha::score(-9)
);

static::assertSame(
'recaptcha.score:1.0,null,g-recaptcha-response',
'recaptcha.score:1.0',
(string) ReCaptcha::score()->threshold(1.7)
);

static::assertSame(
'recaptcha.score:0.0,null,g-recaptcha-response',
'recaptcha.score:0.0',
(string) ReCaptcha::score()->threshold(-9)
);
}

public function test_cast_to_string(): void
{
static::assertEquals('recaptcha.score:0.7', ReCaptcha::score(0.7)->toString());
static::assertEquals('recaptcha.score:0.7', ReCaptcha::score(0.7)->__toString());
}
}

0 comments on commit 3436a57

Please sign in to comment.