Skip to content

Commit

Permalink
more testing and cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
RahulDey12 committed Oct 5, 2023
1 parent d4b7bdc commit c668a62
Show file tree
Hide file tree
Showing 14 changed files with 58 additions and 80 deletions.
2 changes: 1 addition & 1 deletion resources/views/components/button.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<button {{ $attributes->merge([
'class' => $containerClass,
'data-sitekey' => $sitekey,
'data-sitekey' => $site_key,
'data-callback' => $callback
]) }}>
{{ $slot }}
Expand Down
2 changes: 1 addition & 1 deletion resources/views/components/container.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
'class' => $containerClass,
'data-theme' => $theme,
'data-size' => $size,
'data-sitekey' => $sitekey,
'data-sitekey' => $site_key,
]) }}></div>
6 changes: 2 additions & 4 deletions src/CaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ class CaptchaServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->mergeConfigFrom(
__DIR__.'/../config/captcha.php', 'captcha'
);
$this->mergeConfigFrom(__DIR__.'/../config/captcha.php', 'captcha');

$this->app->singleton(CaptchaContract::class, fn ($app): CaptchaManager => new CaptchaManager($app));

Expand Down Expand Up @@ -50,7 +48,7 @@ protected function bootBladeComponents(): void

protected function bootValidations(): void
{
Validator::extend('captcha', Captcha::class.'@passes', __(Captcha::MESSAGE));
Validator::extend('captcha', Captcha::class.'@passes', __(Captcha::$message));
}

protected function bootPublishing(): void
Expand Down
12 changes: 0 additions & 12 deletions src/Contracts/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,11 @@

interface Captcha
{
/**
* Verify the captcha response.
*/
public function verify(string $token): bool;

/**
* Returns the name of captcha service input.
*/
public function getResponseName(): string;

/**
* Returns the name of captcha container class.
*/
public function getContainerClassName(): string;

/**
* Get javascript for captcha service.
*/
public function getJs(?string $hl): string;
}
6 changes: 5 additions & 1 deletion src/Drivers/FakeCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

