5a02e39a65
- renamed a lot of App methods to CamelCase - replaced direct public variables with get-/set-Methods
3700 lines
94 KiB
PHP
3700 lines
94 KiB
PHP
<?php
|
|
/**
|
|
* ApiTest class.
|
|
*/
|
|
|
|
namespace Friendica\Test;
|
|
|
|
use Friendica\BaseObject;
|
|
use Friendica\Core\Config;
|
|
use Friendica\Core\PConfig;
|
|
use Friendica\Core\Protocol;
|
|
use Friendica\Core\System;
|
|
use Friendica\Network\HTTPException;
|
|
|
|
/**
|
|
* Tests for the API functions.
|
|
*
|
|
* Functions that use header() need to be tested in a separate process.
|
|
* @see https://phpunit.de/manual/5.7/en/appendixes.annotations.html#appendixes.annotations.runTestsInSeparateProcesses
|
|
*/
|
|
class ApiTest extends DatabaseTest
|
|
{
|
|
|
|
/**
|
|
* Create variables used by tests.
|
|
*/
|
|
public function setUp()
|
|
{
|
|
parent::setUp();
|
|
|
|
require_once __DIR__.'/../include/api.php';
|
|
|
|
// User data that the test database is populated with
|
|
$this->selfUser = [
|
|
'id' => 42,
|
|
'name' => 'Self contact',
|
|
'nick' => 'selfcontact',
|
|
'nurl' => 'http://localhost/profile/selfcontact'
|
|
];
|
|
$this->friendUser = [
|
|
'id' => 44,
|
|
'name' => 'Friend contact',
|
|
'nick' => 'friendcontact',
|
|
'nurl' => 'http://localhost/profile/friendcontact'
|
|
];
|
|
$this->otherUser = [
|
|
'id' => 43,
|
|
'name' => 'othercontact',
|
|
'nick' => 'othercontact',
|
|
'nurl' => 'http://localhost/profile/othercontact'
|
|
];
|
|
|
|
// User ID that we know is not in the database
|
|
$this->wrongUserId = 666;
|
|
|
|
// Most API require login so we force the session
|
|
$_SESSION = [
|
|
'allow_api' => true,
|
|
'authenticated' => true,
|
|
'uid' => $this->selfUser['id']
|
|
];
|
|
|
|
// Default config
|
|
Config::set('config', 'hostname', 'localhost');
|
|
Config::set('system', 'throttle_limit_day', 100);
|
|
Config::set('system', 'throttle_limit_week', 100);
|
|
Config::set('system', 'throttle_limit_month', 100);
|
|
Config::set('system', 'theme', 'system_theme');
|
|
}
|
|
|
|
/**
|
|
* Cleanup variables used by tests.
|
|
*/
|
|
protected function tearDown()
|
|
{
|
|
parent::tearDown();
|
|
|
|
$this->app->argc = 1;
|
|
$this->app->argv = ['home'];
|
|
}
|
|
|
|
/**
|
|
* Assert that an user array contains expected keys.
|
|
* @param array $user User array
|
|
* @return void
|
|
*/
|
|
private function assertSelfUser(array $user)
|
|
{
|
|
$this->assertEquals($this->selfUser['id'], $user['uid']);
|
|
$this->assertEquals($this->selfUser['id'], $user['cid']);
|
|
$this->assertEquals(1, $user['self']);
|
|
$this->assertEquals('Friendica', $user['location']);
|
|
$this->assertEquals($this->selfUser['name'], $user['name']);
|
|
$this->assertEquals($this->selfUser['nick'], $user['screen_name']);
|
|
$this->assertEquals('dfrn', $user['network']);
|
|
$this->assertTrue($user['verified']);
|
|
}
|
|
|
|
/**
|
|
* Assert that an user array contains expected keys.
|
|
* @param array $user User array
|
|
* @return void
|
|
*/
|
|
private function assertOtherUser(array $user)
|
|
{
|
|
$this->assertEquals($this->otherUser['id'], $user['id']);
|
|
$this->assertEquals($this->otherUser['id'], $user['id_str']);
|
|
$this->assertEquals(0, $user['self']);
|
|
$this->assertEquals($this->otherUser['name'], $user['name']);
|
|
$this->assertEquals($this->otherUser['nick'], $user['screen_name']);
|
|
$this->assertFalse($user['verified']);
|
|
}
|
|
|
|
/**
|
|
* Assert that a status array contains expected keys.
|
|
* @param array $status Status array
|
|
* @return void
|
|
*/
|
|
private function assertStatus(array $status)
|
|
{
|
|
$this->assertInternalType('string', $status['text']);
|
|
$this->assertInternalType('int', $status['id']);
|
|
// We could probably do more checks here.
|
|
}
|
|
|
|
/**
|
|
* Assert that a list array contains expected keys.
|
|
* @param array $list List array
|
|
* @return void
|
|
*/
|
|
private function assertList(array $list)
|
|
{
|
|
$this->assertInternalType('string', $list['name']);
|
|
$this->assertInternalType('int', $list['id']);
|
|
$this->assertInternalType('string', $list['id_str']);
|
|
$this->assertContains($list['mode'], ['public', 'private']);
|
|
// We could probably do more checks here.
|
|
}
|
|
|
|
/**
|
|
* Assert that the string is XML and contain the root element.
|
|
* @param string $result XML string
|
|
* @param string $root_element Root element name
|
|
* @return void
|
|
*/
|
|
private function assertXml($result, $root_element)
|
|
{
|
|
$this->assertStringStartsWith('<?xml version="1.0"?>', $result);
|
|
$this->assertContains('<'.$root_element, $result);
|
|
// We could probably do more checks here.
|
|
}
|
|
|
|
/**
|
|
* Get the path to a temporary empty PNG image.
|
|
* @return string Path
|
|
*/
|
|
private function getTempImage()
|
|
{
|
|
$tmpFile = tempnam(sys_get_temp_dir(), 'tmp_file');
|
|
file_put_contents(
|
|
$tmpFile,
|
|
base64_decode(
|
|
// Empty 1x1 px PNG image
|
|
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg=='
|
|
)
|
|
);
|
|
|
|
return $tmpFile;
|
|
}
|
|
|
|
/**
|
|
* Test the api_user() function.
|
|
* @return void
|
|
*/
|
|
public function testApiUser()
|
|
{
|
|
$this->assertEquals($this->selfUser['id'], api_user());
|
|
}
|
|
|
|
/**
|
|
* Test the api_user() function with an unallowed user.
|
|
* @return void
|
|
*/
|
|
public function testApiUserWithUnallowedUser()
|
|
{
|
|
$_SESSION = ['allow_api' => false];
|
|
$this->assertEquals(false, api_user());
|
|
}
|
|
|
|
/**
|
|
* Test the api_source() function.
|
|
* @return void
|
|
*/
|
|
public function testApiSource()
|
|
{
|
|
$this->assertEquals('api', api_source());
|
|
}
|
|
|
|
/**
|
|
* Test the api_source() function with a Twidere user agent.
|
|
* @return void
|
|
*/
|
|
public function testApiSourceWithTwidere()
|
|
{
|
|
$_SERVER['HTTP_USER_AGENT'] = 'Twidere';
|
|
$this->assertEquals('Twidere', api_source());
|
|
}
|
|
|
|
/**
|
|
* Test the api_source() function with a GET parameter.
|
|
* @return void
|
|
*/
|
|
public function testApiSourceWithGet()
|
|
{
|
|
$_GET['source'] = 'source_name';
|
|
$this->assertEquals('source_name', api_source());
|
|
}
|
|
|
|
/**
|
|
* Test the api_date() function.
|
|
* @return void
|
|
*/
|
|
public function testApiDate()
|
|
{
|
|
$this->assertEquals('Wed Oct 10 00:00:00 +0000 1990', api_date('1990-10-10'));
|
|
}
|
|
|
|
/**
|
|
* Test the api_register_func() function.
|
|
* @return void
|
|
*/
|
|
public function testApiRegisterFunc()
|
|
{
|
|
global $API;
|
|
$this->assertNull(
|
|
api_register_func(
|
|
'api_path',
|
|
function () {
|
|
},
|
|
true,
|
|
'method'
|
|
)
|
|
);
|
|
$this->assertTrue($API['api_path']['auth']);
|
|
$this->assertEquals('method', $API['api_path']['method']);
|
|
$this->assertTrue(is_callable($API['api_path']['func']));
|
|
}
|
|
|
|
/**
|
|
* Test the api_login() function without any login.
|
|
* @return void
|
|
* @runInSeparateProcess
|
|
* @expectedException Friendica\Network\HTTPException\UnauthorizedException
|
|
*/
|
|
public function testApiLoginWithoutLogin()
|
|
{
|
|
api_login($this->app);
|
|
}
|
|
|
|
/**
|
|
* Test the api_login() function with a bad login.
|
|
* @return void
|
|
* @runInSeparateProcess
|
|
* @expectedException Friendica\Network\HTTPException\UnauthorizedException
|
|
*/
|
|
public function testApiLoginWithBadLogin()
|
|
{
|
|
$_SERVER['PHP_AUTH_USER'] = 'user@server';
|
|
api_login($this->app);
|
|
}
|
|
|
|
/**
|
|
* Test the api_login() function with oAuth.
|
|
* @return void
|
|
*/
|
|
public function testApiLoginWithOauth()
|
|
{
|
|
$this->markTestIncomplete('Can we test this easily?');
|
|
}
|
|
|
|
/**
|
|
* Test the api_login() function with authentication provided by an addon.
|
|
* @return void
|
|
*/
|
|
public function testApiLoginWithAddonAuth()
|
|
{
|
|
$this->markTestIncomplete('Can we test this easily?');
|
|
}
|
|
|
|
/**
|
|
* Test the api_login() function with a correct login.
|
|
* @return void
|
|
* @runInSeparateProcess
|
|
*/
|
|
public function testApiLoginWithCorrectLogin()
|
|
{
|
|
$_SERVER['PHP_AUTH_USER'] = 'Test user';
|
|
$_SERVER['PHP_AUTH_PW'] = 'password';
|
|
api_login($this->app);
|
|
}
|
|
|
|
/**
|
|
* Test the api_login() function with a remote user.
|
|
* @return void
|
|
* @runInSeparateProcess
|
|
* @expectedException Friendica\Network\HTTPException\UnauthorizedException
|
|
*/
|
|
public function testApiLoginWithRemoteUser()
|
|
{
|
|
$_SERVER['REDIRECT_REMOTE_USER'] = '123456dXNlcjpwYXNzd29yZA==';
|
|
api_login($this->app);
|
|
}
|
|
|
|
/**
|
|
* Test the api_check_method() function.
|
|
* @return void
|
|
*/
|
|
public function testApiCheckMethod()
|
|
{
|
|
$this->assertFalse(api_check_method('method'));
|
|
}
|
|
|
|
/**
|
|
* Test the api_check_method() function with a correct method.
|
|
* @return void
|
|
*/
|
|
public function testApiCheckMethodWithCorrectMethod()
|
|
{
|
|
$_SERVER['REQUEST_METHOD'] = 'method';
|
|
$this->assertTrue(api_check_method('method'));
|
|
}
|
|
|
|
/**
|
|
* Test the api_check_method() function with a wildcard.
|
|
* @return void
|
|
*/
|
|
public function testApiCheckMethodWithWildcard()
|
|
{
|
|
$this->assertTrue(api_check_method('*'));
|
|
}
|
|
|
|
/**
|
|
* Test the api_call() function.
|
|
* @return void
|
|
* @runInSeparateProcess
|
|
*/
|
|
public function testApiCall()
|
|
{
|
|
global $API;
|
|
$API['api_path'] = [
|
|
'method' => 'method',
|
|
'func' => function () {
|
|
return ['data' => ['some_data']];
|
|
}
|
|
];
|
|
$_SERVER['REQUEST_METHOD'] = 'method';
|
|
$_GET['callback'] = 'callback_name';
|
|
|
|
$this->app->query_string = 'api_path';
|
|
$this->assertEquals(
|
|
'callback_name(["some_data"])',
|
|
api_call($this->app)
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Test the api_call() function with the profiled enabled.
|
|
* @return void
|
|
* @runInSeparateProcess
|
|
*/
|
|
public function testApiCallWithProfiler()
|
|
|