From 901068f77086f553b5d8d3ec5d83461121637825 Mon Sep 17 00:00:00 2001 From: Philipp Date: Tue, 30 Nov 2021 20:53:47 +0100 Subject: [PATCH] Fix test --- tests/Util/AuthTestConfig.php | 11 ++ tests/Util/authtest/authtest.php | 4 +- tests/legacy/ApiTest.php | 91 ---------------- tests/src/Module/Api/ApiTest.php | 12 +++ .../Module/Api/Twitter/Media/UploadTest.php | 101 ++++++++++++++++++ 5 files changed, 126 insertions(+), 93 deletions(-) create mode 100644 tests/Util/AuthTestConfig.php create mode 100644 tests/src/Module/Api/Twitter/Media/UploadTest.php diff --git a/tests/Util/AuthTestConfig.php b/tests/Util/AuthTestConfig.php new file mode 100644 index 000000000..ad2592d07 --- /dev/null +++ b/tests/Util/AuthTestConfig.php @@ -0,0 +1,11 @@ +markTestIncomplete(); } - /** - * Test the \Friendica\Module\Api\Twitter\Media\Upload module. - * @runInSeparateProcess - * @preserveGlobalState disabled - */ - public function testApiMediaUpload() - { - $this->expectException(\Friendica\Network\HTTPException\BadRequestException::class); - $_SERVER['REQUEST_METHOD'] = Router::POST; - (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run(); - } - /** - * Test the \Friendica\Module\Api\Twitter\Media\Upload module without an authenticated user. - * - * @return void - */ - public function testApiMediaUploadWithoutAuthenticatedUser() - { - $this->expectException(\Friendica\Network\HTTPException\UnauthorizedException::class); - BasicAuth::setCurrentUserID(); - $_SESSION['authenticated'] = false; - $_SERVER['REQUEST_METHOD'] = Router::POST; - (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run(); - } - - /** - * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an invalid uploaded media. - * - * @return void - */ - public function testApiMediaUploadWithMedia() - { - $this->expectException(\Friendica\Network\HTTPException\InternalServerErrorException::class); - $_FILES = [ - 'media' => [ - 'id' => 666, - 'tmp_name' => 'tmp_name' - ] - ]; - $_SERVER['REQUEST_METHOD'] = Router::POST; - (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run(); - } - - /** - * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an valid uploaded media. - * - * @return void - */ - public function testApiMediaUploadWithValidMedia() - { - $_FILES = [ - 'media' => [ - 'id' => 666, - 'size' => 666, - 'width' => 666, - 'height' => 666, - 'tmp_name' => $this->getTempImage(), - 'name' => 'spacer.png', - 'type' => 'image/png' - ] - ]; - - $_SERVER['REQUEST_METHOD'] = Router::POST; - - $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), $_SERVER))->run(); - $media = json_decode($response->getBody(), true); - - self::assertEquals('image/png', $media['image']['image_type']); - self::assertEquals(1, $media['image']['w']); - self::assertEquals(1, $media['image']['h']); - self::assertNotEmpty($media['image']['friendica_preview_url']); - } /** * Test the api_statuses_repeat() function. diff --git a/tests/src/Module/Api/ApiTest.php b/tests/src/Module/Api/ApiTest.php index 98e9b2a03..154f54a1c 100644 --- a/tests/src/Module/Api/ApiTest.php +++ b/tests/src/Module/Api/ApiTest.php @@ -27,9 +27,11 @@ use Friendica\Core\Hook; use Friendica\Database\Database; use Friendica\DI; use Friendica\Security\Authentication; +use Friendica\Security\BasicAuth; use Friendica\Test\FixtureTest; use Friendica\Test\Util\AppDouble; use Friendica\Test\Util\AuthenticationDouble; +use Friendica\Test\Util\AuthTestConfig; abstract class ApiTest extends FixtureTest { @@ -60,9 +62,19 @@ abstract class ApiTest extends FixtureTest // Manual override to bypass API authentication DI::app()->setIsLoggedIn(true); + AuthTestConfig::$authenticated = true; + AuthTestConfig::$user_id = 42; + $this->installAuthTest(); } + protected function tearDown(): void + { + BasicAuth::setCurrentUserID(); + + parent::tearDown(); // TODO: Change the autogenerated stub + } + /** * installs auththest. * diff --git a/tests/src/Module/Api/Twitter/Media/UploadTest.php b/tests/src/Module/Api/Twitter/Media/UploadTest.php new file mode 100644 index 000000000..abc9d8fdc --- /dev/null +++ b/tests/src/Module/Api/Twitter/Media/UploadTest.php @@ -0,0 +1,101 @@ +expectException(BadRequestException::class); + $upload = new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]); + $upload->run(); + } + + /** + * Test the \Friendica\Module\Api\Twitter\Media\Upload module without an authenticated user. + * + * @return void + */ + public function testApiMediaUploadWithoutAuthenticatedUser() + { + $this->expectException(UnauthorizedException::class); + AuthTestConfig::$authenticated = false; + (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(); + } + + /** + * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an invalid uploaded media. + * + * @return void + */ + public function testApiMediaUploadWithMedia() + { + $this->expectException(InternalServerErrorException::class); + $_FILES = [ + 'media' => [ + 'id' => 666, + 'tmp_name' => 'tmp_name' + ] + ]; + (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(); + } + + /** + * Test the \Friendica\Module\Api\Twitter\Media\Upload module with an valid uploaded media. + * + * @return void + */ + public function testApiMediaUploadWithValidMedia() + { + $_FILES = [ + 'media' => [ + 'id' => 666, + 'size' => 666, + 'width' => 666, + 'height' => 666, + 'tmp_name' => $this->getTempImage(), + 'name' => 'spacer.png', + 'type' => 'image/png' + ] + ]; + + $response = (new Upload(DI::app(), DI::l10n(), DI::baseUrl(), DI::args(), DI::logger(), DI::profiler(), DI::apiResponse(), ['REQUEST_METHOD' => Router::POST]))->run(); + $media = json_decode($response->getBody(), true); + + self::assertEquals('image/png', $media['image']['image_type']); + self::assertEquals(1, $media['image']['w']); + self::assertEquals(1, $media['image']['h']); + self::assertNotEmpty($media['image']['friendica_preview_url']); + } + + /** + * 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; + } +}