2017-05-30 05:56:01 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Tests\Wallabag\Controller\Api;
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
use Craue\ConfigBundle\Util\Config;
|
2017-05-30 05:56:01 +00:00
|
|
|
|
|
|
|
class UserRestControllerTest extends WallabagApiTestCase
|
|
|
|
{
|
|
|
|
public function testGetUser()
|
|
|
|
{
|
|
|
|
$this->client->request('GET', '/api/user.json');
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
|
2017-05-30 05:56:01 +00:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('bigboss@wallabag.org', $content['email']);
|
|
|
|
$this->assertSame('Big boss', $content['name']);
|
|
|
|
$this->assertSame('admin', $content['username']);
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
|
2017-05-30 05:56:01 +00:00
|
|
|
}
|
|
|
|
|
2017-06-02 08:19:33 +00:00
|
|
|
public function testGetUserWithoutAuthentication()
|
|
|
|
{
|
2023-12-24 19:37:54 +00:00
|
|
|
$client = $this->createUnauthorizedClient();
|
2017-06-02 08:19:33 +00:00
|
|
|
$client->request('GET', '/api/user.json');
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame(401, $client->getResponse()->getStatusCode());
|
2017-06-02 08:19:33 +00:00
|
|
|
|
|
|
|
$content = json_decode($client->getResponse()->getContent(), true);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('error', $content);
|
|
|
|
$this->assertArrayHasKey('error_description', $content);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('access_denied', $content['error']);
|
2017-06-02 08:19:33 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
|
2017-06-02 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
public function testCreateNewUser()
|
|
|
|
{
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->client->getContainer()->get(Config::class)->set('api_user_registration', 1);
|
2017-05-30 05:56:01 +00:00
|
|
|
$this->client->request('PUT', '/api/user.json', [
|
|
|
|
'username' => 'google',
|
|
|
|
'password' => 'googlegoogle',
|
|
|
|
'email' => 'wallabag@google.com',
|
|
|
|
]);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame(201, $this->client->getResponse()->getStatusCode());
|
2017-05-30 05:56:01 +00:00
|
|
|
|
|
|
|
$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);
|
2017-06-07 21:23:28 +00:00
|
|
|
$this->assertArrayHasKey('default_client', $content);
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('wallabag@google.com', $content['email']);
|
|
|
|
$this->assertSame('google', $content['username']);
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-06-07 21:23:28 +00:00
|
|
|
$this->assertArrayHasKey('client_secret', $content['default_client']);
|
|
|
|
$this->assertArrayHasKey('client_id', $content['default_client']);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('Default client', $content['default_client']['name']);
|
2017-06-07 21:23:28 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
|
2017-05-30 07:57:57 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->client->getContainer()->get(Config::class)->set('api_user_registration', 0);
|
2017-06-02 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateNewUserWithoutAuthentication()
|
|
|
|
{
|
|
|
|
// create a new client instead of using $this->client to be sure client isn't authenticated
|
2023-12-24 19:37:54 +00:00
|
|
|
$client = $this->createUnauthorizedClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$client->getContainer()->get(Config::class)->set('api_user_registration', 1);
|
2017-06-02 08:19:33 +00:00
|
|
|
$client->request('PUT', '/api/user.json', [
|
|
|
|
'username' => 'google',
|
|
|
|
'password' => 'googlegoogle',
|
|
|
|
'email' => 'wallabag@google.com',
|
2017-06-08 12:25:44 +00:00
|
|
|
'client_name' => 'My client name !!',
|
2017-06-02 08:19:33 +00:00
|
|
|
]);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame(201, $client->getResponse()->getStatusCode());
|
2017-06-02 08:19:33 +00:00
|
|
|
|
|
|
|
$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);
|
2017-06-07 21:23:28 +00:00
|
|
|
$this->assertArrayHasKey('default_client', $content);
|
2017-06-02 08:19:33 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('wallabag@google.com', $content['email']);
|
|
|
|
$this->assertSame('google', $content['username']);
|
2017-05-30 07:57:57 +00:00
|
|
|
|
2017-06-07 21:23:28 +00:00
|
|
|
$this->assertArrayHasKey('client_secret', $content['default_client']);
|
|
|
|
$this->assertArrayHasKey('client_id', $content['default_client']);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('My client name !!', $content['default_client']['name']);
|
2017-06-07 21:23:28 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
|
2017-05-30 07:57:57 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$client->getContainer()->get(Config::class)->set('api_user_registration', 0);
|
2017-05-30 05:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateNewUserWithExistingEmail()
|
|
|
|
{
|
2023-12-24 19:37:54 +00:00
|
|
|
$client = $this->createUnauthorizedClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$client->getContainer()->get(Config::class)->set('api_user_registration', 1);
|
2017-06-02 08:19:33 +00:00
|
|
|
$client->request('PUT', '/api/user.json', [
|
2017-05-30 07:57:57 +00:00
|
|
|
'username' => 'admin',
|
2017-05-30 05:56:01 +00:00
|
|
|
'password' => 'googlegoogle',
|
|
|
|
'email' => 'bigboss@wallabag.org',
|
|
|
|
]);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame(400, $client->getResponse()->getStatusCode());
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-06-02 08:19:33 +00:00
|
|
|
$content = json_decode($client->getResponse()->getContent(), true);
|
2017-05-30 05:56:01 +00:00
|
|
|
|
|
|
|
$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 ...
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('This value is already used.', $content['error']['username'][0]);
|
|
|
|
$this->assertSame('This value is already used.', $content['error']['email'][0]);
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
|
2017-06-02 08:19:33 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$client->getContainer()->get(Config::class)->set('api_user_registration', 0);
|
2017-05-30 05:56:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateNewUserWithTooShortPassword()
|
|
|
|
{
|
2023-12-24 19:37:54 +00:00
|
|
|
$client = $this->createUnauthorizedClient();
|
2022-08-28 00:01:46 +00:00
|
|
|
$client->getContainer()->get(Config::class)->set('api_user_registration', 1);
|
2017-06-02 08:19:33 +00:00
|
|
|
$client->request('PUT', '/api/user.json', [
|
2017-05-30 05:56:01 +00:00
|
|
|
'username' => 'facebook',
|
|
|
|
'password' => 'face',
|
|
|
|
'email' => 'facebook@wallabag.org',
|
|
|
|
]);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame(400, $client->getResponse()->getStatusCode());
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-06-02 08:19:33 +00:00
|
|
|
$content = json_decode($client->getResponse()->getContent(), true);
|
2017-05-30 05:56:01 +00:00
|
|
|
|
|
|
|
$this->assertArrayHasKey('error', $content);
|
|
|
|
$this->assertArrayHasKey('password', $content['error']);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('validator.password_too_short', $content['error']['password'][0]);
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
|
2017-06-02 08:19:33 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$client->getContainer()->get(Config::class)->set('api_user_registration', 0);
|
2017-06-02 08:19:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreateNewUserWhenRegistrationIsDisabled()
|
|
|
|
{
|
2023-12-24 19:37:54 +00:00
|
|
|
$client = $this->createUnauthorizedClient();
|
2017-06-02 08:19:33 +00:00
|
|
|
$client->request('PUT', '/api/user.json', [
|
|
|
|
'username' => 'facebook',
|
|
|
|
'password' => 'face',
|
|
|
|
'email' => 'facebook@wallabag.org',
|
|
|
|
]);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame(403, $client->getResponse()->getStatusCode());
|
2017-06-02 08:19:33 +00:00
|
|
|
|
|
|
|
$content = json_decode($client->getResponse()->getContent(), true);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('error', $content);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
|
2017-05-30 05:56:01 +00:00
|
|
|
}
|
|
|
|
}
|