diff options
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 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; | |||
5 | use Doctrine\Common\DataFixtures\AbstractFixture; | 5 | use Doctrine\Common\DataFixtures\AbstractFixture; |
6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; | 6 | use Doctrine\Common\DataFixtures\OrderedFixtureInterface; |
7 | use Doctrine\Common\Persistence\ObjectManager; | 7 | use Doctrine\Common\Persistence\ObjectManager; |
8 | use Symfony\Component\DependencyInjection\ContainerAwareInterface; | ||
9 | use Symfony\Component\DependencyInjection\ContainerInterface; | ||
8 | use Wallabag\CoreBundle\Entity\SiteCredential; | 10 | use Wallabag\CoreBundle\Entity\SiteCredential; |
9 | 11 | ||
10 | class LoadSiteCredentialData extends AbstractFixture implements OrderedFixtureInterface | 12 | class 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 718441bd..c7502bac 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php | |||
@@ -62,21 +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 | $hosts = [$host]; | 67 | |
68 | // will try to see for a host without the first subdomain (fr.example.org & .example.org) | 68 | return false; |
69 | $split = explode('.', $host); | 69 | } |
70 | 70 | ||
71 | if (\count($split) > 1) { | 71 | $hosts = [$host]; |
72 | // remove first subdomain | 72 | // will try to see for a host without the first subdomain (fr.example.org & .example.org) |
73 | array_shift($split); | 73 | $split = explode('.', $host); |
74 | $hosts[] = '.' . implode('.', $split); | ||
75 | } | ||
76 | 74 | ||
77 | $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId()); | 75 | if (\count($split) > 1) { |
76 | // remove first subdomain | ||
77 | array_shift($split); | ||
78 | $hosts[] = '.' . implode('.', $split); | ||
78 | } | 79 | } |
79 | 80 | ||
81 | $credentials = $this->credentialRepository->findOneByHostsAndUser($hosts, $this->currentUser->getId()); | ||
82 | |||
80 | if (null === $credentials) { | 83 | if (null === $credentials) { |
81 | $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); | 84 | $this->logger->debug('Auth: no credentials available for host.', ['host' => $host]); |
82 | 85 | ||
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 90609180..845762dc 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php | |||
@@ -5,19 +5,15 @@ namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator; | |||
5 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; | 5 | use Graby\SiteConfig\SiteConfig as GrabySiteConfig; |
6 | use Monolog\Handler\TestHandler; | 6 | use Monolog\Handler\TestHandler; |
7 | use Monolog\Logger; | 7 | use Monolog\Logger; |
8 | use PHPUnit\Framework\TestCase; | ||
9 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; | 8 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
10 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; | 9 | use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken; |
10 | use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; | ||
11 | use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; | 11 | use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder; |
12 | 12 | ||
13 | class GrabySiteConfigBuilderTest extends TestCase | 13 | class 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(); |
@@ -59,14 +55,14 @@ 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('api.example.com'); | 65 | $config = $builder->buildForHost('api.example.com'); |
70 | 66 | ||
71 | $this->assertSame('api.example.com', $config->getHost()); | 67 | $this->assertSame('api.example.com', $config->getHost()); |
72 | $this->assertTrue($config->requiresLogin()); | 68 | $this->assertTrue($config->requiresLogin()); |
@@ -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(); |
@@ -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 | } |