2021-11-12 20:08:36 +00:00
|
|
|
<?php
|
2021-11-12 20:50:02 +00:00
|
|
|
/**
|
2023-01-01 14:36:24 +00:00
|
|
|
* @copyright Copyright (C) 2010-2023, the Friendica project
|
2021-11-12 20:50:02 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2021-11-12 20:08:36 +00:00
|
|
|
|
|
|
|
namespace Friendica\Test\src\Module\Api;
|
|
|
|
|
2021-11-28 12:11:12 +00:00
|
|
|
use Friendica\App;
|
2021-12-09 19:53:29 +00:00
|
|
|
use Friendica\Capabilities\ICanCreateResponses;
|
2021-11-12 20:08:36 +00:00
|
|
|
use Friendica\Core\Addon;
|
2023-02-21 20:01:40 +00:00
|
|
|
use Friendica\Core\Config\Capability\IManageConfigValues;
|
2021-11-12 20:08:36 +00:00
|
|
|
use Friendica\Core\Hook;
|
|
|
|
use Friendica\Database\Database;
|
|
|
|
use Friendica\DI;
|
2022-12-26 20:17:32 +00:00
|
|
|
use Friendica\Module\Special\HTTPException;
|
2021-11-12 20:08:36 +00:00
|
|
|
use Friendica\Security\Authentication;
|
2021-11-30 19:53:47 +00:00
|
|
|
use Friendica\Security\BasicAuth;
|
2021-11-12 20:08:36 +00:00
|
|
|
use Friendica\Test\FixtureTest;
|
2021-11-28 12:11:12 +00:00
|
|
|
use Friendica\Test\Util\AppDouble;
|
2021-11-12 20:08:36 +00:00
|
|
|
use Friendica\Test\Util\AuthenticationDouble;
|
2021-11-30 19:53:47 +00:00
|
|
|
use Friendica\Test\Util\AuthTestConfig;
|
2022-12-26 20:17:32 +00:00
|
|
|
use Mockery\MockInterface;
|
2021-12-09 19:53:29 +00:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
2021-11-12 20:08:36 +00:00
|
|
|
|
2021-11-12 21:30:48 +00:00
|
|
|
abstract class ApiTest extends FixtureTest
|
2021-11-12 20:08:36 +00:00
|
|
|
{
|
2021-12-09 20:32:17 +00:00
|
|
|
// User data that the test database is populated with
|
|
|
|
const SELF_USER = [
|
|
|
|
'id' => 42,
|
|
|
|
'name' => 'Self contact',
|
|
|
|
'nick' => 'selfcontact',
|
|
|
|
'nurl' => 'http://localhost/profile/selfcontact'
|
|
|
|
];
|
|
|
|
|
|
|
|
const FRIEND_USER = [
|
|
|
|
'id' => 44,
|
|
|
|
'name' => 'Friend contact',
|
|
|
|
'nick' => 'friendcontact',
|
|
|
|
'nurl' => 'http://localhost/profile/friendcontact'
|
|
|
|
];
|
|
|
|
|
|
|
|
const OTHER_USER = [
|
|
|
|
'id' => 43,
|
|
|
|
'name' => 'othercontact',
|
|
|
|
'nick' => 'othercontact',
|
|
|
|
'nurl' => 'http://localhost/profile/othercontact'
|
|
|
|
];
|
|
|
|
|
2022-12-26 20:17:32 +00:00
|
|
|
/** @var HTTPException */
|
|
|
|
protected $httpExceptionMock;
|
|
|
|
|
2021-12-09 20:32:17 +00:00
|
|
|
// User ID that we know is not in the database
|
|
|
|
const WRONG_USER_ID = 666;
|
|
|
|
|
2021-11-12 21:03:41 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
protected function assertXml(string $result = '', string $root_element = '')
|
|
|
|
{
|
|
|
|
self::assertStringStartsWith('<?xml version="1.0"?>', $result);
|
|
|
|
self::assertStringContainsString('<' . $root_element, $result);
|
|
|
|
// We could probably do more checks here.
|
|
|
|
}
|
|
|
|
|
2021-12-09 20:32:17 +00:00
|
|
|
/**
|
|
|
|
* Assert that an user array contains expected keys.
|
|
|
|
*
|
|
|
|
* @param \stdClass $user User
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function assertSelfUser(\stdClass $user)
|
|
|
|
{
|
|
|
|
self::assertEquals(self::SELF_USER['id'], $user->uid);
|
|
|
|
self::assertEquals(self::SELF_USER['id'], $user->cid);
|
|
|
|
self::assertEquals(1, $user->self);
|
|
|
|
self::assertEquals('DFRN', $user->location);
|
|
|
|
self::assertEquals(self::SELF_USER['name'], $user->name);
|
|
|
|
self::assertEquals(self::SELF_USER['nick'], $user->screen_name);
|
|
|
|
self::assertEquals('dfrn', $user->network);
|
|
|
|
self::assertTrue($user->verified);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that an user array contains expected keys.
|
|
|
|
*
|
|
|
|
* @param \stdClass $user User
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function assertOtherUser(\stdClass $user)
|
|
|
|
{
|
|
|
|
self::assertEquals(self::OTHER_USER['id'], $user->id);
|
|
|
|
self::assertEquals(self::OTHER_USER['id'], $user->id_str);
|
|
|
|
self::assertEquals(self::OTHER_USER['name'], $user->name);
|
|
|
|
self::assertEquals(self::OTHER_USER['nick'], $user->screen_name);
|
|
|
|
self::assertFalse($user->verified);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assert that a status array contains expected keys.
|
|
|
|
*
|
2021-12-09 20:52:40 +00:00
|
|
|
* @param \stdClass $status Status
|
2021-12-09 20:32:17 +00:00
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2021-12-09 20:52:40 +00:00
|
|
|
protected function assertStatus(\stdClass $status)
|
2021-12-09 20:32:17 +00:00
|
|
|
{
|
2021-12-09 20:52:40 +00:00
|
|
|
self::assertIsString($status->text);
|
|
|
|
self::assertIsInt($status->id);
|
2021-12-09 20:32:17 +00:00
|
|
|
// We could probably do more checks here.
|
|
|
|
}
|
|
|
|
|
2021-12-09 21:08:31 +00:00
|
|
|
/**
|
|
|
|
* Get the path to a temporary empty PNG image.
|
|
|
|
*
|
|
|
|
* @return string Path
|
|
|
|
*/
|
|
|
|
protected 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;
|
|
|
|
}
|
|
|
|
|
2021-12-09 19:53:29 +00:00
|
|
|
/**
|
|
|
|
* Transforms a response into a JSON class
|
|
|
|
*
|
|
|
|
* @param ResponseInterface $response
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function toJson(ResponseInterface $response)
|
|
|
|
{
|
|
|
|
self::assertEquals(ICanCreateResponses::TYPE_JSON, $response->getHeaderLine(ICanCreateResponses::X_HEADER));
|
|
|
|
|
|
|
|
$body = (string)$response->getBody();
|
|
|
|
|
|
|
|
self::assertJson($body);
|
|
|
|
|
|
|
|
return json_decode($body);
|
|
|
|
}
|
|
|
|
|
2021-11-12 20:08:36 +00:00
|
|
|
protected function setUp(): void
|
|
|
|
{
|
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
|
|
|
|
|
$this->dice = $this->dice
|
2021-11-28 12:11:12 +00:00
|
|
|
->addRule(Authentication::class, ['instanceOf' => AuthenticationDouble::class, 'shared' => true])
|
|
|
|
->addRule(App::class, ['instanceOf' => AppDouble::class, 'shared' => true]);
|
2021-11-12 20:08:36 +00:00
|
|
|
DI::init($this->dice);
|
|
|
|
|
2021-11-28 13:26:53 +00:00
|
|
|
// Manual override to bypass API authentication
|
2021-11-28 12:11:12 +00:00
|
|
|
DI::app()->setIsLoggedIn(true);
|
|
|
|
|
2022-12-26 20:17:32 +00:00
|
|
|
$this->httpExceptionMock = $this->dice->create(HTTPException::class);
|
|
|
|
|
2021-11-30 19:53:47 +00:00
|
|
|
AuthTestConfig::$authenticated = true;
|
2021-12-09 19:53:29 +00:00
|
|
|
AuthTestConfig::$user_id = 42;
|
2021-11-30 19:53:47 +00:00
|
|
|
|
2021-11-12 20:08:36 +00:00
|
|
|
$this->installAuthTest();
|
|
|
|
}
|
|
|
|
|
2021-11-30 19:53:47 +00:00
|
|
|
protected function tearDown(): void
|
|
|
|
{
|
|
|
|
BasicAuth::setCurrentUserID();
|
|
|
|
|
|
|
|
parent::tearDown(); // TODO: Change the autogenerated stub
|
|
|
|
}
|
|
|
|
|
2021-11-12 20:08:36 +00:00
|
|
|
/**
|
|
|
|
* installs auththest.
|
|
|
|
*
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function installAuthTest()
|
|
|
|
{
|
|
|
|
$addon = 'authtest';
|
|
|
|
$addon_file_path = __DIR__ . '/../../../Util/authtest/authtest.php';
|
|
|
|
$t = @filemtime($addon_file_path);
|
|
|
|
|
|
|
|
@include_once($addon_file_path);
|
|
|
|
if (function_exists($addon . '_install')) {
|
|
|
|
$func = $addon . '_install';
|
|
|
|
$func(DI::app());
|
|
|
|
}
|
|
|
|
|
2023-02-21 20:01:40 +00:00
|
|
|
/** @var $config IManageConfigValues */
|
|
|
|
$config = $this->dice->create(IManageConfigValues::class);
|
2021-11-12 20:08:36 +00:00
|
|
|
|
2023-02-21 20:01:40 +00:00
|
|
|
$config->set('addons', $addon, [
|
2021-11-12 20:08:36 +00:00
|
|
|
'name' => $addon,
|
|
|
|
'installed' => true,
|
|
|
|
'timestamp' => $t,
|
|
|
|
'plugin_admin' => function_exists($addon . '_addon_admin'),
|
|
|
|
]);
|
|
|
|
|
|
|
|
Addon::loadAddons();
|
|
|
|
Hook::loadHooks();
|
|
|
|
}
|
|
|
|
}
|