From f45496336f5bbd31b69553fcfae9cdfd03b280cc Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 23 Apr 2019 22:28:36 +0200 Subject: [PATCH 1/3] Add ability to match many domains for credentials MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of fetching one domain, we use the same method as in site config (to retrieve the matching file) and handle api.example.org, example.org, .org (yes the last one isn’t useful). If one of these match, we got it and use it. --- .../GrabySiteConfigBuilder.php | 12 +++++++++++- .../Repository/SiteCredentialRepository.php | 8 ++++---- .../Resources/translations/messages.da.yml | 2 +- .../Resources/translations/messages.de.yml | 2 +- .../Resources/translations/messages.en.yml | 2 +- .../Resources/translations/messages.es.yml | 2 +- .../Resources/translations/messages.fa.yml | 2 +- .../Resources/translations/messages.fr.yml | 2 +- .../Resources/translations/messages.it.yml | 2 +- .../Resources/translations/messages.oc.yml | 2 +- .../Resources/translations/messages.pl.yml | 2 +- .../Resources/translations/messages.pt.yml | 2 +- .../Resources/translations/messages.ro.yml | 2 +- .../Resources/translations/messages.th.yml | 2 +- .../GrabySiteConfigBuilderTest.php | 18 +++++++++--------- 15 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php index 90e00c62d..718441bd4 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php @@ -64,7 +64,17 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder $credentials = null; if ($this->currentUser) { - $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId()); + $hosts = [$host]; + // will try to see for a host without the first subdomain (fr.example.org & .example.org) + $split = explode('.', $host); + + if (\count($split) > 1) { + // remove first subdomain + array_shift($split); + $hosts[] = '.' . implode('.', $split); + } + + $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId()); } if (null === $credentials) { diff --git a/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php index b2e212a41..aeadd7704 100644 --- a/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php +++ b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php @@ -19,16 +19,16 @@ class SiteCredentialRepository extends \Doctrine\ORM\EntityRepository /** * Retrieve one username/password for the given host and userId. * - * @param string $host - * @param int $userId + * @param array $hosts An array of host to look for + * @param int $userId * * @return array|null */ - public function findOneByHostAndUser($host, $userId) + public function findOneByHostsAndUser($hosts, $userId) { $res = $this->createQueryBuilder('s') ->select('s.username', 's.password') - ->where('s.host = :hostname')->setParameter('hostname', $host) + ->where('s.host IN (:hosts)')->setParameter('hosts', $hosts) ->andWhere('s.user = :userId')->setParameter('userId', $userId) ->setMaxResults(1) ->getQuery() diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml index 97eb874d2..6f8425340 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml @@ -550,7 +550,7 @@ site_credential: # create_new_one: Create a new credential # form: # username_label: 'Username' - # host_label: 'Host' + # host_label: 'Host (subdomain.example.org, .example.org, etc.)' # password_label: 'Password' # save: Save # delete: Delete diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml index 0cf3b138e..874908b9e 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml @@ -550,7 +550,7 @@ site_credential: create_new_one: 'Einen neuen Seitenzugang anlegen' form: username_label: 'Benutzername' - host_label: 'Host' + host_label: 'Host (subdomain.example.org, .example.org, etc.)' password_label: 'Passwort' save: 'Speichern' delete: 'Löschen' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml index 6085be140..598ad58da 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml @@ -550,7 +550,7 @@ site_credential: create_new_one: Create a new credential form: username_label: 'Username' - host_label: 'Host' + host_label: 'Host (subdomain.example.org, .example.org, etc.)' password_label: 'Password' save: Save delete: Delete diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml index f2a8fb890..f8aa41093 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml @@ -550,7 +550,7 @@ site_credential: # create_new_one: Create a new credential # form: # username_label: 'Username' - # host_label: 'Host' + # host_label: 'Host (subdomain.example.org, .example.org, etc.)' # password_label: 'Password' # save: Save # delete: Delete diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml index a5cbd7ecb..785e39ee9 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml @@ -550,7 +550,7 @@ site_credential: # create_new_one: Create a new credential # form: # username_label: 'Username' - # host_label: 'Host' + # host_label: 'Host (subdomain.example.org, .example.org, etc.)' # password_label: 'Password' # save: Save # delete: Delete diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml index a36d84ae1..b2fa1c50c 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml @@ -550,7 +550,7 @@ site_credential: create_new_one: Créer un nouvel accès à un site form: username_label: 'Identifiant' - host_label: 'Domaine' + host_label: 'Domaine (subdomain.example.org, .example.org, etc.)' password_label: 'Mot de passe' save: "Sauvegarder" delete: "Supprimer" diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml index 1649c0e4f..ecaa3b60d 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml @@ -550,7 +550,7 @@ site_credential: # create_new_one: Create a new credential # form: # username_label: 'Username' - # host_label: 'Host' + # host_label: 'Host (subdomain.example.org, .example.org, etc.)' # password_label: 'Password' # save: Save # delete: Delete diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml index e2298f1f6..848c88d23 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml @@ -550,7 +550,7 @@ site_credential: create_new_one: Crear un novèl identificant form: username_label: "Nom d'utilizaire" - host_label: 'Òste' + host_label: 'Òste (subdomain.example.org, .example.org, etc.)' password_label: 'Senhal' save: 'Enregistrar' delete: 'Suprimir' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml index a5712733b..a0032fe8f 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml @@ -550,7 +550,7 @@ site_credential: create_new_one: Stwórz nowe poświadczenie form: username_label: 'Nazwa użytkownika' - host_label: 'Host' + host_label: 'Host (subdomain.example.org, .example.org, etc.)' password_label: 'Hasło' save: Zapisz delete: Usuń diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml index 1ccf49e1b..292fad618 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml @@ -550,7 +550,7 @@ site_credential: # create_new_one: Create a new credential form: # username_label: 'Username' - # host_label: 'Host' + # host_label: 'Host (subdomain.example.org, .example.org, etc.)' # password_label: 'Password' save: 'Salvar' delete: 'Apagar' diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml index 6c0e18e1c..9e8d68b3c 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml @@ -550,7 +550,7 @@ site_credential: # create_new_one: Create a new credential # form: # username_label: 'Username' - # host_label: 'Host' + # host_label: 'Host (subdomain.example.org, .example.org, etc.)' # password_label: 'Password' # save: Save # delete: Delete diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml index 5524b1f12..cb3b0f23f 100644 --- a/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml +++ b/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml @@ -548,7 +548,7 @@ site_credential: create_new_one: สร้างข้อมูลส่วนตัวใหม่ form: username_label: 'ชื่อผู้ใช้' - host_label: 'โฮส' + host_label: 'โฮส (subdomain.example.org, .example.org, etc.)' password_label: 'รหัสผ่าน' save: บันทึก delete: ลบ diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 1173fc3de..90609180f 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php @@ -24,7 +24,7 @@ class GrabySiteConfigBuilderTest extends TestCase $grabySiteConfig = new GrabySiteConfig(); $grabySiteConfig->requires_login = true; - $grabySiteConfig->login_uri = 'http://www.example.com/login'; + $grabySiteConfig->login_uri = 'http://api.example.com/login'; $grabySiteConfig->login_username_field = 'login'; $grabySiteConfig->login_password_field = 'password'; $grabySiteConfig->login_extra_fields = ['field=value']; @@ -32,7 +32,7 @@ class GrabySiteConfigBuilderTest extends TestCase $grabyConfigBuilderMock ->method('buildForHost') - ->with('example.com') + ->with('api.example.com') ->will($this->returnValue($grabySiteConfig)); $logger = new Logger('foo'); @@ -43,8 +43,8 @@ class GrabySiteConfigBuilderTest extends TestCase ->disableOriginalConstructor() ->getMock(); $siteCrentialRepo->expects($this->once()) - ->method('findOneByHostAndUser') - ->with('example.com', 1) + ->method('findOneByHostsAndUser') + ->with(['api.example.com', '.example.com'], 1) ->willReturn(['username' => 'foo', 'password' => 'bar']); $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') @@ -66,11 +66,11 @@ class GrabySiteConfigBuilderTest extends TestCase $logger ); - $config = $this->builder->buildForHost('www.example.com'); + $config = $this->builder->buildForHost('api.example.com'); - $this->assertSame('example.com', $config->getHost()); + $this->assertSame('api.example.com', $config->getHost()); $this->assertTrue($config->requiresLogin()); - $this->assertSame('http://www.example.com/login', $config->getLoginUri()); + $this->assertSame('http://api.example.com/login', $config->getLoginUri()); $this->assertSame('login', $config->getUsernameField()); $this->assertSame('password', $config->getPasswordField()); $this->assertSame(['field' => 'value'], $config->getExtraFields()); @@ -103,8 +103,8 @@ class GrabySiteConfigBuilderTest extends TestCase ->disableOriginalConstructor() ->getMock(); $siteCrentialRepo->expects($this->once()) - ->method('findOneByHostAndUser') - ->with('unknown.com', 1) + ->method('findOneByHostsAndUser') + ->with(['unknown.com', '.com'], 1) ->willReturn(null); $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') From ff8f338dc2cf73b310626d9f4b41efc1d352e182 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Tue, 23 Apr 2019 22:48:04 +0200 Subject: [PATCH 2/3] FIx unrelated failing test --- tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 479e07000..2cd6aee37 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -166,7 +166,7 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertSame($this->url, $content->getUrl()); $this->assertContains('Google', $content->getTitle()); $this->assertSame('fr', $content->getLanguage()); - $this->assertSame('2016-04-07 19:01:35', $content->getPublishedAt()->format('Y-m-d H:i:s')); + $this->assertSame('2015-03-28 11:43:19', $content->getPublishedAt()->format('Y-m-d H:i:s')); $this->assertArrayHasKey('x-frame-options', $content->getHeaders()); $client->getContainer()->get('craue_config')->set('store_article_headers', 0); } From 35359bd3c67e5b6c6371e2e547a3411ca0a8027b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 24 Apr 2019 15:28:15 +0200 Subject: [PATCH 3/3] Adding more tests to cover different scenario --- .../ORM/LoadSiteCredentialData.php | 27 +++- .../GrabySiteConfigBuilder.php | 27 ++-- .../GrabySiteConfigBuilderTest.php | 134 ++++++++++++++++-- 3 files changed, 161 insertions(+), 27 deletions(-) diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSiteCredentialData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSiteCredentialData.php index 866f55a40..faf29da66 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSiteCredentialData.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSiteCredentialData.php @@ -5,19 +5,38 @@ namespace Wallabag\CoreBundle\DataFixtures\ORM; use Doctrine\Common\DataFixtures\AbstractFixture; use Doctrine\Common\DataFixtures\OrderedFixtureInterface; use Doctrine\Common\Persistence\ObjectManager; +use Symfony\Component\DependencyInjection\ContainerAwareInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; use Wallabag\CoreBundle\Entity\SiteCredential; -class LoadSiteCredentialData extends AbstractFixture implements OrderedFixtureInterface +class LoadSiteCredentialData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface { + /** + * @var ContainerInterface + */ + private $container; + + public function setContainer(ContainerInterface $container = null) + { + $this->container = $container; + } + /** * {@inheritdoc} */ public function load(ObjectManager $manager) { $credential = new SiteCredential($this->getReference('admin-user')); - $credential->setHost('example.com'); - $credential->setUsername('foo'); - $credential->setPassword('bar'); + $credential->setHost('.super.com'); + $credential->setUsername($this->container->get('wallabag_core.helper.crypto_proxy')->crypt('.super')); + $credential->setPassword($this->container->get('wallabag_core.helper.crypto_proxy')->crypt('bar')); + + $manager->persist($credential); + + $credential = new SiteCredential($this->getReference('admin-user')); + $credential->setHost('paywall.example.com'); + $credential->setUsername($this->container->get('wallabag_core.helper.crypto_proxy')->crypt('paywall.example')); + $credential->setPassword($this->container->get('wallabag_core.helper.crypto_proxy')->crypt('bar')); $manager->persist($credential); diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php index 718441bd4..c7502bacc 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php @@ -62,21 +62,24 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder $host = substr($host, 4); } - $credentials = null; - if ($this->currentUser) { - $hosts = [$host]; - // will try to see for a host without the first subdomain (fr.example.org & .example.org) - $split = explode('.', $host); + if (!$this->currentUser) { + $this->logger->debug('Auth: no current user defined.'); - if (\count($split) > 1) { - // remove first subdomain - array_shift($split); - $hosts[] = '.' . implode('.', $split); - } - - $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId()); + return false; } + $hosts = [$host]; + // will try to see for a host without the first subdomain (fr.example.org & .example.org) + $split = explode('.', $host); + + if (\count($split) > 1) { + // remove first subdomain + array_shift($split); + $hosts[] = '.' . implode('.', $split); + } + + $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId()); + if (null === $credentials) { $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 90609180f..845762dc1 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php @@ -5,19 +5,15 @@ namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; use Graby\SiteConfig\SiteConfig as GrabySiteConfig; use Monolog\Handler\TestHandler; use Monolog\Logger; -use PHPUnit\Framework\TestCase; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; +use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; -class GrabySiteConfigBuilderTest extends TestCase +class GrabySiteConfigBuilderTest extends WallabagCoreTestCase { - /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */ - protected $builder; - public function testBuildConfigExists() { - /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') ->disableOriginalConstructor() ->getMock(); @@ -59,14 +55,14 @@ class GrabySiteConfigBuilderTest extends TestCase $tokenStorage = new TokenStorage(); $tokenStorage->setToken($token); - $this->builder = new GrabySiteConfigBuilder( + $builder = new GrabySiteConfigBuilder( $grabyConfigBuilderMock, $tokenStorage, $siteCrentialRepo, $logger ); - $config = $this->builder->buildForHost('api.example.com'); + $config = $builder->buildForHost('api.example.com'); $this->assertSame('api.example.com', $config->getHost()); $this->assertTrue($config->requiresLogin()); @@ -85,7 +81,6 @@ class GrabySiteConfigBuilderTest extends TestCase public function testBuildConfigDoesntExist() { - /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */ $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') ->disableOriginalConstructor() ->getMock(); @@ -119,14 +114,14 @@ class GrabySiteConfigBuilderTest extends TestCase $tokenStorage = new TokenStorage(); $tokenStorage->setToken($token); - $this->builder = new GrabySiteConfigBuilder( + $builder = new GrabySiteConfigBuilder( $grabyConfigBuilderMock, $tokenStorage, $siteCrentialRepo, $logger ); - $config = $this->builder->buildForHost('unknown.com'); + $config = $builder->buildForHost('unknown.com'); $this->assertFalse($config); @@ -134,4 +129,121 @@ class GrabySiteConfigBuilderTest extends TestCase $this->assertCount(1, $records, 'One log was recorded'); } + + public function testBuildConfigUserNotDefined() + { + $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') + ->disableOriginalConstructor() + ->getMock(); + + $grabyConfigBuilderMock + ->method('buildForHost') + ->with('unknown.com') + ->will($this->returnValue(new GrabySiteConfig())); + + $logger = new Logger('foo'); + $handler = new TestHandler(); + $logger->pushHandler($handler); + + $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository') + ->disableOriginalConstructor() + ->getMock(); + + $tokenStorage = new TokenStorage(); + + $builder = new GrabySiteConfigBuilder( + $grabyConfigBuilderMock, + $tokenStorage, + $siteCrentialRepo, + $logger + ); + + $config = $builder->buildForHost('unknown.com'); + + $this->assertFalse($config); + } + + public function dataProviderCredentials() + { + return [ + [ + 'host' => 'example.com', + ], + [ + 'host' => 'other.example.com', + ], + [ + 'host' => 'paywall.example.com', + 'expectedUsername' => 'paywall.example', + 'expectedPassword' => 'bar', + ], + [ + 'host' => 'api.super.com', + 'expectedUsername' => '.super', + 'expectedPassword' => 'bar', + ], + [ + 'host' => '.super.com', + 'expectedUsername' => '.super', + 'expectedPassword' => 'bar', + ], + ]; + } + + /** + * @dataProvider dataProviderCredentials + */ + public function testBuildConfigWithDbAccess($host, $expectedUsername = null, $expectedPassword = null) + { + $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') + ->disableOriginalConstructor() + ->getMock(); + + $grabySiteConfig = new GrabySiteConfig(); + $grabySiteConfig->requires_login = true; + $grabySiteConfig->login_uri = 'http://api.example.com/login'; + $grabySiteConfig->login_username_field = 'login'; + $grabySiteConfig->login_password_field = 'password'; + $grabySiteConfig->login_extra_fields = ['field=value']; + $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]'; + + $grabyConfigBuilderMock + ->method('buildForHost') + ->with($host) + ->will($this->returnValue($grabySiteConfig)); + + $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); + + $logger = new Logger('foo'); + $handler = new TestHandler(); + $logger->pushHandler($handler); + + $builder = new GrabySiteConfigBuilder( + $grabyConfigBuilderMock, + $tokenStorage, + $this->getClient()->getContainer()->get('wallabag_core.site_credential_repository'), + $logger + ); + + $config = $builder->buildForHost($host); + + if (null === $expectedUsername && null === $expectedPassword) { + $this->assertFalse($config); + + return; + } + + $this->assertSame($expectedUsername, $config->getUsername()); + $this->assertSame($expectedPassword, $config->getPassword()); + } }