Add a live test for restricted article

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 😓
This commit is contained in:
Jeremy Benoist 2017-05-03 10:23:49 +02:00
parent fd7fde9515
commit 9de9f1e5ce
No known key found for this signature in database
GPG key ID: BCA73962457ACC3C
5 changed files with 81 additions and 31 deletions

View file

@ -24,9 +24,7 @@ class SiteCredentialController extends Controller
*/ */
public function indexAction() public function indexAction()
{ {
$em = $this->getDoctrine()->getManager(); $credentials = $this->get('wallabag_core.site_credential_repository')->findByUser($this->getUser());
$credentials = $em->getRepository('WallabagCoreBundle:SiteCredential')->findByUser($this->getUser());
return $this->render('WallabagCoreBundle:SiteCredential:index.html.twig', array( return $this->render('WallabagCoreBundle:SiteCredential:index.html.twig', array(
'credentials' => $credentials, 'credentials' => $credentials,

View file

@ -31,14 +31,13 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
*/ */
private $currentUser; private $currentUser;
/** /**
* GrabySiteConfigBuilder constructor. * GrabySiteConfigBuilder constructor.
* *
* @param ConfigBuilder $grabyConfigBuilder * @param ConfigBuilder $grabyConfigBuilder
* @param TokenStorage $token * @param TokenStorage $token
* @param SiteCredentialRepository $credentialRepository * @param SiteCredentialRepository $credentialRepository
* @param LoggerInterface $logger * @param LoggerInterface $logger
*/ */
public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger) public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger)
{ {

View file

@ -5,6 +5,7 @@ namespace Tests\Wallabag\CoreBundle\Controller;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\Config; use Wallabag\CoreBundle\Entity\Config;
use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\SiteCredential;
class EntryControllerTest extends WallabagCoreTestCase class EntryControllerTest extends WallabagCoreTestCase
{ {
@ -1321,4 +1322,56 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->assertEquals($url, $content->getUrl()); $this->assertEquals($url, $content->getUrl());
$this->assertEquals($expectedLanguage, $content->getLanguage()); $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);
}
} }

View file

@ -2,7 +2,9 @@
namespace Tests\Wallabag\CoreBundle\Controller; namespace Tests\Wallabag\CoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Client;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\SiteCredential;
class SiteCredentialControllerTest extends WallabagCoreTestCase 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]); $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]);
} }
/**
* @depends testNewSiteCredential
*/
public function testEditSiteCredential() public function testEditSiteCredential()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$credential = $client->getContainer() $credential = $this->createSiteCredential($client);
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:SiteCredential')
->findOneByHost('google.io');
$crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); $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')); $this->assertContains('larry', $crawler->filter('input[id=site_credential_username]')->attr('value'));
} }
/**
* @depends testNewSiteCredential
*/
public function testEditFromADifferentUserSiteCredential() public function testEditFromADifferentUserSiteCredential()
{ {
$this->logInAs('bob'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$credential = $client->getContainer() $credential = $this->createSiteCredential($client);
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:SiteCredential') $this->logInAs('bob');
->findOneByHost('google.io');
$client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); $client->request('GET', '/site-credentials/'.$credential->getId().'/edit');
$this->assertEquals(403, $client->getResponse()->getStatusCode()); $this->assertEquals(403, $client->getResponse()->getStatusCode());
} }
/**
* @depends testNewSiteCredential
*/
public function testDeleteSiteCredential() public function testDeleteSiteCredential()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$credential = $client->getContainer() $credential = $this->createSiteCredential($client);
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:SiteCredential')
->findOneByHost('google.io');
$crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); $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]); $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;
}
} }

View file

@ -25,7 +25,7 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
$grabySiteConfig = new GrabySiteConfig(); $grabySiteConfig = new GrabySiteConfig();
$grabySiteConfig->requires_login = true; $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_username_field = 'login';
$grabySiteConfig->login_password_field = 'password'; $grabySiteConfig->login_password_field = 'password';
$grabySiteConfig->login_extra_fields = ['field=value']; $grabySiteConfig->login_extra_fields = ['field=value'];
@ -67,13 +67,13 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase
$logger $logger
); );
$config = $this->builder->buildForHost('example.com'); $config = $this->builder->buildForHost('www.example.com');
$this->assertEquals( $this->assertEquals(
new SiteConfig([ new SiteConfig([
'host' => 'example.com', 'host' => 'example.com',
'requiresLogin' => true, 'requiresLogin' => true,
'loginUri' => 'http://example.com/login', 'loginUri' => 'http://www.example.com/login',
'usernameField' => 'login', 'usernameField' => 'login',
'passwordField' => 'password', 'passwordField' => 'password',
'extraFields' => ['field' => 'value'], 'extraFields' => ['field' => 'value'],