aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php155
1 files changed, 134 insertions, 21 deletions
diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
index 277d8012..9e0a9136 100644
--- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
+++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php
@@ -5,26 +5,24 @@ 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 */ 15 private $builder;
16 protected $builder;
17 16
18 public function testBuildConfigExists() 17 public function testBuildConfigExists()
19 { 18 {
20 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
21 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') 19 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
22 ->disableOriginalConstructor() 20 ->disableOriginalConstructor()
23 ->getMock(); 21 ->getMock();
24 22
25 $grabySiteConfig = new GrabySiteConfig(); 23 $grabySiteConfig = new GrabySiteConfig();
26 $grabySiteConfig->requires_login = true; 24 $grabySiteConfig->requires_login = true;
27 $grabySiteConfig->login_uri = 'http://www.example.com/login'; 25 $grabySiteConfig->login_uri = 'http://api.example.com/login';
28 $grabySiteConfig->login_username_field = 'login'; 26 $grabySiteConfig->login_username_field = 'login';
29 $grabySiteConfig->login_password_field = 'password'; 27 $grabySiteConfig->login_password_field = 'password';
30 $grabySiteConfig->login_extra_fields = ['field=value']; 28 $grabySiteConfig->login_extra_fields = ['field=value'];
@@ -32,7 +30,7 @@ class GrabySiteConfigBuilderTest extends TestCase
32 30
33 $grabyConfigBuilderMock 31 $grabyConfigBuilderMock
34 ->method('buildForHost') 32 ->method('buildForHost')
35 ->with('example.com') 33 ->with('api.example.com')
36 ->willReturn($grabySiteConfig); 34 ->willReturn($grabySiteConfig);
37 35
38 $logger = new Logger('foo'); 36 $logger = new Logger('foo');
@@ -43,8 +41,8 @@ class GrabySiteConfigBuilderTest extends TestCase
43 ->disableOriginalConstructor() 41 ->disableOriginalConstructor()
44 ->getMock(); 42 ->getMock();
45 $siteCrentialRepo->expects($this->once()) 43 $siteCrentialRepo->expects($this->once())
46 ->method('findOneByHostAndUser') 44 ->method('findOneByHostsAndUser')
47 ->with('example.com', 1) 45 ->with(['api.example.com', '.example.com'], 1)
48 ->willReturn(['username' => 'foo', 'password' => 'bar']); 46 ->willReturn(['username' => 'foo', 'password' => 'bar']);
49 47
50 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') 48 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
@@ -59,18 +57,18 @@ class GrabySiteConfigBuilderTest extends TestCase
59 $tokenStorage = new TokenStorage(); 57 $tokenStorage = new TokenStorage();
60 $tokenStorage->setToken($token); 58 $tokenStorage->setToken($token);
61 59
62 $this->builder = new GrabySiteConfigBuilder( 60 $builder = new GrabySiteConfigBuilder(
63 $grabyConfigBuilderMock, 61 $grabyConfigBuilderMock,
64 $tokenStorage, 62 $tokenStorage,
65 $siteCrentialRepo, 63 $siteCrentialRepo,
66 $logger 64 $logger
67 ); 65 );
68 66
69 $config = $this->builder->buildForHost('www.example.com'); 67 $config = $builder->buildForHost('api.example.com');
70 68
71 $this->assertSame('example.com', $config->getHost()); 69 $this->assertSame('api.example.com', $config->getHost());
72 $this->assertTrue($config->requiresLogin()); 70 $this->assertTrue($config->requiresLogin());
73 $this->assertSame('http://www.example.com/login', $config->getLoginUri()); 71 $this->assertSame('http://api.example.com/login', $config->getLoginUri());
74 $this->assertSame('login', $config->getUsernameField()); 72 $this->assertSame('login', $config->getUsernameField());
75 $this->assertSame('password', $config->getPasswordField()); 73 $this->assertSame('password', $config->getPasswordField());
76 $this->assertSame(['field' => 'value'], $config->getExtraFields()); 74 $this->assertSame(['field' => 'value'], $config->getExtraFields());
@@ -85,7 +83,6 @@ class GrabySiteConfigBuilderTest extends TestCase
85 83
86 public function testBuildConfigDoesntExist() 84 public function testBuildConfigDoesntExist()
87 { 85 {
88 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
89 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder') 86 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder')
90 ->disableOriginalConstructor() 87 ->disableOriginalConstructor()
91 ->getMock(); 88 ->getMock();
@@ -103,8 +100,8 @@ class GrabySiteConfigBuilderTest extends TestCase
103 ->disableOriginalConstructor() 100 ->disableOriginalConstructor()
104 ->getMock(); 101 ->getMock();
105 $siteCrentialRepo->expects($this->once()) 102 $siteCrentialRepo->expects($this->once())
106 ->method('findOneByHostAndUser') 103 ->method('findOneByHostsAndUser')
107 ->with('unknown.com', 1) 104 ->with(['unknown.com', '.com'], 1)
108 ->willReturn(null); 105 ->willReturn(null);
109 106
110 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') 107 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
@@ -119,14 +116,14 @@ class GrabySiteConfigBuilderTest extends TestCase
119 $tokenStorage = new TokenStorage(); 116 $tokenStorage = new TokenStorage();
120 $tokenStorage->setToken($token); 117 $tokenStorage->setToken($token);
121 118
122 $this->builder = new GrabySiteConfigBuilder( 119 $builder = new GrabySiteConfigBuilder(
123 $grabyConfigBuilderMock, 120 $grabyConfigBuilderMock,
124 $tokenStorage, 121 $tokenStorage,
125 $siteCrentialRepo, 122 $siteCrentialRepo,
126 $logger 123 $logger
127 ); 124 );
128 125
129 $config = $this->builder->buildForHost('unknown.com'); 126 $config = $builder->buildForHost('unknown.com');
130 127
131 $this->assertFalse($config); 128 $this->assertFalse($config);
132 129
@@ -137,7 +134,6 @@ class GrabySiteConfigBuilderTest extends TestCase
137 134
138 public function testBuildConfigWithBadExtraFields() 135 public function testBuildConfigWithBadExtraFields()
139 { 136 {
140 /* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
141 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder') 137 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
142 ->disableOriginalConstructor() 138 ->disableOriginalConstructor()
143 ->getMock(); 139 ->getMock();
@@ -163,8 +159,8 @@ class GrabySiteConfigBuilderTest extends TestCase
163 ->disableOriginalConstructor() 159 ->disableOriginalConstructor()
164 ->getMock(); 160 ->getMock();
165 $siteCrentialRepo->expects($this->once()) 161 $siteCrentialRepo->expects($this->once())
166 ->method('findOneByHostAndUser') 162 ->method('findOneByHostsAndUser')
167 ->with('example.com', 1) 163 ->with(['example.com', '.com'], 1)
168 ->willReturn(['username' => 'foo', 'password' => 'bar']); 164 ->willReturn(['username' => 'foo', 'password' => 'bar']);
169 165
170 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User') 166 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
@@ -202,4 +198,121 @@ class GrabySiteConfigBuilderTest extends TestCase
202 198
203 $this->assertCount(1, $records, 'One log was recorded'); 199 $this->assertCount(1, $records, 'One log was recorded');
204 } 200 }
201
202 public function testBuildConfigUserNotDefined()
203 {
204 $grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder')
205 ->disableOriginalConstructor()
206 ->getMock();
207
208 $grabyConfigBuilderMock
209 ->method('buildForHost')
210 ->with('unknown.com')
211 ->willReturn(new GrabySiteConfig());
212
213 $logger = new Logger('foo');
214 $handler = new TestHandler();
215 $logger->pushHandler($handler);
216
217 $siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
218 ->disableOriginalConstructor()
219 ->getMock();
220
221 $tokenStorage = new TokenStorage();
222
223 $builder = new GrabySiteConfigBuilder(
224 $grabyConfigBuilderMock,
225 $tokenStorage,
226 $siteCrentialRepo,
227 $logger
228 );
229
230 $config = $builder->buildForHost('unknown.com');
231
232 $this->assertFalse($config);
233 }
234
235 public function dataProviderCredentials()
236 {
237 return [
238 [
239 'host' => 'example.com',
240 ],
241 [
242 'host' => 'other.example.com',
243 ],
244 [
245 'host' => 'paywall.example.com',
246 'expectedUsername' => 'paywall.example',
247 'expectedPassword' => 'bar',
248 ],
249 [
250 'host' => 'api.super.com',
251 'expectedUsername' => '.super',
252 'expectedPassword' => 'bar',
253 ],
254 [
255 'host' => '.super.com',
256 'expectedUsername' => '.super',
257 'expectedPassword' => 'bar',
258 ],
259 ];
260 }
261
262 /**
263 * @dataProvider dataProviderCredentials
264 */
265 public function testBuildConfigWithDbAccess($host, $expectedUsername = null, $expectedPassword = null)
266 {
267 $grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
268 ->disableOriginalConstructor()
269 ->getMock();
270
271 $grabySiteConfig = new GrabySiteConfig();
272 $grabySiteConfig->requires_login = true;
273 $grabySiteConfig->login_uri = 'http://api.example.com/login';
274 $grabySiteConfig->login_username_field = 'login';
275 $grabySiteConfig->login_password_field = 'password';
276 $grabySiteConfig->login_extra_fields = ['field=value'];
277 $grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
278
279 $grabyConfigBuilderMock
280 ->method('buildForHost')
281 ->with($host)
282 ->willReturn($grabySiteConfig);
283
284 $user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
285 ->disableOriginalConstructor()
286 ->getMock();
287 $user->expects($this->once())
288 ->method('getId')
289 ->willReturn(1);
290
291 $token = new UsernamePasswordToken($user, 'pass', 'provider');
292
293 $tokenStorage = new TokenStorage();
294 $tokenStorage->setToken($token);
295
296 $logger = new Logger('foo');
297 $handler = new TestHandler();
298 $logger->pushHandler($handler);
299
300 $builder = new GrabySiteConfigBuilder(
301 $grabyConfigBuilderMock,
302 $tokenStorage,
303 $this->getClient()->getContainer()->get('wallabag_core.site_credential_repository'),
304 $logger
305 );
306
307 $config = $builder->buildForHost($host);
308
309 if (null === $expectedUsername && null === $expectedPassword) {
310 $this->assertFalse($config);
311
312 return;
313 }
314
315 $this->assertSame($expectedUsername, $config->getUsername());
316 $this->assertSame($expectedPassword, $config->getPassword());
317 }
205} 318}