diff options
author | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-06-24 11:55:45 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2016-06-24 11:55:47 +0200 |
commit | fdc90ceb172bb7b237e34a1a01f53018c09f514b (patch) | |
tree | 6b2a4ad27428497713eef68ce57d4b58f18507cc /tests | |
parent | 2bc9cad78ed43eaadfc8844c0b55700e7045cbd7 (diff) | |
download | wallabag-fdc90ceb172bb7b237e34a1a01f53018c09f514b.tar.gz wallabag-fdc90ceb172bb7b237e34a1a01f53018c09f514b.tar.zst wallabag-fdc90ceb172bb7b237e34a1a01f53018c09f514b.zip |
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
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php | 2 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/Controller/TagControllerTest.php | 6 | ||||
-rw-r--r-- | tests/Wallabag/CoreBundle/WallabagCoreTestCase.php | 32 | ||||
-rw-r--r-- | tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php | 57 | ||||
-rw-r--r-- | tests/Wallabag/ImportBundle/fixtures/unnamed.png | bin | 0 -> 3688 bytes |
5 files changed, 89 insertions, 8 deletions
diff --git a/tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php index f503ff4b..03355f5a 100644 --- a/tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/SecurityControllerTest.php | |||
@@ -36,7 +36,7 @@ class SecurityControllerTest extends WallabagCoreTestCase | |||
36 | $em->persist($user); | 36 | $em->persist($user); |
37 | $em->flush(); | 37 | $em->flush(); |
38 | 38 | ||
39 | $this->logInAs('admin'); | 39 | $this->logInAsUsingHttp('admin'); |
40 | $crawler = $client->request('GET', '/config'); | 40 | $crawler = $client->request('GET', '/config'); |
41 | $this->assertContains('scheb_two_factor.trusted', $crawler->filter('body')->extract(['_text'])[0]); | 41 | $this->assertContains('scheb_two_factor.trusted', $crawler->filter('body')->extract(['_text'])[0]); |
42 | 42 | ||
diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php index a019d36c..58450e5f 100644 --- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php | |||
@@ -39,6 +39,12 @@ class TagControllerTest extends WallabagCoreTestCase | |||
39 | $client->submit($form, $data); | 39 | $client->submit($form, $data); |
40 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | 40 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); |
41 | 41 | ||
42 | // be sure to reload the entry | ||
43 | $entry = $client->getContainer() | ||
44 | ->get('doctrine.orm.entity_manager') | ||
45 | ->getRepository('WallabagCoreBundle:Entry') | ||
46 | ->findOneByUsernameAndNotArchived('admin'); | ||
47 | |||
42 | $this->assertEquals(1, count($entry->getTags())); | 48 | $this->assertEquals(1, count($entry->getTags())); |
43 | 49 | ||
44 | # tag already exists and already assigned | 50 | # tag already exists and already assigned |
diff --git a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php index c69e8330..c0055888 100644 --- a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php +++ b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php | |||
@@ -3,6 +3,7 @@ | |||
3 | namespace Tests\Wallabag\CoreBundle; | 3 | namespace Tests\Wallabag\CoreBundle; |
4 | 4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; | 5 | use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; |
6 | use Symfony\Component\BrowserKit\Cookie; | ||
6 | 7 | ||
7 | abstract class WallabagCoreTestCase extends WebTestCase | 8 | abstract class WallabagCoreTestCase extends WebTestCase |
8 | { | 9 | { |
@@ -20,8 +21,39 @@ abstract class WallabagCoreTestCase extends WebTestCase | |||
20 | $this->client = static::createClient(); | 21 | $this->client = static::createClient(); |
21 | } | 22 | } |
22 | 23 | ||
24 | /** | ||
25 | * Login a user without making a HTTP request. | ||
26 | * If we make a HTTP request we lose ability to mock service in the container. | ||
27 | * | ||
28 | * @param string $username User to log in | ||
29 | */ | ||
23 | public function logInAs($username) | 30 | public function logInAs($username) |
24 | { | 31 | { |
32 | $container = $this->client->getContainer(); | ||
33 | $session = $container->get('session'); | ||
34 | |||
35 | $userManager = $container->get('fos_user.user_manager'); | ||
36 | $loginManager = $container->get('fos_user.security.login_manager'); | ||
37 | $firewallName = $container->getParameter('fos_user.firewall_name'); | ||
38 | |||
39 | $user = $userManager->findUserBy(array('username' => $username)); | ||
40 | $loginManager->loginUser($firewallName, $user); | ||
41 | |||
42 | $session->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken())); | ||
43 | $session->save(); | ||
44 | |||
45 | $cookie = new Cookie($session->getName(), $session->getId()); | ||
46 | $this->client->getCookieJar()->set($cookie); | ||
47 | } | ||
48 | |||
49 | /** | ||
50 | * Instead of `logInAs` this method use a HTTP request to log in the user. | ||
51 | * Could be better for some tests. | ||
52 | * | ||
53 | * @param string $username User to log in | ||
54 | */ | ||
55 | public function logInAsUsingHttp($username) | ||
56 | { | ||
25 | $crawler = $this->client->request('GET', '/login'); | 57 | $crawler = $this->client->request('GET', '/login'); |
26 | $form = $crawler->filter('button[type=submit]')->form(); | 58 | $form = $crawler->filter('button[type=submit]')->form(); |
27 | $data = [ | 59 | $data = [ |
diff --git a/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php b/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php index 6aaf1b57..e0e61df8 100644 --- a/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php +++ b/tests/Wallabag/ImportBundle/Controller/PocketControllerTest.php | |||
@@ -22,15 +22,13 @@ class PocketControllerTest extends WallabagCoreTestCase | |||
22 | $this->logInAs('admin'); | 22 | $this->logInAs('admin'); |
23 | $client = $this->getClient(); | 23 | $client = $this->getClient(); |
24 | 24 | ||
25 | $crawler = $client->request('GET', '/import/pocket/auth'); | 25 | $client->request('GET', '/import/pocket/auth'); |
26 | 26 | ||
27 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | 27 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); |
28 | } | 28 | } |
29 | 29 | ||
30 | public function testImportPocketAuth() | 30 | public function testImportPocketAuth() |
31 | { | 31 | { |
32 | $this->markTestSkipped('PocketImport: Find a way to properly mock a service.'); | ||
33 | |||
34 | $this->logInAs('admin'); | 32 | $this->logInAs('admin'); |
35 | $client = $this->getClient(); | 33 | $client = $this->getClient(); |
36 | 34 | ||
@@ -43,9 +41,9 @@ class PocketControllerTest extends WallabagCoreTestCase | |||
43 | ->method('getRequestToken') | 41 | ->method('getRequestToken') |
44 | ->willReturn('token'); | 42 | ->willReturn('token'); |
45 | 43 | ||
46 | $client->getContainer()->set('wallabag_import.pocket.import', $pocketImport); | 44 | static::$kernel->getContainer()->set('wallabag_import.pocket.import', $pocketImport); |
47 | 45 | ||
48 | $crawler = $client->request('GET', '/import/pocket/auth'); | 46 | $client->request('GET', '/import/pocket/auth'); |
49 | 47 | ||
50 | $this->assertEquals(301, $client->getResponse()->getStatusCode()); | 48 | $this->assertEquals(301, $client->getResponse()->getStatusCode()); |
51 | $this->assertContains('getpocket.com/auth/authorize', $client->getResponse()->headers->get('location')); | 49 | $this->assertContains('getpocket.com/auth/authorize', $client->getResponse()->headers->get('location')); |
@@ -56,10 +54,55 @@ class PocketControllerTest extends WallabagCoreTestCase | |||
56 | $this->logInAs('admin'); | 54 | $this->logInAs('admin'); |
57 | $client = $this->getClient(); | 55 | $client = $this->getClient(); |
58 | 56 | ||
59 | $crawler = $client->request('GET', '/import/pocket/callback'); | 57 | $pocketImport = $this->getMockBuilder('Wallabag\ImportBundle\Import\PocketImport') |
58 | ->disableOriginalConstructor() | ||
59 | ->getMock(); | ||
60 | |||
61 | $pocketImport | ||
62 | ->expects($this->once()) | ||
63 | ->method('authorize') | ||
64 | ->willReturn(false); | ||
65 | |||
66 | static::$kernel->getContainer()->set('wallabag_import.pocket.import', $pocketImport); | ||
67 | |||
68 | $client->request('GET', '/import/pocket/callback'); | ||
60 | 69 | ||
61 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | 70 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); |
62 | $this->assertContains('import/pocket', $client->getResponse()->headers->get('location')); | 71 | $this->assertContains('/', $client->getResponse()->headers->get('location'), 'Import is ok, redirect to homepage'); |
63 | $this->assertEquals('flashes.import.notice.failed', $client->getContainer()->get('session')->getFlashBag()->peek('notice')[0]); | 72 | $this->assertEquals('flashes.import.notice.failed', $client->getContainer()->get('session')->getFlashBag()->peek('notice')[0]); |
64 | } | 73 | } |
74 | |||
75 | public function testImportPocketCallback() | ||
76 | { | ||
77 | $this->logInAs('admin'); | ||
78 | $client = $this->getClient(); | ||
79 | |||
80 | $pocketImport = $this->getMockBuilder('Wallabag\ImportBundle\Import\PocketImport') | ||
81 | ->disableOriginalConstructor() | ||
82 | ->getMock(); | ||
83 | |||
84 | $pocketImport | ||
85 | ->expects($this->once()) | ||
86 | ->method('authorize') | ||
87 | ->willReturn(true); | ||
88 | |||
89 | $pocketImport | ||
90 | ->expects($this->once()) | ||
91 | ->method('setMarkAsRead') | ||
92 | ->with(false) | ||
93 | ->willReturn($pocketImport); | ||
94 | |||
95 | $pocketImport | ||
96 | ->expects($this->once()) | ||
97 | ->method('import') | ||
98 | ->willReturn(true); | ||
99 | |||
100 | static::$kernel->getContainer()->set('wallabag_import.pocket.import', $pocketImport); | ||
101 | |||
102 | $client->request('GET', '/import/pocket/callback'); | ||
103 | |||
104 | $this->assertEquals(302, $client->getResponse()->getStatusCode()); | ||
105 | $this->assertContains('/', $client->getResponse()->headers->get('location'), 'Import is ok, redirect to homepage'); | ||
106 | $this->assertEquals('flashes.import.notice.summary', $client->getContainer()->get('session')->getFlashBag()->peek('notice')[0]); | ||
107 | } | ||
65 | } | 108 | } |
diff --git a/tests/Wallabag/ImportBundle/fixtures/unnamed.png b/tests/Wallabag/ImportBundle/fixtures/unnamed.png new file mode 100644 index 00000000..e6dd9caa --- /dev/null +++ b/tests/Wallabag/ImportBundle/fixtures/unnamed.png | |||
Binary files differ | |||