aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ApiBundle
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/ApiBundle')
-rw-r--r--tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php63
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php212
-rw-r--r--tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php63
-rw-r--r--tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php185
-rw-r--r--tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php10
-rw-r--r--tests/Wallabag/ApiBundle/WallabagApiTestCase.php6
6 files changed, 493 insertions, 46 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
index 6659443b..53aed12b 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 public function testCreateTokenFromPasswords()
37 * @depends testCreateClient
38 */
39 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',
@@ -59,6 +56,26 @@ class DeveloperControllerTest extends WallabagCoreTestCase
59 $this->assertArrayHasKey('refresh_token', $data); 56 $this->assertArrayHasKey('refresh_token', $data);
60 } 57 }
61 58
59 public function testCreateTokenFromClientCredentialsOnly()
60 {
61 $client = $this->getClient();
62 $apiClient = $this->createApiClientForUser('admin', ['client_credentials']);
63
64 $client->request('POST', '/oauth/v2/token', [
65 'grant_type' => 'client_credentials',
66 'client_id' => $apiClient->getPublicId(),
67 'client_secret' => $apiClient->getSecret(),
68 ]);
69
70 $this->assertEquals(200, $client->getResponse()->getStatusCode());
71
72 $data = json_decode($client->getResponse()->getContent(), true);
73 $this->assertArrayHasKey('access_token', $data);
74 $this->assertArrayHasKey('expires_in', $data);
75 $this->assertArrayHasKey('token_type', $data);
76 // Client Credentials created-clients have no refresh tokens
77 }
78
62 public function testListingClient() 79 public function testListingClient()
63 { 80 {
64 $this->logInAs('admin'); 81 $this->logInAs('admin');
@@ -83,6 +100,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
83 public function testRemoveClient() 100 public function testRemoveClient()
84 { 101 {
85 $client = $this->getClient(); 102 $client = $this->getClient();
103 $adminApiClient = $this->createApiClientForUser('admin');
86 $em = $client->getContainer()->get('doctrine.orm.entity_manager'); 104 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
87 105
88 // Try to remove an admin's client with a wrong user 106 // Try to remove an admin's client with a wrong user
@@ -90,12 +108,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase
90 $client->request('GET', '/developer'); 108 $client->request('GET', '/developer');
91 $this->assertContains('no_client', $client->getResponse()->getContent()); 109 $this->assertContains('no_client', $client->getResponse()->getContent());
92 110
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'); 111 $this->logInAs('bob');
98 $client->request('GET', '/developer/client/delete/'.$nbClients[0]->getId()); 112 $client->request('GET', '/developer/client/delete/'.$adminApiClient->getId());
99 $this->assertEquals(403, $client->getResponse()->getStatusCode()); 113 $this->assertEquals(403, $client->getResponse()->getStatusCode());
100 114
101 // Try to remove the admin's client with the good user 115 // Try to remove the admin's client with the good user
@@ -111,7 +125,30 @@ class DeveloperControllerTest extends WallabagCoreTestCase
111 $client->click($link); 125 $client->click($link);
112 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 126 $this->assertEquals(302, $client->getResponse()->getStatusCode());
113 127
114 $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId()); 128 $this->assertNull(
115 $this->assertGreaterThan(count($newNbClients), count($nbClients)); 129 $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()),
130 'The client should have been removed'
131 );
132 }
133
134 /**
135 * @param string $username
136 *
137 * @param array $grantTypes
138 * @return Client
139 */
140 private function createApiClientForUser($username, $grantTypes = ['password'])
141 {
142 $client = $this->getClient();
143 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
144 $userManager = $client->getContainer()->get('fos_user.user_manager');
145 $user = $userManager->findUserBy(array('username' => $username));
146 $apiClient = new Client($user);
147 $apiClient->setName('My app');
148 $apiClient->setAllowedGrantTypes($grantTypes);
149 $em->persist($apiClient);
150 $em->flush();
151
152 return $apiClient;
116 } 153 }
117} 154}
diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
index 0a65f9ce..067aed2c 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{
@@ -126,6 +128,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
126 'perPage' => 2, 128 'perPage' => 2,
127 'tags' => 'foo', 129 'tags' => 'foo',
128 'since' => 1443274283, 130 'since' => 1443274283,
131 'public' => 0,
129 ]); 132 ]);
130 133
131 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 134 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -152,6 +155,53 @@ class EntryRestControllerTest extends WallabagApiTestCase
152 $this->assertContains('order=asc', $content['_links'][$link]['href']); 155 $this->assertContains('order=asc', $content['_links'][$link]['href']);
153 $this->assertContains('tags=foo', $content['_links'][$link]['href']); 156 $this->assertContains('tags=foo', $content['_links'][$link]['href']);
154 $this->assertContains('since=1443274283', $content['_links'][$link]['href']); 157 $this->assertContains('since=1443274283', $content['_links'][$link]['href']);
158 $this->assertContains('public=0', $content['_links'][$link]['href']);
159 }
160
161 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
162 }
163
164 public function testGetEntriesPublicOnly()
165 {
166 $entry = $this->client->getContainer()
167 ->get('doctrine.orm.entity_manager')
168 ->getRepository('WallabagCoreBundle:Entry')
169 ->findOneByUser(1);
170
171 if (!$entry) {
172 $this->markTestSkipped('No content found in db.');
173 }
174
175 // generate at least one public entry
176 $entry->generateUid();
177
178 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
179 $em->persist($entry);
180 $em->flush();
181
182 $this->client->request('GET', '/api/entries', [
183 'public' => 1,
184 ]);
185
186 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
187
188 $content = json_decode($this->client->getResponse()->getContent(), true);
189
190 $this->assertGreaterThanOrEqual(1, count($content));
191 $this->assertArrayHasKey('items', $content['_embedded']);
192 $this->assertGreaterThanOrEqual(1, $content['total']);
193 $this->assertEquals(1, $content['page']);
194 $this->assertEquals(30, $content['limit']);
195 $this->assertGreaterThanOrEqual(1, $content['pages']);
196
197 $this->assertArrayHasKey('_links', $content);
198 $this->assertArrayHasKey('self', $content['_links']);
199 $this->assertArrayHasKey('first', $content['_links']);
200 $this->assertArrayHasKey('last', $content['_links']);
201
202 foreach (['self', 'first', 'last'] as $link) {
203 $this->assertArrayHasKey('href', $content['_links'][$link]);
204 $this->assertContains('public=1', $content['_links'][$link]['href']);
155 } 205 }
156 206
157 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 207 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
@@ -315,7 +365,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
315 $entry = $this->client->getContainer() 365 $entry = $this->client->getContainer()
316 ->get('doctrine.orm.entity_manager') 366 ->get('doctrine.orm.entity_manager')
317 ->getRepository('WallabagCoreBundle:Entry') 367 ->getRepository('WallabagCoreBundle:Entry')
318 ->findOneByUser(1); 368 ->findOneByUser(1, ['id' => 'asc']);
319 369
320 if (!$entry) { 370 if (!$entry) {
321 $this->markTestSkipped('No content found in db.'); 371 $this->markTestSkipped('No content found in db.');
@@ -342,6 +392,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
342 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', 392 'url' => 'http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html',
343 'tags' => 'google', 393 'tags' => 'google',
344 'title' => 'New title for my article', 394 'title' => 'New title for my article',
395 'content' => 'my content',
396 'language' => 'de',
397 'published_at' => '2016-09-08T11:55:58+0200',
398 'authors' => 'bob,helen',
399 'public' => 1,
345 ]); 400 ]);
346 401
347 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 402 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -354,7 +409,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
354 $this->assertEquals(false, $content['is_starred']); 409 $this->assertEquals(false, $content['is_starred']);
355 $this->assertEquals('New title for my article', $content['title']); 410 $this->assertEquals('New title for my article', $content['title']);
356 $this->assertEquals(1, $content['user_id']); 411 $this->assertEquals(1, $content['user_id']);
357 $this->assertCount(1, $content['tags']); 412 $this->assertCount(2, $content['tags']);
413 $this->assertSame('my content', $content['content']);
414 $this->assertSame('de', $content['language']);
415 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
416 $this->assertCount(2, $content['published_by']);
417 $this->assertContains('bob', $content['published_by']);
418 $this->assertContains('helen', $content['published_by']);
419 $this->assertTrue($content['is_public'], 'A public link has been generated for that entry');
358 } 420 }
359 421
360 public function testPostSameEntry() 422 public function testPostSameEntry()
@@ -373,7 +435,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
373 $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']); 435 $this->assertEquals('http://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html', $content['url']);
374 $this->assertEquals(true, $content['is_archived']); 436 $this->assertEquals(true, $content['is_archived']);
375 $this->assertEquals(false, $content['is_starred']); 437 $this->assertEquals(false, $content['is_starred']);
376 $this->assertCount(2, $content['tags']); 438 $this->assertCount(3, $content['tags']);
377 } 439 }
378 440
379 public function testPostEntryWhenFetchContentFails() 441 public function testPostEntryWhenFetchContentFails()
@@ -465,6 +527,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
465 'tags' => 'new tag '.uniqid(), 527 'tags' => 'new tag '.uniqid(),
466 'starred' => '1', 528 'starred' => '1',
467 'archive' => '0', 529 'archive' => '0',
530 'language' => 'de_AT',
531 'preview_picture' => 'http://preview.io/picture.jpg',
532 'authors' => 'bob,sponge',
533 'content' => 'awesome',
534 'public' => 0,
468 ]); 535 ]);
469 536
470 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 537 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -476,6 +543,12 @@ class EntryRestControllerTest extends WallabagApiTestCase
476 $this->assertEquals('New awesome title', $content['title']); 543 $this->assertEquals('New awesome title', $content['title']);
477 $this->assertGreaterThan($nbTags, count($content['tags'])); 544 $this->assertGreaterThan($nbTags, count($content['tags']));
478 $this->assertEquals(1, $content['user_id']); 545 $this->assertEquals(1, $content['user_id']);
546 $this->assertEquals('de_AT', $content['language']);
547 $this->assertEquals('http://preview.io/picture.jpg', $content['preview_picture']);
548 $this->assertContains('sponge', $content['published_by']);
549 $this->assertContains('bob', $content['published_by']);
550 $this->assertEquals('awesome', $content['content']);
551 $this->assertFalse($content['is_public'], 'Entry is no more shared');
479 } 552 }
480 553
481 public function testPatchEntryWithoutQuotes() 554 public function testPatchEntryWithoutQuotes()
@@ -497,6 +570,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
497 'tags' => 'new tag '.uniqid(), 570 'tags' => 'new tag '.uniqid(),
498 'starred' => 1, 571 'starred' => 1,
499 'archive' => 0, 572 'archive' => 0,
573 'authors' => ['bob', 'sponge'],
500 ]); 574 ]);
501 575
502 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 576 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -507,6 +581,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
507 $this->assertEquals($entry->getUrl(), $content['url']); 581 $this->assertEquals($entry->getUrl(), $content['url']);
508 $this->assertEquals('New awesome title', $content['title']); 582 $this->assertEquals('New awesome title', $content['title']);
509 $this->assertGreaterThan($nbTags, count($content['tags'])); 583 $this->assertGreaterThan($nbTags, count($content['tags']));
584 $this->assertTrue(empty($content['published_by']), 'Authors were not saved because of an array instead of a string');
510 } 585 }
511 586
512 public function testGetTagsEntry() 587 public function testGetTagsEntry()
@@ -692,7 +767,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
692 767
693 $content = json_decode($this->client->getResponse()->getContent(), true); 768 $content = json_decode($this->client->getResponse()->getContent(), true);
694 769
695 $this->assertEquals(true, $content['exists']); 770 $this->assertEquals(2, $content['exists']);
696 } 771 }
697 772
698 public function testGetEntriesExistsWithManyUrls() 773 public function testGetEntriesExistsWithManyUrls()
@@ -707,7 +782,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
707 782
708 $this->assertArrayHasKey($url1, $content); 783 $this->assertArrayHasKey($url1, $content);
709 $this->assertArrayHasKey($url2, $content); 784 $this->assertArrayHasKey($url2, $content);
710 $this->assertEquals(true, $content[$url1]); 785 $this->assertEquals(2, $content[$url1]);
711 $this->assertEquals(false, $content[$url2]); 786 $this->assertEquals(false, $content[$url2]);
712 } 787 }
713 788
@@ -764,4 +839,131 @@ class EntryRestControllerTest extends WallabagApiTestCase
764 839
765 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 840 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
766 } 841 }
842
843 public function testPostEntriesTagsListAction()
844 {
845 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
846 ->getRepository('WallabagCoreBundle:Entry')
847 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
848
849 $tags = $entry->getTags();
850
851 $this->assertCount(2, $tags);
852
853 $list = [
854 [
855 'url' => 'http://0.0.0.0/entry4',
856 'tags' => 'new tag 1, new tag 2',
857 ],
858 ];
859
860 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
861
862 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
863
864 $content = json_decode($this->client->getResponse()->getContent(), true);
865
866 $this->assertInternalType('int', $content[0]['entry']);
867 $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
868
869 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
870 ->getRepository('WallabagCoreBundle:Entry')
871 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
872
873 $tags = $entry->getTags();
874 $this->assertCount(4, $tags);
875 }
876
877 public function testDeleteEntriesTagsListAction()
878 {
879 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
880 $entry = new Entry($em->getReference(User::class, 1));
881 $entry->setUrl('http://0.0.0.0/test-entry');
882 $entry->addTag((new Tag())->setLabel('foo-tag'));
883 $entry->addTag((new Tag())->setLabel('bar-tag'));
884 $em->persist($entry);
885 $em->flush();
886
887 $em->clear();
888
889 $list = [
890 [
891 'url' => 'http://0.0.0.0/test-entry',
892 'tags' => 'foo-tag, bar-tag',
893 ],
894 ];
895
896 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
897 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
898
899 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
900 $this->assertCount(0, $entry->getTags());
901 }
902
903 public function testPostEntriesListAction()
904 {
905 $list = [
906 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
907 'http://0.0.0.0/entry2',
908 ];
909
910 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
911
912 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
913
914 $content = json_decode($this->client->getResponse()->getContent(), true);
915
916 $this->assertInternalType('int', $content[0]['entry']);
917 $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']);
918
919 $this->assertInternalType('int', $content[1]['entry']);
920 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
921 }
922
923 public function testDeleteEntriesListAction()
924 {
925 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
926 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
927
928 $em->flush();
929 $em->clear();
930 $list = [
931 'http://0.0.0.0/test-entry1',
932 'http://0.0.0.0/test-entry-not-exist',
933 ];
934
935 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
936
937 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
938
939 $content = json_decode($this->client->getResponse()->getContent(), true);
940
941 $this->assertTrue($content[0]['entry']);
942 $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
943
944 $this->assertFalse($content[1]['entry']);
945 $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
946 }
947
948 public function testLimitBulkAction()
949 {
950 $list = [
951 'http://0.0.0.0/entry1',
952 'http://0.0.0.0/entry1',
953 'http://0.0.0.0/entry1',
954 'http://0.0.0.0/entry1',
955 'http://0.0.0.0/entry1',
956 'http://0.0.0.0/entry1',
957 'http://0.0.0.0/entry1',
958 'http://0.0.0.0/entry1',
959 'http://0.0.0.0/entry1',
960 'http://0.0.0.0/entry1',
961 'http://0.0.0.0/entry1',
962 ];
963
964 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
965
966 $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
967 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
968 }
767} 969}
diff --git a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
index bde5251f..7f69bd67 100644
--- a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
@@ -22,39 +22,49 @@ 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);
31 30
32 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); 31 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
32 $em->persist($tag);
33 $em->flush();
34 $em->clear();
35
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') 52
51 ->getRepository('WallabagCoreBundle:Tag') 53 $this->assertNull($tag, $tagLabel.' was removed because it begun an orphan tag');
52 ->findOneByLabel($tagName); 54 }
53 55
54 $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag'); 56 public function dataForDeletingTagByLabel()
57 {
58 return [
59 'by_query' => [true],
60 'by_body' => [false],
61 ];
55 } 62 }
56 63
57 public function testDeleteTagByLabel() 64 /**
65 * @dataProvider dataForDeletingTagByLabel
66 */
67 public function testDeleteTagByLabel($useQueryString)
58 { 68 {
59 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); 69 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
60 $entry = $this->client->getContainer() 70 $entry = $this->client->getContainer()
@@ -73,7 +83,11 @@ class TagRestControllerTest extends WallabagApiTestCase
73 $em->persist($entry); 83 $em->persist($entry);
74 $em->flush(); 84 $em->flush();
75 85
76 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]); 86 if ($useQueryString) {
87 $this->client->request('DELETE', '/api/tag/label.json?tag='.$tag->getLabel());
88 } else {
89 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
90 }
77 91
78 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 92 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
79 93
@@ -98,7 +112,10 @@ class TagRestControllerTest extends WallabagApiTestCase
98 $this->assertEquals(404, $this->client->getResponse()->getStatusCode()); 112 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
99 } 113 }
100 114
101 public function testDeleteTagsByLabel() 115 /**
116 * @dataProvider dataForDeletingTagByLabel
117 */
118 public function testDeleteTagsByLabel($useQueryString)
102 { 119 {
103 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager'); 120 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
104 $entry = $this->client->getContainer() 121 $entry = $this->client->getContainer()
@@ -122,7 +139,11 @@ class TagRestControllerTest extends WallabagApiTestCase
122 $em->persist($entry); 139 $em->persist($entry);
123 $em->flush(); 140 $em->flush();
124 141
125 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]); 142 if ($useQueryString) {
143 $this->client->request('DELETE', '/api/tags/label.json?tags='.$tag->getLabel().','.$tag2->getLabel());
144 } else {
145 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]);
146 }
126 147
127 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 148 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
128 149
diff --git a/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php
new file mode 100644
index 00000000..4e65f130
--- /dev/null
+++ b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php
@@ -0,0 +1,185 @@
1<?php
2
3namespace Tests\Wallabag\ApiBundle\Controller;
4
5use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6
7class UserRestControllerTest extends WallabagApiTestCase
8{
9 public function testGetUser()
10 {
11 $this->client->request('GET', '/api/user.json');
12 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
13
14 $content = json_decode($this->client->getResponse()->getContent(), true);
15
16 $this->assertArrayHasKey('id', $content);
17 $this->assertArrayHasKey('email', $content);
18 $this->assertArrayHasKey('name', $content);
19 $this->assertArrayHasKey('username', $content);
20 $this->assertArrayHasKey('created_at', $content);
21 $this->assertArrayHasKey('updated_at', $content);
22
23 $this->assertEquals('bigboss@wallabag.org', $content['email']);
24 $this->assertEquals('Big boss', $content['name']);
25 $this->assertEquals('admin', $content['username']);
26
27 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
28 }
29
30 public function testGetUserWithoutAuthentication()
31 {
32 $client = static::createClient();
33 $client->request('GET', '/api/user.json');
34 $this->assertEquals(401, $client->getResponse()->getStatusCode());
35
36 $content = json_decode($client->getResponse()->getContent(), true);
37
38 $this->assertArrayHasKey('error', $content);
39 $this->assertArrayHasKey('error_description', $content);
40
41 $this->assertEquals('access_denied', $content['error']);
42
43 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
44 }
45
46 public function testCreateNewUser()
47 {
48 $this->client->getContainer()->get('craue_config')->set('api_user_registration', 1);
49 $this->client->request('PUT', '/api/user.json', [
50 'username' => 'google',
51 'password' => 'googlegoogle',
52 'email' => 'wallabag@google.com',
53 ]);
54
55 $this->assertEquals(201, $this->client->getResponse()->getStatusCode());
56
57 $content = json_decode($this->client->getResponse()->getContent(), true);
58
59 $this->assertArrayHasKey('id', $content);
60 $this->assertArrayHasKey('email', $content);
61 $this->assertArrayHasKey('username', $content);
62 $this->assertArrayHasKey('created_at', $content);
63 $this->assertArrayHasKey('updated_at', $content);
64 $this->assertArrayHasKey('default_client', $content);
65
66 $this->assertEquals('wallabag@google.com', $content['email']);
67 $this->assertEquals('google', $content['username']);
68
69 $this->assertArrayHasKey('client_secret', $content['default_client']);
70 $this->assertArrayHasKey('client_id', $content['default_client']);
71
72 $this->assertEquals('Default client', $content['default_client']['name']);
73
74 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
75
76 $this->client->getContainer()->get('craue_config')->set('api_user_registration', 0);
77 }
78
79 public function testCreateNewUserWithoutAuthentication()
80 {
81 // create a new client instead of using $this->client to be sure client isn't authenticated
82 $client = static::createClient();
83 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
84 $client->request('PUT', '/api/user.json', [
85 'username' => 'google',
86 'password' => 'googlegoogle',
87 'email' => 'wallabag@google.com',
88 'client_name' => 'My client name !!',
89 ]);
90
91 $this->assertEquals(201, $client->getResponse()->getStatusCode());
92
93 $content = json_decode($client->getResponse()->getContent(), true);
94
95 $this->assertArrayHasKey('id', $content);
96 $this->assertArrayHasKey('email', $content);
97 $this->assertArrayHasKey('username', $content);
98 $this->assertArrayHasKey('created_at', $content);
99 $this->assertArrayHasKey('updated_at', $content);
100 $this->assertArrayHasKey('default_client', $content);
101
102 $this->assertEquals('wallabag@google.com', $content['email']);
103 $this->assertEquals('google', $content['username']);
104
105 $this->assertArrayHasKey('client_secret', $content['default_client']);
106 $this->assertArrayHasKey('client_id', $content['default_client']);
107
108 $this->assertEquals('My client name !!', $content['default_client']['name']);
109
110 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
111
112 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
113 }
114
115 public function testCreateNewUserWithExistingEmail()
116 {
117 $client = static::createClient();
118 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
119 $client->request('PUT', '/api/user.json', [
120 'username' => 'admin',
121 'password' => 'googlegoogle',
122 'email' => 'bigboss@wallabag.org',
123 ]);
124
125 $this->assertEquals(400, $client->getResponse()->getStatusCode());
126
127 $content = json_decode($client->getResponse()->getContent(), true);
128
129 $this->assertArrayHasKey('error', $content);
130 $this->assertArrayHasKey('username', $content['error']);
131 $this->assertArrayHasKey('email', $content['error']);
132
133 // $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]);
134 // $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]);
135 // This shouldn't be translated ...
136 $this->assertEquals('This value is already used.', $content['error']['username'][0]);
137 $this->assertEquals('This value is already used.', $content['error']['email'][0]);
138
139 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
140
141 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
142 }
143
144 public function testCreateNewUserWithTooShortPassword()
145 {
146 $client = static::createClient();
147 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
148 $client->request('PUT', '/api/user.json', [
149 'username' => 'facebook',
150 'password' => 'face',
151 'email' => 'facebook@wallabag.org',
152 ]);
153
154 $this->assertEquals(400, $client->getResponse()->getStatusCode());
155
156 $content = json_decode($client->getResponse()->getContent(), true);
157
158 $this->assertArrayHasKey('error', $content);
159 $this->assertArrayHasKey('password', $content['error']);
160
161 $this->assertEquals('validator.password_too_short', $content['error']['password'][0]);
162
163 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
164
165 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
166 }
167
168 public function testCreateNewUserWhenRegistrationIsDisabled()
169 {
170 $client = static::createClient();
171 $client->request('PUT', '/api/user.json', [
172 'username' => 'facebook',
173 'password' => 'face',
174 'email' => 'facebook@wallabag.org',
175 ]);
176
177 $this->assertEquals(403, $client->getResponse()->getStatusCode());
178
179 $content = json_decode($client->getResponse()->getContent(), true);
180
181 $this->assertArrayHasKey('error', $content);
182
183 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
184 }
185}
diff --git a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
index c87e58de..df638e8f 100644
--- a/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
+++ b/tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php
@@ -8,12 +8,14 @@ class WallabagRestControllerTest extends WallabagApiTestCase
8{ 8{
9 public function testGetVersion() 9 public function testGetVersion()
10 { 10 {
11 $this->client->request('GET', '/api/version'); 11 // create a new client instead of using $this->client to be sure client isn't authenticated
12 $client = static::createClient();
13 $client->request('GET', '/api/version');
12 14
13 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 15 $this->assertEquals(200, $client->getResponse()->getStatusCode());
14 16
15 $content = json_decode($this->client->getResponse()->getContent(), true); 17 $content = json_decode($client->getResponse()->getContent(), true);
16 18
17 $this->assertEquals($this->client->getContainer()->getParameter('wallabag_core.version'), $content); 19 $this->assertEquals($client->getContainer()->getParameter('wallabag_core.version'), $content);
18 } 20 }
19} 21}
diff --git a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php
index cf9b3347..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 {
@@ -37,7 +37,7 @@ abstract class WallabagApiTestCase extends WebTestCase
37 $firewallName = $container->getParameter('fos_user.firewall_name'); 37 $firewallName = $container->getParameter('fos_user.firewall_name');
38 38
39 $this->user = $userManager->findUserBy(['username' => 'admin']); 39 $this->user = $userManager->findUserBy(['username' => 'admin']);
40 $loginManager->loginUser($firewallName, $this->user); 40 $loginManager->logInUser($firewallName, $this->user);
41 41
42 // save the login token into the session and put it in a cookie 42 // save the login token into the session and put it in a cookie
43 $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); 43 $container->get('session')->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken()));