Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Domain/Captcha/GoogleRecaptchaV3CaptchaProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ public function validate_token(string $captcha_token, string $validated_action):
return false;
}

/** @var string|float $min_Score */
$min_Score = $this->settingsService->getSetting('google_recaptcha_min_score')->value;
$min_Score = is_string($min_Score) ? (float) str_replace(',', '.', trim($min_Score)) : $min_Score;

return !($score < $min_Score);
if ($min_Score <= 0) {
$min_Score = 0.1;
}

return (float) $score >= $min_Score;
}
}
67 changes: 67 additions & 0 deletions tests/Unit/CaptchaServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Tests\Unit;

use Domain\Captcha\GoogleRecaptchaV3CaptchaProvider;
use Domain\Setting\Models\Setting;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Http;
use Tests\TestCase;

class CaptchaServiceTest extends TestCase
{
public function setUp(): void
{
parent::setUp();
}

/**
* @dataProvider captchaDataProvider
*/
public function testValidateTokenLogic(
float $google_score,
string $db_min_score,
bool $expected_result
): void {
Config::set('captcha.google_recaptcha_url', 'www.google.com/recaptcha/api/siteverify');
Config::set('captcha.google_recaptcha_secret', 'secret');

Setting::query()->where('name', 'google_recaptcha_min_score')
->firstOrCreate([
'name' => 'google_recaptcha_min_score',
'value' => $db_min_score,
'public' => false,
]);

Http::fake([
'www.google.com/recaptcha/api/siteverify' => Http::response([
'success' => true,
'action' => 'login',
'score' => $google_score,
], 200),
]);

/** @var GoogleRecaptchaV3CaptchaProvider $captchaProvider */
$captchaProvider = App::make(GoogleRecaptchaV3CaptchaProvider::class);

$result = $captchaProvider->validate_token('test-token', 'login');

$this->assertEquals($expected_result, $result);
}

public static function captchaDataProvider(): array
{
return [
['google_score' => 0.9, 'db_min_score' => '0.5', 'expected' => true],
['google_score' => 0.1, 'db_min_score' => '0.5', 'expected' => false],
['google_score' => 0.5, 'db_min_score' => '0.5', 'expected' => true],

['google_score' => 0.3, 'db_min_score' => '0,5', 'expected' => false],

['google_score' => 0.0, 'db_min_score' => '', 'expected' => false],
// Default value is 0.1
['google_score' => 0.2, 'db_min_score' => '', 'expected' => true],
];
}
}