aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJérémy Benoist <j0k3r@users.noreply.github.com>2019-04-27 10:58:26 +0200
committerGitHub <noreply@github.com>2019-04-27 10:58:26 +0200
commitea54c2adb126a77c244c30059c40149754d4dbb7 (patch)
tree7f9fee18f321bef35ed845ce0f921dab2bddc72b
parentbfd69c74e5b4f2ebfcb304b1983bf771c2bb11f7 (diff)
parent35359bd3c67e5b6c6371e2e547a3411ca0a8027b (diff)
downloadwallabag-ea54c2adb126a77c244c30059c40149754d4dbb7.tar.gz
wallabag-ea54c2adb126a77c244c30059c40149754d4dbb7.tar.zst
wallabag-ea54c2adb126a77c244c30059c40149754d4dbb7.zip
Merge pull request #3937 from wallabag/fix/credential-subdomain
Add ability to match many domains for credentials
-rw-r--r--src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSiteCredentialData.php27
-rw-r--r--src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php19
-rw-r--r--src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php8
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.da.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.de.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.en.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.es.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.it.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml2
-rw-r--r--src/Wallabag/CoreBundle/Resources/translations/messages.th.yml2
-rw-r--r--tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php2
-rw-r--r--tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php150
17 files changed, 187 insertions, 43 deletions
diff --git a/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSiteCredentialData.php b/src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSiteCredentialData.php
index 866f55a4..faf29da6 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;
5use Doctrine\Common\DataFixtures\AbstractFixture; 5use Doctrine\Common\DataFixtures\AbstractFixture;
6use Doctrine\Common\DataFixtures\OrderedFixtureInterface; 6use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
7use Doctrine\Common\Persistence\ObjectManager; 7use Doctrine\Common\Persistence\ObjectManager;
8use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9use Symfony\Component\DependencyInjection\ContainerInterface;
8use Wallabag\CoreBundle\Entity\SiteCredential; 10use Wallabag\CoreBundle\Entity\SiteCredential;
9 11
10class LoadSiteCredentialData extends AbstractFixture implements OrderedFixtureInterface 12class LoadSiteCredentialData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
11{ 13{
12 /** 14 /**
15 * @var ContainerInterface
16 */
17 private $container;
18
19 public function setContainer(ContainerInterface $container = null)
20 {
21 $this->container = $container;
22 }
23
24 /**
13 * {@inheritdoc} 25 * {@inheritdoc}
14 */ 26 */
15 public function load(ObjectManager $manager) 27 public function load(ObjectManager $manager)
16 { 28 {
17 $credential = new SiteCredential($this->getReference('admin-user')); 29 $credential = new SiteCredential($this->getReference('admin-user'));
18 $credential->setHost('example.com'); 30 $credential->setHost('.super.com');
19 $credential->setUsername('foo'); 31 $credential->setUsername($this->container->get('wallabag_core.helper.crypto_proxy')->crypt('.super'));
20 $credential->setPassword('bar'); 32 $credential->setPassword($this->container->get('wallabag_core.helper.crypto_proxy')->crypt('bar'));
33
34 $manager->persist($credential);
35
36 $credential = new SiteCredential($this->getReference('admin-user'));
37 $credential->setHost('paywall.example.com');
38 $credential->setUsername($this->container->get('wallabag_core.helper.crypto_proxy')->crypt('paywall.example'));
39 $credential->setPassword($this->container->get('wallabag_core.helper.crypto_proxy')->crypt('bar'));
21 40
22 $manager->persist($credential); 41 $manager->persist($credential);
23 42
diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
index 90e00c62..c7502bac 100644
--- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
+++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
@@ -62,11 +62,24 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder
62 $host = substr($host, 4); 62 $host = substr($host, 4);
63 } 63 }
64 64
65 $credentials = null; 65 if (!$this->currentUser) {
66 if ($this->currentUser) { 66 $this->logger->debug('Auth: no current user defined.');
67 $credentials = $this->credentialRepository->findOneByHostAndUser($host, $this->currentUser->getId()); 67
68 return false;
69 }
70
71 $hosts = [$host];
72 // will try to see for a host without the first subdomain (fr.example.org & .example.org)
73 $split = explode('.', $host);
74
75 if (\count($split) > 1) {
76 // remove first subdomain
77 array_shift($split);
78 $hosts[] = '.' . implode('.', $split);
68 } 79 }
69 80
81 $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId());
82
70 if (null === $credentials) { 83 if (null === $credentials) {
71 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); 84 $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]);
72 85
diff --git a/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php b/src/Wallabag/CoreBundle/Repository/SiteCredentialRepository.php
index b2e212a4..aeadd770 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
19 /** 19 /**
20 * Retrieve one username/password for the given host and userId. 20 * Retrieve one username/password for the given host and userId.
21 * 21 *
22 * @param string $host 22 * @param array $hosts An array of host to look for
23 * @param int $userId 23 * @param int $userId
24 * 24 *
25 * @return array|null 25 * @return array|null
26 */ 26 */
27 public function findOneByHostAndUser($host, $userId) 27 public function findOneByHostsAndUser($hosts, $userId)
28 { 28 {
29 $res = $this->createQueryBuilder('s') 29 $res = $this->createQueryBuilder('s')
30 ->select('s.username', 's.password') 30 ->select('s.username', 's.password')
31 ->where('s.host = :hostname')->setParameter('hostname', $host) 31 ->where('s.host IN (:hosts)')->setParameter('hosts', $hosts)
32 ->andWhere('s.user = :userId')->setParameter('userId', $userId) 32 ->andWhere('s.user = :userId')->setParameter('userId', $userId)
33 ->setMaxResults(1) 33 ->setMaxResults(1)
34 ->getQuery() 34 ->getQuery()
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.da.yml
index 97eb874d..6f842534 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:
550 # create_new_one: Create a new credential 550 # create_new_one: Create a new credential
551 # form: 551 # form:
552 # username_label: 'Username' 552 # username_label: 'Username'
553 # host_label: 'Host' 553 # host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 # password_label: 'Password' 554 # password_label: 'Password'
555 # save: Save 555 # save: Save
556 # delete: Delete 556 # delete: Delete
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.de.yml
index 0cf3b138..874908b9 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:
550 create_new_one: 'Einen neuen Seitenzugang anlegen' 550 create_new_one: 'Einen neuen Seitenzugang anlegen'
551 form: 551 form:
552 username_label: 'Benutzername' 552 username_label: 'Benutzername'
553 host_label: 'Host' 553 host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 password_label: 'Passwort' 554 password_label: 'Passwort'
555 save: 'Speichern' 555 save: 'Speichern'
556 delete: 'Löschen' 556 delete: 'Löschen'
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.en.yml
index 6085be14..598ad58d 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:
550 create_new_one: Create a new credential 550 create_new_one: Create a new credential
551 form: 551 form:
552 username_label: 'Username' 552 username_label: 'Username'
553 host_label: 'Host' 553 host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 password_label: 'Password' 554 password_label: 'Password'
555 save: Save 555 save: Save
556 delete: Delete 556 delete: Delete
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.es.yml
index f2a8fb89..f8aa4109 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:
550 # create_new_one: Create a new credential 550 # create_new_one: Create a new credential
551 # form: 551 # form:
552 # username_label: 'Username' 552 # username_label: 'Username'
553 # host_label: 'Host' 553 # host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 # password_label: 'Password' 554 # password_label: 'Password'
555 # save: Save 555 # save: Save
556 # delete: Delete 556 # delete: Delete
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fa.yml
index a5cbd7ec..785e39ee 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:
550 # create_new_one: Create a new credential 550 # create_new_one: Create a new credential
551 # form: 551 # form:
552 # username_label: 'Username' 552 # username_label: 'Username'
553 # host_label: 'Host' 553 # host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 # password_label: 'Password' 554 # password_label: 'Password'
555 # save: Save 555 # save: Save
556 # delete: Delete 556 # delete: Delete
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.fr.yml
index a36d84ae..b2fa1c50 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:
550 create_new_one: Créer un nouvel accès à un site 550 create_new_one: Créer un nouvel accès à un site
551 form: 551 form:
552 username_label: 'Identifiant' 552 username_label: 'Identifiant'
553 host_label: 'Domaine' 553 host_label: 'Domaine (subdomain.example.org, .example.org, etc.)'
554 password_label: 'Mot de passe' 554 password_label: 'Mot de passe'
555 save: "Sauvegarder" 555 save: "Sauvegarder"
556 delete: "Supprimer" 556 delete: "Supprimer"
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.it.yml
index 1649c0e4..ecaa3b60 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:
550 # create_new_one: Create a new credential 550 # create_new_one: Create a new credential
551 # form: 551 # form:
552 # username_label: 'Username' 552 # username_label: 'Username'
553 # host_label: 'Host' 553 # host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 # password_label: 'Password' 554 # password_label: 'Password'
555 # save: Save 555 # save: Save
556 # delete: Delete 556 # delete: Delete
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.oc.yml
index e2298f1f..848c88d2 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:
550 create_new_one: Crear un novèl identificant 550 create_new_one: Crear un novèl identificant
551 form: 551 form:
552 username_label: "Nom d'utilizaire" 552 username_label: "Nom d'utilizaire"
553 host_label: 'Òste' 553 host_label: 'Òste (subdomain.example.org, .example.org, etc.)'
554 password_label: 'Senhal' 554 password_label: 'Senhal'
555 save: 'Enregistrar' 555 save: 'Enregistrar'
556 delete: 'Suprimir' 556 delete: 'Suprimir'
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.pl.yml
index a5712733..a0032fe8 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:
550 create_new_one: Stwórz nowe poświadczenie 550 create_new_one: Stwórz nowe poświadczenie
551 form: 551 form:
552 username_label: 'Nazwa użytkownika' 552 username_label: 'Nazwa użytkownika'
553 host_label: 'Host' 553 host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 password_label: 'Hasło' 554 password_label: 'Hasło'
555 save: Zapisz 555 save: Zapisz
556 delete: Usuń 556 delete: Usuń
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.pt.yml
index 1ccf49e1..292fad61 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:
550 # create_new_one: Create a new credential 550 # create_new_one: Create a new credential
551 form: 551 form:
552 # username_label: 'Username' 552 # username_label: 'Username'
553 # host_label: 'Host' 553 # host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 # password_label: 'Password' 554 # password_label: 'Password'
555 save: 'Salvar' 555 save: 'Salvar'
556 delete: 'Apagar' 556 delete: 'Apagar'
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.ro.yml
index 6c0e18e1..9e8d68b3 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:
550 # create_new_one: Create a new credential 550 # create_new_one: Create a new credential
551 # form: 551 # form:
552 # username_label: 'Username' 552 # username_label: 'Username'
553 # host_label: 'Host' 553 # host_label: 'Host (subdomain.example.org, .example.org, etc.)'
554 # password_label: 'Password' 554 # password_label: 'Password'
555 # save: Save 555 # save: Save
556 # delete: Delete 556 # delete: Delete
diff --git a/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml b/src/Wallabag/CoreBundle/Resources/translations/messages.th.yml
index 5524b1f1..cb3b0f23 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:
548 create_new_one: สร้างข้อมูลส่วนตัวใหม่ 548 create_new_one: สร้างข้อมูลส่วนตัวใหม่
549 form: 549 form:
550 username_label: 'ชื่อผู้ใช้' 550 username_label: 'ชื่อผู้ใช้'
551 host_label: 'โฮส' 551 host_label: 'โฮส (subdomain.example.org, .example.org, etc.)'
552 password_label: 'รหัสผ่าน' 552 password_label: 'รหัสผ่าน'
553 save: บันทึก 553 save: บันทึก
554 delete: ลบ 554 delete: ลบ
diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
index 479e0700..2cd6aee3 100644
--- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
+++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
@@ -166,7 +166,7 @@ class EntryControllerTest extends WallabagCoreTestCase
166 $this->assertSame($this->url, $content->getUrl()); 166 $this->assertSame($this->url, $content->getUrl());
167 $this->assertContains('Google', $content->getTitle()); 167 $this->assertContains('Google', $content->getTitle());
168 $this->assertSame('fr', $content->getLanguage()); 168 $this->assertSame('fr', $content->getLanguage());
169 $this->assertSame('2016-04-07 19:01:35', $content->getPublishedAt()->format('Y-m-d H:i:s')); 169 $this->assertSame('2015-03-28 11:43:19', $content->getPublishedAt()->format('Y-m-d H:i:s'));
170 $this->assertArrayHasKey('x-frame-options', $content->getHeaders()); 170 $this->assertArrayHasKey('x-frame-options', $content->getHeaders());
171 $client->getContainer()->get('craue_config')->set('store_article_headers', 0); 171 $client->getContainer()->get('craue_config')->set('store_article_headers', 0);
172 } 172 }
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
index 1173fc3d..845762dc 100644
--- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
+++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
@@ -5,26 +5,22 @@ namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator;
5use Graby\SiteConfig\SiteConfig as GrabySiteConfig; 5use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
6use Monolog\Handler\TestHandler; 6use Monolog\Handler\TestHandler;
7use Monolog\Logger; 7use Monolog\Logger;
8use PHPUnit\Framework\TestCase;
9use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; 8use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
10use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; 9use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
10use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
11use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; 11use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder;
12 12
13class GrabySiteConfigBuilderTest extends TestCase 13class GrabySiteConfigBuilderTest extends WallabagCoreTestCase
14{ 14{
15 /** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */
16 protected $builder;
17
18 public function testBuildConfigExists() 15 public function testBuildConfigExists()
19 { 16 {
20 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
21 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') 17 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
22 ->disableOriginalConstructor() 18 ->disableOriginalConstructor()
23 ->getMock(); 19 ->getMock();
24 20
25 $grabySiteConfig = new GrabySiteConfig(); 21 $grabySiteConfig = new GrabySiteConfig();
26 $grabySiteConfig->requires_login = true; 22 $grabySiteConfig->requires_login = true;
27 $grabySiteConfig->login_uri = 'http://www.example.com/login'; 23 $grabySiteConfig->login_uri = 'http://api.example.com/login';
28 $grabySiteConfig->login_username_field = 'login'; 24 $grabySiteConfig->login_username_field = 'login';
29 $grabySiteConfig->login_password_field = 'password'; 25 $grabySiteConfig->login_password_field = 'password';
30 $grabySiteConfig->login_extra_fields = ['field=value']; 26 $grabySiteConfig->login_extra_fields = ['field=value'];
@@ -32,7 +28,7 @@ class GrabySiteConfigBuilderTest extends TestCase
32 28
33 $grabyConfigBuilderMock 29 $grabyConfigBuilderMock
34 ->method('buildForHost') 30 ->method('buildForHost')
35 ->with('example.com') 31 ->with('api.example.com')
36 ->will($this->returnValue($grabySiteConfig)); 32 ->will($this->returnValue($grabySiteConfig));
37 33
38 $logger = new Logger('foo'); 34 $logger = new Logger('foo');
@@ -43,8 +39,8 @@ class GrabySiteConfigBuilderTest extends TestCase
43 ->disableOriginalConstructor() 39 ->disableOriginalConstructor()
44 ->getMock(); 40 ->getMock();
45 $siteCrentialRepo->expects($this->once()) 41 $siteCrentialRepo->expects($this->once())
46 ->method('findOneByHostAndUser') 42 ->method('findOneByHostsAndUser')
47 ->with('example.com', 1) 43 ->with(['api.example.com', '.example.com'], 1)
48 ->willReturn(['username' => 'foo', 'password' => 'bar']); 44 ->willReturn(['username' => 'foo', 'password' => 'bar']);
49 45
50 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') 46 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
@@ -59,18 +55,18 @@ class GrabySiteConfigBuilderTest extends TestCase
59 $tokenStorage = new TokenStorage(); 55 $tokenStorage = new TokenStorage();
60 $tokenStorage->setToken($token); 56 $tokenStorage->setToken($token);
61 57
62 $this->builder = new GrabySiteConfigBuilder( 58 $builder = new GrabySiteConfigBuilder(
63 $grabyConfigBuilderMock, 59 $grabyConfigBuilderMock,
64 $tokenStorage, 60 $tokenStorage,
65 $siteCrentialRepo, 61 $siteCrentialRepo,
66 $logger 62 $logger
67 ); 63 );
68 64
69 $config = $this->builder->buildForHost('www.example.com'); 65 $config = $builder->buildForHost('api.example.com');
70 66
71 $this->assertSame('example.com', $config->getHost()); 67 $this->assertSame('api.example.com', $config->getHost());
72 $this->assertTrue($config->requiresLogin()); 68 $this->assertTrue($config->requiresLogin());
73 $this->assertSame('http://www.example.com/login', $config->getLoginUri()); 69 $this->assertSame('http://api.example.com/login', $config->getLoginUri());
74 $this->assertSame('login', $config->getUsernameField()); 70 $this->assertSame('login', $config->getUsernameField());
75 $this->assertSame('password', $config->getPasswordField()); 71 $this->assertSame('password', $config->getPasswordField());
76 $this->assertSame(['field' => 'value'], $config->getExtraFields()); 72 $this->assertSame(['field' => 'value'], $config->getExtraFields());
@@ -85,7 +81,6 @@ class GrabySiteConfigBuilderTest extends TestCase
85 81
86 public function testBuildConfigDoesntExist() 82 public function testBuildConfigDoesntExist()
87 { 83 {
88 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
89 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') 84 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder')
90 ->disableOriginalConstructor() 85 ->disableOriginalConstructor()
91 ->getMock(); 86 ->getMock();
@@ -103,8 +98,8 @@ class GrabySiteConfigBuilderTest extends TestCase
103 ->disableOriginalConstructor() 98 ->disableOriginalConstructor()
104 ->getMock(); 99 ->getMock();
105 $siteCrentialRepo->expects($this->once()) 100 $siteCrentialRepo->expects($this->once())
106 ->method('findOneByHostAndUser') 101 ->method('findOneByHostsAndUser')
107 ->with('unknown.com', 1) 102 ->with(['unknown.com', '.com'], 1)
108 ->willReturn(null); 103 ->willReturn(null);
109 104
110 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') 105 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
@@ -119,14 +114,14 @@ class GrabySiteConfigBuilderTest extends TestCase
119 $tokenStorage = new TokenStorage(); 114 $tokenStorage = new TokenStorage();
120 $tokenStorage->setToken($token); 115 $tokenStorage->setToken($token);
121 116
122 $this->builder = new GrabySiteConfigBuilder( 117 $builder = new GrabySiteConfigBuilder(
123 $grabyConfigBuilderMock, 118 $grabyConfigBuilderMock,
124 $tokenStorage, 119 $tokenStorage,
125 $siteCrentialRepo, 120 $siteCrentialRepo,
126 $logger 121 $logger
127 ); 122 );
128 123
129 $config = $this->builder->buildForHost('unknown.com'); 124 $config = $builder->buildForHost('unknown.com');
130 125
131 $this->assertFalse($config); 126 $this->assertFalse($config);
132 127
@@ -134,4 +129,121 @@ class GrabySiteConfigBuilderTest extends TestCase
134 129
135 $this->assertCount(1, $records, 'One log was recorded'); 130 $this->assertCount(1, $records, 'One log was recorded');
136 } 131 }
132
133 public function testBuildConfigUserNotDefined()
134 {
135 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder')
136 ->disableOriginalConstructor()
137 ->getMock();
138
139 $grabyConfigBuilderMock
140 ->method('buildForHost')
141 ->with('unknown.com')
142 ->will($this->returnValue(new GrabySiteConfig()));
143
144 $logger = new Logger('foo');
145 $handler = new TestHandler();
146 $logger->pushHandler($handler);
147
148 $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
149 ->disableOriginalConstructor()
150 ->getMock();
151
152 $tokenStorage = new TokenStorage();
153
154 $builder = new GrabySiteConfigBuilder(
155 $grabyConfigBuilderMock,
156 $tokenStorage,
157 $siteCrentialRepo,
158 $logger
159 );
160
161 $config = $builder->buildForHost('unknown.com');
162
163 $this->assertFalse($config);
164 }
165
166 public function dataProviderCredentials()
167 {
168 return [
169 [
170 'host' => 'example.com',
171 ],
172 [
173 'host' => 'other.example.com',
174 ],
175 [
176 'host' => 'paywall.example.com',
177 'expectedUsername' => 'paywall.example',
178 'expectedPassword' => 'bar',
179 ],
180 [
181 'host' => 'api.super.com',
182 'expectedUsername' => '.super',
183 'expectedPassword' => 'bar',
184 ],
185 [
186 'host' => '.super.com',
187 'expectedUsername' => '.super',
188 'expectedPassword' => 'bar',
189 ],
190 ];
191 }
192
193 /**
194 * @dataProvider dataProviderCredentials
195 */
196 public function testBuildConfigWithDbAccess($host, $expectedUsername = null, $expectedPassword = null)
197 {
198 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
199 ->disableOriginalConstructor()
200 ->getMock();
201
202 $grabySiteConfig = new GrabySiteConfig();
203 $grabySiteConfig->requires_login = true;
204 $grabySiteConfig->login_uri = 'http://api.example.com/login';
205 $grabySiteConfig->login_username_field = 'login';
206 $grabySiteConfig->login_password_field = 'password';
207 $grabySiteConfig->login_extra_fields = ['field=value'];
208 $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
209
210 $grabyConfigBuilderMock
211 ->method('buildForHost')
212 ->with($host)
213 ->will($this->returnValue($grabySiteConfig));
214
215 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
216 ->disableOriginalConstructor()
217 ->getMock();
218 $user->expects($this->once())
219 ->method('getId')
220 ->willReturn(1);
221
222 $token = new UsernamePasswordToken($user, 'pass', 'provider');
223
224 $tokenStorage = new TokenStorage();
225 $tokenStorage->setToken($token);
226
227 $logger = new Logger('foo');
228 $handler = new TestHandler();
229 $logger->pushHandler($handler);
230
231 $builder = new GrabySiteConfigBuilder(
232 $grabyConfigBuilderMock,
233 $tokenStorage,
234 $this->getClient()->getContainer()->get('wallabag_core.site_credential_repository'),
235 $logger
236 );
237
238 $config = $builder->buildForHost($host);
239
240 if (null === $expectedUsername && null === $expectedPassword) {
241 $this->assertFalse($config);
242
243 return;
244 }
245
246 $this->assertSame($expectedUsername, $config->getUsername());
247 $this->assertSame($expectedPassword, $config->getPassword());
248 }
137} 249}