2024-06-04 07:13:55 +02:00
|
|
|
<?php declare(strict_types=1);
|
2022-10-23 13:09:54 +02:00
|
|
|
|
2022-10-25 06:41:42 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2022-10-23 13:09:54 +02:00
|
|
|
use PrivateBin\Configuration;
|
|
|
|
use PrivateBin\YourlsProxy;
|
|
|
|
|
2022-10-25 06:39:50 +02:00
|
|
|
class YourlsProxyTest extends TestCase
|
2022-10-23 13:09:54 +02:00
|
|
|
{
|
|
|
|
private $_conf;
|
|
|
|
|
|
|
|
private $_path;
|
|
|
|
|
|
|
|
private $_mock_yourls_service;
|
|
|
|
|
2022-10-25 06:43:17 +02:00
|
|
|
public function setUp(): void
|
2022-10-23 13:09:54 +02:00
|
|
|
{
|
|
|
|
/* Setup Routine */
|
|
|
|
$this->_path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'privatebin_data';
|
|
|
|
if (!is_dir($this->_path)) {
|
|
|
|
mkdir($this->_path);
|
|
|
|
}
|
|
|
|
$this->_mock_yourls_service = $this->_path . DIRECTORY_SEPARATOR . 'yourls.json';
|
|
|
|
$options = parse_ini_file(CONF_SAMPLE, true);
|
2022-10-29 07:54:42 +02:00
|
|
|
$options['main']['basepath'] = 'https://example.com/';
|
2022-10-23 13:09:54 +02:00
|
|
|
$options['main']['urlshortener'] = 'https://example.com/shortenviayourls?link=';
|
|
|
|
$options['yourls']['apiurl'] = $this->_mock_yourls_service;
|
|
|
|
Helper::confBackup();
|
|
|
|
Helper::createIniFile(CONF, $options);
|
|
|
|
$this->_conf = new Configuration;
|
|
|
|
}
|
|
|
|
|
2022-10-25 06:43:17 +02:00
|
|
|
public function tearDown(): void
|
2022-10-23 13:09:54 +02:00
|
|
|
{
|
|
|
|
/* Tear Down Routine */
|
|
|
|
unlink(CONF);
|
|
|
|
Helper::confRestore();
|
|
|
|
Helper::rmDir($this->_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testYourlsProxy()
|
|
|
|
{
|
|
|
|
// the real service answer is more complex, but we only look for the shorturl & statusCode
|
|
|
|
file_put_contents($this->_mock_yourls_service, '{"shorturl":"https:\/\/example.com\/1","statusCode":200}');
|
|
|
|
|
|
|
|
$yourls = new YourlsProxy($this->_conf, 'https://example.com/?foo#bar');
|
|
|
|
$this->assertFalse($yourls->isError());
|
|
|
|
$this->assertEquals($yourls->getUrl(), 'https://example.com/1');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testForeignUrl()
|
|
|
|
{
|
|
|
|
$yourls = new YourlsProxy($this->_conf, 'https://other.example.com/?foo#bar');
|
|
|
|
$this->assertTrue($yourls->isError());
|
|
|
|
$this->assertEquals($yourls->getError(), 'Trying to shorten a URL that isn\'t pointing at our instance.');
|
2024-06-29 20:26:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSneakyForeignUrl()
|
|
|
|
{
|
|
|
|
$yourls = new YourlsProxy($this->_conf, 'https://other.example.com/?q=https://example.com/?foo#bar');
|
|
|
|
$this->assertTrue($yourls->isError());
|
|
|
|
$this->assertEquals($yourls->getError(), 'Trying to shorten a URL that isn\'t pointing at our instance.');
|
2022-10-23 13:09:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testYourlsError()
|
|
|
|
{
|
|
|
|
// when statusCode is not 200, shorturl may not have been set
|
|
|
|
file_put_contents($this->_mock_yourls_service, '{"statusCode":403}');
|
|
|
|
|
|
|
|
$yourls = new YourlsProxy($this->_conf, 'https://example.com/?foo#bar');
|
|
|
|
$this->assertTrue($yourls->isError());
|
|
|
|
$this->assertEquals($yourls->getError(), 'Error parsing YOURLS response.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testServerError()
|
|
|
|
{
|
|
|
|
// simulate some other server error that results in a non-JSON reply
|
2022-10-23 13:21:22 +02:00
|
|
|
file_put_contents($this->_mock_yourls_service, '500 Internal Server Error');
|
2022-10-23 13:09:54 +02:00
|
|
|
|
|
|
|
$yourls = new YourlsProxy($this->_conf, 'https://example.com/?foo#bar');
|
|
|
|
$this->assertTrue($yourls->isError());
|
|
|
|
$this->assertEquals($yourls->getError(), 'Error calling YOURLS. Probably a configuration issue, like wrong or missing "apiurl" or "signature".');
|
|
|
|
}
|
2022-10-23 13:13:12 +02:00
|
|
|
}
|