From d0c26956da160b2fbfd4855ca7fe31eeebe6528d Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 25 May 2020 12:46:14 +0200 Subject: [PATCH 1/4] User: Don't error out if we want to refresh a user but can't --- lib/pleroma/user.ex | 17 +++++++---- .../activity_pub_controller_test.exs | 30 +++++++++++++++++++ 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index d2eeeb479..842b28c06 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1618,12 +1618,19 @@ def html_filter_policy(_), do: Pleroma.Config.get([:markup, :scrub_policy]) def fetch_by_ap_id(ap_id), do: ActivityPub.make_user_from_ap_id(ap_id) def get_or_fetch_by_ap_id(ap_id) do - user = get_cached_by_ap_id(ap_id) + cached_user = get_cached_by_ap_id(ap_id) - if !is_nil(user) and !needs_update?(user) do - {:ok, user} - else - fetch_by_ap_id(ap_id) + maybe_fetched_user = needs_update?(cached_user) && fetch_by_ap_id(ap_id) + + case {cached_user, maybe_fetched_user} do + {_, {:ok, %User{} = user}} -> + {:ok, user} + + {%User{} = user, _} -> + {:ok, user} + + _ -> + {:error, :not_found} end end diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index dd2a48a61..24edab41a 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -451,6 +451,36 @@ test "it inserts an incoming activity into the database", %{conn: conn} do assert Activity.get_by_ap_id(data["id"]) end + @tag capture_log: true + test "it inserts an incoming activity into the database" <> + "even if we can't fetch the user but have it in our db", + %{conn: conn} do + user = + insert(:user, + ap_id: "https://mastodon.example.org/users/raymoo", + ap_enabled: true, + local: false, + last_refreshed_at: nil + ) + + data = + File.read!("test/fixtures/mastodon-post-activity.json") + |> Poison.decode!() + |> Map.put("actor", user.ap_id) + |> put_in(["object", "attridbutedTo"], user.ap_id) + + conn = + conn + |> assign(:valid_signature, true) + |> put_req_header("content-type", "application/activity+json") + |> post("/inbox", data) + + assert "ok" == json_response(conn, 200) + + ObanHelpers.perform(all_enqueued(worker: ReceiverWorker)) + assert Activity.get_by_ap_id(data["id"]) + end + test "it clears `unreachable` federation status of the sender", %{conn: conn} do data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() From 0c970a9d44fc0ceddbb52483f6f2fab11243e0ca Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 25 May 2020 12:49:38 +0200 Subject: [PATCH 2/4] UserTest: Add test for user refreshing. --- test/user_test.exs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test/user_test.exs b/test/user_test.exs index 45125f704..3556ef1b4 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -586,6 +586,26 @@ test "updates an existing user, if stale" do refute user.last_refreshed_at == orig_user.last_refreshed_at end + + @tag capture_log: true + test "it returns the old user if stale, but unfetchable" do + a_week_ago = NaiveDateTime.add(NaiveDateTime.utc_now(), -604_800) + + orig_user = + insert( + :user, + local: false, + nickname: "admin@mastodon.example.org", + ap_id: "http://mastodon.example.org/users/raymoo", + last_refreshed_at: a_week_ago + ) + + assert orig_user.last_refreshed_at == a_week_ago + + {:ok, user} = User.get_or_fetch_by_ap_id("http://mastodon.example.org/users/raymoo") + + assert user.last_refreshed_at == orig_user.last_refreshed_at + end end test "returns an ap_id for a user" do From 3bec0d2e50a0c468508ae884a3e5dc371106501e Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 25 May 2020 12:59:42 +0200 Subject: [PATCH 3/4] Factory: Set users to be ap_enabled by default. --- test/support/factory.ex | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/support/factory.ex b/test/support/factory.ex index d4284831c..6e3676aca 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -34,7 +34,8 @@ def user_factory do last_digest_emailed_at: NaiveDateTime.utc_now(), last_refreshed_at: NaiveDateTime.utc_now(), notification_settings: %Pleroma.User.NotificationSetting{}, - multi_factor_authentication_settings: %Pleroma.MFA.Settings{} + multi_factor_authentication_settings: %Pleroma.MFA.Settings{}, + ap_enabled: true } %{ From aeb0875025cf37ad8cefe43c637f56a5a16f8628 Mon Sep 17 00:00:00 2001 From: lain Date: Mon, 25 May 2020 13:48:47 +0200 Subject: [PATCH 4/4] StealEmojiPolicyTest: Fix flaky test. --- test/web/activity_pub/mrf/steal_emoji_policy_test.exs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/test/web/activity_pub/mrf/steal_emoji_policy_test.exs b/test/web/activity_pub/mrf/steal_emoji_policy_test.exs index 8882c8c13..0e7e57c77 100644 --- a/test/web/activity_pub/mrf/steal_emoji_policy_test.exs +++ b/test/web/activity_pub/mrf/steal_emoji_policy_test.exs @@ -14,8 +14,6 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicyTest do end setup do - clear_config(:mrf_steal_emoji) - emoji_path = Path.join(Config.get([:instance, :static_dir]), "emoji/stolen") File.rm_rf!(emoji_path) File.mkdir!(emoji_path) @@ -53,8 +51,8 @@ test "Steals emoji on unknown shortcode from allowed remote host" do } } - Config.put([:mrf_steal_emoji, :hosts], ["example.org"]) - Config.put([:mrf_steal_emoji, :size_limit], 284_468) + clear_config([:mrf_steal_emoji, :hosts], ["example.org"]) + clear_config([:mrf_steal_emoji, :size_limit], 284_468) assert {:ok, message} == StealEmojiPolicy.filter(message)