2016-09-29 08:14:43 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-02 20:33:09 +00:00
|
|
|
namespace Tests\Wallabag\CoreBundle\SiteConfig;
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
use Graby\SiteConfig\ConfigBuilder;
|
2016-09-29 08:14:43 +00:00
|
|
|
use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Monolog\Handler\TestHandler;
|
|
|
|
use Monolog\Logger;
|
2017-05-01 20:13:35 +00:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
2019-04-24 13:28:15 +00:00
|
|
|
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
|
2023-12-30 22:32:36 +00:00
|
|
|
use Wallabag\CoreBundle\Entity\User;
|
2022-04-24 15:58:57 +00:00
|
|
|
use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
|
2024-02-02 20:33:09 +00:00
|
|
|
use Wallabag\CoreBundle\SiteConfig\GrabySiteConfigBuilder;
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2019-04-24 13:28:15 +00:00
|
|
|
class GrabySiteConfigBuilderTest extends WallabagCoreTestCase
|
2016-09-29 08:14:43 +00:00
|
|
|
{
|
2019-05-15 12:58:40 +00:00
|
|
|
private $builder;
|
|
|
|
|
2016-09-29 08:14:43 +00:00
|
|
|
public function testBuildConfigExists()
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2016-09-29 08:14:43 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabySiteConfig = new GrabySiteConfig();
|
|
|
|
$grabySiteConfig->requires_login = true;
|
2019-04-23 20:28:36 +00:00
|
|
|
$grabySiteConfig->login_uri = 'http://api.example.com/login';
|
2016-09-29 08:14:43 +00:00
|
|
|
$grabySiteConfig->login_username_field = 'login';
|
|
|
|
$grabySiteConfig->login_password_field = 'password';
|
2017-01-21 23:42:05 +00:00
|
|
|
$grabySiteConfig->login_extra_fields = ['field=value'];
|
2016-09-29 08:14:43 +00:00
|
|
|
$grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
2019-04-23 20:28:36 +00:00
|
|
|
->with('api.example.com')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($grabySiteConfig);
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2017-05-09 20:25:18 +00:00
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
|
2017-05-01 20:13:35 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$siteCrentialRepo->expects($this->once())
|
2019-04-23 20:28:36 +00:00
|
|
|
->method('findOneByHostsAndUser')
|
|
|
|
->with(['api.example.com', '.example.com'], 1)
|
2017-05-01 20:13:35 +00:00
|
|
|
->willReturn(['username' => 'foo', 'password' => 'bar']);
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$user = $this->getMockBuilder(User::class)
|
2017-05-01 20:13:35 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn(1);
|
|
|
|
|
|
|
|
$token = new UsernamePasswordToken($user, 'pass', 'provider');
|
|
|
|
|
|
|
|
$tokenStorage = new TokenStorage();
|
|
|
|
$tokenStorage->setToken($token);
|
|
|
|
|
2019-04-24 13:28:15 +00:00
|
|
|
$builder = new GrabySiteConfigBuilder(
|
2016-09-29 08:14:43 +00:00
|
|
|
$grabyConfigBuilderMock,
|
2017-05-01 20:13:35 +00:00
|
|
|
$tokenStorage,
|
|
|
|
$siteCrentialRepo,
|
2017-05-09 20:25:18 +00:00
|
|
|
$logger
|
2016-09-29 08:14:43 +00:00
|
|
|
);
|
|
|
|
|
2019-04-24 13:28:15 +00:00
|
|
|
$config = $builder->buildForHost('api.example.com');
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2019-04-23 20:28:36 +00:00
|
|
|
$this->assertSame('api.example.com', $config->getHost());
|
2017-11-21 09:30:35 +00:00
|
|
|
$this->assertTrue($config->requiresLogin());
|
2019-04-23 20:28:36 +00:00
|
|
|
$this->assertSame('http://api.example.com/login', $config->getLoginUri());
|
2017-07-03 05:30:54 +00:00
|
|
|
$this->assertSame('login', $config->getUsernameField());
|
|
|
|
$this->assertSame('password', $config->getPasswordField());
|
|
|
|
$this->assertSame(['field' => 'value'], $config->getExtraFields());
|
|
|
|
$this->assertSame('//div[@class="need-login"]', $config->getNotLoggedInXpath());
|
|
|
|
$this->assertSame('foo', $config->getUsername());
|
|
|
|
$this->assertSame('bar', $config->getPassword());
|
2017-05-09 20:25:18 +00:00
|
|
|
|
|
|
|
$records = $handler->getRecords();
|
|
|
|
|
|
|
|
$this->assertCount(1, $records, 'One log was recorded');
|
2016-09-29 08:14:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBuildConfigDoesntExist()
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2016-09-29 08:14:43 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
|
|
|
->with('unknown.com')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn(new GrabySiteConfig());
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2017-05-09 20:25:18 +00:00
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
|
2017-05-01 20:13:35 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$siteCrentialRepo->expects($this->once())
|
2019-04-23 20:28:36 +00:00
|
|
|
->method('findOneByHostsAndUser')
|
|
|
|
->with(['unknown.com', '.com'], 1)
|
2017-05-01 20:13:35 +00:00
|
|
|
->willReturn(null);
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$user = $this->getMockBuilder(User::class)
|
2017-05-01 20:13:35 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn(1);
|
|
|
|
|
|
|
|
$token = new UsernamePasswordToken($user, 'pass', 'provider');
|
|
|
|
|
|
|
|
$tokenStorage = new TokenStorage();
|
|
|
|
$tokenStorage->setToken($token);
|
|
|
|
|
2019-04-24 13:28:15 +00:00
|
|
|
$builder = new GrabySiteConfigBuilder(
|
2017-05-09 20:25:18 +00:00
|
|
|
$grabyConfigBuilderMock,
|
2017-05-01 20:13:35 +00:00
|
|
|
$tokenStorage,
|
|
|
|
$siteCrentialRepo,
|
2017-05-09 20:25:18 +00:00
|
|
|
$logger
|
|
|
|
);
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2019-04-24 13:28:15 +00:00
|
|
|
$config = $builder->buildForHost('unknown.com');
|
2016-09-29 08:14:43 +00:00
|
|
|
|
2017-05-09 20:25:18 +00:00
|
|
|
$this->assertFalse($config);
|
|
|
|
|
|
|
|
$records = $handler->getRecords();
|
|
|
|
|
|
|
|
$this->assertCount(1, $records, 'One log was recorded');
|
2016-09-29 08:14:43 +00:00
|
|
|
}
|
2019-02-27 13:59:50 +00:00
|
|
|
|
|
|
|
public function testBuildConfigWithBadExtraFields()
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2019-02-27 13:59:50 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabySiteConfig = new GrabySiteConfig();
|
|
|
|
$grabySiteConfig->requires_login = true;
|
|
|
|
$grabySiteConfig->login_uri = 'http://www.example.com/login';
|
|
|
|
$grabySiteConfig->login_username_field = 'login';
|
|
|
|
$grabySiteConfig->login_password_field = 'password';
|
|
|
|
$grabySiteConfig->login_extra_fields = ['field'];
|
|
|
|
$grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
|
|
|
->with('example.com')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($grabySiteConfig);
|
2019-02-27 13:59:50 +00:00
|
|
|
|
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
|
2019-02-27 13:59:50 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$siteCrentialRepo->expects($this->once())
|
2019-05-15 12:58:40 +00:00
|
|
|
->method('findOneByHostsAndUser')
|
|
|
|
->with(['example.com', '.com'], 1)
|
2019-02-27 13:59:50 +00:00
|
|
|
->willReturn(['username' => 'foo', 'password' => 'bar']);
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$user = $this->getMockBuilder(User::class)
|
2019-02-27 13:59:50 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn(1);
|
|
|
|
|
|
|
|
$token = new UsernamePasswordToken($user, 'pass', 'provider');
|
|
|
|
|
|
|
|
$tokenStorage = new TokenStorage();
|
|
|
|
$tokenStorage->setToken($token);
|
|
|
|
|
|
|
|
$this->builder = new GrabySiteConfigBuilder(
|
|
|
|
$grabyConfigBuilderMock,
|
|
|
|
$tokenStorage,
|
|
|
|
$siteCrentialRepo,
|
|
|
|
$logger
|
|
|
|
);
|
|
|
|
|
|
|
|
$config = $this->builder->buildForHost('www.example.com');
|
|
|
|
|
|
|
|
$this->assertSame('example.com', $config->getHost());
|
|
|
|
$this->assertTrue($config->requiresLogin());
|
|
|
|
$this->assertSame('http://www.example.com/login', $config->getLoginUri());
|
|
|
|
$this->assertSame('login', $config->getUsernameField());
|
|
|
|
$this->assertSame('password', $config->getPasswordField());
|
|
|
|
$this->assertSame([], $config->getExtraFields());
|
|
|
|
$this->assertSame('//div[@class="need-login"]', $config->getNotLoggedInXpath());
|
|
|
|
$this->assertSame('foo', $config->getUsername());
|
|
|
|
$this->assertSame('bar', $config->getPassword());
|
|
|
|
|
|
|
|
$records = $handler->getRecords();
|
|
|
|
|
|
|
|
$this->assertCount(1, $records, 'One log was recorded');
|
|
|
|
}
|
2019-05-15 12:38:07 +00:00
|
|
|
|
2019-04-24 13:28:15 +00:00
|
|
|
public function testBuildConfigUserNotDefined()
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2019-04-24 13:28:15 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
|
|
|
->with('unknown.com')
|
2019-05-15 12:38:07 +00:00
|
|
|
->willReturn(new GrabySiteConfig());
|
2019-04-24 13:28:15 +00:00
|
|
|
|
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
|
2019-04-24 13:28:15 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$tokenStorage = new TokenStorage();
|
|
|
|
|
|
|
|
$builder = new GrabySiteConfigBuilder(
|
|
|
|
$grabyConfigBuilderMock,
|
|
|
|
$tokenStorage,
|
|
|
|
$siteCrentialRepo,
|
|
|
|
$logger
|
|
|
|
);
|
|
|
|
|
|
|
|
$config = $builder->buildForHost('unknown.com');
|
|
|
|
|
|
|
|
$this->assertFalse($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataProviderCredentials()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'host' => 'example.com',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'host' => 'other.example.com',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'host' => 'paywall.example.com',
|
|
|
|
'expectedUsername' => 'paywall.example',
|
|
|
|
'expectedPassword' => 'bar',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'host' => 'api.super.com',
|
|
|
|
'expectedUsername' => '.super',
|
|
|
|
'expectedPassword' => 'bar',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'host' => '.super.com',
|
|
|
|
'expectedUsername' => '.super',
|
|
|
|
'expectedPassword' => 'bar',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataProviderCredentials
|
|
|
|
*/
|
|
|
|
public function testBuildConfigWithDbAccess($host, $expectedUsername = null, $expectedPassword = null)
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2019-04-24 13:28:15 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabySiteConfig = new GrabySiteConfig();
|
|
|
|
$grabySiteConfig->requires_login = true;
|
|
|
|
$grabySiteConfig->login_uri = 'http://api.example.com/login';
|
|
|
|
$grabySiteConfig->login_username_field = 'login';
|
|
|
|
$grabySiteConfig->login_password_field = 'password';
|
|
|
|
$grabySiteConfig->login_extra_fields = ['field=value'];
|
|
|
|
$grabySiteConfig->not_logged_in_xpath = '//div[@class="need-login"]';
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
|
|
|
->with($host)
|
2019-05-15 12:38:07 +00:00
|
|
|
->willReturn($grabySiteConfig);
|
2019-04-24 13:28:15 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$user = $this->getMockBuilder(User::class)
|
2019-04-24 13:28:15 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$user->expects($this->once())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn(1);
|
|
|
|
|
|
|
|
$token = new UsernamePasswordToken($user, 'pass', 'provider');
|
|
|
|
|
|
|
|
$tokenStorage = new TokenStorage();
|
|
|
|
$tokenStorage->setToken($token);
|
|
|
|
|
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
|
|
|
$builder = new GrabySiteConfigBuilder(
|
|
|
|
$grabyConfigBuilderMock,
|
|
|
|
$tokenStorage,
|
2022-11-23 16:09:32 +00:00
|
|
|
$this->getTestClient()->getContainer()->get(SiteCredentialRepository::class),
|
2019-04-24 13:28:15 +00:00
|
|
|
$logger
|
|
|
|
);
|
|
|
|
|
|
|
|
$config = $builder->buildForHost($host);
|
|
|
|
|
|
|
|
if (null === $expectedUsername && null === $expectedPassword) {
|
|
|
|
$this->assertFalse($config);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame($expectedUsername, $config->getUsername());
|
|
|
|
$this->assertSame($expectedPassword, $config->getPassword());
|
|
|
|
}
|
2016-09-29 08:14:43 +00:00
|
|
|
}
|