Add test for announce

This commit is contained in:
Alex Kirk 2022-12-02 13:43:09 +01:00
parent a82dea0685
commit b3a26788eb
4 changed files with 169 additions and 51 deletions

View file

@ -156,9 +156,8 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
),
true
) ) {
return false;
return false;
}
$actor_url = $object['actor'];
$user_feed = false;
if ( \wp_http_validate_url( $actor_url ) ) {
@ -216,8 +215,13 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
* @param \Friends\User_Feed $user_feed The user feed.
*/
private function handle_incoming_post( $object, \Friends\User_Feed $user_feed ) {
$permalink = $object['id'];
if ( isset( $object['url'] ) ) {
$permalink = $object['url'];
}
$data = array(
'permalink' => $object['url'],
'permalink' => $permalink,
'content' => $object['content'],
'post_format' => $this->map_type_to_post_format( $object['type'] ),
'date' => $object['published'],
@ -236,7 +240,7 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
$this->log(
'Received feed item',
array(
'url' => $object['url'],
'url' => $permalink,
'data' => $data,
)
);
@ -254,10 +258,12 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser {
* @param \Friends\User_Feed $user_feed The user feed.
*/
private function handle_incoming_announce( $url, \Friends\User_Feed $user_feed, $user_id ) {
$this->log( 'Received announce for ' . $url );
if ( ! \wp_http_validate_url( $url ) ) {
$this->log( 'Received invalid announce', compact( 'url' ) );
return false;
}
$this->log( 'Received announce for ' . $url );
$response = \Activitypub\safe_remote_get( $url, $user_id );
if ( \is_wp_error( $response ) ) {
return $response;

View file

@ -19,6 +19,8 @@ require_once $_tests_dir . '/includes/functions.php';
*/
function _manually_load_plugin() {
require \dirname( \dirname( __FILE__ ) ) . '/activitypub.php';
// Load the Friends plugin if available to test the integrations.
$friends_plugin = \dirname( \dirname( \dirname( __FILE__ ) ) ) . '/friends/friends.php';
if ( file_exists( $friends_plugin ) ) {
require $friends_plugin;

File diff suppressed because one or more lines are too long

View file

@ -2,6 +2,9 @@
class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
public static $users = array();
private $friend_id;
private $friend_name;
private $actor;
public function set_up() {
if ( ! class_exists( '\Friends\Friends' ) ) {
@ -23,14 +26,52 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
);
add_filter( 'pre_http_request', array( get_called_class(), 'pre_http_request' ), 10, 3 );
add_filter( 'http_response', array( get_called_class(), 'http_response' ), 10, 3 );
add_filter( 'http_request_host_is_external', array( get_called_class(), 'http_request_host_is_external' ), 10, 2 );
add_filter( 'http_request_args', array( get_called_class(), 'http_request_args' ), 10, 2 );
add_filter( 'pre_get_remote_metadata_by_actor', array( get_called_class(), 'pre_get_remote_metadata_by_actor' ), 10, 2 );
$user_id = $this->factory->user->create(
array(
'role' => 'administrator',
)
);
wp_set_current_user( $user_id );
$this->friend_name = 'Alex Kirk';
$this->actor = 'https://mastodon.local/users/akirk';
$user_feed = \Friends\User_Feed::get_by_url( $this->actor );
if ( is_wp_error( $user_feed ) ) {
$this->friend_id = $this->factory->user->create(
array(
'display_name' => $this->friend_name,
'role' => 'friend',
)
);
\Friends\User_Feed::save(
new \Friends\User( $this->friend_id ),
$this->actor,
array(
'parser' => 'activitypub',
)
);
} else {
$this->friend_id = $user_feed->get_friend_user()->ID;
}
self::$users[ $this->actor ] = array(
'url' => $this->actor,
'name' => $this->friend_name,
);
self::$users['https://mastodon.local/@akirk'] = self::$users[ $this->actor ];
_delete_all_posts();
}
public function tear_down() {
remove_filter( 'pre_http_request', array( get_called_class(), 'pre_http_request' ) );
remove_filter( 'http_response', array( get_called_class(), 'http_response' ) );
remove_filter( 'http_request_host_is_external', array( get_called_class(), 'http_request_host_is_external' ) );
remove_filter( 'http_request_args', array( get_called_class(), 'http_request_args' ) );
remove_filter( 'pre_get_remote_metadata_by_actor', array( get_called_class(), 'pre_get_remote_metadata_by_actor' ) );
@ -55,6 +96,18 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
return $args;
}
public static function pre_http_request( $preempt, $request, $url ) {
$p = wp_parse_url( $url );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.response';
if ( file_exists( $cache ) ) {
return apply_filters(
'fake_http_response',
unserialize( file_get_contents( $cache ) ),
$p['scheme'] . '://' . $p['host'],
$url,
$request
);
}
$home_url = home_url();
// Pretend the url now is the requested one.
@ -96,57 +149,58 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
);
}
public static function http_response( $response, $args, $url ) {
$p = wp_parse_url( $url );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.response';
if ( ! file_exists( $cache ) ) {
$headers = wp_remote_retrieve_headers( $response );
file_put_contents(
$cache,
serialize(
array(
'headers' => $headers->getAll(),
'body' => wp_remote_retrieve_body( $response ),
'response' => array(
'code' => wp_remote_retrieve_response_code( $response ),
),
)
)
);
}
return $response;
}
public function test_incoming_post() {
$now = time() - 10;
$status_id = 123;
$friend_name = 'Alex';
$actor = 'https://mastodon.local/users/alex';
$friend_id = $this->factory->user->create(
array(
'user_login' => 'alex-mastodon.local',
'display_name' => $friend_name,
'role' => 'friend',
)
);
\Friends\User_Feed::save(
new \Friends\User( $friend_id ),
$actor,
array(
'parser' => 'activitypub',
)
);
self::$users[ $actor ] = array(
'url' => $actor,
'name' => $friend_name,
);
self::$users['https://mastodon.local/@alex'] = self::$users[ $actor ];
$posts = get_posts(
array(
'post_type' => \Friends\Friends::CPT,
'author' => $friend_id,
'author' => $this->friend_id,
)
);
$this->assertEquals( 0, count( $posts ) );
$post_count = count( $posts );
// Let's post a new Note through the REST API.
$date = gmdate( \DATE_W3C, $now++ );
$id = 'test' . $status_id;
$content = 'Test ' . $date . ' ' . rand();
$request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' );
$request->set_param( 'type', 'Create' );
$request->set_param( 'id', 'test1' );
$request->set_param( 'actor', $actor );
$date = date( \DATE_W3C, $now++ );
$content = 'Test ' . $date . ' ' . rand();
$request->set_param( 'id', $id );
$request->set_param( 'actor', $this->actor );
$request->set_param(
'object',
array(
'type' => 'Note',
'id' => 'test1',
'attributedTo' => $actor,
'id' => $id,
'attributedTo' => $this->actor,
'content' => $content,
'url' => 'https://mastodon.local/users/alex/statuses/' . ( $status_id++ ),
'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ),
'published' => $date,
)
);
@ -157,28 +211,31 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
$posts = get_posts(
array(
'post_type' => \Friends\Friends::CPT,
'author' => $friend_id,
'author' => $this->friend_id,
)
);
$this->assertEquals( 1, count( $posts ) );
$this->assertEquals( $post_count + 1, count( $posts ) );
$this->assertEquals( $content, $posts[0]->post_content );
$this->assertEquals( $friend_id, $posts[0]->post_author );
$this->assertEquals( $this->friend_id, $posts[0]->post_author );
// Do another test post, this time with a URL that has an @-id.
$date = gmdate( \DATE_W3C, $now++ );
$id = 'test' . $status_id;
$content = 'Test ' . $date . ' ' . rand();
$request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' );
$request->set_param( 'type', 'Create' );
$request->set_param( 'id', 'test1' );
$request->set_param( 'actor', 'https://mastodon.local/@alex' );
$date = date( \DATE_W3C, $now++ );
$content = 'Test ' . $date . ' ' . rand();
$request->set_param( 'id', $id );
$request->set_param( 'actor', 'https://mastodon.local/@akirk' );
$request->set_param(
'object',
array(
'type' => 'Note',
'id' => 'test2',
'attributedTo' => 'https://mastodon.local/@alex',
'id' => $id,
'attributedTo' => 'https://mastodon.local/@akirk',
'content' => $content,
'url' => 'https://mastodon.local/users/alex/statuses/' . ( $status_id++ ),
'url' => 'https://mastodon.local/users/akirk/statuses/' . ( $status_id++ ),
'published' => $date,
)
);
@ -189,12 +246,64 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
$posts = get_posts(
array(
'post_type' => \Friends\Friends::CPT,
'author' => $friend_id,
'author' => $this->friend_id,
)
);
$this->assertEquals( 2, count( $posts ) );
$this->assertEquals( $post_count + 2, count( $posts ) );
$this->assertEquals( $content, $posts[0]->post_content );
$this->assertEquals( $friend_id, $posts[0]->post_author );
$this->assertEquals( $this->friend_id, $posts[0]->post_author );
}
public function test_incoming_announce() {
$now = time() - 10;
$status_id = 123;
self::$users['https://notiz.blog/author/matthias-pfefferle/'] = array(
'url' => 'https://notiz.blog/author/matthias-pfefferle/',
'name' => 'Matthias Pfefferle',
);
$posts = get_posts(
array(
'post_type' => \Friends\Friends::CPT,
'author' => $this->friend_id,
)
);
$post_count = count( $posts );
$date = gmdate( \DATE_W3C, $now++ );
$id = 'test' . $status_id;
$object = 'https://notiz.blog/2022/11/14/the-at-protocol/';
$request = new \WP_REST_Request( 'POST', '/activitypub/1.0/users/' . get_current_user_id() . '/inbox' );
$request->set_param( 'type', 'Announce' );
$request->set_param( 'id', $id );
$request->set_param( 'actor', $this->actor );
$request->set_param( 'published', $date );
$request->set_param( 'object', $object );
$response = $this->server->dispatch( $request );
$this->assertEquals( 202, $response->get_status() );
$p = wp_parse_url( $object );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.response';
$this->assertFileExists( $cache );
$object = json_decode( wp_remote_retrieve_body( unserialize( file_get_contents( $cache ) ) ) );
$posts = get_posts(
array(
'post_type' => \Friends\Friends::CPT,
'author' => $this->friend_id,
)
);
$this->assertEquals( $post_count + 1, count( $posts ) );
$this->assertStringContainsString( 'Dezentrale Netzwerke', $posts[0]->post_content );
$this->assertEquals( $this->friend_id, $posts[0]->post_author );
}
}