From fdc90ceb172bb7b237e34a1a01f53018c09f514b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 24 Jun 2016 11:55:45 +0200 Subject: [PATCH] Change the way to login user in tests Instead of using a HTTP request we just login user like FOSUser does. It allows us to mock service in container for functional tests. Also, fix a bad config name in fos_user for firewall And finally, add functional test to PocketImport --- app/config/config.yml | 2 +- .../Controller/SecurityControllerTest.php | 2 +- .../Controller/TagControllerTest.php | 6 ++ .../CoreBundle/WallabagCoreTestCase.php | 32 ++++++++++ .../Controller/PocketControllerTest.php | 57 +++++++++++++++--- .../ImportBundle/fixtures/unnamed.png | Bin 0 -> 3688 bytes 6 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 tests/Wallabag/ImportBundle/fixtures/unnamed.png diff --git a/app/config/config.yml b/app/config/config.yml index f1321d67b..6a8078ccd 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -176,7 +176,7 @@ liip_theme: fos_user: db_driver: orm - firewall_name: main + firewall_name: secured_area user_class: Wallabag\UserBundle\Entity\User registration: confirmation: diff --git a/tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php index f503ff4bd..03355f5ab 100644 --- a/tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php @@ -36,7 +36,7 @@ class SecurityControllerTest extends WallabagCoreTestCase $em->persist($user); $em->flush(); - $this->logInAs('admin'); + $this->logInAsUsingHttp('admin'); $crawler = $client->request('GET', '/config'); $this->assertContains('scheb_two_factor.trusted', $crawler->filter('body')->extract(['_text'])[0]); diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php index a019d36c9..58450e5fb 100644 --- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php @@ -39,6 +39,12 @@ class TagControllerTest extends WallabagCoreTestCase $client->submit($form, $data); $this->assertEquals(302, $client->getResponse()->getStatusCode()); + // be sure to reload the entry + $entry = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findOneByUsernameAndNotArchived('admin'); + $this->assertEquals(1, count($entry->getTags())); # tag already exists and already assigned diff --git a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php index c69e83301..c0055888a 100644 --- a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php +++ b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php @@ -3,6 +3,7 @@ namespace Tests\Wallabag\CoreBundle; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; +use Symfony\Component\BrowserKit\Cookie; abstract class WallabagCoreTestCase extends WebTestCase { @@ -20,7 +21,38 @@ abstract class WallabagCoreTestCase extends WebTestCase $this->client = static::createClient(); } + /** + * Login a user without making a HTTP request. + * If we make a HTTP request we lose ability to mock service in the container. + * + * @param string $username User to log in + */ public function logInAs($username) + { + $container = $this->client->getContainer(); + $session = $container->get('session'); + + $userManager = $container->get('fos_user.user_manager'); + $loginManager = $container->get('fos_user.security.login_manager'); + $firewallName = $container->getParameter('fos_user.firewall_name'); + + $user = $userManager->findUserBy(array('username' => $username)); + $loginManager->loginUser($firewallName, $user); + + $session->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); + $session->save(); + + $cookie = new Cookie($session->getName(), $session->getId()); + $this->client->getCookieJar()->set($cookie); + } + + /** + * Instead of `logInAs` this method use a HTTP request to log in the user. + * Could be better for some tests. + * + * @param string $username User to log in + */ + public function logInAsUsingHttp($username) { $crawler = $this->client->request('GET', '/login'); $form = $crawler->filter('button[type=submit]')->form(); diff --git a/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php b/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php index 6aaf1b57b..e0e61df88 100644 --- a/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php +++ b/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php @@ -22,15 +22,13 @@ class PocketControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $crawler = $client->request('GET', '/import/pocket/auth'); + $client->request('GET', '/import/pocket/auth'); $this->assertEquals(302, $client->getResponse()->getStatusCode()); } public function testImportPocketAuth() { - $this->markTestSkipped('PocketImport: Find a way to properly mock a service.'); - $this->logInAs('admin'); $client = $this->getClient(); @@ -43,9 +41,9 @@ class PocketControllerTest extends WallabagCoreTestCase ->method('getRequestToken') ->willReturn('token'); - $client->getContainer()->set('wallabag_import.pocket.import', $pocketImport); + static::$kernel->getContainer()->set('wallabag_import.pocket.import', $pocketImport); - $crawler = $client->request('GET', '/import/pocket/auth'); + $client->request('GET', '/import/pocket/auth'); $this->assertEquals(301, $client->getResponse()->getStatusCode()); $this->assertContains('getpocket.com/auth/authorize', $client->getResponse()->headers->get('location')); @@ -56,10 +54,55 @@ class PocketControllerTest extends WallabagCoreTestCase $this->logInAs('admin'); $client = $this->getClient(); - $crawler = $client->request('GET', '/import/pocket/callback'); + $pocketImport = $this->getMockBuilder('Wallabag\ImportBundle\Import\PocketImport') + ->disableOriginalConstructor() + ->getMock(); + + $pocketImport + ->expects($this->once()) + ->method('authorize') + ->willReturn(false); + + static::$kernel->getContainer()->set('wallabag_import.pocket.import', $pocketImport); + + $client->request('GET', '/import/pocket/callback'); $this->assertEquals(302, $client->getResponse()->getStatusCode()); - $this->assertContains('import/pocket', $client->getResponse()->headers->get('location')); + $this->assertContains('/', $client->getResponse()->headers->get('location'), 'Import is ok, redirect to homepage'); $this->assertEquals('flashes.import.notice.failed', $client->getContainer()->get('session')->getFlashBag()->peek('notice')[0]); } + + public function testImportPocketCallback() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $pocketImport = $this->getMockBuilder('Wallabag\ImportBundle\Import\PocketImport') + ->disableOriginalConstructor() + ->getMock(); + + $pocketImport + ->expects($this->once()) + ->method('authorize') + ->willReturn(true); + + $pocketImport + ->expects($this->once()) + ->method('setMarkAsRead') + ->with(false) + ->willReturn($pocketImport); + + $pocketImport + ->expects($this->once()) + ->method('import') + ->willReturn(true); + + static::$kernel->getContainer()->set('wallabag_import.pocket.import', $pocketImport); + + $client->request('GET', '/import/pocket/callback'); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + $this->assertContains('/', $client->getResponse()->headers->get('location'), 'Import is ok, redirect to homepage'); + $this->assertEquals('flashes.import.notice.summary', $client->getContainer()->get('session')->getFlashBag()->peek('notice')[0]); + } } diff --git a/tests/Wallabag/ImportBundle/fixtures/unnamed.png b/tests/Wallabag/ImportBundle/fixtures/unnamed.png new file mode 100644 index 0000000000000000000000000000000000000000..e6dd9caadb29929e09d80dcc30e593a9b6020e54 GIT binary patch literal 3688 zcmV-u4wvzXP)K!~nK5rQotCocwP|LL#lEZI==N9E75sehvaa z5`t+m{3bX*LnJ{wEjf&lv#l2ZRL*UW2Co6e+Q7KHOQ3t}%7NrbHcHWy7+vhU`kERp zgv)qtt0^I^zzHP;&{TQj*8Lj~G$8CnyYnPyNzE8gKJ4NiS%={)q{!5D`P(0UzN0CD zuosNCf+1Zok6zcma4=~BfB+JiYkL*F^4r;F7fy*gZ1yd$uKnxbRB0HL000-^!hQoQrM0)S_0U)>$u(VtSp)1Y~!1%f6($n#!Z{Z&^zbM#oE6U3kGy>#sN$CM3R z^Tv;7^(|E!;%G_VF|4Zg%?0}o)n767nPkR2w}c0teDR^hD;~UR+N&?!G}5afY2|Pi zXdXF|CG%MGkTU<1k6z>R*o^a58H5laxYuKD+_v+F3l@(UaK*;e_xa2G|GW9KX?N9D z$>FW<-cnX+h^Um;<@=QW50U- zgC#HQgg;yv@^r}&+mQfVmhKkZS|6WscWqp6%5X;7P*VcqzD^|<{0U_jQ z^dLkFvVH+lIA{V0fC*OQJCU-`Y~~hn$^??O-4d&M=|eZ>5CdRD0Z}r7#1q*_*Z|P2 z$QU%y|L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