From b8427f22f06cab58383ec3080f09715c712c65ef Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Mon, 1 May 2017 22:13:35 +0200 Subject: Add menu access to site credentials CRUD --- .../Controller/SiteCredentialControllerTest.php | 140 +++++++++++++++++++++ .../GrabySiteConfigBuilderTest.php | 50 +++++++- 2 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php (limited to 'tests/Wallabag') diff --git a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php new file mode 100644 index 00000000..47bf0907 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php @@ -0,0 +1,140 @@ +logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/site-credentials/'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $body = $crawler->filter('body')->extract(['_text'])[0]; + + $this->assertContains('site_credential.description', $body); + $this->assertContains('site_credential.list.create_new_one', $body); + } + + public function testNewSiteCredential() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/site-credentials/new'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $body = $crawler->filter('body')->extract(['_text'])[0]; + + $this->assertContains('site_credential.new_site_credential', $body); + $this->assertContains('site_credential.form.back_to_list', $body); + + $form = $crawler->filter('button[id=site_credential_save]')->form(); + + $data = [ + 'site_credential[host]' => 'google.io', + 'site_credential[username]' => 'sergei', + 'site_credential[password]' => 'microsoft', + ]; + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $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'); + + $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $body = $crawler->filter('body')->extract(['_text'])[0]; + + $this->assertContains('site_credential.edit_site_credential', $body); + $this->assertContains('site_credential.form.back_to_list', $body); + + $form = $crawler->filter('button[id=site_credential_save]')->form(); + + $data = [ + 'site_credential[host]' => 'google.io', + 'site_credential[username]' => 'larry', + 'site_credential[password]' => 'microsoft', + ]; + + $client->submit($form, $data); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]); + $this->assertContains('larry', $crawler->filter('input[id=site_credential_username]')->attr('value')); + } + + /** + * @depends testNewSiteCredential + */ + public function testEditFromADifferentUserSiteCredential() + { + $this->logInAs('bob'); + $client = $this->getClient(); + + $credential = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:SiteCredential') + ->findOneByHost('google.io'); + + $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'); + + $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form(); + + $client->submit($deleteForm, []); + + $this->assertEquals(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]); + } +} diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 8b50bce9..980f7579 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php @@ -8,6 +8,8 @@ use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; use Graby\SiteConfig\SiteConfig as GrabySiteConfig; use PHPUnit_Framework_TestCase; use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; +use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase { @@ -17,7 +19,7 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase public function testBuildConfigExists() { /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ - $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') + $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') ->disableOriginalConstructor() ->getMock(); @@ -38,9 +40,30 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase $handler = new TestHandler(); $logger->pushHandler($handler); + $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository') + ->disableOriginalConstructor() + ->getMock(); + $siteCrentialRepo->expects($this->once()) + ->method('findOneByHostAndUser') + ->with('example.com', 1) + ->willReturn(['username' => 'foo', 'password' => 'bar']); + + $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') + ->disableOriginalConstructor() + ->getMock(); + $user->expects($this->once()) + ->method('getId') + ->willReturn(1); + + $token = new UsernamePasswordToken($user, 'pass', 'provider'); + + $tokenStorage = new TokenStorage(); + $tokenStorage->setToken($token); + $this->builder = new GrabySiteConfigBuilder( $grabyConfigBuilderMock, - ['example.com' => ['username' => 'foo', 'password' => 'bar']], + $tokenStorage, + $siteCrentialRepo, $logger ); @@ -82,9 +105,30 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase $handler = new TestHandler(); $logger->pushHandler($handler); + $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository') + ->disableOriginalConstructor() + ->getMock(); + $siteCrentialRepo->expects($this->once()) + ->method('findOneByHostAndUser') + ->with('unknown.com', 1) + ->willReturn(null); + + $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') + ->disableOriginalConstructor() + ->getMock(); + $user->expects($this->once()) + ->method('getId') + ->willReturn(1); + + $token = new UsernamePasswordToken($user, 'pass', 'provider'); + + $tokenStorage = new TokenStorage(); + $tokenStorage->setToken($token); + $this->builder = new GrabySiteConfigBuilder( $grabyConfigBuilderMock, - [], + $tokenStorage, + $siteCrentialRepo, $logger ); -- cgit v1.2.3 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 From 906424c1b6fd884bf2081bfe6dd0b1f9651c2801 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 11 Jun 2017 23:05:19 +0200 Subject: Crypt site credential password --- .../CoreBundle/Controller/EntryControllerTest.php | 2 +- .../GrabySiteConfigBuilderTest.php | 3 +- .../Wallabag/CoreBundle/Helper/CryptoProxyTest.php | 40 ++++++++++++++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php (limited to 'tests/Wallabag') diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 104f60f1..80380685 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -1341,7 +1341,7 @@ class EntryControllerTest extends WallabagCoreTestCase $credential = new SiteCredential($user); $credential->setHost('monde-diplomatique.fr'); $credential->setUsername('foo'); - $credential->setPassword('bar'); + $credential->setPassword($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('bar')); $em->persist($credential); $em->flush(); diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 1e1e8989..b0c81e7b 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php @@ -6,12 +6,11 @@ use Monolog\Handler\TestHandler; use Monolog\Logger; use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig; use Graby\SiteConfig\SiteConfig as GrabySiteConfig; -use PHPUnit_Framework_TestCase; use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; -class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase +class GrabySiteConfigBuilderTest extends \PHPUnit_Framework_TestCase { /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */ protected $builder; diff --git a/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php new file mode 100644 index 00000000..cede8696 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Helper/CryptoProxyTest.php @@ -0,0 +1,40 @@ +crypt('test'); + $decrypted = $crypto->decrypt($crypted); + + $this->assertSame('test', $decrypted); + + $records = $logHandler->getRecords(); + $this->assertCount(2, $records); + $this->assertContains('Crypto: crypting value', $records[0]['message']); + $this->assertContains('Crypto: decrypting value', $records[1]['message']); + } + + /** + * @expectedException RuntimeException + * @expectedExceptionMessage Decrypt fail + * + * @return [type] [description] + */ + public function testDecryptBadValue() + { + $crypto = new CryptoProxy(sys_get_temp_dir().'/'.uniqid('', true).'.txt', new NullLogger()); + $crypto->decrypt('badvalue'); + } +} -- cgit v1.2.3 From bead8b42da4f17238dc0d5e0f90184b224ec5df7 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Wed, 14 Jun 2017 15:02:34 +0200 Subject: Fix reviews Encrypt username too Redirect to list after saving credentials Fix typos Signed-off-by: Thomas Citharel --- tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php | 2 +- tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'tests/Wallabag') diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 80380685..f17dc97b 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -1340,7 +1340,7 @@ class EntryControllerTest extends WallabagCoreTestCase $user = $client->getContainer()->get('security.token_storage')->getToken()->getUser(); $credential = new SiteCredential($user); $credential->setHost('monde-diplomatique.fr'); - $credential->setUsername('foo'); + $credential->setUsername($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('foo')); $credential->setPassword($client->getContainer()->get('wallabag_core.helper.crypto_proxy')->crypt('bar')); $em->persist($credential); diff --git a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php index 7e6dafee..e73a9743 100644 --- a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php @@ -85,7 +85,6 @@ class SiteCredentialControllerTest extends WallabagCoreTestCase $crawler = $client->followRedirect(); $this->assertContains('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]); - $this->assertContains('larry', $crawler->filter('input[id=site_credential_username]')->attr('value')); } public function testEditFromADifferentUserSiteCredential() -- cgit v1.2.3