1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-15 18:57:05 +00:00

Move test files directly under tests/ directory

This commit is contained in:
Yassine Guedidi 2024-02-19 00:45:58 +01:00
parent a37b385c23
commit 24da70e338
117 changed files with 4 additions and 4 deletions

View file

@ -0,0 +1,26 @@
<?php
namespace Tests\Wallabag\CoreBundle\SiteConfig;
use PHPUnit\Framework\TestCase;
use Wallabag\CoreBundle\SiteConfig\ArraySiteConfigBuilder;
use Wallabag\CoreBundle\SiteConfig\SiteConfig;
class ArraySiteConfigBuilderTest extends TestCase
{
public function testItReturnsSiteConfigThatExists()
{
$builder = new ArraySiteConfigBuilder(['example.com' => []]);
$res = $builder->buildForHost('www.example.com');
$this->assertInstanceOf(SiteConfig::class, $res);
}
public function testItReturnsFalseOnAHostThatDoesNotExist()
{
$builder = new ArraySiteConfigBuilder(['anotherexample.com' => []]);
$res = $builder->buildForHost('example.com');
$this->assertfalse($res);
}
}

View file

@ -0,0 +1,235 @@
<?php
namespace Tests\Wallabag\CoreBundle\SiteConfig\Authenticator;
use GuzzleHttp\Client;
use GuzzleHttp\Message\Response;
use GuzzleHttp\Stream\Stream;
use GuzzleHttp\Subscriber\Mock;
use PHPUnit\Framework\TestCase;
use Wallabag\CoreBundle\SiteConfig\Authenticator\LoginFormAuthenticator;
use Wallabag\CoreBundle\SiteConfig\SiteConfig;
class LoginFormAuthenticatorTest extends TestCase
{
public function testLoginPost()
{
$response = new Response(
200,
['content-type' => 'text/html'],
Stream::factory('')
);
$guzzle = new Client();
$guzzle->getEmitter()->attach(new Mock([$response]));
$siteConfig = new SiteConfig([
'host' => 'example.com',
'loginUri' => 'http://example.com/login',
'usernameField' => 'username',
'passwordField' => 'password',
'extraFields' => [
'action' => 'login',
'foo' => 'bar',
],
'username' => 'johndoe',
'password' => 'unkn0wn',
]);
$auth = new LoginFormAuthenticator($siteConfig);
$res = $auth->login($guzzle);
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
}
public function testLoginPostWithExtraFieldsButEmptyHtml()
{
$response = new Response(
200,
['content-type' => 'text/html'],
Stream::factory('<html></html>')
);
$guzzle = new Client();
$guzzle->getEmitter()->attach(new Mock([$response, $response]));
$siteConfig = new SiteConfig([
'host' => 'example.com',
'loginUri' => 'http://example.com/login',
'usernameField' => 'username',
'passwordField' => 'password',
'extraFields' => [
'action' => 'login',
'foo' => 'bar',
'security' => '@=xpath(\'substring(//script[contains(text(), "security")]/text(), 112, 10)\', request_html(\'https://aoc.media/\'))',
],
'username' => 'johndoe',
'password' => 'unkn0wn',
]);
$auth = new LoginFormAuthenticator($siteConfig);
$res = $auth->login($guzzle);
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
}
// testing preg_match
public function testLoginPostWithExtraFieldsWithRegex()
{
$response = $this->getMockBuilder(Response::class)
->disableOriginalConstructor()
->getMock();
$response->expects($this->any())
->method('getBody')
->willReturn(file_get_contents(__DIR__ . '/../../fixtures/aoc.media.html'));
$response->expects($this->any())
->method('getStatusCode')
->willReturn(200);
$client = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$client->expects($this->any())
->method('post')
->with(
$this->equalTo('https://aoc.media/wp-admin/admin-ajax.php'),
$this->equalTo([
'body' => [
'nom' => 'johndoe',
'password' => 'unkn0wn',
'security' => 'c506c1b8bc',
'action' => 'login_user',
],
'allow_redirects' => true,
'verify' => false,
])
)
->willReturn($response);
$client->expects($this->any())
->method('get')
->with(
$this->equalTo('https://aoc.media/'),
$this->equalTo([])
)
->willReturn($response);
$siteConfig = new SiteConfig([
'host' => 'aoc.media',
'loginUri' => 'https://aoc.media/wp-admin/admin-ajax.php',
'usernameField' => 'nom',
'passwordField' => 'password',
'extraFields' => [
'action' => 'login_user',
'security' => '@=preg_match(\'/security\\\":\\\"([a-z0-9]+)\\\"/si\', request_html(\'https://aoc.media/\'))',
],
'username' => 'johndoe',
'password' => 'unkn0wn',
]);
$auth = new LoginFormAuthenticator($siteConfig);
$res = $auth->login($client);
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
}
public function testLoginPostWithExtraFieldsWithData()
{
$response = $this->getMockBuilder(Response::class)
->disableOriginalConstructor()
->getMock();
$response->expects($this->any())
->method('getBody')
->willReturn(file_get_contents(__DIR__ . '/../../fixtures/nextinpact-login.html'));
$response->expects($this->any())
->method('getStatusCode')
->willReturn(200);
$client = $this->getMockBuilder(Client::class)
->disableOriginalConstructor()
->getMock();
$client->expects($this->any())
->method('post')
->with(
$this->equalTo('https://compte.nextinpact.com/Account/Login'),
$this->equalTo([
'body' => [
'UserName' => 'johndoe',
'Password' => 'unkn0wn',
'__RequestVerificationToken' => 's6x2QcnQDUL92mkKSi_JuUBXcgUYx_Plf-KyQ2eJypKAjQZIeTvaFHOsfEdTrcSXt3dt2CW39V7r9V16LUtvjszodAU1',
'returnUrl' => 'https://www.nextinpact.com/news/102835-pour-cour-comptes-fonctionnement-actuel-vote-par-internet-nest-pas-satisfaisant.htm',
],
'allow_redirects' => true,
'verify' => false,
])
)
->willReturn($response);
$client->expects($this->any())
->method('get')
->with(
$this->equalTo('https://compte.nextinpact.com/Account/Login?http://www.nextinpact.com/'),
$this->equalTo([
'headers' => [
'X-Requested-With' => 'XMLHttpRequest',
],
])
)
->willReturn($response);
$siteConfig = new SiteConfig([
'host' => 'nextinpact.com',
'loginUri' => 'https://compte.nextinpact.com/Account/Login',
'usernameField' => 'UserName',
'passwordField' => 'Password',
'extraFields' => [
'__RequestVerificationToken' => '@=xpath(\'//form[@action="/Account/Login"]/input[@name="__RequestVerificationToken"]\', request_html(\'https://compte.nextinpact.com/Account/Login?http://www.nextinpact.com/\', {\'headers\': {\'X-Requested-With\':\'XMLHttpRequest\'}}))',
'returnUrl' => 'https://www.nextinpact.com/news/102835-pour-cour-comptes-fonctionnement-actuel-vote-par-internet-nest-pas-satisfaisant.htm',
],
'username' => 'johndoe',
'password' => 'unkn0wn',
]);
$auth = new LoginFormAuthenticator($siteConfig);
$res = $auth->login($client);
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
}
public function testLoginWithBadSiteConfigNotLoggedInData()
{
$siteConfig = new SiteConfig([
'host' => 'nextinpact.com',
'loginUri' => 'https://compte.nextinpact.com/Account/Login',
'usernameField' => 'UserName',
'username' => 'johndoe',
'password' => 'unkn0wn',
]);
$auth = new LoginFormAuthenticator($siteConfig);
$loginRequired = $auth->isLoginRequired(file_get_contents(__DIR__ . '/../../fixtures/nextinpact-login.html'));
$this->assertFalse($loginRequired);
}
public function testLoginWithGoodSiteConfigNotLoggedInData()
{
$siteConfig = new SiteConfig([
'host' => 'nextinpact.com',
'loginUri' => 'https://compte.nextinpact.com/Account/Login',
'usernameField' => 'UserName',
'username' => 'johndoe',
'password' => 'unkn0wn',
'notLoggedInXpath' => '//h2[@class="title_reserve_article"]',
]);
$auth = new LoginFormAuthenticator($siteConfig);
$loginRequired = $auth->isLoginRequired(file_get_contents(__DIR__ . '/../../fixtures/nextinpact-article.html'));
$this->assertTrue($loginRequired);
}
}

