From ee9cc1be72314e0aeb66e553429d7483038585c6 Mon Sep 17 00:00:00 2001 From: luke Date: Sat, 28 Nov 2020 13:34:54 +0000 Subject: [PATCH 1/4] Adds Livewire support for the basic inputs. --- .phpunit.result.cache | 2 +- composer.json | 3 +- composer.lock | 74 ++++++++++++++++++++++++++- src/Traits/HoneyForm.php | 31 +++++++++++ src/Views/Honey.php | 6 +-- tests/Livewire/HoneyFormTraitTest.php | 66 ++++++++++++++++++++++++ tests/TestCase.php | 3 +- 7 files changed, 178 insertions(+), 7 deletions(-) create mode 100644 src/Traits/HoneyForm.php create mode 100644 tests/Livewire/HoneyFormTraitTest.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache index 8ae371a..bdd7c60 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":6522:{a:2:{s:7:"defects";a:26:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;}s:5:"times";a:31:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.007;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.007;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.007;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.051;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.007;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.044;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.01;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.002;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.009;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.002;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.043;}}} \ No newline at end of file +C:37:"PHPUnit\Runner\DefaultTestResultCache":7334:{a:2:{s:7:"defects";a:29:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";i:3;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";i:3;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";i:3;}s:5:"times";a:35:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.007;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.007;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.007;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.051;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.007;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.044;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.01;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.002;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.009;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.002;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.043;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";d:0.059;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";d:0.009;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";d:0.005;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";d:0.066;}}} \ No newline at end of file diff --git a/composer.json b/composer.json index 1231cb3..4b8efc7 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ } }, "require-dev": { - "orchestra/testbench": "^6.4" + "orchestra/testbench": "^6.4", + "livewire/livewire": "^2.3" }, "extra": { "laravel": { diff --git a/composer.lock b/composer.lock index 2d34689..3f5f2f4 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "e4aea2e451c53bc0cf6851f95f544dd4", + "content-hash": "43c9feea0e3f6ed15405e9dd722bfb25", "packages": [ { "name": "brick/math", @@ -4577,6 +4577,78 @@ }, "time": "2020-07-09T08:09:16+00:00" }, + { + "name": "livewire/livewire", + "version": "v2.3.2", + "source": { + "type": "git", + "url": "https://github.com/livewire/livewire.git", + "reference": "8aa271bc3af74d8fea8480b372ecaa0d5ae3f6d2" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/livewire/livewire/zipball/8aa271bc3af74d8fea8480b372ecaa0d5ae3f6d2", + "reference": "8aa271bc3af74d8fea8480b372ecaa0d5ae3f6d2", + "shasum": "" + }, + "require": { + "illuminate/database": "^7.0|^8.0", + "illuminate/support": "^7.0|^8.0", + "illuminate/validation": "^7.0|^8.0", + "php": "^7.2.5", + "symfony/http-kernel": "^5.0" + }, + "require-dev": { + "calebporzio/sushi": "^2.1", + "laravel/framework": "^7.0|^8.0", + "mockery/mockery": "^1.3.1", + "orchestra/testbench": "^5.0|^6.0", + "orchestra/testbench-dusk": "^5.2|^6.0", + "phpunit/phpunit": "^8.4|^9.0", + "psy/psysh": "@stable" + }, + "type": "library", + "extra": { + "laravel": { + "providers": [ + "Livewire\\LivewireServiceProvider" + ], + "aliases": { + "Livewire": "Livewire\\Livewire" + } + } + }, + "autoload": { + "files": [ + "src/helpers.php" + ], + "psr-4": { + "Livewire\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Caleb Porzio", + "email": "calebporzio@gmail.com" + } + ], + "description": "A front-end framework for Laravel.", + "support": { + "issues": "https://github.com/livewire/livewire/issues", + "source": "https://github.com/livewire/livewire/tree/v2.3.2" + }, + "funding": [ + { + "url": "https://github.com/calebporzio", + "type": "github" + } + ], + "time": "2020-11-17T14:58:56+00:00" + }, { "name": "mockery/mockery", "version": "1.4.2", diff --git a/src/Traits/HoneyForm.php b/src/Traits/HoneyForm.php new file mode 100644 index 0000000..7ae259d --- /dev/null +++ b/src/Traits/HoneyForm.php @@ -0,0 +1,31 @@ +honeyInputs[$inputNameSelector->getPresentButEmptyInputName()] = null; + $this->honeyInputs[$inputNameSelector->getTimeOfPageLoadInputName()] = Values::timeOfPageLoad()->getValue(); + $this->honeyInputs[$inputNameSelector->getAlpineInputName()] = null; + } + + protected function honey(): Honey + { + return app('honey'); + } + + protected function passesHoneyChecks() + { + return $this->honey()->check($this->honeyInputs); + } +} \ No newline at end of file diff --git a/src/Views/Honey.php b/src/Views/Honey.php index c286884..1d56653 100644 --- a/src/Views/Honey.php +++ b/src/Views/Honey.php @@ -21,9 +21,9 @@ public function render() { return <<<'blade'
- - - + + + {{ $slot }}
blade; diff --git a/tests/Livewire/HoneyFormTraitTest.php b/tests/Livewire/HoneyFormTraitTest.php new file mode 100644 index 0000000..49d3a82 --- /dev/null +++ b/tests/Livewire/HoneyFormTraitTest.php @@ -0,0 +1,66 @@ +assertSet('honeyInputs.honey_present', null); + $this->assertEquals((int) microtime(true), (int) Crypt::decrypt($test->viewData('honeyInputs')['honey_time'])); + $test->assertSet('honeyInputs.honey_alpine', null); + } + + /** @test */ + public function checks_can_be_run_against_the_inputs() + { + $test = Livewire::test(Example::class); + $test->assertSet('passesCheck', false); + $test->set('honeyInputs.honey_time', Crypt::encrypt(microtime(true) - 5)); + $test->set('honeyInputs.honey_alpine', Values::alpine()->getValue()); + $test->call('check'); + $test->assertSet('passesCheck', true); + } + + /** @test */ + public function if_the_values_are_incorrect_the_check_fails() + { + $test = Livewire::test(Example::class); + $test->assertSet('passesCheck', false); + $test->call('check'); + $test->assertSet('passesCheck', false); + } +} + +class Example extends Component +{ + use HoneyForm; + + public $passesCheck = false; + + public function render() + { + return <<<'blade' +
+ +
+ blade; + } + + public function check() + { + $this->passesCheck = $this->passesHoneyChecks(); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php index c658943..d0b5f65 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,6 +3,7 @@ namespace Lukeraymonddowning\Honey\Tests; use Illuminate\Support\Facades\Crypt; +use Livewire\LivewireServiceProvider; use Lukeraymonddowning\Honey\Providers\HoneyServiceProvider; use Orchestra\Testbench\TestCase as OrchestraTestCase; @@ -10,7 +11,7 @@ abstract class TestCase extends OrchestraTestCase { protected function getPackageProviders($app) { - return [HoneyServiceProvider::class]; + return [HoneyServiceProvider::class, LivewireServiceProvider::class]; } protected function getEnvironmentSetUp($app) From bd70e9857d78d2a6cbb089e3a16b3753893c6f87 Mon Sep 17 00:00:00 2001 From: luke Date: Sun, 29 Nov 2020 18:14:26 +0000 Subject: [PATCH 2/4] Adds recaptcha support for Livewire. --- .phpunit.result.cache | 2 +- config/config.php | 11 ++++-- src/Traits/HoneyForm.php | 21 +++++++++-- src/Views/Recaptcha.php | 42 +++++++++++++++------ tests/CheckRecaptchaTokenMiddlewareTest.php | 2 +- tests/Livewire/HoneyFormTraitTest.php | 29 +++++++++++++- 6 files changed, 84 insertions(+), 23 deletions(-) diff --git a/.phpunit.result.cache b/.phpunit.result.cache index bdd7c60..edbed56 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":7334:{a:2:{s:7:"defects";a:29:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";i:3;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";i:3;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";i:3;}s:5:"times";a:35:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.007;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.007;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.007;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.051;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.007;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.044;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.01;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.002;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.009;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.002;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.043;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";d:0.059;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";d:0.009;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";d:0.005;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";d:0.066;}}} \ No newline at end of file +C:37:"PHPUnit\Runner\DefaultTestResultCache":7437:{a:2:{s:7:"defects";a:29:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";i:3;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";i:3;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";i:3;}s:5:"times";a:36:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.006;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.007;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.006;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.006;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.058;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.006;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.006;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.051;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.012;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.003;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.003;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.003;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.003;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";d:0.024;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";d:0.01;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";d:0.005;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";d:0.066;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";d:0.007;}}} \ No newline at end of file diff --git a/config/config.php b/config/config.php index fe8a738..cdf94be 100644 --- a/config/config.php +++ b/config/config.php @@ -111,11 +111,10 @@ 'present_but_empty' => 'honey_present', 'time_of_page_load' => 'honey_time', 'alpine_input' => 'honey_alpine', - 'recaptcha_input' => 'honey_recapture_token' + 'recaptcha_input' => 'honey_recaptcha_token' ] ], - ] ], @@ -150,7 +149,13 @@ * user. This informs the recaptcha middleware of * minimum score a user can get to pass. */ - 'minimum_score' => env("RECAPTCHA_MINIMUM_SCORE", 0.3), + 'minimum_score' => env("RECAPTCHA_MINIMUM_SCORE", 0.5), + /** + * The Recaptcha input will request a token on page load. As + * Recaptcha tokens only last for 2 minutes, the input + * refreshes based on this timeout (in milliseconds). + */ + 'token_refresh_interval' => 60000, ] ]; \ No newline at end of file diff --git a/src/Traits/HoneyForm.php b/src/Traits/HoneyForm.php index 7ae259d..248b842 100644 --- a/src/Traits/HoneyForm.php +++ b/src/Traits/HoneyForm.php @@ -12,11 +12,12 @@ trait HoneyForm { public $honeyInputs = []; - public function mountHoneyForm(InputNameSelector $inputNameSelector) + public function mountHoneyForm() { - $this->honeyInputs[$inputNameSelector->getPresentButEmptyInputName()] = null; - $this->honeyInputs[$inputNameSelector->getTimeOfPageLoadInputName()] = Values::timeOfPageLoad()->getValue(); - $this->honeyInputs[$inputNameSelector->getAlpineInputName()] = null; + $this->honeyInputs[static::inputs()->getPresentButEmptyInputName()] = null; + $this->honeyInputs[static::inputs()->getTimeOfPageLoadInputName()] = Values::timeOfPageLoad()->getValue(); + $this->honeyInputs[static::inputs()->getAlpineInputName()] = null; + $this->honeyInputs[static::inputs()->getRecaptchaInputName()] = null; } protected function honey(): Honey @@ -28,4 +29,16 @@ protected function passesHoneyChecks() { return $this->honey()->check($this->honeyInputs); } + + protected function recaptcha() + { + return $this->honey() + ->recaptcha() + ->checkToken($this->honeyInputs[static::inputs()->getRecaptchaInputName()]); + } + + protected static function inputs(): InputNameSelector + { + return app(InputNameSelector::class); + } } \ No newline at end of file diff --git a/src/Views/Recaptcha.php b/src/Views/Recaptcha.php index f62c564..0d9b5a1 100644 --- a/src/Views/Recaptcha.php +++ b/src/Views/Recaptcha.php @@ -20,18 +20,26 @@ public function render(callable $callback = null) return <<<'blade' @once + @endonce - @@ -40,7 +48,17 @@ public function render(callable $callback = null) public function siteKey() { - return $this->siteKey ??= config('honey.recaptcha.site_key'); + return $this->siteKey ??= static::config()['site_key']; + } + + public function tokenRefreshInterval() + { + return static::config()['token_refresh_interval']; + } + + protected static function config() + { + return config('honey.recaptcha'); } } \ No newline at end of file diff --git a/tests/CheckRecaptchaTokenMiddlewareTest.php b/tests/CheckRecaptchaTokenMiddlewareTest.php index 8ea8128..f4166d1 100644 --- a/tests/CheckRecaptchaTokenMiddlewareTest.php +++ b/tests/CheckRecaptchaTokenMiddlewareTest.php @@ -18,7 +18,7 @@ public function it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_th { Http::fake(['*' => ['score' => 0.8]]); - request()->merge(['honey_recapture_token' => 'foobar']); + request()->merge(['honey_recaptcha_token' => 'foobar']); (new CheckRecaptchaToken())->handle( request(), function () { diff --git a/tests/Livewire/HoneyFormTraitTest.php b/tests/Livewire/HoneyFormTraitTest.php index 49d3a82..bd6e57b 100644 --- a/tests/Livewire/HoneyFormTraitTest.php +++ b/tests/Livewire/HoneyFormTraitTest.php @@ -5,12 +5,12 @@ use Illuminate\Support\Facades\Crypt; +use Illuminate\Support\Facades\Http; use Livewire\Component; use Livewire\Livewire; use Lukeraymonddowning\Honey\InputValues\Values; use Lukeraymonddowning\Honey\Tests\TestCase; use Lukeraymonddowning\Honey\Traits\HoneyForm; -use Symfony\Component\HttpKernel\Exception\HttpException; class HoneyFormTraitTest extends TestCase { @@ -42,6 +42,25 @@ public function if_the_values_are_incorrect_the_check_fails() $test->call('check'); $test->assertSet('passesCheck', false); } + + /** @test */ + public function it_can_run_a_recaptcha_test() + { + Http::fake(['*' => [ + 'success' => true, + 'score' => 0.8, + 'action' => 'submit', + 'challenge_ts' => now()->toIso8601String(), + 'hostname' => config('app.url'), + 'error-codes' => [] + ]]); + + $test = Livewire::test(Example::class); + $test->assertSet('passesRecaptcha', false); + $test->set('honeyInputs.honey_recaptcha_input', 'foobar'); + $test->call('checkRecaptcha'); + $test->assertSet('passesRecaptcha', true); + } } class Example extends Component @@ -49,12 +68,13 @@ class Example extends Component use HoneyForm; public $passesCheck = false; + public $passesRecaptcha = false; public function render() { return <<<'blade'
- +
blade; } @@ -63,4 +83,9 @@ public function check() { $this->passesCheck = $this->passesHoneyChecks(); } + + public function checkRecaptcha() + { + $this->passesRecaptcha = !$this->recaptcha()->isSpam(); + } } \ No newline at end of file From fba17c26d7149ee22ddb04371cf2c2a45b4f59c2 Mon Sep 17 00:00:00 2001 From: luke Date: Sun, 29 Nov 2020 21:38:03 +0000 Subject: [PATCH 3/4] Added methods and traits for checking pass value of honey and recaptcha in Livewire. --- .phpunit.result.cache | 2 +- src/Facades/Honey.php | 2 + src/Honey.php | 6 +++ src/RecaptchaResponse.php | 6 +++ src/Traits/HoneyForm.php | 44 --------------------- src/Traits/WithHoney.php | 40 +++++++++++++++++++ src/Traits/WithRecaptcha.php | 33 ++++++++++++++++ tests/Livewire/HoneyFormTraitTest.php | 55 ++++++++++++++++++++++++--- 8 files changed, 138 insertions(+), 50 deletions(-) delete mode 100644 src/Traits/HoneyForm.php create mode 100644 src/Traits/WithHoney.php create mode 100644 src/Traits/WithRecaptcha.php diff --git a/.phpunit.result.cache b/.phpunit.result.cache index edbed56..a043885 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":7437:{a:2:{s:7:"defects";a:29:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";i:3;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";i:3;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";i:3;}s:5:"times";a:36:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.006;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.007;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.006;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.006;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.058;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.006;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.006;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.051;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.012;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.003;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.003;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.003;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.003;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";d:0.024;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";d:0.01;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";d:0.005;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";d:0.066;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";d:0.007;}}} \ No newline at end of file +C:37:"PHPUnit\Runner\DefaultTestResultCache":7962:{a:2:{s:7:"defects";a:32:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";i:4;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";i:4;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";i:3;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";i:4;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";i:4;s:141:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too";i:3;}s:5:"times";a:37:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.006;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.006;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.006;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.006;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.059;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.007;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.05;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.012;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.003;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.003;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.003;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.003;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";d:0.016;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";d:0.01;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";d:0.006;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";d:0.066;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";d:0.007;s:141:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too";d:0.01;}}} \ No newline at end of file diff --git a/src/Facades/Honey.php b/src/Facades/Honey.php index e828695..360b1db 100644 --- a/src/Facades/Honey.php +++ b/src/Facades/Honey.php @@ -5,6 +5,7 @@ use Illuminate\Support\Facades\Facade; +use Lukeraymonddowning\Honey\InputNameSelectors\InputNameSelector; use Lukeraymonddowning\Honey\Recaptcha; /** @@ -17,6 +18,7 @@ * @method static fail() Call the registered failure callback. * @method static setMinimumTimePassed($time) Set the minimum amount of time between a page being loaded and submitted. * @method static Recaptcha recaptcha() Resolve the Recaptcha object out of the container. + * @method static InputNameSelector inputs() */ class Honey extends Facade { diff --git a/src/Honey.php b/src/Honey.php index d2670bf..a93ea45 100644 --- a/src/Honey.php +++ b/src/Honey.php @@ -7,6 +7,7 @@ use Illuminate\Http\Request; use Illuminate\Support\Collection; use Illuminate\Support\Facades\App; +use Lukeraymonddowning\Honey\InputNameSelectors\InputNameSelector; use Lukeraymonddowning\Honey\Models\Spammer; class Honey @@ -86,4 +87,9 @@ public function recaptcha() return app('honey-recaptcha'); } + public function inputs() + { + return app(InputNameSelector::class); + } + } \ No newline at end of file diff --git a/src/RecaptchaResponse.php b/src/RecaptchaResponse.php index 3aa0396..5058b4c 100644 --- a/src/RecaptchaResponse.php +++ b/src/RecaptchaResponse.php @@ -8,6 +8,12 @@ use Illuminate\Support\Traits\ForwardsCalls; use Lukeraymonddowning\Honey\Facades\Honey; +/** + * Class RecaptchaResponse + * @package Lukeraymonddowning\Honey + * + * @method boolean isSpam() + */ class RecaptchaResponse implements ArrayAccess { use ForwardsCalls; diff --git a/src/Traits/HoneyForm.php b/src/Traits/HoneyForm.php deleted file mode 100644 index 248b842..0000000 --- a/src/Traits/HoneyForm.php +++ /dev/null @@ -1,44 +0,0 @@ -honeyInputs[static::inputs()->getPresentButEmptyInputName()] = null; - $this->honeyInputs[static::inputs()->getTimeOfPageLoadInputName()] = Values::timeOfPageLoad()->getValue(); - $this->honeyInputs[static::inputs()->getAlpineInputName()] = null; - $this->honeyInputs[static::inputs()->getRecaptchaInputName()] = null; - } - - protected function honey(): Honey - { - return app('honey'); - } - - protected function passesHoneyChecks() - { - return $this->honey()->check($this->honeyInputs); - } - - protected function recaptcha() - { - return $this->honey() - ->recaptcha() - ->checkToken($this->honeyInputs[static::inputs()->getRecaptchaInputName()]); - } - - protected static function inputs(): InputNameSelector - { - return app(InputNameSelector::class); - } -} \ No newline at end of file diff --git a/src/Traits/WithHoney.php b/src/Traits/WithHoney.php new file mode 100644 index 0000000..9a12f5a --- /dev/null +++ b/src/Traits/WithHoney.php @@ -0,0 +1,40 @@ +honeyInputs[Honey::inputs()->getPresentButEmptyInputName()] = null; + $this->honeyInputs[Honey::inputs()->getTimeOfPageLoadInputName()] = Values::timeOfPageLoad()->getValue(); + $this->honeyInputs[Honey::inputs()->getAlpineInputName()] = null; + } + + public function getHoneyPassedProperty() + { + if (!in_array(WithRecaptcha::class, class_uses_recursive(static::class))) { + return Honey::check($this->honeyInputs); + } + + return Honey::check($this->honeyInputs) && $this->recaptchaPasses(); + } + + public function honeyPasses() + { + return $this->honeyPassed; + } +} \ No newline at end of file diff --git a/src/Traits/WithRecaptcha.php b/src/Traits/WithRecaptcha.php new file mode 100644 index 0000000..41e902e --- /dev/null +++ b/src/Traits/WithRecaptcha.php @@ -0,0 +1,33 @@ +honeyInputs[Honey::inputs()->getRecaptchaInputName()] = null; + } + + public function getRecaptchaPassedProperty() + { + return !Honey::recaptcha()->checkToken($this->honeyInputs[Honey::inputs()->getRecaptchaInputName()])->isSpam(); + } + + public function recaptchaPasses() + { + return $this->recaptchaPassed; + } +} \ No newline at end of file diff --git a/tests/Livewire/HoneyFormTraitTest.php b/tests/Livewire/HoneyFormTraitTest.php index bd6e57b..5125f4f 100644 --- a/tests/Livewire/HoneyFormTraitTest.php +++ b/tests/Livewire/HoneyFormTraitTest.php @@ -10,7 +10,8 @@ use Livewire\Livewire; use Lukeraymonddowning\Honey\InputValues\Values; use Lukeraymonddowning\Honey\Tests\TestCase; -use Lukeraymonddowning\Honey\Traits\HoneyForm; +use Lukeraymonddowning\Honey\Traits\WithHoney; +use Lukeraymonddowning\Honey\Traits\WithRecaptcha; class HoneyFormTraitTest extends TestCase { @@ -55,19 +56,63 @@ public function it_can_run_a_recaptcha_test() 'error-codes' => [] ]]); - $test = Livewire::test(Example::class); + $test = Livewire::test(AnotherExample::class); $test->assertSet('passesRecaptcha', false); $test->set('honeyInputs.honey_recaptcha_input', 'foobar'); $test->call('checkRecaptcha'); $test->assertSet('passesRecaptcha', true); } + + /** @test */ + public function if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too() + { + Http::fake(['*' => [ + 'success' => true, + 'score' => 0.8, + 'action' => 'submit', + 'challenge_ts' => now()->toIso8601String(), + 'hostname' => config('app.url'), + 'error-codes' => [] + ]]); + + $test = Livewire::test(AnotherExample::class); + $test->set('honeyInputs.honey_time', Crypt::encrypt(microtime(true) - 5)); + $test->set('honeyInputs.honey_alpine', Values::alpine()->getValue()); + $test->set('honeyInputs.honey_recaptcha_input', 'foobar'); + $test->assertSet('passesEverything', false); + $test->call('check'); + $test->assertSet('passesEverything', true); + + Http::assertSentCount(1); + } } class Example extends Component { - use HoneyForm; + use WithHoney; public $passesCheck = false; + + public function render() + { + return <<<'blade' +
+ +
+ blade; + } + + public function check() + { + $this->passesCheck = $this->honeyPasses(); + } +} + +class AnotherExample extends Component +{ + use WithHoney, WithRecaptcha; + + public $passesEverything = false; public $passesRecaptcha = false; public function render() @@ -81,11 +126,11 @@ public function render() public function check() { - $this->passesCheck = $this->passesHoneyChecks(); + $this->passesEverything = $this->honeyPassed; } public function checkRecaptcha() { - $this->passesRecaptcha = !$this->recaptcha()->isSpam(); + $this->passesRecaptcha = $this->recaptchaPassed; } } \ No newline at end of file From 1c3dfab204a8d39509c1f81e2dbad9a949b6f65b Mon Sep 17 00:00:00 2001 From: luke Date: Wed, 2 Dec 2020 17:28:51 +0000 Subject: [PATCH 4/4] Updates the README.md --- .phpunit.result.cache | 2 +- README.md | 60 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 60 insertions(+), 2 deletions(-) diff --git a/.phpunit.result.cache b/.phpunit.result.cache index a043885..87986e8 100644 --- a/.phpunit.result.cache +++ b/.phpunit.result.cache @@ -1 +1 @@ -C:37:"PHPUnit\Runner\DefaultTestResultCache":7962:{a:2:{s:7:"defects";a:32:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";i:4;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";i:4;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";i:3;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";i:4;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";i:4;s:141:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too";i:3;}s:5:"times";a:37:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.007;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.006;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.006;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.006;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.006;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.059;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.007;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.05;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.012;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.003;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.003;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.003;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.003;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";d:0.016;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";d:0.01;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";d:0.006;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";d:0.066;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";d:0.007;s:141:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too";d:0.01;}}} \ No newline at end of file +C:37:"PHPUnit\Runner\DefaultTestResultCache":7964:{a:2:{s:7:"defects";a:32:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";i:5;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";i:3;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";i:3;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";i:3;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";i:5;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";i:4;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";i:4;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";i:3;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";i:5;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";i:3;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";i:5;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";i:3;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";i:4;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";i:3;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";i:3;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";i:4;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";i:4;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";i:3;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";i:3;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";i:4;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";i:4;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";i:5;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";i:3;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";i:4;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";i:4;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";i:4;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";i:3;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";i:4;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";i:4;s:141:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too";i:3;}s:5:"times";a:37:{s:87:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_requires_a_present_but_unfilled_check";d:0.042;s:82:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_present_but_unfilled_check";d:0.003;s:66:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::it_has_a_time_check";d:0.003;s:76:"Lukeraymonddowning\Honey\Tests\MiddlewareTest::the_handler_can_be_configured";d:0.003;s:81:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component";d:0.041;s:91:"Lukeraymonddowning\Honey\Tests\BladeComponentTest::it_registers_a_blade_component_for_honey";d:0.003;s:103:"Lukeraymonddowning\Honey\Tests\FailHooksTest::hooks_can_be_fired_before_the_registered_fail_takes_place";d:0.003;s:123:"Lukeraymonddowning\Honey\Tests\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.058;s:131:"Lukeraymonddowning\Honey\Tests\Feature\SpammerBlockTest::if_an_ip_address_hits_the_maximum_spam_attempts_they_are_marked_as_blocked";d:0.006;s:92:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_blocks_known_spammers";d:0.006;s:104:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_unblocked_spammers_through";d:0.006;s:96:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_allows_new_people_through";d:0.006;s:103:"Lukeraymonddowning\Honey\Tests\Feature\UserIsKnownSpammerCheckTest::it_skips_if_the_feature_is_disabled";d:0.006;s:92:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_blocks_known_spammers";d:0.053;s:104:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_unblocked_spammers_through";d:0.007;s:107:"Lukeraymonddowning\Honey\Tests\Feature\BlockSpammersMiddlewareTest::it_allows_anybody_not_a_spammer_through";d:0.006;s:94:"Lukeraymonddowning\Honey\Tests\AlpineInputFilledCheckTest::it_requires_the_exact_defined_input";d:0.05;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_return_a_score_based_on_the_given_token";d:0.003;s:136:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\EnsureRecaptchaPassesCheckTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:106:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_throws_an_exception_with_the_error_codes_if_it_fails";d:0.003;s:105:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::you_can_call_the_check_token_as_many_times_as_you_want";d:0.003;s:139:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_submits_the_token_to_recaptcha_and_ensures_the_score_meets_the_minimum";d:0.012;s:108:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_score_is_below_the_minimum";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_the_token_is_missing";d:0.003;s:100:"Lukeraymonddowning\Honey\Tests\CheckRecaptchaTokenMiddlewareTest::it_fails_if_an_exception_is_thrown";d:0.003;s:97:"Lukeraymonddowning\Honey\Tests\RecaptchaTokenTest::it_can_chain_recaptcha_methods_after_the_check";d:0.003;s:99:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_is_only_enabled_in_specified_environments";d:0.003;s:67:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::it_wont_work";d:0.003;s:98:"Lukeraymonddowning\Honey\Tests\EnvironmentDisableTest::check_will_always_return_true_when_disabled";d:0.003;s:77:"Lukeraymonddowning\Honey\Tests\ManualDisableTest::it_can_be_disabled_manually";d:0.003;s:116:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::the_honeyInputs_attribute_is_filled_with_the_input_names";d:0.026;s:96:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::checks_can_be_run_against_the_inputs";d:0.015;s:103:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_the_values_are_incorrect_the_check_fails";d:0.006;s:91:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_perform_the_fail_for_you";d:0.066;s:87:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::it_can_run_a_recaptcha_test";d:0.008;s:141:"Lukeraymonddowning\Honey\Tests\Livewire\HoneyFormTraitTest::if_honeyPasses_is_called_and_WithRecaptcha_is_a_trait_it_checks_the_recaptcha_too";d:0.011;}}} \ No newline at end of file diff --git a/README.md b/README.md index 65e65be..d019df4 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,20 @@ A spam prevention package for Laravel, providing honeypot techniques, ip blocking and beautifully simple [Recaptcha](https://developers.google.com/recaptcha) integration. Stop spam. Use Honey. +## TOC +- [Installation](#installation) +- [Usage](#usage) + * [Recaptcha](#recaptcha) + - [Via Middleware](#via-middleware) + - [Manually](#manually) + * [Customising what happens on failure](#customising-what-happens-on-failure) + * [Hooks](#hooks) + * [Manually running Honey checks](#manually-running-honey-checks) + * [Integrating with Livewire](#integrating-with-livewire) + * [Configuring Honey](#configuring-honey) +- [Testing](#testing) +- [Accreditations](#accreditations) + ## Installation You can install Honey via Composer. @@ -74,12 +88,21 @@ We'll use the example from earlier: ``` -As a note, you can use `` alongside ``, or separately. You do you. +As a note, you can use `` alongside ``, or separately. You do you. However, if +you're using both components, you can simplify to this: + +```blade + +``` + To allow you to track different action types in your Google console, you can pass an action attribute to the recaptcha blade component. ```blade + +// Or... + ``` You now have 2 options. You can allow Honey to make the Recaptcha request for you and fail automatically if it @@ -161,6 +184,41 @@ Note that this won't fail for you, but will return a boolean instead. You can fo Honey::fail(); ``` +### Integrating with Livewire +Honey offers out of the box [Livewire](https://laravel-livewire.com) support. To get started, add the +`WithHoney` trait to your Livewire component. You don't need to change your form at all, everything will +just work. Note that the honey inputs are [deferred](https://laravel-livewire.com/docs/2.x/properties#deferred-updating), +so they'll only sync back to the server after calling an action. Here's an example form: + +```blade +
+ @csrf + + + + +``` + +When the form is submitted and the `signup` action is called, the Honey inputs will sync back to your component. +In your component, you can check if Honey passed by calling `$this->honeyPasses()`. Note that your standard fail +method will not be called in Livewire components, because the use case is so different. + +Honey also supports reCaptcha for Livewire. Again, your form just needs to include the recaptcha component or +include the `recaptcha` attribute on the `` component: + +```blade +
+ @csrf + + + + +``` + +You also need to add the `WithRecaptcha` trait to your Livewire component (even if you're using Honey too). You can +check for reCaptcha success by calling `$this->recaptchaPasses()`. If you're using `WithHoney`, the `$this->honeyPasses()` +method will also check reCaptcha for you, so there is no need to call both methods. + ### Configuring Honey Honey is built with a great set of defaults, but we understand that one size rarely fits all. That's why we provide plenty of config options for you. You can access them from the `honey.php` config file. Let's look at the