aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/Wallabag
diff options
context:
space:
mode:
Diffstat (limited to 'tests/Wallabag')
-rw-r--r--tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php2
-rw-r--r--tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php150
2 files changed, 132 insertions, 20 deletions
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}