Files
wallabag-mirror/tests/functional/Controller/Api/UserRestControllerTest.php
Yassine Guedidi 24592d245e Keep API user registration CAPTCHA-free
Enable the global CAPTCHA flag in an API registration regression test while submitting the existing username, password, and email payload only.

The test fixes the intended boundary around HTML user-creation surfaces by asserting the established 201 response fields and confirming no CAPTCHA field enters the API contract.

NewUserType's false default remains the compatibility mechanism for this consumer.
2026-07-21 23:30:57 +02:00

223 lines
9.1 KiB
PHP

<?php
namespace Wallabag\Tests\Functional\Controller\Api;
use Craue\ConfigBundle\Util\Config;
class UserRestControllerTest extends WallabagApiTestCase
{
public function testGetUser(): void
{
$this->client->request('GET', '/api/user.json');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('id', $content);
$this->assertArrayHasKey('email', $content);
$this->assertArrayHasKey('name', $content);
$this->assertArrayHasKey('username', $content);
$this->assertArrayHasKey('created_at', $content);
$this->assertArrayHasKey('updated_at', $content);
$this->assertSame('bigboss@wallabag.org', $content['email']);
$this->assertSame('Big boss', $content['name']);
$this->assertSame('admin', $content['username']);
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
}
public function testGetUserWithoutAuthentication(): void
{
$client = $this->createUnauthorizedClient();
$client->request('GET', '/api/user.json');
$this->assertSame(401, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('error', $content);
$this->assertArrayHasKey('error_description', $content);
$this->assertSame('access_denied', $content['error']);
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
}
public function testCreateNewUser(): void
{
$this->client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$this->client->request('PUT', '/api/user.json', [
'username' => 'google',
'password' => 'googlegoogle',
'email' => 'wallabag@google.com',
]);
$this->assertSame(201, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('id', $content);
$this->assertArrayHasKey('email', $content);
$this->assertArrayHasKey('username', $content);
$this->assertArrayHasKey('created_at', $content);
$this->assertArrayHasKey('updated_at', $content);
$this->assertArrayHasKey('default_client', $content);
$this->assertSame('wallabag@google.com', $content['email']);
$this->assertSame('google', $content['username']);
$this->assertArrayHasKey('client_secret', $content['default_client']);
$this->assertArrayHasKey('client_id', $content['default_client']);
$this->assertSame('Default client', $content['default_client']['name']);
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
$this->client->getContainer()->get(Config::class)->set('api_user_registration', '0');
}
public function testCreateNewUserWithoutAuthentication(): void
{
// create a new client instead of using $this->client to be sure client isn't authenticated
$client = $this->createUnauthorizedClient();
$client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$client->request('PUT', '/api/user.json', [
'username' => 'google',
'password' => 'googlegoogle',
'email' => 'wallabag@google.com',
'client_name' => 'My client name !!',
]);
$this->assertSame(201, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('id', $content);
$this->assertArrayHasKey('email', $content);
$this->assertArrayHasKey('username', $content);
$this->assertArrayHasKey('created_at', $content);
$this->assertArrayHasKey('updated_at', $content);
$this->assertArrayHasKey('default_client', $content);
$this->assertSame('wallabag@google.com', $content['email']);
$this->assertSame('google', $content['username']);
$this->assertArrayHasKey('client_secret', $content['default_client']);
$this->assertArrayHasKey('client_id', $content['default_client']);
$this->assertSame('My client name !!', $content['default_client']['name']);
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$client->getContainer()->get(Config::class)->set('api_user_registration', '0');
}
public function testCreateNewUserRemainsCaptchaFreeWhenCaptchaIsEnabled(): void
{
putenv('WALLABAG_CAPTCHA_ENABLED=1');
$_ENV['WALLABAG_CAPTCHA_ENABLED'] = $_SERVER['WALLABAG_CAPTCHA_ENABLED'] = '1';
$client = $this->createUnauthorizedClient();
$config = $client->getContainer()->get(Config::class);
$config->set('api_user_registration', '1');
try {
$client->request('PUT', '/api/user.json', [
'username' => 'captcha-api',
'password' => 'captcha-password',
'email' => 'captcha-api@wallabag.org',
]);
$this->assertSame(201, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('id', $content);
$this->assertArrayHasKey('email', $content);
$this->assertArrayHasKey('username', $content);
$this->assertArrayHasKey('created_at', $content);
$this->assertArrayHasKey('updated_at', $content);
$this->assertArrayHasKey('default_client', $content);
$this->assertArrayNotHasKey('captcha', $content);
$this->assertSame('captcha-api@wallabag.org', $content['email']);
$this->assertSame('captcha-api', $content['username']);
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
} finally {
$config->set('api_user_registration', '0');
putenv('WALLABAG_CAPTCHA_ENABLED=0');
$_ENV['WALLABAG_CAPTCHA_ENABLED'] = $_SERVER['WALLABAG_CAPTCHA_ENABLED'] = '0';
}
}
public function testCreateNewUserWithExistingEmail(): void
{
$client = $this->createUnauthorizedClient();
$client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$client->request('PUT', '/api/user.json', [
'username' => 'admin',
'password' => 'googlegoogle',
'email' => 'bigboss@wallabag.org',
]);
$this->assertSame(400, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('error', $content);
$this->assertArrayHasKey('username', $content['error']);
$this->assertArrayHasKey('email', $content['error']);
// $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]);
// $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]);
// This shouldn't be translated ...
$this->assertSame('This value is already used.', $content['error']['username'][0]);
$this->assertSame('This value is already used.', $content['error']['email'][0]);
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$client->getContainer()->get(Config::class)->set('api_user_registration', '0');
}
public function testCreateNewUserWithTooShortPassword(): void
{
$client = $this->createUnauthorizedClient();
$client->getContainer()->get(Config::class)->set('api_user_registration', '1');
$client->request('PUT', '/api/user.json', [
'username' => 'facebook',
'password' => 'face',
'email' => 'facebook@wallabag.org',
]);
$this->assertSame(400, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('error', $content);
$this->assertArrayHasKey('password', $content['error']);
$this->assertSame('validator.password_too_short', $content['error']['password'][0]);
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
$client->getContainer()->get(Config::class)->set('api_user_registration', '0');
}
public function testCreateNewUserWhenRegistrationIsDisabled(): void
{
$client = $this->createUnauthorizedClient();
$client->request('PUT', '/api/user.json', [
'username' => 'facebook',
'password' => 'face',
'email' => 'facebook@wallabag.org',
]);
$this->assertSame(403, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('error', $content);
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
}
}