class FakeCaptcha implements Captcha
{
public function __construct(protected bool $verify = true)
{
}

public function verify(string $token): bool
{
return true;
return $this->verify;
}

public function getResponseName(): string
Expand Down
14 changes: 0 additions & 14 deletions src/Drivers/HCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@

class HCaptcha implements Captcha
{
/**
* The url to verify the captcha response.
*
* @var string
*/
public const VERIFY_URL = 'https://hcaptcha.com/siteverify';

/**
* Verify the captcha response.
*/
public function verify(string $token): bool
{
$captcha_resp = Http::asForm()->post(self::VERIFY_URL, [
Expand All @@ -30,9 +22,6 @@ public function verify(string $token): bool
return (bool) collect($captcha_resp)->get('success');
}

/**
* Returns the name of captcha service input.
*/
public function getResponseName(): string
{
return 'h-captcha-response';
Expand All @@ -43,9 +32,6 @@ public function getContainerClassName(): string
return 'h-captcha';
}

/**
* Get javascript for captcha service.
*/
public function getJs(?string $hl = null): string
{
$hl ??= config('captcha.locale', 'en');
Expand Down
14 changes: 0 additions & 14 deletions src/Drivers/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@

class ReCaptcha implements Captcha
{
/**
* The url to verify the captcha response.
*
* @var string
*/
public const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';

/**
* Verify the captcha response.
*/
public function verify(string $token): bool
{
$captcha_resp = Http::asForm()->post(self::VERIFY_URL, [
Expand All @@ -30,9 +22,6 @@ public function verify(string $token): bool
return (bool) collect($captcha_resp)->get('success');
}

/**
* Returns the name of captcha service input.
*/
public function getResponseName(): string
{
return 'g-recaptcha-response';
Expand All @@ -43,9 +32,6 @@ public function getContainerClassName(): string
return 'g-recaptcha';
}

/**
* Get javascript for captcha service.
*/
public function getJs(?string $hl = null): string
{
$hl ??= config('captcha.locale', 'en');
Expand Down
11 changes: 0 additions & 11 deletions src/Drivers/TurnstileCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@

class TurnstileCaptcha implements Captcha
{
/**
* The url to verify the captcha response.
*
* @var string
*/
public const VERIFY_URL = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';

/**
* Verify the captcha response.
*/
public function verify(string $token): bool
{
$captcha_resp = Http::asForm()->post(self::VERIFY_URL, [
Expand All @@ -30,9 +22,6 @@ public function verify(string $token): bool
return (bool) collect($captcha_resp)->get('success');
}

/**
* Returns the name of captcha service input.
*/
public function getResponseName(): string
{
return 'cf-turnstile-response';
Expand Down
11 changes: 2 additions & 9 deletions src/Facades/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,20 @@
/**
* @method static bool verify(string $token)
* @method static string getJs(null|string $hl = null)
* @method static string getContainer(null|string $theme = null, null|string $size = null)
* @method static string getResponseName()
* @method static string getContainerClassName()
*
* @see \Rahul900day\Captcha\Contracts\Captcha
*/
class Captcha extends Facade
{
/**
* Get the registered name of the component.
*/
protected static function getFacadeAccessor(): string
{
return CaptchaContract::class;
}

/**
* Register a fake captcha service in request.
*/
public static function fake(): void
public static function fake(bool $result = true): void
{
self::swap(new FakeCaptcha());
self::swap(new FakeCaptcha($result));
}
}
4 changes: 2 additions & 2 deletions src/Rules/Captcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class Captcha implements Rule
{
public const MESSAGE = 'The captcha verification failed.';
public static string $message = 'The captcha verification failed.';

/**
* Determine if the validation rule passes.
Expand All @@ -27,6 +27,6 @@ public function passes($attribute, $value): bool
*/
public function message(): string
{
return __(self::MESSAGE);
return __(self::$message);
}
}
4 changes: 2 additions & 2 deletions src/Views/Components/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Button extends Component
{
public string $containerClass;

public string $sitekey;
public string $site_key;

public string $callback;

Expand All @@ -23,7 +23,7 @@ class Button extends Component

public function __construct(public string $formId, string $callback = null)
{
$this->sitekey = config('captcha.sitekey', '');
$this->site_key = config('captcha.sitekey', '');
$this->containerClass = Captcha::getContainerClassName();
$this->nonce = Str::random(10);
$this->callback = $callback ?? "onCaptchaSubmit_{$this->nonce}";
Expand Down
4 changes: 2 additions & 2 deletions src/Views/Components/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class Container extends Component
{
public string $containerClass;

public string $sitekey;
public string $site_key;

public ?string $theme;

public ?string $size;

public function __construct(string $theme = null, string $size = null)
{
$this->sitekey = config('captcha.sitekey', '');
$this->site_key = config('captcha.sitekey', '');
$this->theme = $theme ?? config('captcha.theme', 'light');
$this->size = $size ?? config('captcha.size', 'normal');
$this->containerClass = Captcha::getContainerClassName();
Expand Down
12 changes: 6 additions & 6 deletions tests/FakeCaptchaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@

use Rahul900day\Captcha\Facades\Captcha;

beforeEach(function () {
Captcha::fake();
});

it('can be succeed', function () {
Captcha::fake();
$res = Captcha::verify('test_success_token');

expect($res)->toBeTrue();
});

it('can\'t be failed', function () {
it('can be failed', function () {
Captcha::fake(false);
$res = Captcha::verify('test_fail_token');

expect($res)->toBeTrue();
expect($res)->not->toBeTrue();
});

it('can give captcha class', function () {
Captcha::fake();
expect(Captcha::getContainerClassName())->toBe('fake-captcha-container');
});

it('can give fake captcha javascript', function () {
Captcha::fake();
$js_code = Captcha::getJs('fr');

expect($js_code)->toMatchSnapshot();
Expand Down
36 changes: 35 additions & 1 deletion tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Rahul900day\Captcha\Facades\Captcha;
use Rahul900day\Captcha\Rules\Captcha as CaptchaRule;

it('can validate', function () {
it('can be pass', function () {
Captcha::fake();

$validator = Validator::make([Captcha::getResponseName() => 'test_resp'], [
Expand All @@ -17,6 +17,40 @@
expect($validator->fails())->toBeFalse();
});

it('can be fail', function () {
Captcha::fake(false);

$validator = Validator::make([Captcha::getResponseName() => 'test_resp'], [
Captcha::getResponseName() => [
'required',
new CaptchaRule(),
],
]);

expect($validator->fails())->toBeTrue();

$messages = $validator->messages();
expect($messages->get(Captcha::getResponseName())[0])->toBe(CaptchaRule::$message);
});

it('can have custom error messages', function ($message) {
Captcha::fake(false);
CaptchaRule::$message = $message;

$validator = Validator::make([Captcha::getResponseName() => 'test_resp'], [
Captcha::getResponseName() => [
'required',
new CaptchaRule(),
],
]);

$messages = $validator->messages();
expect($messages->get(Captcha::getResponseName())[0])->toBe($message);
})->with([
'Failed.',
'Validation Failed.',
]);

it('can validate with alias', function () {
Captcha::fake();

Expand Down

0 comments on commit c668a62

Please sign in to comment.