2024-02-02 21:56:25 +01:00
|
|
|
<?php
|
|
|
|
|
2024-11-23 20:07:07 +01:00
|
|
|
namespace Tests\Wallabag\SiteConfig;
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
2025-01-09 01:14:27 +01:00
|
|
|
use Symfony\Component\BrowserKit\HttpBrowser;
|
2024-12-23 00:02:59 +01:00
|
|
|
use Symfony\Component\HttpClient\MockHttpClient;
|
|
|
|
use Symfony\Component\HttpClient\Response\MockResponse;
|
2025-01-09 01:14:27 +01:00
|
|
|
use Symfony\Contracts\HttpClient\HttpClientInterface;
|
|
|
|
use Symfony\Contracts\HttpClient\ResponseInterface;
|
2024-12-23 00:02:59 +01:00
|
|
|
use Wallabag\ExpressionLanguage\AuthenticatorProvider;
|
2024-11-23 20:07:07 +01:00
|
|
|
use Wallabag\SiteConfig\LoginFormAuthenticator;
|
2024-02-19 01:30:12 +01:00
|
|
|
use Wallabag\SiteConfig\SiteConfig;
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
class LoginFormAuthenticatorTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testLoginPost()
|
|
|
|
{
|
|
|
|
$siteConfig = new SiteConfig([
|
|
|
|
'host' => 'example.com',
|
|
|
|
'loginUri' => 'http://example.com/login',
|
|
|
|
'usernameField' => 'username',
|
|
|
|
'passwordField' => 'password',
|
|
|
|
'extraFields' => [
|
|
|
|
'action' => 'login',
|
|
|
|
'foo' => 'bar',
|
|
|
|
],
|
|
|
|
'username' => 'johndoe',
|
|
|
|
'password' => 'unkn0wn',
|
|
|
|
]);
|
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$browserResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$browserClient = new MockHttpClient([$browserResponse]);
|
|
|
|
$browser = new HttpBrowser($browserClient);
|
2025-01-09 00:13:50 +01:00
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$requestHtmlFunctionResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$requestHtmlFunctionClient = new MockHttpClient([$requestHtmlFunctionResponse]);
|
|
|
|
$authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient);
|
2025-01-09 00:13:50 +01:00
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$auth = new LoginFormAuthenticator($browser, $authenticatorProvider);
|
|
|
|
|
|
|
|
$res = $auth->login($siteConfig);
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginPostWithExtraFieldsButEmptyHtml()
|
|
|
|
{
|
|
|
|
$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',
|
|
|
|
]);
|
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$browserResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$browserClient = new MockHttpClient([$browserResponse]);
|
|
|
|
$browser = new HttpBrowser($browserClient);
|
|
|
|
|
|
|
|
$requestHtmlFunctionResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$requestHtmlFunctionClient = new MockHttpClient([$requestHtmlFunctionResponse]);
|
|
|
|
$authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient);
|
2025-01-09 00:13:50 +01:00
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$auth = new LoginFormAuthenticator($browser, $authenticatorProvider);
|
2025-01-09 00:13:50 +01:00
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$res = $auth->login($siteConfig);
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
$this->assertInstanceOf(LoginFormAuthenticator::class, $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
// testing preg_match
|
|
|
|
public function testLoginPostWithExtraFieldsWithRegex()
|
|
|
|
{
|
2025-01-09 00:13:50 +01:00
|
|
|
$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',
|
|
|
|
]);
|
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$browserResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$browserClient = new MockHttpClient([$browserResponse]);
|
|
|
|
$browser = $this->getMockBuilder(HttpBrowser::class)
|
|
|
|
->setConstructorArgs([$browserClient])
|
2024-02-02 21:56:25 +01:00
|
|
|
->getMock();
|
2025-01-09 01:14:27 +01:00
|
|
|
$browser->expects($this->any())
|
|
|
|
->method('request')
|
2024-02-02 21:56:25 +01:00
|
|
|
->with(
|
2025-01-09 01:14:27 +01:00
|
|
|
$this->equalTo('POST'),
|
2024-02-02 21:56:25 +01:00
|
|
|
$this->equalTo('https://aoc.media/wp-admin/admin-ajax.php'),
|
|
|
|
$this->equalTo([
|
2025-01-09 01:14:27 +01:00
|
|
|
'nom' => 'johndoe',
|
|
|
|
'password' => 'unkn0wn',
|
|
|
|
'security' => 'c506c1b8bc',
|
|
|
|
'action' => 'login_user',
|
2024-02-02 21:56:25 +01:00
|
|
|
])
|
|
|
|
)
|
2025-01-09 01:14:27 +01:00
|
|
|
;
|
|
|
|
|
|
|
|
$requestHtmlFunctionResponse = $this->getMockBuilder(ResponseInterface::class)->getMock();
|
|
|
|
$requestHtmlFunctionResponse->expects($this->any())
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn(file_get_contents(__DIR__ . '/../fixtures/aoc.media.html'))
|
|
|
|
;
|
|
|
|
$requestHtmlFunctionClient = $this->getMockBuilder(HttpClientInterface::class)->getMock();
|
|
|
|
$requestHtmlFunctionClient->expects($this->any())
|
|
|
|
->method('request')
|
2024-02-02 21:56:25 +01:00
|
|
|
->with(
|
2025-01-09 01:14:27 +01:00
|
|
|
$this->equalTo('GET'),
|
2024-02-02 21:56:25 +01:00
|
|
|
$this->equalTo('https://aoc.media/'),
|
|
|
|
)
|
2025-01-09 01:14:27 +01:00
|
|
|
->willReturn($requestHtmlFunctionResponse)
|
|
|
|
;
|
|
|
|
$authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient);
|
2024-02-02 21:56:25 +01:00
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$auth = new LoginFormAuthenticator($browser, $authenticatorProvider);
|
2024-02-02 21:56:25 +01:00
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$res = $auth->login($siteConfig);
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
$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',
|
|
|
|
]);
|
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$browserResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$browserClient = new MockHttpClient([$browserResponse]);
|
|
|
|
$browser = new HttpBrowser($browserClient);
|
|
|
|
|
|
|
|
$requestHtmlFunctionResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$requestHtmlFunctionClient = new MockHttpClient([$requestHtmlFunctionResponse]);
|
|
|
|
$authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient);
|
|
|
|
|
|
|
|
$auth = new LoginFormAuthenticator($browser, $authenticatorProvider);
|
2024-12-23 00:02:59 +01:00
|
|
|
|
2024-11-23 20:07:07 +01:00
|
|
|
$loginRequired = $auth->isLoginRequired($siteConfig, file_get_contents(__DIR__ . '/../fixtures/nextinpact-login.html'));
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
$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"]',
|
|
|
|
]);
|
|
|
|
|
2025-01-09 01:14:27 +01:00
|
|
|
$browserResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$browserClient = new MockHttpClient([$browserResponse]);
|
|
|
|
$browser = new HttpBrowser($browserClient);
|
|
|
|
|
|
|
|
$requestHtmlFunctionResponse = new MockResponse('<html></html>', ['http_code' => 200, 'response_headers' => ['content-type' => 'text/html']]);
|
|
|
|
$requestHtmlFunctionClient = new MockHttpClient([$requestHtmlFunctionResponse]);
|
|
|
|
$authenticatorProvider = new AuthenticatorProvider($requestHtmlFunctionClient);
|
|
|
|
|
|
|
|
$auth = new LoginFormAuthenticator($browser, $authenticatorProvider);
|
2024-12-23 00:02:59 +01:00
|
|
|
|
2024-11-23 20:07:07 +01:00
|
|
|
$loginRequired = $auth->isLoginRequired($siteConfig, file_get_contents(__DIR__ . '/../fixtures/nextinpact-article.html'));
|
2024-02-02 21:56:25 +01:00
|
|
|
|
|
|
|
$this->assertTrue($loginRequired);
|
|
|
|
}
|
|
|
|
}
|