2016-09-29 10:14:43 +02:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 01:30:12 +01:00
|
|
|
namespace Tests\Wallabag\SiteConfig;
|
2016-09-29 10:14:43 +02:00
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
use Graby\SiteConfig\ConfigBuilder;
|
2016-09-29 10:14:43 +02:00
|
|
|
use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
|
2017-07-01 09:52:38 +02:00
|
|
|
use Monolog\Handler\TestHandler;
|
|
|
|
use Monolog\Logger;
|
2017-05-01 22:13:35 +02:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
2017-07-01 09:52:38 +02:00
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
2024-02-24 20:24:51 +01:00
|
|
|
use Tests\Wallabag\WallabagTestCase;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\Entity\User;
|
|
|
|
use Wallabag\Repository\SiteCredentialRepository;
|
|
|
|
use Wallabag\SiteConfig\GrabySiteConfigBuilder;
|
2016-09-29 10:14:43 +02:00
|
|
|
|
2024-02-24 20:24:51 +01:00
|
|
|
class GrabySiteConfigBuilderTest extends WallabagTestCase
|
2016-09-29 10:14:43 +02:00
|
|
|
{
|
2019-05-15 14:58:40 +02:00
|
|
|
private $builder;
|
|
|
|
|
2016-09-29 10:14:43 +02:00
|
|
|
public function testBuildConfigExists()
|
|
|
|
{
|
2022-09-01 20:54:56 +02:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2016-09-29 10:14:43 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabySiteConfig = new GrabySiteConfig();
|
|
|
|
$grabySiteConfig->requires_login = true;
|
2019-04-23 22:28:36 +02:00
|
|
|
$grabySiteConfig->login_uri = 'http://api.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')
|
2019-04-23 22:28:36 +02:00
|
|
|
->with('api.example.com')
|
2019-05-10 15:49:39 +02:00
|
|
|
->willReturn($grabySiteConfig);
|
2016-09-29 10:14:43 +02:00
|
|
|
|
2017-05-09 22:25:18 +02:00
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
|
2017-05-01 22:13:35 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$siteCrentialRepo->expects($this->once())
|
2019-04-23 22:28:36 +02:00
|
|
|
->method('findOneByHostsAndUser')
|
|
|
|
->with(['api.example.com', '.example.com'], 1)
|
2017-05-01 22:13:35 +02:00
|
|
|
->willReturn(['username' => 'foo', 'password' => 'bar']);
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$user = $this->getMockBuilder(User::class)
|
2017-05-01 22:13:35 +02: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 15:28:15 +02:00
|
|
|
$builder = new GrabySiteConfigBuilder(
|
2016-09-29 10:14:43 +02:00
|
|
|
$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
|
|
|
);
|
|
|
|
|
2019-04-24 15:28:15 +02:00
|
|
|
$config = $builder->buildForHost('api.example.com');
|
2016-09-29 10:14:43 +02:00
|
|
|
|
2019-04-23 22:28:36 +02:00
|
|
|
$this->assertSame('api.example.com', $config->getHost());
|
2017-11-21 10:30:35 +01:00
|
|
|
$this->assertTrue($config->requiresLogin());
|
2019-04-23 22:28:36 +02:00
|
|
|
$this->assertSame('http://api.example.com/login', $config->getLoginUri());
|
2017-07-03 07:30:54 +02: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 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()
|
|
|
|
{
|
2022-09-01 20:54:56 +02:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2016-09-29 10:14:43 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
|
|
|
->with('unknown.com')
|
2019-05-10 15:49:39 +02:00
|
|
|
->willReturn(new GrabySiteConfig());
|
2016-09-29 10:14:43 +02:00
|
|
|
|
2017-05-09 22:25:18 +02:00
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
|
2017-05-01 22:13:35 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$siteCrentialRepo->expects($this->once())
|
2019-04-23 22:28:36 +02:00
|
|
|
->method('findOneByHostsAndUser')
|
|
|
|
->with(['unknown.com', '.com'], 1)
|
2017-05-01 22:13:35 +02:00
|
|
|
->willReturn(null);
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$user = $this->getMockBuilder(User::class)
|
2017-05-01 22:13:35 +02: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 15:28:15 +02:00
|
|
|
$builder = new GrabySiteConfigBuilder(
|
2017-05-09 22:25:18 +02:00
|
|
|
$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
|
|
|
|
2019-04-24 15:28:15 +02:00
|
|
|
$config = $builder->buildForHost('unknown.com');
|
2016-09-29 10:14:43 +02:00
|
|
|
|
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
|
|
|
}
|
2019-02-27 14:59:50 +01:00
|
|
|
|
|
|
|
public function testBuildConfigWithBadExtraFields()
|
|
|
|
{
|
2022-09-01 20:54:56 +02:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2019-02-27 14:59:50 +01: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 15:49:39 +02:00
|
|
|
->willReturn($grabySiteConfig);
|
2019-02-27 14:59:50 +01:00
|
|
|
|
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
|
2019-02-27 14:59:50 +01:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$siteCrentialRepo->expects($this->once())
|
2019-05-15 14:58:40 +02:00
|
|
|
->method('findOneByHostsAndUser')
|
|
|
|
->with(['example.com', '.com'], 1)
|
2019-02-27 14:59:50 +01:00
|
|
|
->willReturn(['username' => 'foo', 'password' => 'bar']);
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$user = $this->getMockBuilder(User::class)
|
2019-02-27 14:59:50 +01: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 14:38:07 +02:00
|
|
|
|
2019-04-24 15:28:15 +02:00
|
|
|
public function testBuildConfigUserNotDefined()
|
|
|
|
{
|
2022-09-01 20:54:56 +02:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2019-04-24 15:28:15 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$grabyConfigBuilderMock
|
|
|
|
->method('buildForHost')
|
|
|
|
->with('unknown.com')
|
2019-05-15 14:38:07 +02:00
|
|
|
->willReturn(new GrabySiteConfig());
|
2019-04-24 15:28:15 +02:00
|
|
|
|
|
|
|
$logger = new Logger('foo');
|
|
|
|
$handler = new TestHandler();
|
|
|
|
$logger->pushHandler($handler);
|
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
|
2019-04-24 15:28:15 +02: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 20:54:56 +02:00
|
|
|
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
|
2019-04-24 15:28:15 +02: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 14:38:07 +02:00
|
|
|
->willReturn($grabySiteConfig);
|
2019-04-24 15:28:15 +02:00
|
|
|
|
2022-09-01 20:54:56 +02:00
|
|
|
$user = $this->getMockBuilder(User::class)
|
2019-04-24 15:28:15 +02: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 17:09:32 +01:00
|
|
|
$this->getTestClient()->getContainer()->get(SiteCredentialRepository::class),
|
2019-04-24 15:28:15 +02: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 10:14:43 +02:00
|
|
|
}
|