aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php151
1 files changed, 131 insertions, 20 deletions
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
index 277d8012..44affde8 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 ->willReturn($grabySiteConfig); 32 ->willReturn($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
@@ -137,7 +132,6 @@ class GrabySiteConfigBuilderTest extends TestCase
137 132
138 public function testBuildConfigWithBadExtraFields() 133 public function testBuildConfigWithBadExtraFields()
139 { 134 {
140 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
141 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') 135 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
142 ->disableOriginalConstructor() 136 ->disableOriginalConstructor()
143 ->getMock(); 137 ->getMock();
@@ -202,4 +196,121 @@ class GrabySiteConfigBuilderTest extends TestCase
202 196
203 $this->assertCount(1, $records, 'One log was recorded'); 197 $this->assertCount(1, $records, 'One log was recorded');
204 } 198 }
199
200 public function testBuildConfigUserNotDefined()
201 {
202 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder')
203 ->disableOriginalConstructor()
204 ->getMock();
205
206 $grabyConfigBuilderMock
207 ->method('buildForHost')
208 ->with('unknown.com')
209 ->willReturn(new GrabySiteConfig());
210
211 $logger = new Logger('foo');
212 $handler = new TestHandler();
213 $logger->pushHandler($handler);
214
215 $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
216 ->disableOriginalConstructor()
217 ->getMock();
218
219 $tokenStorage = new TokenStorage();
220
221 $builder = new GrabySiteConfigBuilder(
222 $grabyConfigBuilderMock,
223 $tokenStorage,
224 $siteCrentialRepo,
225 $logger
226 );
227
228 $config = $builder->buildForHost('unknown.com');
229
230 $this->assertFalse($config);
231 }
232
233 public function dataProviderCredentials()
234 {
235 return [
236 [
237 'host' => 'example.com',
238 ],
239 [
240 'host' => 'other.example.com',
241 ],
242 [
243 'host' => 'paywall.example.com',
244 'expectedUsername' => 'paywall.example',
245 'expectedPassword' => 'bar',
246 ],
247 [
248 'host' => 'api.super.com',
249 'expectedUsername' => '.super',
250 'expectedPassword' => 'bar',
251 ],
252 [
253 'host' => '.super.com',
254 'expectedUsername' => '.super',
255 'expectedPassword' => 'bar',
256 ],
257 ];
258 }
259
260 /**
261 * @dataProvider dataProviderCredentials
262 */
263 public function testBuildConfigWithDbAccess($host, $expectedUsername = null, $expectedPassword = null)
264 {
265 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
266 ->disableOriginalConstructor()
267 ->getMock();
268
269 $grabySiteConfig = new GrabySiteConfig();
270 $grabySiteConfig->requires_login = true;
271 $grabySiteConfig->login_uri = 'http://api.example.com/login';
272 $grabySiteConfig->login_username_field = 'login';
273 $grabySiteConfig->login_password_field = 'password';
274 $grabySiteConfig->login_extra_fields = ['field=value'];
275 $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
276
277 $grabyConfigBuilderMock
278 ->method('buildForHost')
279 ->with($host)
280 ->willReturn($grabySiteConfig);
281
282 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
283 ->disableOriginalConstructor()
284 ->getMock();
285 $user->expects($this->once())
286 ->method('getId')
287 ->willReturn(1);
288
289 $token = new UsernamePasswordToken($user, 'pass', 'provider');
290
291 $tokenStorage = new TokenStorage();
292 $tokenStorage->setToken($token);
293
294 $logger = new Logger('foo');
295 $handler = new TestHandler();
296 $logger->pushHandler($handler);
297
298 $builder = new GrabySiteConfigBuilder(
299 $grabyConfigBuilderMock,
300 $tokenStorage,
301 $this->getClient()->getContainer()->get('wallabag_core.site_credential_repository'),
302 $logger
303 );
304
305 $config = $builder->buildForHost($host);
306
307 if (null === $expectedUsername && null === $expectedPassword) {
308 $this->assertFalse($config);
309
310 return;
311 }
312
313 $this->assertSame($expectedUsername, $config->getUsername());
314 $this->assertSame($expectedPassword, $config->getPassword());
315 }
205} 316}