diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php | 134 |
1 files changed, 123 insertions, 11 deletions
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 | } |