Use the reCAPTCHA Enterprise API.

This commit is contained in:
tim
2026-09-26 15:27:51 +02:00
parent cc47d0ee51
commit 77800174c9
11 changed files with 265 additions and 60 deletions
+14 -1
View File
@@ -50,7 +50,8 @@ class LoginTest extends PageTestCase {
public function testTheFormHasATokenAndTheCaptcha(): void {
$body = $this->get('/publish/login')['body'];
$this->assertSame($this->session()['csrf_token'], $this->csrfToken($body));
$this->assertStringContainsString('data-sitekey="test-site-key"', $body);
$this->assertStringContainsString('data-sitekey="test-site-key" data-action="login"', $body);
$this->assertStringContainsString('https://www.google.com/recaptcha/enterprise.js', $body);
}
public function testTheRightPasswordLogsIn(): void {
@@ -130,6 +131,18 @@ class LoginTest extends PageTestCase {
$this->assertSame(array(), $this->queries('/FROM editors/'));
}
public static function failedAssessments(): array {
return array(array('wrong-host'), array('wrong-action'), array('api-error'), array('unreachable'));
}
#[DataProvider('failedAssessments')]
public function testAFailedAssessmentBlocksTheLogin(string $response): void {
$answer = $this->login('secret', array('g-recaptcha-response' => $response));
$this->assertStringContainsString('Please complete the captcha.', $answer['body']);
$this->assertSame(array(), $this->queries('/FROM editors/'));
$this->assertArrayNotHasKey('editor', $this->session());
}
public function testFiveFailuresBlockTheLogin(): void {
for ($i = 0; $i < 5; $i++)
$this->login('wrong');
+17 -2
View File
@@ -1,6 +1,7 @@
<?php
namespace Xxx\Tests\Pages;
use PHPUnit\Framework\Attributes\DataProvider;
use Xxx\Tests\Support\PageTestCase;
use Xxx\Tests\Support\TestCase;
@@ -24,8 +25,8 @@ class SubmitTest extends PageTestCase {
$page = $this->get('/publish');
$this->assertSame(200, $page['status']);
$this->assertSame($this->session()['csrf_token'], $this->csrfToken($page['body']));
$this->assertStringContainsString('<div class="g-recaptcha" data-sitekey="test-site-key"></div>', $page['body']);
$this->assertStringContainsString('https://www.google.com/recaptcha/api.js', $page['body']);
$this->assertStringContainsString('<div class="g-recaptcha" data-sitekey="test-site-key" data-action="submit"></div>', $page['body']);
$this->assertStringContainsString('https://www.google.com/recaptcha/enterprise.js', $page['body']);
$this->assertStringContainsString('<script type="text/javascript" src="../publish/editor.js"></script>', $page['body']);
}
@@ -104,6 +105,20 @@ class SubmitTest extends PageTestCase {
$this->assertSame(array(), $this->queries('/^INSERT/'));
}
public static function failedAssessments(): array {
return array(array('wrong-host'), array('wrong-action'), array('api-error'), array('unreachable'));
}
#[DataProvider('failedAssessments')]
public function testAFailedAssessmentBlocksTheSubmission(string $response): void {
$token = $this->csrfToken($this->get('/publish')['body']);
$page = $this->post('/publish', $this->story(array('action' => 'submit', 'csrf_token' => $token, 'g-recaptcha-response' => $response)));
$this->assertStringContainsString('Please complete the captcha.', $page['body']);
$this->assertSame(array(), $this->queries('/^INSERT/'));
$this->assertSame(array(), $this->mails());
$this->assertArrayNotHasKey('captcha_ok', $this->session());
}
public function testMissingFieldsAreShown(): void {
$token = $this->csrfToken($this->get('/publish')['body']);
$page = $this->post('/publish', array('action' => 'submit', 'csrf_token' => $token, 'g-recaptcha-response' => 'pass', 'story' => '<p>x</p>'));