-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
140 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
/** | ||
* REST Controller Testcase file. | ||
* | ||
* @package Activitypub | ||
*/ | ||
|
||
namespace Activitypub\Tests; | ||
|
||
/** | ||
* REST Controller Testcase. | ||
*/ | ||
abstract class REST_Controller_Testcase extends \WP_Test_REST_TestCase { | ||
|
||
/** | ||
* The REST server. | ||
* | ||
* @var \WP_REST_Server | ||
*/ | ||
protected $server; | ||
|
||
/** | ||
* Set up the test. | ||
*/ | ||
public function set_up() { | ||
parent::set_up(); | ||
add_filter( 'rest_url', array( $this, 'filter_rest_url_for_leading_slash' ), 10, 2 ); | ||
|
||
/** @var \WP_REST_Server $wp_rest_server */ | ||
global $wp_rest_server; | ||
$wp_rest_server = new \Spy_REST_Server(); | ||
do_action( 'rest_api_init', $wp_rest_server ); | ||
} | ||
|
||
/** | ||
* Tear down the test. | ||
*/ | ||
public function tear_down() { | ||
remove_filter( 'rest_url', array( $this, 'test_rest_url_for_leading_slash' ) ); | ||
|
||
/** @var WP_REST_Server $wp_rest_server */ | ||
global $wp_rest_server; | ||
$wp_rest_server = null; | ||
|
||
parent::tear_down(); | ||
} | ||
|
||
/** | ||
* Test get_item. | ||
*/ | ||
abstract public function test_get_item(); | ||
|
||
/** | ||
* Test register_routes. | ||
*/ | ||
abstract public function test_get_item_schema(); | ||
|
||
/** | ||
* Filter REST URL for leading slash. | ||
* | ||
* @param string $url URL. | ||
* @param string $path Path. | ||
* @return string | ||
*/ | ||
public function filter_rest_url_for_leading_slash( $url, $path ) { | ||
if ( is_multisite() || get_option( 'permalink_structure' ) ) { | ||
return $url; | ||
} | ||
|
||
// Make sure path for rest_url has a leading slash for proper resolution. | ||
if ( 0 !== strpos( $path, '/' ) ) { | ||
$this->fail( | ||
sprintf( | ||
'REST API URL "%s" should have a leading slash.', | ||
$path | ||
) | ||
); | ||
} | ||
|
||
return $url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,19 +5,16 @@ | |
* @package Activitypub | ||
*/ | ||
|
||
namespace Activitypub\Tests\Rest; | ||
|
||
use Activitypub\Tests\REST_Controller_Testcase; | ||
|
||
/** | ||
* Tests for WebFinger REST API endpoint. | ||
* | ||
* @coversDefaultClass \Activitypub\Rest\Webfinger | ||
* @coversDefaultClass \Activitypub\Rest\Webfinger_Controller | ||
*/ | ||
class Test_Webfinger extends \WP_UnitTestCase { | ||
|
||
/** | ||
* The REST server. | ||
* | ||
* @var \WP_REST_Server | ||
*/ | ||
protected $server; | ||
class Test_Webfinger_Controller extends REST_Controller_Testcase { | ||
|
||
/** | ||
* Test user. | ||
|
@@ -29,9 +26,9 @@ class Test_Webfinger extends \WP_UnitTestCase { | |
/** | ||
* Set up class test fixtures. | ||
* | ||
* @param WP_UnitTest_Factory $factory WordPress unit test factory. | ||
* @param \WP_UnitTest_Factory $factory WordPress unit test factory. | ||
*/ | ||
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | ||
public static function wpSetUpBeforeClass( \WP_UnitTest_Factory $factory ) { | ||
self::$user = $factory->user->create_and_get( | ||
array( | ||
'user_login' => 'test_user', | ||
|
@@ -43,8 +40,6 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { | |
|
||
/** | ||
* Clean up test fixtures. | ||
* | ||
* @since 5.6.0 | ||
*/ | ||
public static function wpTearDownAfterClass() { | ||
self::delete_user( self::$user->ID ); | ||
|
@@ -56,13 +51,7 @@ public static function wpTearDownAfterClass() { | |
public function set_up() { | ||
parent::set_up(); | ||
|
||
global $wp_rest_server; | ||
|
||
$wp_rest_server = new \WP_REST_Server(); | ||
$this->server = $wp_rest_server; | ||
|
||
\do_action( 'rest_api_init' ); | ||
( new \Activitypub\Rest\Webfinger() )->register_routes(); | ||
|
||
\add_filter( 'webfinger_data', array( '\Activitypub\Integration\Webfinger', 'add_pseudo_user_discovery' ), 1, 2 ); | ||
} | ||
|
@@ -73,7 +62,7 @@ public function set_up() { | |
* @covers ::register_routes | ||
*/ | ||
public function test_register_routes() { | ||
$routes = $this->server->get_routes(); | ||
$routes = rest_get_server()->get_routes(); | ||
$this->assertArrayHasKey( '/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger', $routes ); | ||
} | ||
|
||
|
@@ -83,7 +72,11 @@ public function test_register_routes() { | |
* @covers ::get_item_schema | ||
*/ | ||
public function test_get_item_schema() { | ||
$schema = ( new \Activitypub\Rest\Webfinger() )->get_item_schema(); | ||
$request = new \WP_REST_Request( 'OPTIONS', '/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger' ); | ||
$response = rest_get_server()->dispatch( $request )->get_data(); | ||
|
||
$this->assertArrayHasKey( 'schema', $response ); | ||
$schema = $response['schema']; | ||
|
||
$this->assertIsArray( $schema ); | ||
$this->assertArrayHasKey( 'properties', $schema ); | ||
|
@@ -97,11 +90,10 @@ public function test_get_item_schema() { | |
* | ||
* @covers ::get_item | ||
*/ | ||
public function test_get_item_with_valid_resource() { | ||
public function test_get_item() { | ||
$request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger' ); | ||
$request->set_param( 'resource', 'acct:[email protected]' ); | ||
|
||
$response = $this->server->dispatch( $request ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
|
||
$this->assertEquals( 200, $response->get_status() ); | ||
$this->assertStringContainsString( 'application/jrd+json', $response->get_headers()['Content-Type'] ); | ||
|
@@ -116,8 +108,8 @@ public function test_get_item_with_valid_resource() { | |
public function test_get_item_with_invalid_resource() { | ||
$request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger' ); | ||
$request->set_param( 'resource', 'invalid-resource' ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
|
||
$response = $this->server->dispatch( $request ); | ||
$this->assertEquals( 400, $response->get_status() ); | ||
} | ||
|
||
|
@@ -128,7 +120,8 @@ public function test_get_item_with_invalid_resource() { | |
*/ | ||
public function test_get_item_with_missing_resource() { | ||
$request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger' ); | ||
$response = $this->server->dispatch( $request ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
|
||
$this->assertEquals( 400, $response->get_status() ); | ||
} | ||
|
||
|
@@ -163,7 +156,7 @@ function( $data, $resource ) use ( $test_data ) { | |
$request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger' ); | ||
$request->set_param( 'resource', 'acct:[email protected]' ); | ||
|
||
$response = $this->server->dispatch( $request ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$data = $response->get_data(); | ||
|
||
$this->assertEquals( $test_data, $data ); | ||
|
@@ -179,12 +172,30 @@ public function test_get_item_with_author_url() { | |
$request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger' ); | ||
$request->set_param( 'resource', $author_url ); | ||
|
||
$response = $this->server->dispatch( $request ); | ||
$response = rest_get_server()->dispatch( $request ); | ||
$data = $response->get_data(); | ||
|
||
$this->assertEquals( 200, $response->get_status() ); | ||
$this->assertStringContainsString( 'application/jrd+json', $response->get_headers()['Content-Type'] ); | ||
$this->assertContains( $author_url, $data['aliases'] ); | ||
$this->assertArrayHasKey( 'links', $data ); | ||
} | ||
|
||
/** | ||
* Test that the Webfinger response matches its schema. | ||
* | ||
* @covers ::get_item | ||
* @covers ::get_item_schema | ||
*/ | ||
public function test_response_matches_schema() { | ||
$request = new \WP_REST_Request( 'GET', '/' . ACTIVITYPUB_REST_NAMESPACE . '/webfinger' ); | ||
$request->set_param( 'resource', 'acct:[email protected]' ); | ||
|
||
$response = rest_get_server()->dispatch( $request ); | ||
$data = $response->get_data(); | ||
$schema = ( new \Activitypub\Rest\Webfinger_Controller() )->get_item_schema(); | ||
|
||
$valid = \rest_validate_value_from_schema( $data, $schema ); | ||
$this->assertNotWPError( $valid, 'Response failed schema validation: ' . ( \is_wp_error( $valid ) ? $valid->get_error_message() : '' ) ); | ||
} | ||
} |