View file

@ -0,0 +1,321 @@
<?php
namespace Tests\Wallabag\CoreBundle\SiteConfig;
use Graby\SiteConfig\ConfigBuilder;
use Graby\SiteConfig\SiteConfig as GrabySiteConfig;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\User;
use Wallabag\CoreBundle\Repository\SiteCredentialRepository;
use Wallabag\CoreBundle\SiteConfig\GrabySiteConfigBuilder;
class GrabySiteConfigBuilderTest extends WallabagCoreTestCase
{
private $builder;
public function testBuildConfigExists()
{
$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')
->willReturn($grabySiteConfig);
$logger = new Logger('foo');
$handler = new TestHandler();
$logger->pushHandler($handler);
$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']);
$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());
$this->assertTrue($config->requiresLogin());
$this->assertSame('http://api.example.com/login', $config->getLoginUri());
$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()
{
$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);
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
->disableOriginalConstructor()
->getMock();
$siteCrentialRepo->expects($this->once())
->method('findOneByHostsAndUser')
->with(['unknown.com', '.com'], 1)
->willReturn(null);
$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');
}
public function testBuildConfigWithBadExtraFields()
{
$grabyConfigBuilderMock = $this->getMockBuilder(ConfigBuilder::class)
->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')
->willReturn($grabySiteConfig);
$logger = new Logger('foo');
$handler = new TestHandler();
$logger->pushHandler($handler);
$siteCrentialRepo = $this->getMockBuilder(SiteCredentialRepository::class)
->disableOriginalConstructor()
->getMock();
$siteCrentialRepo->expects($this->once())
->method('findOneByHostsAndUser')
->with(['example.com', '.com'], 1)
->willReturn(['username' => 'foo', 'password' => 'bar']);
$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);
$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()
{
$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);
$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)
{
$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);
$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());
}
}

View file

@ -0,0 +1,44 @@
<?php
namespace Tests\Wallabag\CoreBundle\SiteConfig;
use PHPUnit\Framework\TestCase;
use Wallabag\CoreBundle\SiteConfig\SiteConfig;
class SiteConfigTest extends TestCase
{
public function testInitSiteConfig()
{
$config = new SiteConfig([]);
$this->assertInstanceOf(SiteConfig::class, $config);
}
public function testUnknownProperty()
{
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Unknown property: "bad"');
new SiteConfig(['bad' => true]);
}
public function testInitSiteConfigWillFullOptions()
{
$config = new SiteConfig([
'host' => 'example.com',
'requiresLogin' => true,
'notLoggedInXpath' => '//all',
'loginUri' => 'https://example.com/login',
'usernameField' => 'username',
'passwordField' => 'password',
'extraFields' => [
'action' => 'login',
'foo' => 'bar',
],
'username' => 'johndoe',
'password' => 'unkn0wn',
]);
$this->assertInstanceOf(SiteConfig::class, $config);
}
}