From bc1af78bf7d99e572cbbfeb7adfd511fd0e97d75 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Thu, 25 Jun 2020 15:35:20 +0100 Subject: [PATCH] [TESTS] Move AcceptHeader from ActivityPub plugin to Core Delete temporary ActivityPub tests (they were to be deleted long ago) --- .../ActivityPub/tests/CreatesApplication.php | 52 ----- plugins/ActivityPub/tests/TestCase.php | 39 ---- .../tests/Unit/ActivitypubProfileTest.php | 183 ------------------ .../ActivityPub/tests/Unit/ExampleTest.php | 42 ---- .../tests/Unit/HTTPSignatureTest.php | 165 ---------------- .../Unit => tests/Core}/AcceptHeaderTest.php | 30 +-- 6 files changed, 19 insertions(+), 492 deletions(-) delete mode 100644 plugins/ActivityPub/tests/CreatesApplication.php delete mode 100644 plugins/ActivityPub/tests/TestCase.php delete mode 100644 plugins/ActivityPub/tests/Unit/ActivitypubProfileTest.php delete mode 100644 plugins/ActivityPub/tests/Unit/ExampleTest.php delete mode 100644 plugins/ActivityPub/tests/Unit/HTTPSignatureTest.php rename {plugins/ActivityPub/tests/Unit => tests/Core}/AcceptHeaderTest.php (82%) diff --git a/plugins/ActivityPub/tests/CreatesApplication.php b/plugins/ActivityPub/tests/CreatesApplication.php deleted file mode 100644 index 841c204468..0000000000 --- a/plugins/ActivityPub/tests/CreatesApplication.php +++ /dev/null @@ -1,52 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * @link http://www.gnu.org/software/social/ - */ - -namespace Tests; - -trait CreatesApplication -{ - /** - * Creates the application. - * - * @return todo - */ - public static function createApplication() - { - if (!defined('INSTALLDIR')) { - define('INSTALLDIR', __DIR__ . '/../../../'); - } - if (!defined('GNUSOCIAL')) { - define('GNUSOCIAL', true); - } - if (!defined('STATUSNET')) { - define('STATUSNET', true); // compatibility - } - - require INSTALLDIR . '/lib/common.php'; - - return true; - } -} diff --git a/plugins/ActivityPub/tests/TestCase.php b/plugins/ActivityPub/tests/TestCase.php deleted file mode 100644 index 24a4d296fd..0000000000 --- a/plugins/ActivityPub/tests/TestCase.php +++ /dev/null @@ -1,39 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * @link http://www.gnu.org/software/social/ - */ - -namespace Tests; - -use PHPUnit\Framework\TestCase as BaseTestCase; - -abstract class TestCase extends BaseTestCase -{ - use CreatesApplication; - - protected function setUp() - { - $this->createApplication(); - } -} diff --git a/plugins/ActivityPub/tests/Unit/ActivitypubProfileTest.php b/plugins/ActivityPub/tests/Unit/ActivitypubProfileTest.php deleted file mode 100644 index babd65209e..0000000000 --- a/plugins/ActivityPub/tests/Unit/ActivitypubProfileTest.php +++ /dev/null @@ -1,183 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * @link http://www.gnu.org/software/social/ - */ - -namespace Tests\Unit; - -use Tests\TestCase; - -class ProfileObjectTest extends TestCase -{ - public function testLibraryInstalled() - { - $this->assertTrue(class_exists('\Activitypub_profile')); - } - - public function testActivitypubProfile() - { - // Mimic proper ACCEPT header - $_SERVER['HTTP_ACCEPT'] = 'application/ld+json; profile="https://www.w3.org/ns/activitystreams'; - - /* Test do_insert() */ - $aprofile = new \Activitypub_profile(); - $aprofile->uri = 'https://testinstance.net/index.php/user/1'; - $aprofile->nickname = 'test1'; - $aprofile->fullname = 'Test User 1'; - $aprofile->bio = 'I am a nice test 1 guy'; - $aprofile->inboxuri = "https://testinstance.net/index.php/user/1/inbox.json"; - $aprofile->sharedInboxuri = "https://testinstance.net/inbox.json"; - $aprofile->do_insert(); - - /* Test local_profile() */ - $profile = $aprofile->local_profile(); - - /* Test from_profile() and create_from_local_profile() */ - $this->assertTrue($this->compare_aprofiles($aprofile, \Activitypub_profile::from_profile($profile))); - - /* Create Keys for Test User 1 */ - $apRSA = new \Activitypub_rsa(); - $apRSA->profile_id = $profile->getID(); - \Activitypub_rsa::generate_keys($apRSA->private_key, $apRSA->public_key); - $apRSA->store_keys(); - - /* Test profile_to_array() */ - // Fetch ActivityPub Actor Object representation - $profile_array = \Activitypub_profile::profile_to_array($profile); - // Check type - $this->assertTrue(is_array($profile_array)); - // Test with Explorer's Profile Tester - $this->assertTrue(\Activitypub_explorer::validate_remote_response($profile_array)); - - /* Test get_inbox() */ - $this->assertTrue($aprofile->sharedInboxuri == $aprofile->get_inbox()); - - /* Test getUri() */ - $this->assertTrue($aprofile->uri == $aprofile->getUri()); - - /* Test getUrl() */ - $this->assertTrue($profile->getUrl() == $aprofile->getUrl()); - - /* Test getID() */ - $this->assertTrue($profile->getID() == $aprofile->getID()); - - /* Test fromUri() */ - $this->assertTrue($this->compare_aprofiles($aprofile, \Activitypub_profile::fromUri($aprofile->uri))); - - /* Remove Remote User Test 1 */ - $old_id = $profile->getID(); - $apRSA->delete(); - $aprofile->delete(); - $profile->delete(); - // Check if successfuly removed - try { - \Profile::getById($old_id); - $this->assertTrue(false); - } catch (\NoResultException $e) { - $this->assertTrue(true); - } - - /* Test ensure_web_finger() */ - // TODO: Maybe elaborate on this function's tests - try { - \Activitypub_profile::ensure_webfinger('test1@testinstance.net'); - $this->assertTrue(false); - } catch (\Exception $e) { - $this->assertTrue($e->getMessage() == 'Not a valid webfinger address.' || - $e->getMessage() == 'Not a valid webfinger address (via cache).'); - } - } - - // Helpers - - private function compare_profiles(\Profile $a, \Profile $b) - { - if (($av = $a->getID()) != ($bv = $b->getID())) { - throw new Exception('Compare Profiles 1 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->getNickname()) != ($bv = $b->getNickname())) { - throw new Exception('Compare Profiles 2 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->getFullname()) != ($bv = $b->getFullname())) { - throw new Exception('Compare Profiles 3 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->getUrl()) != ($bv = $b->getUrl())) { - throw new Exception('Compare Profiles 4 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->getDescription()) != ($bv = $b->getDescription())) { - throw new Exception('Compare Profiles 5 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->getLocation()) != ($bv = $b->getLocation())) { - throw new Exception('Compare Profiles 6 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->getNickname()) != ($bv = $b->getNickname())) { - throw new Exception('Compare Profiles 7 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->lat) != ($bv = $b->lat)) { - throw new Exception('Compare Profiles 8 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->lon) != ($bv = $b->lon)) { - throw new Exception('Compare Profiles 9 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - return true; - } - - private function compare_aprofiles(\Activitypub_profile $a, \Activitypub_profile $b) - { - if (($av = $a->getUri()) != ($bv = $b->getUri())) { - throw new Exception('Compare AProfiles 1 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->getUrl()) != ($bv = $b->getUrl())) { - throw new Exception('Compare AProfiles 2 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->getID()) != ($bv = $b->getID())) { - throw new Exception('Compare AProfiles 3 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->profile_id) != ($bv = $b->profile_id)) { - throw new Exception('Compare AProfiles 4 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->inboxuri) != ($bv = $b->inboxuri)) { - throw new Exception('Compare AProfiles 5 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - if (($av = $a->sharedInboxuri) != ($bv = $b->sharedInboxuri)) { - throw new Exception('Compare AProfiles 6 Fail: $a: '.$av.' is different from $b: '.$bv); - } - - return true; - } -} diff --git a/plugins/ActivityPub/tests/Unit/ExampleTest.php b/plugins/ActivityPub/tests/Unit/ExampleTest.php deleted file mode 100644 index 630173a8c6..0000000000 --- a/plugins/ActivityPub/tests/Unit/ExampleTest.php +++ /dev/null @@ -1,42 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * @link http://www.gnu.org/software/social/ - */ - -namespace Tests\Unit; - -use Tests\TestCase; - -class ExampleTest extends TestCase -{ - /** - * A basic test example. - * - * @return void - */ - public function testBasicTest() - { - $this->assertTrue(true); - } -} diff --git a/plugins/ActivityPub/tests/Unit/HTTPSignatureTest.php b/plugins/ActivityPub/tests/Unit/HTTPSignatureTest.php deleted file mode 100644 index b7fb52a64a..0000000000 --- a/plugins/ActivityPub/tests/Unit/HTTPSignatureTest.php +++ /dev/null @@ -1,165 +0,0 @@ -. - -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * @link http://www.gnu.org/software/social/ - */ - -namespace Tests\Unit; - -use Tests\TestCase; -use GuzzleHttp\Client; -use GuzzleHttp\Handler\CurlHandler; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Middleware; -use GuzzleHttp\Psr7\Request; -use GuzzleHttp\Psr7\Response; -use HttpSignatures\Context; -use HttpSignatures\GuzzleHttpSignatures; - -class HTTPSignatureTest extends TestCase -{ - /** - * @var Context - */ - private $context; - /** - * @var Client - */ - private $client; - /** - * @var - */ - private $history = []; - - public function testLibraryInstalled() - { - $this->assertTrue(class_exists('\GuzzleHttp\Client')); - $this->assertTrue(class_exists('\HttpSignatures\Context')); - $this->assertTrue(class_exists('\HttpSignatures\GuzzleHttpSignatures')); - } - - public function setUp() - { - $this->context = new Context([ - 'keys' => ['pda' => 'secret'], - 'algorithm' => 'hmac-sha256', - 'headers' => ['(request-target)', 'date'], - ]); - $stack = new HandlerStack(); - $stack->setHandler(new MockHandler([ - new Response(200, ['Content-Length' => 0]), - ])); - $stack->push(GuzzleHttpSignatures::middlewareFromContext($this->context)); - $stack->push(Middleware::history($this->history)); - $this->client = new Client(['handler' => $stack]); - } - /** - * test signing a message - */ - public function testGuzzleRequestHasExpectedHeaders() - { - $this->client->get('/path?query=123', [ - 'headers' => ['date' => 'today', 'accept' => 'llamas'] - ]); - // get last request - $message = end($this->history); - /** @var Request $request */ - $request = $message['request']; - /** @var Response $response */ - $response = $message['request']; - $expectedString = implode( - ',', - [ - 'keyId="pda"', - 'algorithm="hmac-sha256"', - 'headers="(request-target) date"', - 'signature="SFlytCGpsqb/9qYaKCQklGDvwgmrwfIERFnwt+yqPJw="', - ] - ); - $this->assertEquals( - [$expectedString], - $request->getHeader('Signature') - ); - $this->assertEquals( - ['Signature ' . $expectedString], - $request->getHeader('Authorization') - ); - } - /** - * test signing a message with a URL that doesn't contain a ?query - */ - public function testGuzzleRequestHasExpectedHeaders2() - { - $this->client->get('/path', [ - 'headers' => ['date' => 'today', 'accept' => 'llamas'] - ]); - // get last request - $message = end($this->history); - /** @var Request $request */ - $request = $message['request']; - /** @var Response $response */ - $response = $message['request']; - $expectedString = implode( - ',', - [ - 'keyId="pda"', - 'algorithm="hmac-sha256"', - 'headers="(request-target) date"', - 'signature="DAtF133khP05pS5Gh8f+zF/UF7mVUojMj7iJZO3Xk4o="', - ] - ); - $this->assertEquals( - [$expectedString], - $request->getHeader('Signature') - ); - $this->assertEquals( - ['Signature ' . $expectedString], - $request->getHeader('Authorization') - ); - } - public function getVerifyGuzzleRequestVectors() - { - return [ - /* path, headers */ - ['/path?query=123', ['date' => 'today', 'accept' => 'llamas']], - ['/path?z=zebra&a=antelope', ['date' => 'today']], - ]; - } - /** - * @dataProvider getVerifyGuzzleRequestVectors - * @param string $path - * @param array $headers - */ - public function testVerifyGuzzleRequest($path, $headers) - { - $this->client->get($path, ['headers' => $headers]); - // get last request - $message = end($this->history); - /** @var Request $request */ - $request = $message['request']; - /** @var Response $response */ - $response = $message['request']; - $this->assertTrue($this->context->verifier()->isValid($request)); - } -} diff --git a/plugins/ActivityPub/tests/Unit/AcceptHeaderTest.php b/tests/Core/AcceptHeaderTest.php similarity index 82% rename from plugins/ActivityPub/tests/Unit/AcceptHeaderTest.php rename to tests/Core/AcceptHeaderTest.php index 8d5c649d3c..1b34e7a2f2 100644 --- a/plugins/ActivityPub/tests/Unit/AcceptHeaderTest.php +++ b/tests/Core/AcceptHeaderTest.php @@ -14,19 +14,27 @@ // You should have received a copy of the GNU Affero General Public License // along with GNU social. If not, see . -/** - * ActivityPub implementation for GNU social - * - * @package GNUsocial - * @author Diogo Cordeiro - * @copyright 2018-2019 Free Software Foundation, Inc http://www.fsf.org - * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later - * @link http://www.gnu.org/software/social/ - */ +namespace Tests\Unit; -require 'AcceptHeader.php'; +use AcceptHeader; +use PHPUnit\Framework\TestCase; -class ContainerTest extends \PHPUnit_Framework_TestCase +if (!defined('INSTALLDIR')) { + define('INSTALLDIR', dirname(dirname(__DIR__))); +} +if (!defined('PUBLICDIR')) { + define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public'); +} +if (!defined('GNUSOCIAL')) { + define('GNUSOCIAL', true); +} +if (!defined('STATUSNET')) { // Compatibility + define('STATUSNET', true); +} + +require_once INSTALLDIR . '/lib/util/common.php'; + +final class AcceptHeaderTest extends TestCase { public function testHeader1() {