]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
Cast client id to avoid PG error
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / DeveloperControllerTest.php
index 6659443ba44ea461f3be2280a9a3341e9ab7904a..e1a0ac7e49d2d1ee92405e7f3c3ab03a86e6edc2 100644 (file)
@@ -3,6 +3,7 @@
 namespace Tests\Wallabag\ApiBundle\Controller;
 
 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
+use Wallabag\ApiBundle\Entity\Client;
 
 class DeveloperControllerTest extends WallabagCoreTestCase
 {
@@ -14,9 +15,9 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
 
         $crawler = $client->request('GET', '/developer/client/create');
-        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+        $this->assertSame(200, $client->getResponse()->getStatusCode());
 
-        $form = $crawler->filter('button[type=submit]')->form();
+        $form = $crawler->filter('button[id=client_save]')->form();
 
         $data = [
             'client[name]' => 'My app',
@@ -24,23 +25,19 @@ class DeveloperControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->submit($form, $data);
 
-        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+        $this->assertSame(200, $client->getResponse()->getStatusCode());
 
         $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
-        $this->assertGreaterThan(count($nbClients), count($newNbClients));
+        $this->assertGreaterThan(\count($nbClients), \count($newNbClients));
 
         $this->assertGreaterThan(1, $alert = $crawler->filter('.settings ul li strong')->extract(['_text']));
         $this->assertContains('My app', $alert[0]);
     }
 
-    /**
-     * @depends testCreateClient
-     */
     public function testCreateToken()
     {
         $client = $this->getClient();
-        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
-        $apiClient = $em->getRepository('WallabagApiBundle:Client')->findOneByName('My app');
+        $apiClient = $this->createApiClientForUser('admin');
 
         $client->request('POST', '/oauth/v2/token', [
             'grant_type' => 'password',
@@ -50,7 +47,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
             'password' => 'mypassword',
         ]);
 
-        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+        $this->assertSame(200, $client->getResponse()->getStatusCode());
 
         $data = json_decode($client->getResponse()->getContent(), true);
         $this->assertArrayHasKey('access_token', $data);
@@ -59,6 +56,20 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $this->assertArrayHasKey('refresh_token', $data);
     }
 
+    public function testCreateTokenWithBadClientId()
+    {
+        $client = $this->getClient();
+        $client->request('POST', '/oauth/v2/token', [
+            'grant_type' => 'password',
+            'client_id' => '$WALLABAG_CLIENT_ID',
+            'client_secret' => 'secret',
+            'username' => 'admin',
+            'password' => 'mypassword',
+        ]);
+
+        $this->assertSame(400, $client->getResponse()->getStatusCode());
+    }
+
     public function testListingClient()
     {
         $this->logInAs('admin');
@@ -67,8 +78,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $nbClients = $em->getRepository('WallabagApiBundle:Client')->findAll();
 
         $crawler = $client->request('GET', '/developer');
-        $this->assertEquals(200, $client->getResponse()->getStatusCode());
-        $this->assertEquals(count($nbClients), $crawler->filter('ul[class=collapsible] li')->count());
+        $this->assertSame(200, $client->getResponse()->getStatusCode());
+        $this->assertSame(\count($nbClients), $crawler->filter('ul[class=collapsible] li')->count());
     }
 
     public function testDeveloperHowto()
@@ -77,12 +88,13 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/developer/howto/first-app');
-        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+        $this->assertSame(200, $client->getResponse()->getStatusCode());
     }
 
     public function testRemoveClient()
     {
         $client = $this->getClient();
+        $adminApiClient = $this->createApiClientForUser('admin');
         $em = $client->getContainer()->get('doctrine.orm.entity_manager');
 
         // Try to remove an admin's client with a wrong user
@@ -90,13 +102,9 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $client->request('GET', '/developer');
         $this->assertContains('no_client', $client->getResponse()->getContent());
 
-        // get an ID of a admin's client
-        $this->logInAs('admin');
-        $nbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId());
-
         $this->logInAs('bob');
-        $client->request('GET', '/developer/client/delete/'.$nbClients[0]->getId());
-        $this->assertEquals(403, $client->getResponse()->getStatusCode());
+        $client->request('GET', '/developer/client/delete/' . $adminApiClient->getId());
+        $this->assertSame(403, $client->getResponse()->getStatusCode());
 
         // Try to remove the admin's client with the good user
         $this->logInAs('admin');
@@ -109,9 +117,32 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         ;
 
         $client->click($link);
-        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+
+        $this->assertNull(
+            $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()),
+            'The client should have been removed'
+        );
+    }
 
-        $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId());
-        $this->assertGreaterThan(count($newNbClients), count($nbClients));
+    /**
+     * @param string $username
+     * @param array  $grantTypes
+     *
+     * @return Client
+     */
+    private function createApiClientForUser($username, $grantTypes = ['password'])
+    {
+        $client = $this->getClient();
+        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
+        $userManager = $client->getContainer()->get('fos_user.user_manager');
+        $user = $userManager->findUserBy(['username' => $username]);
+        $apiClient = new Client($user);
+        $apiClient->setName('My app');
+        $apiClient->setAllowedGrantTypes($grantTypes);
+        $em->persist($apiClient);
+        $em->flush();
+
+        return $apiClient;
     }
 }