aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ApiBundle
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2017-06-01 07:58:17 +0200
committerGitHub <noreply@github.com>2017-06-01 07:58:17 +0200
commit2a0eec07a5630401a9ceb7add65604f79238f10c (patch)
treebab2f2f763d4dca9f273a8ec1848f5da6c03ed20 /tests/Wallabag/ApiBundle
parent757ec837bea7f57dcefb71b3b90c66299ed6a922 (diff)
parent7ab5eb9508921d84b4b4ec84a59135d536da748e (diff)
downloadwallabag-2a0eec07a5630401a9ceb7add65604f79238f10c.tar.gz
wallabag-2a0eec07a5630401a9ceb7add65604f79238f10c.tar.zst
wallabag-2a0eec07a5630401a9ceb7add65604f79238f10c.zip
Merge pull request #3137 from aaa2000/isolated-tests
Isolated tests
Diffstat (limited to 'tests/Wallabag/ApiBundle')
-rw-r--r--tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php40
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php37
-rw-r--r--tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php33
-rw-r--r--tests/Wallabag/ApiBundle/WallabagApiTestCase.php4
4 files changed, 71 insertions, 43 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
index 6659443b..d37cbbf9 100644
--- a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
@@ -3,6 +3,7 @@
3namespace Tests\Wallabag\ApiBundle\Controller; 3namespace Tests\Wallabag\ApiBundle\Controller;
4 4
5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; 5use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
6use Wallabag\ApiBundle\Entity\Client;
6 7
7class DeveloperControllerTest extends WallabagCoreTestCase 8class DeveloperControllerTest extends WallabagCoreTestCase
8{ 9{
@@ -33,14 +34,10 @@ class DeveloperControllerTest extends WallabagCoreTestCase
33 $this->assertContains('My app', $alert[0]); 34 $this->assertContains('My app', $alert[0]);
34 } 35 }
35 36
36 /**
37 * @depends testCreateClient
38 */
39 public function testCreateToken() 37 public function testCreateToken()
40 { 38 {
41 $client = $this->getClient(); 39 $client = $this->getClient();
42 $em = $client->getContainer()->get('doctrine.orm.entity_manager'); 40 $apiClient = $this->createApiClientForUser('admin');
43 $apiClient = $em->getRepository('WallabagApiBundle:Client')->findOneByName('My app');
44 41
45 $client->request('POST', '/oauth/v2/token', [ 42 $client->request('POST', '/oauth/v2/token', [
46 'grant_type' => 'password', 43 'grant_type' => 'password',
@@ -83,6 +80,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
83 public function testRemoveClient() 80 public function testRemoveClient()
84 { 81 {
85 $client = $this->getClient(); 82 $client = $this->getClient();
83 $adminApiClient = $this->createApiClientForUser('admin');
86 $em = $client->getContainer()->get('doctrine.orm.entity_manager'); 84 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
87 85
88 // Try to remove an admin's client with a wrong user 86 // Try to remove an admin's client with a wrong user
@@ -90,12 +88,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase
90 $client->request('GET', '/developer'); 88 $client->request('GET', '/developer');
91 $this->assertContains('no_client', $client->getResponse()->getContent()); 89 $this->assertContains('no_client', $client->getResponse()->getContent());
92 90
93 // get an ID of a admin's client
94 $this->logInAs('admin');
95 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId());
96
97 $this->logInAs('bob'); 91 $this->logInAs('bob');
98 $client->request('GET', '/developer/client/delete/'.$nbClients[0]->getId()); 92 $client->request('GET', '/developer/client/delete/'.$adminApiClient->getId());
99 $this->assertEquals(403, $client->getResponse()->getStatusCode()); 93 $this->assertEquals(403, $client->getResponse()->getStatusCode());
100 94
101 // Try to remove the admin's client with the good user 95 // Try to remove the admin's client with the good user
@@ -111,7 +105,29 @@ class DeveloperControllerTest extends WallabagCoreTestCase
111 $client->click($link); 105 $client->click($link);
112 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 106 $this->assertEquals(302, $client->getResponse()->getStatusCode());
113 107
114 $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId()); 108 $this->assertNull(
115 $this->assertGreaterThan(count($newNbClients), count($nbClients)); 109 $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()),
110 'The client should have been removed'
111 );
112 }
113
114 /**
115 * @param string $username
116 *
117 * @return Client
118 */
119 private function createApiClientForUser($username)
120 {
121 $client = $this->getClient();
122 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
123 $userManager = $client->getContainer()->get('fos_user.user_manager');
124 $user = $userManager->findUserBy(array('username' => $username));
125 $apiClient = new Client($user);
126 $apiClient->setName('My app');
127 $apiClient->setAllowedGrantTypes(['password']);
128 $em->persist($apiClient);
129 $em->flush();
130
131 return $apiClient;
116 } 132 }
117} 133}
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index e9ba4634..0968cfaf 100644
--- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
@@ -3,8 +3,10 @@
3namespace Tests\Wallabag\ApiBundle\Controller; 3namespace Tests\Wallabag\ApiBundle\Controller;
4 4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase; 5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6use Wallabag\CoreBundle\Entity\Entry;
6use Wallabag\CoreBundle\Entity\Tag; 7use Wallabag\CoreBundle\Entity\Tag;
7use Wallabag\CoreBundle\Helper\ContentProxy; 8use Wallabag\CoreBundle\Helper\ContentProxy;
9use Wallabag\UserBundle\Entity\User;
8 10
9class EntryRestControllerTest extends WallabagApiTestCase 11class EntryRestControllerTest extends WallabagApiTestCase
10{ 12{
@@ -811,22 +813,28 @@ class EntryRestControllerTest extends WallabagApiTestCase
811 813
812 public function testDeleteEntriesTagsListAction() 814 public function testDeleteEntriesTagsListAction()
813 { 815 {
814 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') 816 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
815 ->getRepository('WallabagCoreBundle:Entry') 817 $entry = new Entry($em->getReference(User::class, 1));
816 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); 818 $entry->setUrl('http://0.0.0.0/test-entry');
819 $entry->addTag((new Tag())->setLabel('foo-tag'));
820 $entry->addTag((new Tag())->setLabel('bar-tag'));
821 $em->persist($entry);
822 $em->flush();
817 823
818 $tags = $entry->getTags(); 824 $em->clear();
819
820 $this->assertCount(4, $tags);
821 825
822 $list = [ 826 $list = [
823 [ 827 [
824 'url' => 'http://0.0.0.0/entry4', 828 'url' => 'http://0.0.0.0/test-entry',
825 'tags' => 'new tag 1, new tag 2', 829 'tags' => 'foo-tag, bar-tag',
826 ], 830 ],
827 ]; 831 ];
828 832
829 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); 833 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
834 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
835
836 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
837 $this->assertCount(0, $entry->getTags());
830 } 838 }
831 839
832 public function testPostEntriesListAction() 840 public function testPostEntriesListAction()
@@ -851,9 +859,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
851 859
852 public function testDeleteEntriesListAction() 860 public function testDeleteEntriesListAction()
853 { 861 {
862 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
863 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
864
865 $em->flush();
866 $em->clear();
854 $list = [ 867 $list = [
855 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', 868 'http://0.0.0.0/test-entry1',
856 'http://0.0.0.0/entry3', 869 'http://0.0.0.0/test-entry-not-exist',
857 ]; 870 ];
858 871
859 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); 872 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
@@ -863,10 +876,10 @@ class EntryRestControllerTest extends WallabagApiTestCase
863 $content = json_decode($this->client->getResponse()->getContent(), true); 876 $content = json_decode($this->client->getResponse()->getContent(), true);
864 877
865 $this->assertTrue($content[0]['entry']); 878 $this->assertTrue($content[0]['entry']);
866 $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']); 879 $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
867 880
868 $this->assertFalse($content[1]['entry']); 881 $this->assertFalse($content[1]['entry']);
869 $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); 882 $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
870 } 883 }
871 884
872 public function testLimitBulkAction() 885 public function testLimitBulkAction()
diff --git a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
index 90b132eb..7f69bd67 100644
--- a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
@@ -22,36 +22,35 @@ class TagRestControllerTest extends WallabagApiTestCase
22 return end($content); 22 return end($content);
23 } 23 }
24 24
25 /** 25 public function testDeleteUserTag()
26 * @depends testGetUserTags
27 */
28 public function testDeleteUserTag($tag)
29 { 26 {
30 $tagName = $tag['label']; 27 $tagLabel = 'tagtest';
28 $tag = new Tag();
29 $tag->setLabel($tagLabel);
30
31 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
32 $em->persist($tag);
33 $em->flush();
34 $em->clear();
31 35
32 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); 36 $this->client->request('DELETE', '/api/tags/'.$tag->getId().'.json');
33 37
34 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 38 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
35 39
36 $content = json_decode($this->client->getResponse()->getContent(), true); 40 $content = json_decode($this->client->getResponse()->getContent(), true);
37 41
38 $this->assertArrayHasKey('label', $content); 42 $this->assertArrayHasKey('label', $content);
39 $this->assertEquals($tag['label'], $content['label']); 43 $this->assertEquals($tag->getLabel(), $content['label']);
40 $this->assertEquals($tag['slug'], $content['slug']); 44 $this->assertEquals($tag->getSlug(), $content['slug']);
41 45
42 $entries = $this->client->getContainer() 46 $entries = $em->getRepository('WallabagCoreBundle:Entry')
43 ->get('doctrine.orm.entity_manager') 47 ->findAllByTagId($this->user->getId(), $tag->getId());
44 ->getRepository('WallabagCoreBundle:Entry')
45 ->findAllByTagId($this->user->getId(), $tag['id']);
46 48
47 $this->assertCount(0, $entries); 49 $this->assertCount(0, $entries);
48 50
49 $tag = $this->client->getContainer() 51 $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
50 ->get('doctrine.orm.entity_manager')
51 ->getRepository('WallabagCoreBundle:Tag')
52 ->findOneByLabel($tagName);
53 52
54 $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag'); 53 $this->assertNull($tag, $tagLabel.' was removed because it begun an orphan tag');
55 } 54 }
56 55
57 public function dataForDeletingTagByLabel() 56 public function dataForDeletingTagByLabel()
diff --git a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php
index a67655c8..8709da70 100644
--- a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php
+++ b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php
@@ -8,7 +8,7 @@ use Symfony\Component\BrowserKit\Cookie;
8abstract class WallabagApiTestCase extends WebTestCase 8abstract class WallabagApiTestCase extends WebTestCase
9{ 9{
10 /** 10 /**
11 * @var Client 11 * @var \Symfony\Bundle\FrameworkBundle\Client
12 */ 12 */
13 protected $client = null; 13 protected $client = null;
14 14
@@ -23,7 +23,7 @@ abstract class WallabagApiTestCase extends WebTestCase
23 } 23 }
24 24
25 /** 25 /**
26 * @return Client 26 * @return \Symfony\Bundle\FrameworkBundle\Client
27 */ 27 */
28 protected function createAuthorizedClient() 28 protected function createAuthorizedClient()
29 { 29 {