aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php')
-rw-r--r--tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php57
1 files changed, 37 insertions, 20 deletions
diff --git a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
index 6659443b..e9e5ee3b 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{
@@ -14,7 +15,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
14 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); 15 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
15 16
16 $crawler = $client->request('GET', '/developer/client/create'); 17 $crawler = $client->request('GET', '/developer/client/create');
17 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 18 $this->assertSame(200, $client->getResponse()->getStatusCode());
18 19
19 $form = $crawler->filter('button[type=submit]')->form(); 20 $form = $crawler->filter('button[type=submit]')->form();
20 21
@@ -24,7 +25,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
24 25
25 $crawler = $client->submit($form, $data); 26 $crawler = $client->submit($form, $data);
26 27
27 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 28 $this->assertSame(200, $client->getResponse()->getStatusCode());
28 29
29 $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); 30 $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
30 $this->assertGreaterThan(count($nbClients), count($newNbClients)); 31 $this->assertGreaterThan(count($nbClients), count($newNbClients));
@@ -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',
@@ -50,7 +47,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
50 'password' => 'mypassword', 47 'password' => 'mypassword',
51 ]); 48 ]);
52 49
53 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 50 $this->assertSame(200, $client->getResponse()->getStatusCode());
54 51
55 $data = json_decode($client->getResponse()->getContent(), true); 52 $data = json_decode($client->getResponse()->getContent(), true);
56 $this->assertArrayHasKey('access_token', $data); 53 $this->assertArrayHasKey('access_token', $data);
@@ -67,8 +64,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase
67 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll(); 64 $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
68 65
69 $crawler = $client->request('GET', '/developer'); 66 $crawler = $client->request('GET', '/developer');
70 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 67 $this->assertSame(200, $client->getResponse()->getStatusCode());
71 $this->assertEquals(count($nbClients), $crawler->filter('ul[class=collapsible] li')->count()); 68 $this->assertSame(count($nbClients), $crawler->filter('ul[class=collapsible] li')->count());
72 } 69 }
73 70
74 public function testDeveloperHowto() 71 public function testDeveloperHowto()
@@ -77,12 +74,13 @@ class DeveloperControllerTest extends WallabagCoreTestCase
77 $client = $this->getClient(); 74 $client = $this->getClient();
78 75
79 $crawler = $client->request('GET', '/developer/howto/first-app'); 76 $crawler = $client->request('GET', '/developer/howto/first-app');
80 $this->assertEquals(200, $client->getResponse()->getStatusCode()); 77 $this->assertSame(200, $client->getResponse()->getStatusCode());
81 } 78 }
82 79
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,13 +88,9 @@ 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->assertSame(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
102 $this->logInAs('admin'); 96 $this->logInAs('admin');
@@ -109,9 +103,32 @@ class DeveloperControllerTest extends WallabagCoreTestCase
109 ; 103 ;
110 104
111 $client->click($link); 105 $client->click($link);
112 $this->assertEquals(302, $client->getResponse()->getStatusCode()); 106 $this->assertSame(302, $client->getResponse()->getStatusCode());
107
108 $this->assertNull(
109 $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()),
110 'The client should have been removed'
111 );
112 }
113 113
114 $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId()); 114 /**
115 $this->assertGreaterThan(count($newNbClients), count($nbClients)); 115 * @param string $username
116 * @param array $grantTypes
117 *
118 * @return Client
119 */
120 private function createApiClientForUser($username, $grantTypes = ['password'])
121 {
122 $client = $this->getClient();
123 $em = $client->getContainer()->get('doctrine.orm.entity_manager');
124 $userManager = $client->getContainer()->get('fos_user.user_manager');
125 $user = $userManager->findUserBy(['username' => $username]);
126 $apiClient = new Client($user);
127 $apiClient->setName('My app');
128 $apiClient->setAllowedGrantTypes($grantTypes);
129 $em->persist($apiClient);
130 $em->flush();
131
132 return $apiClient;
116 } 133 }
117} 134}