Move to Symfony Flex

The structure changed completely.
Bundles are gone. Everything is flatten in the folder `src`.
I separated import & api controllers to avoid _pollution_ in the main controller folder.
This commit is contained in:
Jeremy Benoist
2022-12-20 22:36:02 +01:00
parent 911e0238b7
commit 6b5a518ce2
629 changed files with 7238 additions and 2194 deletions

View File

@ -0,0 +1,47 @@
<?php
namespace App\Tests\Controller\Api;
use App\Tests\WallabagApiTestCase;
class ConfigRestControllerTest extends WallabagApiTestCase
{
public function testGetConfig()
{
$this->client->request('GET', '/api/config.json');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$config = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('id', $config);
$this->assertArrayHasKey('items_per_page', $config);
$this->assertArrayHasKey('language', $config);
$this->assertArrayHasKey('reading_speed', $config);
$this->assertArrayHasKey('action_mark_as_read', $config);
$this->assertArrayHasKey('list_mode', $config);
$this->assertArrayHasKey('display_thumbnails', $config);
$this->assertSame(200.0, $config['reading_speed']);
$this->assertSame('en', $config['language']);
$this->assertCount(7, $config);
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
}
public function testGetConfigWithoutAuthentication()
{
$client = static::createClient();
$client->request('GET', '/api/config.json');
$this->assertSame(401, $client->getResponse()->getStatusCode());
$config = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('error', $config);
$this->assertArrayHasKey('error_description', $config);
$this->assertSame('access_denied', $config['error']);
$this->assertSame('application/json', $client->getResponse()->headers->get('Content-Type'));
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,69 @@
<?php
namespace App\Tests\Controller\Api;
use App\Tests\WallabagApiTestCase;
class SearchRestControllerTest extends WallabagApiTestCase
{
public function testGetSearchWithFullOptions()
{
$this->client->request('GET', '/api/search', [
'page' => 1,
'perPage' => 2,
'term' => 'entry', // 6 results
]);
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertGreaterThanOrEqual(1, \count($content));
$this->assertArrayHasKey('items', $content['_embedded']);
$this->assertGreaterThanOrEqual(0, $content['total']);
$this->assertSame(1, $content['page']);
$this->assertSame(2, $content['limit']);
$this->assertGreaterThanOrEqual(1, $content['pages']);
$this->assertArrayHasKey('_links', $content);
$this->assertArrayHasKey('self', $content['_links']);
$this->assertArrayHasKey('first', $content['_links']);
$this->assertArrayHasKey('last', $content['_links']);
foreach (['self', 'first', 'last'] as $link) {
$this->assertArrayHasKey('href', $content['_links'][$link]);
$this->assertStringContainsString('term=entry', $content['_links'][$link]['href']);
}
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
}
public function testGetSearchWithNoLimit()
{
$this->client->request('GET', '/api/search', [
'term' => 'entry',
]);
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertGreaterThanOrEqual(1, \count($content));
$this->assertArrayHasKey('items', $content['_embedded']);
$this->assertGreaterThanOrEqual(0, $content['total']);
$this->assertSame(1, $content['page']);
$this->assertGreaterThanOrEqual(1, $content['pages']);
$this->assertArrayHasKey('_links', $content);
$this->assertArrayHasKey('self', $content['_links']);
$this->assertArrayHasKey('first', $content['_links']);
$this->assertArrayHasKey('last', $content['_links']);
foreach (['self', 'first', 'last'] as $link) {
$this->assertArrayHasKey('href', $content['_links'][$link]);
$this->assertStringContainsString('term=entry', $content['_links'][$link]['href']);
}
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
}
}

View File

@ -0,0 +1,226 @@
<?php
namespace App\Tests\Controller\Api;
use App\Entity\Entry;
use App\Entity\Tag;
use App\Tests\WallabagApiTestCase;
use Doctrine\ORM\EntityManagerInterface;
class TagRestControllerTest extends WallabagApiTestCase
{
private $otherUserTagLabel = 'bob';
public function testGetUserTags()
{
$this->client->request('GET', '/api/tags.json');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertGreaterThan(0, $content);
$this->assertArrayHasKey('id', $content[0]);
$this->assertArrayHasKey('label', $content[0]);
$this->assertArrayHasKey('nbEntries', $content[0]);
$tagLabels = array_map(function ($i) {
return $i['label'];
}, $content);
$this->assertNotContains($this->otherUserTagLabel, $tagLabels, 'There is a possible tag leak');
}
public function testDeleteUserTag()
{
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
$entry = $this->client->getContainer()
->get(EntityManagerInterface::class)
->getRepository(Entry::class)
->findOneWithTags($this->user->getId());
$entry = $entry[0];
$tagLabel = 'tagtest';
$tag = new Tag();
$tag->setLabel($tagLabel);
$em->persist($tag);
$entry->addTag($tag);
$em->persist($entry);
$em->flush();
$em->clear();
$this->client->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('label', $content);
$this->assertSame($tag->getLabel(), $content['label']);
$this->assertSame($tag->getSlug(), $content['slug']);
$entries = $em->getRepository(Entry::class)
->findAllByTagId($this->user->getId(), $tag->getId());
$this->assertCount(0, $entries);
$tag = $em->getRepository(Tag::class)->findOneByLabel($tagLabel);
$this->assertNull($tag, $tagLabel . ' was removed because it begun an orphan tag');
}
public function testDeleteOtherUserTag()
{
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
$tag = $em->getRepository(Tag::class)->findOneByLabel($this->otherUserTagLabel);
$this->client->request('DELETE', '/api/tags/' . $tag->getId() . '.json');
$this->assertSame(404, $this->client->getResponse()->getStatusCode());
}
public function dataForDeletingTagByLabel()
{
return [
'by_query' => [true],
'by_body' => [false],
];
}
/**
* @dataProvider dataForDeletingTagByLabel
*/
public function testDeleteTagByLabel($useQueryString)
{
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
$entry = $this->client->getContainer()
->get(EntityManagerInterface::class)
->getRepository(Entry::class)
->findOneWithTags($this->user->getId());
$entry = $entry[0];
$tag = new Tag();
$tag->setLabel('Awesome tag for test');
$em->persist($tag);
$entry->addTag($tag);
$em->persist($entry);
$em->flush();
if ($useQueryString) {
$this->client->request('DELETE', '/api/tag/label.json?tag=' . $tag->getLabel());
} else {
$this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
}
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('label', $content);
$this->assertSame($tag->getLabel(), $content['label']);
$this->assertSame($tag->getSlug(), $content['slug']);
$entries = $this->client->getContainer()
->get(EntityManagerInterface::class)
->getRepository(Entry::class)
->findAllByTagId($this->user->getId(), $tag->getId());
$this->assertCount(0, $entries);
}
public function testDeleteTagByLabelNotFound()
{
$this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
$this->assertSame(404, $this->client->getResponse()->getStatusCode());
}
public function testDeleteTagByLabelOtherUser()
{
$this->client->request('DELETE', '/api/tag/label.json', ['tag' => $this->otherUserTagLabel]);
$this->assertSame(404, $this->client->getResponse()->getStatusCode());
}
/**
* @dataProvider dataForDeletingTagByLabel
*/
public function testDeleteTagsByLabel($useQueryString)
{
$em = $this->client->getContainer()->get(EntityManagerInterface::class);
$entry = $this->client->getContainer()
->get(EntityManagerInterface::class)
->getRepository(Entry::class)
->findOneWithTags($this->user->getId());
$entry = $entry[0];
$tag = new Tag();
$tag->setLabel('Awesome tag for tagsLabel');
$em->persist($tag);
$tag2 = new Tag();
$tag2->setLabel('Awesome tag for tagsLabel 2');
$em->persist($tag2);
$entry->addTag($tag);
$entry->addTag($tag2);
$em->persist($entry);
$em->flush();
if ($useQueryString) {
$this->client->request('DELETE', '/api/tags/label.json?tags=' . $tag->getLabel() . ',' . $tag2->getLabel());
} else {
$this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel() . ',' . $tag2->getLabel()]);
}
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertCount(2, $content);
$this->assertArrayHasKey('label', $content[0]);
$this->assertSame($tag->getLabel(), $content[0]['label']);
$this->assertSame($tag->getSlug(), $content[0]['slug']);
$this->assertArrayHasKey('label', $content[1]);
$this->assertSame($tag2->getLabel(), $content[1]['label']);
$this->assertSame($tag2->getSlug(), $content[1]['slug']);
$entries = $this->client->getContainer()
->get(EntityManagerInterface::class)
->getRepository(Entry::class)
->findAllByTagId($this->user->getId(), $tag->getId());
$this->assertCount(0, $entries);
$entries = $this->client->getContainer()
->get(EntityManagerInterface::class)
->getRepository(Entry::class)
->findAllByTagId($this->user->getId(), $tag2->getId());
$this->assertCount(0, $entries);
}
public function testDeleteTagsByLabelNotFound()
{
$this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
$this->assertSame(404, $this->client->getResponse()->getStatusCode());
}
public function testDeleteTagsByLabelOtherUser()
{
$this->client->request('DELETE', '/api/tags/label.json', ['tags' => $this->otherUserTagLabel]);
$this->assertSame(404, $this->client->getResponse()->getStatusCode());
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace App\Tests\Controller\Api;
use App\Tests\WallabagApiTestCase;
class TaggingRuleRestControllerTest extends WallabagApiTestCase
{
public function testExportEntry()
{
$this->client->request('GET', '/api/taggingrule/export');
$this->assertSame(200, $this->client->getResponse()->getStatusCode());
$this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type'));
}
}

View File

@ -0,0 +1,186 @@
<?php
namespace App\Tests\Controller\Api;
use App\Tests\WallabagApiTestCase;
use Craue\ConfigBundle\Util\Config;
class UserRestControllerTest extends WallabagApiTestCase
{
public function testGetUser()
{
$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()
{
$client = static::createClient();
$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()
{
$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()
{
// create a new client instead of using $this->client to be sure client isn't authenticated
$client = static::createClient();
$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 testCreateNewUserWithExistingEmail()
{
$client = static::createClient();
$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()
{
$client = static::createClient();
$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()
{
$client = static::createClient();
$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'));
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace App\Tests\Controller\Api;
use Craue\ConfigBundle\Util\Config;
use App\Tests\WallabagApiTestCase;
class WallabagRestControllerTest extends WallabagApiTestCase
{
public function testGetVersion()
{
// create a new client instead of using $this->client to be sure client isn't authenticated
$client = static::createClient();
$client->request('GET', '/api/version');
$this->assertSame(200, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertSame($client->getContainer()->getParameter('wallabag.version'), $content);
}
public function testGetInfo()
{
// create a new client instead of using $this->client to be sure client isn't authenticated
$client = static::createClient();
$client->request('GET', '/api/info');
$this->assertSame(200, $client->getResponse()->getStatusCode());
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertArrayHasKey('appname', $content);
$this->assertArrayHasKey('version', $content);
$this->assertArrayHasKey('allowed_registration', $content);
$this->assertSame('wallabag', $content['appname']);
}
public function testAllowedRegistration()
{
// create a new client instead of using $this->client to be sure client isn't authenticated
$client = static::createClient();
if (!$client->getContainer()->getParameter('fosuser_registration')) {
$this->markTestSkipped('fosuser_registration is not enabled.');
return;
}
$client->getContainer()->get(Config::class)->set('api_user_registration', 1);
$client->request('GET', '/api/info');
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertTrue($content['allowed_registration']);
$client->getContainer()->get(Config::class)->set('api_user_registration', 0);
$client->request('GET', '/api/info');
$content = json_decode($client->getResponse()->getContent(), true);
$this->assertFalse($content['allowed_registration']);
}
}