]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Adding more tests to cover different scenario 3937/head
authorJeremy Benoist <jeremy.benoist@gmail.com>
Wed, 24 Apr 2019 13:28:15 +0000 (15:28 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Wed, 24 Apr 2019 13:28:15 +0000 (15:28 +0200)
src/Wallabag/CoreBundle/DataFixtures/ORM/LoadSiteCredentialData.php
src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php
tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php

index 866f55a403d40bf8bf5b9a753006c5dec8693dba..faf29da662d0125d4f2a85eaa4d57a9e27120ca1 100644 (file)
@@ -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);
 
index 718441bd459c60a6944781186a7606e87b7a4806..c7502baccdb6a996597bc5502b8502307d42509c 100644 (file)
@@ -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 (\count($split) > 1) {
-                // remove first subdomain
-                array_shift($split);
-                $hosts[] = '.' . implode('.', $split);
-            }
+        if (!$this->currentUser) {
+            $this->logger->debug('Auth: no current user defined.');
+
+            return false;
+        }
+
+        $hosts = [$host];
+        // will try to see for a host without the first subdomain (fr.example.org & .example.org)
+        $split = explode('.', $host);
 
-            $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId());
+        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]);
 
index 90609180f960bd2294bfda7aed577a66ce15b34f..845762dc1bb38b8e2e6c79c8fa9e969f4c9568b9 100644 (file)
@@ -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());
+    }
 }