From 9de9f1e5ceed4ac7ecd27e1cb808e630a831f94b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 3 May 2017 10:23:49 +0200 Subject: Add a live test for restricted article MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It is not aimed to test if we can get the full article (since we aren't using real login/password) but mostly to test the full work (with authentication, etc.) Do not clean fixtured to avoid SQLite to re-use id for entry tag relation 😓 --- .../CoreBundle/Controller/EntryControllerTest.php | 53 ++++++++++++++++++++++ .../Controller/SiteCredentialControllerTest.php | 44 +++++++++--------- .../GrabySiteConfigBuilderTest.php | 6 +-- 3 files changed, 78 insertions(+), 25 deletions(-) (limited to 'tests/Wallabag') diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 8f5c372d..104f60f1 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -5,6 +5,7 @@ namespace Tests\Wallabag\CoreBundle\Controller; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; use Wallabag\CoreBundle\Entity\Config; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\CoreBundle\Entity\SiteCredential; class EntryControllerTest extends WallabagCoreTestCase { @@ -1321,4 +1322,56 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertEquals($url, $content->getUrl()); $this->assertEquals($expectedLanguage, $content->getLanguage()); } + + /** + * This test will require an internet connection. + */ + public function testRestrictedArticle() + { + $url = 'http://www.monde-diplomatique.fr/2017/05/BONNET/57475'; + $this->logInAs('admin'); + $client = $this->getClient(); + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + + // enable restricted access + $client->getContainer()->get('craue_config')->set('restricted_access', 1); + + // create a new site_credential + $user = $client->getContainer()->get('security.token_storage')->getToken()->getUser(); + $credential = new SiteCredential($user); + $credential->setHost('monde-diplomatique.fr'); + $credential->setUsername('foo'); + $credential->setPassword('bar'); + + $em->persist($credential); + $em->flush(); + + $crawler = $client->request('GET', '/new'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('form[name=entry]')->form(); + + $data = [ + 'entry[url]' => $url, + ]; + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertContains('flashes.entry.notice.entry_saved', $crawler->filter('body')->extract(['_text'])[0]); + + $content = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($url, $this->getLoggedInUserId()); + + $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content); + $this->assertSame('Crimes et réformes aux Philippines', $content->getTitle()); + + $client->getContainer()->get('craue_config')->set('restricted_access', 0); + } } diff --git a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php index 47bf0907..7e6dafee 100644 --- a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php @@ -2,7 +2,9 @@ namespace Tests\Wallabag\CoreBundle\Controller; +use Symfony\Bundle\FrameworkBundle\Client; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; +use Wallabag\CoreBundle\Entity\SiteCredential; class SiteCredentialControllerTest extends WallabagCoreTestCase { @@ -52,18 +54,12 @@ class SiteCredentialControllerTest extends WallabagCoreTestCase $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]); } - /** - * @depends testNewSiteCredential - */ public function testEditSiteCredential() { $this->logInAs('admin'); $client = $this->getClient(); - $credential = $client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:SiteCredential') - ->findOneByHost('google.io'); + $credential = $this->createSiteCredential($client); $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); @@ -92,36 +88,26 @@ class SiteCredentialControllerTest extends WallabagCoreTestCase $this->assertContains('larry', $crawler->filter('input[id=site_credential_username]')->attr('value')); } - /** - * @depends testNewSiteCredential - */ public function testEditFromADifferentUserSiteCredential() { - $this->logInAs('bob'); + $this->logInAs('admin'); $client = $this->getClient(); - $credential = $client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:SiteCredential') - ->findOneByHost('google.io'); + $credential = $this->createSiteCredential($client); + + $this->logInAs('bob'); $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); $this->assertEquals(403, $client->getResponse()->getStatusCode()); } - /** - * @depends testNewSiteCredential - */ public function testDeleteSiteCredential() { $this->logInAs('admin'); $client = $this->getClient(); - $credential = $client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:SiteCredential') - ->findOneByHost('google.io'); + $credential = $this->createSiteCredential($client); $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); @@ -137,4 +123,18 @@ class SiteCredentialControllerTest extends WallabagCoreTestCase $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]); } + + private function createSiteCredential(Client $client) + { + $credential = new SiteCredential($this->getLoggedInUser()); + $credential->setHost('google.io'); + $credential->setUsername('sergei'); + $credential->setPassword('microsoft'); + + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + $em->persist($credential); + $em->flush(); + + return $credential; + } } diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 980f7579..1e1e8989 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php @@ -25,7 +25,7 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase $grabySiteConfig = new GrabySiteConfig(); $grabySiteConfig->requires_login = true; - $grabySiteConfig->login_uri = 'http://example.com/login'; + $grabySiteConfig->login_uri = 'http://www.example.com/login'; $grabySiteConfig->login_username_field = 'login'; $grabySiteConfig->login_password_field = 'password'; $grabySiteConfig->login_extra_fields = ['field=value']; @@ -67,13 +67,13 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase $logger ); - $config = $this->builder->buildForHost('example.com'); + $config = $this->builder->buildForHost('www.example.com'); $this->assertEquals( new SiteConfig([ 'host' => 'example.com', 'requiresLogin' => true, - 'loginUri' => 'http://example.com/login', + 'loginUri' => 'http://www.example.com/login', 'usernameField' => 'login', 'passwordField' => 'password', 'extraFields' => ['field' => 'value'], -- cgit v1.2.3