wallabag/tests/SiteConfig/GrabySiteConfigBuilderTest.php

322 lines
11 KiB
PHP
Raw Normal View History

<?php
2024-02-19 01:30:12 +01:00
namespace Tests\Wallabag\SiteConfig;
2022-09-01 20:54:56 +02:00
use Graby\SiteConfig\ConfigBuilder;
use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
2017-07-01 09:52:38 +02:00
use Monolog\Handler\TestHandler;
use Monolog\Logger;
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;
use Tests\Wallabag\WallabagTestCase;
2024-02-19 01:30:12 +01:00
use Wallabag\Entity\User;
use Wallabag\Repository\SiteCredentialRepository;
use Wallabag\SiteConfig\GrabySiteConfigBuilder;
class GrabySiteConfigBuilderTest extends WallabagTestCase
{
2019-05-15 14:58:40 +02:00
private $builder;
public function testBuildConfigExists()
{
2022-09-01 20:54:56 +02:00
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
->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('api.example.com')
2019-05-10 15:49:39 +02:00
->willReturn($grabySiteConfig);
$logger = new Logger('foo');
$handler = new TestHandler();
$logger->pushHandler($handler);
2022-09-01 20:54:56 +02:00
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
->disableOriginalConstructor()
->getMock();
$siteCrentialRepo->expects($this->once())
->method('findOneByHostsAndUser')
->with(['api.example.com', '.example.com'], 1)
->willReturn(['username' => 'foo', 'password' => 'bar']);
2022-09-01 20:54:56 +02:00
$user = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->getMock();
$user->expects($this->once())
->method('getId')
->willReturn(1);
$token = new UsernamePasswordToken($user, 'pass', 'provider');
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);
$builder = new GrabySiteConfigBuilder(
$grabyConfigBuilderMock,
$tokenStorage,
$siteCrentialRepo,
$logger
);
$config = $builder->buildForHost('api.example.com');
$this->assertSame('api.example.com', $config->getHost());
2017-11-21 10:30:35 +01:00
$this->assertTrue($config->requiresLogin());
$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());
$records = $handler->getRecords();
$this->assertCount(1, $records, 'One log was recorded');
}
public function testBuildConfigDoesntExist()
{
2022-09-01 20:54:56 +02:00
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
->disableOriginalConstructor()
->getMock();
$grabyConfigBuilderMock
->method('buildForHost')
->with('unknown.com')
2019-05-10 15:49:39 +02:00
->willReturn(new GrabySiteConfig());
$logger = new Logger('foo');
$handler = new TestHandler();
$logger->pushHandler($handler);
2022-09-01 20:54:56 +02:00
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
->disableOriginalConstructor()
->getMock();
$siteCrentialRepo->expects($this->once())
->method('findOneByHostsAndUser')
->with(['unknown.com', '.com'], 1)
->willReturn(null);
2022-09-01 20:54:56 +02:00
$user = $this->getMockBuilder(User::class)
->disableOriginalConstructor()
->getMock();
$user->expects($this->once())
->method('getId')
->willReturn(1);
$token = new UsernamePasswordToken($user, 'pass', 'provider');
$tokenStorage = new TokenStorage();
$tokenStorage->setToken($token);
$builder = new GrabySiteConfigBuilder(
$grabyConfigBuilderMock,
$tokenStorage,
$siteCrentialRepo,
$logger
);
$config = $builder->buildForHost('unknown.com');
$this->assertFalse($config);
$records = $handler->getRecords();
$this->assertCount(1, $records, 'One log was recorded');
}
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');
}
public function testBuildConfigUserNotDefined()
{
2022-09-01 20:54:56 +02:00
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
->disableOriginalConstructor()
->getMock();
$grabyConfigBuilderMock
->method('buildForHost')
->with('unknown.com')
->willReturn(new GrabySiteConfig());
$logger = new Logger('foo');
$handler = new TestHandler();
$logger->pushHandler($handler);
2022-09-01 20:54:56 +02:00
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
->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)
->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)
->willReturn($grabySiteConfig);
2022-09-01 20:54:56 +02:00
$user = $this->getMockBuilder(User::class)
->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,
$this->getTestClient()->getContainer()->get(SiteCredentialRepository::class),
$logger
);
$config = $builder->buildForHost($host);
if (null === $expectedUsername && null === $expectedPassword) {
$this->assertFalse($config);
return;
}
$this->assertSame($expectedUsername, $config->getUsername());
$this->assertSame($expectedPassword, $config->getPassword());
}
}