X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=tests%2FWallabag%2FApiBundle%2FController%2FDeveloperControllerTest.php;h=35de47f94c387a6f0c8565c9ed9c3e408227d3ab;hb=5614df19cf3ed2be9d56853e5441412d4d09cff4;hp=6659443ba44ea461f3be2280a9a3341e9ab7904a;hpb=6fb06904ecde15b1b07d0a2af945338b416cf0e2;p=github%2Fwallabag%2Fwallabag.git diff --git a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php index 6659443b..35de47f9 100644 --- a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php @@ -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,7 +25,7 @@ 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)); @@ -33,14 +34,10 @@ class DeveloperControllerTest extends WallabagCoreTestCase $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); @@ -67,8 +64,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 +74,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 +88,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 +103,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; } }