diff options
Diffstat (limited to 'tests/Wallabag/ApiBundle')
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 @@ | |||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | 3 | namespace Tests\Wallabag\ApiBundle\Controller; |
4 | 4 | ||
5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | 5 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; |
6 | use Wallabag\ApiBundle\Entity\Client; | ||
6 | 7 | ||
7 | class DeveloperControllerTest extends WallabagCoreTestCase | 8 | class 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 bf7d373a..1ecd03fb 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php | |||
@@ -3,8 +3,10 @@ | |||
3 | namespace Tests\Wallabag\ApiBundle\Controller; | 3 | namespace Tests\Wallabag\ApiBundle\Controller; |
4 | 4 | ||
5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; | 5 | use Tests\Wallabag\ApiBundle\WallabagApiTestCase; |
6 | use Wallabag\CoreBundle\Entity\Entry; | ||
6 | use Wallabag\CoreBundle\Entity\Tag; | 7 | use Wallabag\CoreBundle\Entity\Tag; |
7 | use Wallabag\CoreBundle\Helper\ContentProxy; | 8 | use Wallabag\CoreBundle\Helper\ContentProxy; |
9 | use Wallabag\UserBundle\Entity\User; | ||
8 | 10 | ||
9 | class EntryRestControllerTest extends WallabagApiTestCase | 11 | class EntryRestControllerTest extends WallabagApiTestCase |
10 | { | 12 | { |
@@ -801,22 +803,28 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
801 | 803 | ||
802 | public function testDeleteEntriesTagsListAction() | 804 | public function testDeleteEntriesTagsListAction() |
803 | { | 805 | { |
804 | $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') | 806 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); |
805 | ->getRepository('WallabagCoreBundle:Entry') | 807 | $entry = new Entry($em->getReference(User::class, 1)); |
806 | ->findByUrlAndUserId('http://0.0.0.0/entry4', 1); | 808 | $entry->setUrl('http://0.0.0.0/test-entry'); |
809 | $entry->addTag((new Tag())->setLabel('foo-tag')); | ||
810 | $entry->addTag((new Tag())->setLabel('bar-tag')); | ||
811 | $em->persist($entry); | ||
812 | $em->flush(); | ||
807 | 813 | ||
808 | $tags = $entry->getTags(); | 814 | $em->clear(); |
809 | |||
810 | $this->assertCount(4, $tags); | ||
811 | 815 | ||
812 | $list = [ | 816 | $list = [ |
813 | [ | 817 | [ |
814 | 'url' => 'http://0.0.0.0/entry4', | 818 | 'url' => 'http://0.0.0.0/test-entry', |
815 | 'tags' => 'new tag 1, new tag 2', | 819 | 'tags' => 'foo-tag, bar-tag', |
816 | ], | 820 | ], |
817 | ]; | 821 | ]; |
818 | 822 | ||
819 | $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); | 823 | $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); |
824 | $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); | ||
825 | |||
826 | $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId()); | ||
827 | $this->assertCount(0, $entry->getTags()); | ||
820 | } | 828 | } |
821 | 829 | ||
822 | public function testPostEntriesListAction() | 830 | public function testPostEntriesListAction() |
@@ -841,9 +849,14 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
841 | 849 | ||
842 | public function testDeleteEntriesListAction() | 850 | public function testDeleteEntriesListAction() |
843 | { | 851 | { |
852 | $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); | ||
853 | $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1')); | ||
854 | |||
855 | $em->flush(); | ||
856 | $em->clear(); | ||
844 | $list = [ | 857 | $list = [ |
845 | 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', | 858 | 'http://0.0.0.0/test-entry1', |
846 | 'http://0.0.0.0/entry3', | 859 | 'http://0.0.0.0/test-entry-not-exist', |
847 | ]; | 860 | ]; |
848 | 861 | ||
849 | $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); | 862 | $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); |
@@ -853,10 +866,10 @@ class EntryRestControllerTest extends WallabagApiTestCase | |||
853 | $content = json_decode($this->client->getResponse()->getContent(), true); | 866 | $content = json_decode($this->client->getResponse()->getContent(), true); |
854 | 867 | ||
855 | $this->assertTrue($content[0]['entry']); | 868 | $this->assertTrue($content[0]['entry']); |
856 | $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']); | 869 | $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']); |
857 | 870 | ||
858 | $this->assertFalse($content[1]['entry']); | 871 | $this->assertFalse($content[1]['entry']); |
859 | $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']); | 872 | $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']); |
860 | } | 873 | } |
861 | 874 | ||
862 | public function testLimitBulkAction() | 875 | 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; | |||
8 | abstract class WallabagApiTestCase extends WebTestCase | 8 | abstract 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 | { |