2016-09-29 10:14:43 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Tests\Wallabag\CoreBundle\GuzzleSiteAuthenticator;
|
|
|
|
|
2017-05-09 22:25:18 +02:00
|
|
|
use Monolog\Handler\TestHandler;
|
|
|
|
use Monolog\Logger;
|
2016-09-29 10:14:43 +02:00
|
|
|
use BD\GuzzleSiteAuthenticator\SiteConfig\SiteConfig;
|
|
|
|
use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
|
|
|
|
use Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder;
|
2017-05-01 22:13:35 +02:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
2016-09-29 10:14:43 +02:00
|
|
|
|
2017-06-11 23:05:19 +02:00
|
|
|
class GrabySiteConfigBuilderTest extends \PHPUnit_Framework_TestCase
|
2016-09-29 10:14:43 +02:00
|
|
|
{
|
|
|
|
/** @var \Wallabag\CoreBundle\GuzzleSiteAuthenticator\GrabySiteConfigBuilder */
|
|
|
|
protected $builder;
|
|
|
|
|
|
|
|
public function testBuildConfigExists()
|
|
|
|
{
|
|
|
|
/* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
|
2017-05-01 22:13:35 +02:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder('Graby\SiteConfig\ConfigBuilder')
|
2016-09-29 10:14:43 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabySiteConfig = new GrabySiteConfig();
|
|
|
|
$grabySiteConfig->requires_login = true;
|
2017-05-03 10:23:49 +02:00
|
|
|
$grabySiteConfig->login_uri = 'http://www.example.com/login';
|
2016-09-29 10:14:43 +02:00
|
|
|
$grabySiteConfig->login_username_field = 'login';
|
|
|
|
$grabySiteConfig->login_password_field = 'password';
|
2017-01-22 00:42:05 +01:00
|
|
|
$grabySiteConfig->login_extra_fields = ['field=value'];
|
2016-09-29 10:14:43 +02:00
|
|
|
$grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
|
|
|
->with('example.com')
|
|
|
|
->will($this->returnValue($grabySiteConfig));
|
|
|
|
|
2017-05-09 22:25:18 +02:00
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2017-05-01 22:13:35 +02:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$siteCrentialRepo->expects($this->once())
|
|
|
|
->method('findOneByHostAndUser')
|
|
|
|
->with('example.com', 1)
|
|
|
|
->willReturn(['username' => 'foo', 'password' => 'bar']);
|
|
|
|
|
|
|
|
$user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn(1);
|
|
|
|
|
|
|
|
$token = new UsernamePasswordToken($user, 'pass', 'provider');
|
|
|
|
|
|
|
|
$tokenStorage = new TokenStorage();
|
|
|
|
$tokenStorage->setToken($token);
|
|
|
|
|
2016-09-29 10:14:43 +02:00
|
|
|
$this->builder = new GrabySiteConfigBuilder(
|
|
|
|
$grabyConfigBuilderMock,
|
2017-05-01 22:13:35 +02:00
|
|
|
$tokenStorage,
|
|
|
|
$siteCrentialRepo,
|
2017-05-09 22:25:18 +02:00
|
|
|
$logger
|
2016-09-29 10:14:43 +02:00
|
|
|
);
|
|
|
|
|
2017-05-03 10:23:49 +02:00
|
|
|
$config = $this->builder->buildForHost('www.example.com');
|
2016-09-29 10:14:43 +02:00
|
|
|
|
2017-05-09 22:25:18 +02:00
|
|
|
$this->assertEquals(
|
2016-09-29 10:14:43 +02:00
|
|
|
new SiteConfig([
|
|
|
|
'host' => 'example.com',
|
|
|
|
'requiresLogin' => true,
|
2017-05-03 10:23:49 +02:00
|
|
|
'loginUri' => 'http://www.example.com/login',
|
2016-09-29 10:14:43 +02:00
|
|
|
'usernameField' => 'login',
|
|
|
|
'passwordField' => 'password',
|
|
|
|
'extraFields' => ['field' => 'value'],
|
|
|
|
'notLoggedInXpath' => '//div[@class="need-login"]',
|
|
|
|
'username' => 'foo',
|
|
|
|
'password' => 'bar',
|
|
|
|
]),
|
|
|
|
$config
|
|
|
|
);
|
2017-05-09 22:25:18 +02:00
|
|
|
|
|
|
|
$records = $handler->getRecords();
|
|
|
|
|
|
|
|
$this->assertCount(1, $records, 'One log was recorded');
|
2016-09-29 10:14:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuildConfigDoesntExist()
|
|
|
|
{
|
|
|
|
/* @var \Graby\SiteConfig\ConfigBuilder|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder('\Graby\SiteConfig\ConfigBuilder')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
|
|
|
->with('unknown.com')
|
|
|
|
->will($this->returnValue(new GrabySiteConfig()));
|
|
|
|
|
2017-05-09 22:25:18 +02:00
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2017-05-01 22:13:35 +02:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\SiteCredentialRepository')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$siteCrentialRepo->expects($this->once())
|
|
|
|
->method('findOneByHostAndUser')
|
|
|
|
->with('unknown.com', 1)
|
|
|
|
->willReturn(null);
|
|
|
|
|
|
|
|
$user = $this->getMockBuilder('Wallabag\UserBundle\Entity\User')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn(1);
|
|
|
|
|
|
|
|
$token = new UsernamePasswordToken($user, 'pass', 'provider');
|
|
|
|
|
|
|
|
$tokenStorage = new TokenStorage();
|
|
|
|
$tokenStorage->setToken($token);
|
|
|
|
|
2017-05-09 22:25:18 +02:00
|
|
|
$this->builder = new GrabySiteConfigBuilder(
|
|
|
|
$grabyConfigBuilderMock,
|
2017-05-01 22:13:35 +02:00
|
|
|
$tokenStorage,
|
|
|
|
$siteCrentialRepo,
|
2017-05-09 22:25:18 +02:00
|
|
|
$logger
|
|
|
|
);
|
2016-09-29 10:14:43 +02:00
|
|
|
|
|
|
|
$config = $this->builder->buildForHost('unknown.com');
|
|
|
|
|
2017-05-09 22:25:18 +02:00
|
|
|
$this->assertFalse($config);
|
|
|
|
|
|
|
|
$records = $handler->getRecords();
|
|
|
|
|
|
|
|
$this->assertCount(1, $records, 'One log was recorded');
|
2016-09-29 10:14:43 +02:00
|
|
|
}
|
|
|
|
}
|