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.php40
-rw-r--r--tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php160
-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, 419 insertions, 45 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 0a65f9ce..4aa60e90 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{
@@ -315,7 +317,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
315 $entry = $this->client->getContainer() 317 $entry = $this->client->getContainer()
316 ->get('doctrine.orm.entity_manager') 318 ->get('doctrine.orm.entity_manager')
317 ->getRepository('WallabagCoreBundle:Entry') 319 ->getRepository('WallabagCoreBundle:Entry')
318 ->findOneByUser(1); 320 ->findOneByUser(1, ['id' => 'asc']);
319 321
320 if (!$entry) { 322 if (!$entry) {
321 $this->markTestSkipped('No content found in db.'); 323 $this->markTestSkipped('No content found in db.');
@@ -342,6 +344,10 @@ 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', 344 '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', 345 'tags' => 'google',
344 'title' => 'New title for my article', 346 'title' => 'New title for my article',
347 'content' => 'my content',
348 'language' => 'de',
349 'published_at' => '2016-09-08T11:55:58+0200',
350 'authors' => 'bob,helen',
345 ]); 351 ]);
346 352
347 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 353 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -354,7 +360,13 @@ class EntryRestControllerTest extends WallabagApiTestCase
354 $this->assertEquals(false, $content['is_starred']); 360 $this->assertEquals(false, $content['is_starred']);
355 $this->assertEquals('New title for my article', $content['title']); 361 $this->assertEquals('New title for my article', $content['title']);
356 $this->assertEquals(1, $content['user_id']); 362 $this->assertEquals(1, $content['user_id']);
357 $this->assertCount(1, $content['tags']); 363 $this->assertCount(2, $content['tags']);
364 $this->assertSame('my content', $content['content']);
365 $this->assertSame('de', $content['language']);
366 $this->assertSame('2016-09-08T11:55:58+0200', $content['published_at']);
367 $this->assertCount(2, $content['published_by']);
368 $this->assertContains('bob', $content['published_by']);
369 $this->assertContains('helen', $content['published_by']);
358 } 370 }
359 371
360 public function testPostSameEntry() 372 public function testPostSameEntry()
@@ -373,7 +385,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']); 385 $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']); 386 $this->assertEquals(true, $content['is_archived']);
375 $this->assertEquals(false, $content['is_starred']); 387 $this->assertEquals(false, $content['is_starred']);
376 $this->assertCount(2, $content['tags']); 388 $this->assertCount(3, $content['tags']);
377 } 389 }
378 390
379 public function testPostEntryWhenFetchContentFails() 391 public function testPostEntryWhenFetchContentFails()
@@ -465,6 +477,10 @@ class EntryRestControllerTest extends WallabagApiTestCase
465 'tags' => 'new tag '.uniqid(), 477 'tags' => 'new tag '.uniqid(),
466 'starred' => '1', 478 'starred' => '1',
467 'archive' => '0', 479 'archive' => '0',
480 'language' => 'de_AT',
481 'preview_picture' => 'http://preview.io/picture.jpg',
482 'authors' => 'bob,sponge',
483 'content' => 'awesome',
468 ]); 484 ]);
469 485
470 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 486 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -476,6 +492,11 @@ class EntryRestControllerTest extends WallabagApiTestCase
476 $this->assertEquals('New awesome title', $content['title']); 492 $this->assertEquals('New awesome title', $content['title']);
477 $this->assertGreaterThan($nbTags, count($content['tags'])); 493 $this->assertGreaterThan($nbTags, count($content['tags']));
478 $this->assertEquals(1, $content['user_id']); 494 $this->assertEquals(1, $content['user_id']);
495 $this->assertEquals('de_AT', $content['language']);
496 $this->assertEquals('http://preview.io/picture.jpg', $content['preview_picture']);
497 $this->assertContains('sponge', $content['published_by']);
498 $this->assertContains('bob', $content['published_by']);
499 $this->assertEquals('awesome', $content['content']);
479 } 500 }
480 501
481 public function testPatchEntryWithoutQuotes() 502 public function testPatchEntryWithoutQuotes()
@@ -497,6 +518,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
497 'tags' => 'new tag '.uniqid(), 518 'tags' => 'new tag '.uniqid(),
498 'starred' => 1, 519 'starred' => 1,
499 'archive' => 0, 520 'archive' => 0,
521 'authors' => ['bob', 'sponge'],
500 ]); 522 ]);
501 523
502 $this->assertEquals(200, $this->client->getResponse()->getStatusCode()); 524 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
@@ -507,6 +529,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
507 $this->assertEquals($entry->getUrl(), $content['url']); 529 $this->assertEquals($entry->getUrl(), $content['url']);
508 $this->assertEquals('New awesome title', $content['title']); 530 $this->assertEquals('New awesome title', $content['title']);
509 $this->assertGreaterThan($nbTags, count($content['tags'])); 531 $this->assertGreaterThan($nbTags, count($content['tags']));
532 $this->assertTrue(empty($content['published_by']), 'Authors were not saved because of an array instead of a string');
510 } 533 }
511 534
512 public function testGetTagsEntry() 535 public function testGetTagsEntry()
@@ -692,7 +715,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
692 715
693 $content = json_decode($this->client->getResponse()->getContent(), true); 716 $content = json_decode($this->client->getResponse()->getContent(), true);
694 717
695 $this->assertEquals(true, $content['exists']); 718 $this->assertEquals(2, $content['exists']);
696 } 719 }
697 720
698 public function testGetEntriesExistsWithManyUrls() 721 public function testGetEntriesExistsWithManyUrls()
@@ -707,7 +730,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
707 730
708 $this->assertArrayHasKey($url1, $content); 731 $this->assertArrayHasKey($url1, $content);
709 $this->assertArrayHasKey($url2, $content); 732 $this->assertArrayHasKey($url2, $content);
710 $this->assertEquals(true, $content[$url1]); 733 $this->assertEquals(2, $content[$url1]);
711 $this->assertEquals(false, $content[$url2]); 734 $this->assertEquals(false, $content[$url2]);
712 } 735 }
713 736
@@ -764,4 +787,131 @@ class EntryRestControllerTest extends WallabagApiTestCase
764 787
765 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 788 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
766 } 789 }
790
791 public function testPostEntriesTagsListAction()
792 {
793 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
794 ->getRepository('WallabagCoreBundle:Entry')
795 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
796
797 $tags = $entry->getTags();
798
799 $this->assertCount(2, $tags);
800
801 $list = [
802 [
803 'url' => 'http://0.0.0.0/entry4',
804 'tags' => 'new tag 1, new tag 2',
805 ],
806 ];
807
808 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
809
810 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
811
812 $content = json_decode($this->client->getResponse()->getContent(), true);
813
814 $this->assertInternalType('int', $content[0]['entry']);
815 $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
816
817 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
818 ->getRepository('WallabagCoreBundle:Entry')
819 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
820
821 $tags = $entry->getTags();
822 $this->assertCount(4, $tags);
823 }
824
825 public function testDeleteEntriesTagsListAction()
826 {
827 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
828 $entry = new Entry($em->getReference(User::class, 1));
829 $entry->setUrl('http://0.0.0.0/test-entry');
830 $entry->addTag((new Tag())->setLabel('foo-tag'));
831 $entry->addTag((new Tag())->setLabel('bar-tag'));
832 $em->persist($entry);
833 $em->flush();
834
835 $em->clear();
836
837 $list = [
838 [
839 'url' => 'http://0.0.0.0/test-entry',
840 'tags' => 'foo-tag, bar-tag',
841 ],
842 ];
843
844 $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
845 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
846
847 $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
848 $this->assertCount(0, $entry->getTags());
849 }
850
851 public function testPostEntriesListAction()
852 {
853 $list = [
854 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
855 'http://0.0.0.0/entry2',
856 ];
857
858 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
859
860 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
861
862 $content = json_decode($this->client->getResponse()->getContent(), true);
863
864 $this->assertInternalType('int', $content[0]['entry']);
865 $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']);
866
867 $this->assertInternalType('int', $content[1]['entry']);
868 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
869 }
870
871 public function testDeleteEntriesListAction()
872 {
873 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
874 $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
875
876 $em->flush();
877 $em->clear();
878 $list = [
879 'http://0.0.0.0/test-entry1',
880 'http://0.0.0.0/test-entry-not-exist',
881 ];
882
883 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
884
885 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
886
887 $content = json_decode($this->client->getResponse()->getContent(), true);
888
889 $this->assertTrue($content[0]['entry']);
890 $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
891
892 $this->assertFalse($content[1]['entry']);
893 $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
894 }
895
896 public function testLimitBulkAction()
897 {
898 $list = [
899 'http://0.0.0.0/entry1',
900 'http://0.0.0.0/entry1',
901 'http://0.0.0.0/entry1',
902 'http://0.0.0.0/entry1',
903 'http://0.0.0.0/entry1',
904 'http://0.0.0.0/entry1',
905 'http://0.0.0.0/entry1',
906 'http://0.0.0.0/entry1',
907 'http://0.0.0.0/entry1',
908 'http://0.0.0.0/entry1',
909 'http://0.0.0.0/entry1',
910 ];
911
912 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
913
914 $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
915 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
916 }
767} 917}
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()));