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.php149
-rw-r--r--tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php63
-rw-r--r--tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php172
-rw-r--r--tests/Wallabag/ApiBundle/Controller/WallabagRestControllerTest.php10
-rw-r--r--tests/Wallabag/ApiBundle/WallabagApiTestCase.php6
6 files changed, 395 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..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{
@@ -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_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_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()
@@ -692,7 +704,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
692 704
693 $content = json_decode($this->client->getResponse()->getContent(), true); 705 $content = json_decode($this->client->getResponse()->getContent(), true);
694 706
695 $this->assertEquals(true, $content['exists']); 707 $this->assertEquals(2, $content['exists']);
696 } 708 }
697 709
698 public function testGetEntriesExistsWithManyUrls() 710 public function testGetEntriesExistsWithManyUrls()
@@ -707,7 +719,7 @@ class EntryRestControllerTest extends WallabagApiTestCase
707 719
708 $this->assertArrayHasKey($url1, $content); 720 $this->assertArrayHasKey($url1, $content);
709 $this->assertArrayHasKey($url2, $content); 721 $this->assertArrayHasKey($url2, $content);
710 $this->assertEquals(true, $content[$url1]); 722 $this->assertEquals(2, $content[$url1]);
711 $this->assertEquals(false, $content[$url2]); 723 $this->assertEquals(false, $content[$url2]);
712 } 724 }
713 725
@@ -764,4 +776,131 @@ class EntryRestControllerTest extends WallabagApiTestCase
764 776
765 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type')); 777 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
766 } 778 }
779
780 public function testPostEntriesTagsListAction()
781 {
782 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
783 ->getRepository('WallabagCoreBundle:Entry')
784 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
785
786 $tags = $entry->getTags();
787
788 $this->assertCount(2, $tags);
789
790 $list = [
791 [
792 'url' => 'http://0.0.0.0/entry4',
793 'tags' => 'new tag 1, new tag 2',
794 ],
795 ];
796
797 $this->client->request('POST', '/api/entries/tags/lists?list='.json_encode($list));
798
799 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
800
801 $content = json_decode($this->client->getResponse()->getContent(), true);
802
803 $this->assertInternalType('int', $content[0]['entry']);
804 $this->assertEquals('http://0.0.0.0/entry4', $content[0]['url']);
805
806 $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
807 ->getRepository('WallabagCoreBundle:Entry')
808 ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
809
810 $tags = $entry->getTags();
811 $this->assertCount(4, $tags);
812 }
813
814 public function testDeleteEntriesTagsListAction()
815 {
816 $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
817 $entry = new Entry($em->getReference(User::class, 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();
823
824 $em->clear();
825
826 $list = [
827 [
828 'url' => 'http://0.0.0.0/test-entry',
829 'tags' => 'foo-tag, bar-tag',
830 ],
831 ];
832
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());
838 }
839
840 public function testPostEntriesListAction()
841 {
842 $list = [
843 'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
844 'http://0.0.0.0/entry2',
845 ];
846
847 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
848
849 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
850
851 $content = json_decode($this->client->getResponse()->getContent(), true);
852
853 $this->assertInternalType('int', $content[0]['entry']);
854 $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']);
855
856 $this->assertInternalType('int', $content[1]['entry']);
857 $this->assertEquals('http://0.0.0.0/entry2', $content[1]['url']);
858 }
859
860 public function testDeleteEntriesListAction()
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();
867 $list = [
868 'http://0.0.0.0/test-entry1',
869 'http://0.0.0.0/test-entry-not-exist',
870 ];
871
872 $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
873
874 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
875
876 $content = json_decode($this->client->getResponse()->getContent(), true);
877
878 $this->assertTrue($content[0]['entry']);
879 $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
880
881 $this->assertFalse($content[1]['entry']);
882 $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
883 }
884
885 public function testLimitBulkAction()
886 {
887 $list = [
888 'http://0.0.0.0/entry1',
889 'http://0.0.0.0/entry1',
890 'http://0.0.0.0/entry1',
891 'http://0.0.0.0/entry1',
892 'http://0.0.0.0/entry1',
893 'http://0.0.0.0/entry1',
894 'http://0.0.0.0/entry1',
895 'http://0.0.0.0/entry1',
896 'http://0.0.0.0/entry1',
897 'http://0.0.0.0/entry1',
898 'http://0.0.0.0/entry1',
899 ];
900
901 $this->client->request('POST', '/api/entries/lists?urls='.json_encode($list));
902
903 $this->assertEquals(400, $this->client->getResponse()->getStatusCode());
904 $this->assertContains('API limit reached', $this->client->getResponse()->getContent());
905 }
767} 906}
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..5735bc58
--- /dev/null
+++ b/tests/Wallabag/ApiBundle/Controller/UserRestControllerTest.php
@@ -0,0 +1,172 @@
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
65 $this->assertEquals('wallabag@google.com', $content['email']);
66 $this->assertEquals('google', $content['username']);
67
68 $this->assertEquals('application/json', $this->client->getResponse()->headers->get('Content-Type'));
69
70 $this->client->getContainer()->get('craue_config')->set('api_user_registration', 0);
71 }
72
73 public function testCreateNewUserWithoutAuthentication()
74 {
75 // create a new client instead of using $this->client to be sure client isn't authenticated
76 $client = static::createClient();
77 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
78 $client->request('PUT', '/api/user.json', [
79 'username' => 'google',
80 'password' => 'googlegoogle',
81 'email' => 'wallabag@google.com',
82 ]);
83
84 $this->assertEquals(201, $client->getResponse()->getStatusCode());
85
86 $content = json_decode($client->getResponse()->getContent(), true);
87
88 $this->assertArrayHasKey('id', $content);
89 $this->assertArrayHasKey('email', $content);
90 $this->assertArrayHasKey('username', $content);
91 $this->assertArrayHasKey('created_at', $content);
92 $this->assertArrayHasKey('updated_at', $content);
93
94 $this->assertEquals('wallabag@google.com', $content['email']);
95 $this->assertEquals('google', $content['username']);
96
97 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
98
99 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
100 }
101
102 public function testCreateNewUserWithExistingEmail()
103 {
104 $client = static::createClient();
105 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
106 $client->request('PUT', '/api/user.json', [
107 'username' => 'admin',
108 'password' => 'googlegoogle',
109 'email' => 'bigboss@wallabag.org',
110 ]);
111
112 $this->assertEquals(400, $client->getResponse()->getStatusCode());
113
114 $content = json_decode($client->getResponse()->getContent(), true);
115
116 $this->assertArrayHasKey('error', $content);
117 $this->assertArrayHasKey('username', $content['error']);
118 $this->assertArrayHasKey('email', $content['error']);
119
120 // $this->assertEquals('fos_user.username.already_used', $content['error']['username'][0]);
121 // $this->assertEquals('fos_user.email.already_used', $content['error']['email'][0]);
122 // This shouldn't be translated ...
123 $this->assertEquals('This value is already used.', $content['error']['username'][0]);
124 $this->assertEquals('This value is already used.', $content['error']['email'][0]);
125
126 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
127
128 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
129 }
130
131 public function testCreateNewUserWithTooShortPassword()
132 {
133 $client = static::createClient();
134 $client->getContainer()->get('craue_config')->set('api_user_registration', 1);
135 $client->request('PUT', '/api/user.json', [
136 'username' => 'facebook',
137 'password' => 'face',
138 'email' => 'facebook@wallabag.org',
139 ]);
140
141 $this->assertEquals(400, $client->getResponse()->getStatusCode());
142
143 $content = json_decode($client->getResponse()->getContent(), true);
144
145 $this->assertArrayHasKey('error', $content);
146 $this->assertArrayHasKey('password', $content['error']);
147
148 $this->assertEquals('validator.password_too_short', $content['error']['password'][0]);
149
150 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
151
152 $client->getContainer()->get('craue_config')->set('api_user_registration', 0);
153 }
154
155 public function testCreateNewUserWhenRegistrationIsDisabled()
156 {
157 $client = static::createClient();
158 $client->request('PUT', '/api/user.json', [
159 'username' => 'facebook',
160 'password' => 'face',
161 'email' => 'facebook@wallabag.org',
162 ]);
163
164 $this->assertEquals(403, $client->getResponse()->getStatusCode());
165
166 $content = json_decode($client->getResponse()->getContent(), true);
167
168 $this->assertArrayHasKey('error', $content);
169
170 $this->assertEquals('application/json', $client->getResponse()->headers->get('Content-Type'));
171 }
172}
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()));