diff options
Diffstat (limited to 'test/web')
47 files changed, 1212 insertions, 323 deletions
diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs index e722f7c04..ed900d8f8 100644 --- a/test/web/activity_pub/activity_pub_controller_test.exs +++ b/test/web/activity_pub/activity_pub_controller_test.exs @@ -1082,6 +1082,45 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do        assert object = Object.get_by_ap_id(note_object.data["id"])        assert object.data["like_count"] == 1      end + +    test "it doesn't spreads faulty attributedTo or actor fields", %{ +      conn: conn, +      activity: activity +    } do +      reimu = insert(:user, nickname: "reimu") +      cirno = insert(:user, nickname: "cirno") + +      assert reimu.ap_id +      assert cirno.ap_id + +      activity = +        activity +        |> put_in(["object", "actor"], reimu.ap_id) +        |> put_in(["object", "attributedTo"], reimu.ap_id) +        |> put_in(["actor"], reimu.ap_id) +        |> put_in(["attributedTo"], reimu.ap_id) + +      _reimu_outbox = +        conn +        |> assign(:user, cirno) +        |> put_req_header("content-type", "application/activity+json") +        |> post("/users/#{reimu.nickname}/outbox", activity) +        |> json_response(403) + +      cirno_outbox = +        conn +        |> assign(:user, cirno) +        |> put_req_header("content-type", "application/activity+json") +        |> post("/users/#{cirno.nickname}/outbox", activity) +        |> json_response(201) + +      assert cirno_outbox["attributedTo"] == nil +      assert cirno_outbox["actor"] == cirno.ap_id + +      assert cirno_object = Object.normalize(cirno_outbox["object"]) +      assert cirno_object.data["actor"] == cirno.ap_id +      assert cirno_object.data["attributedTo"] == cirno.ap_id +    end    end    describe "/relay/followers" do diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 1520ffc4b..d6eab7337 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -1179,7 +1179,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do          "id" => activity_ap_id,          "content" => content,          "published" => activity_with_object.object.data["published"], -        "actor" => AccountView.render("show.json", %{user: target_account}) +        "actor" => +          AccountView.render("show.json", %{user: target_account, skip_visibility_check: true})        }        assert %Activity{ @@ -2056,4 +2057,46 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do        assert [%{activity_id: ^id_create}] = Pleroma.ActivityExpiration |> Repo.all()      end    end + +  describe "handling of clashing nicknames" do +    test "renames an existing user with a clashing nickname and a different ap id" do +      orig_user = +        insert( +          :user, +          local: false, +          nickname: "admin@mastodon.example.org", +          ap_id: "http://mastodon.example.org/users/harinezumigari" +        ) + +      %{ +        nickname: orig_user.nickname, +        ap_id: orig_user.ap_id <> "part_2" +      } +      |> ActivityPub.maybe_handle_clashing_nickname() + +      user = User.get_by_id(orig_user.id) + +      assert user.nickname == "#{orig_user.id}.admin@mastodon.example.org" +    end + +    test "does nothing with a clashing nickname and the same ap id" do +      orig_user = +        insert( +          :user, +          local: false, +          nickname: "admin@mastodon.example.org", +          ap_id: "http://mastodon.example.org/users/harinezumigari" +        ) + +      %{ +        nickname: orig_user.nickname, +        ap_id: orig_user.ap_id +      } +      |> ActivityPub.maybe_handle_clashing_nickname() + +      user = User.get_by_id(orig_user.id) + +      assert user.nickname == orig_user.nickname +    end +  end  end diff --git a/test/web/activity_pub/mrf/activity_expiration_policy_test.exs b/test/web/activity_pub/mrf/activity_expiration_policy_test.exs index 8babf49e7..f25cf8b12 100644 --- a/test/web/activity_pub/mrf/activity_expiration_policy_test.exs +++ b/test/web/activity_pub/mrf/activity_expiration_policy_test.exs @@ -7,11 +7,13 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do    alias Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy    @id Pleroma.Web.Endpoint.url() <> "/activities/cofe" +  @local_actor Pleroma.Web.Endpoint.url() <> "/users/cofe"    test "adds `expires_at` property" do      assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =               ActivityExpirationPolicy.filter(%{                 "id" => @id, +               "actor" => @local_actor,                 "type" => "Create",                 "object" => %{"type" => "Note"}               }) @@ -25,6 +27,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do      assert {:ok, %{"type" => "Create", "expires_at" => ^expires_at}} =               ActivityExpirationPolicy.filter(%{                 "id" => @id, +               "actor" => @local_actor,                 "type" => "Create",                 "expires_at" => expires_at,                 "object" => %{"type" => "Note"} @@ -37,6 +40,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do      assert {:ok, %{"type" => "Create", "expires_at" => expires_at}} =               ActivityExpirationPolicy.filter(%{                 "id" => @id, +               "actor" => @local_actor,                 "type" => "Create",                 "expires_at" => too_distant_future,                 "object" => %{"type" => "Note"} @@ -49,6 +53,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do      assert {:ok, activity} =               ActivityExpirationPolicy.filter(%{                 "id" => "https://example.com/123", +               "actor" => "https://example.com/users/cofe",                 "type" => "Create",                 "object" => %{"type" => "Note"}               }) @@ -60,6 +65,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do      assert {:ok, activity} =               ActivityExpirationPolicy.filter(%{                 "id" => "https://example.com/123", +               "actor" => "https://example.com/users/cofe",                 "type" => "Follow"               }) @@ -68,6 +74,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicyTest do      assert {:ok, activity} =               ActivityExpirationPolicy.filter(%{                 "id" => "https://example.com/123", +               "actor" => "https://example.com/users/cofe",                 "type" => "Create",                 "object" => %{"type" => "Cofe"}               }) diff --git a/test/web/activity_pub/mrf/anti_followbot_policy_test.exs b/test/web/activity_pub/mrf/anti_followbot_policy_test.exs index fca0de7c6..3c795f5ac 100644 --- a/test/web/activity_pub/mrf/anti_followbot_policy_test.exs +++ b/test/web/activity_pub/mrf/anti_followbot_policy_test.exs @@ -21,7 +21,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicyTest do          "id" => "https://example.com/activities/1234"        } -      {:reject, nil} = AntiFollowbotPolicy.filter(message) +      assert {:reject, "[AntiFollowbotPolicy]" <> _} = AntiFollowbotPolicy.filter(message)      end      test "matches followbots by display name" do @@ -36,7 +36,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicyTest do          "id" => "https://example.com/activities/1234"        } -      {:reject, nil} = AntiFollowbotPolicy.filter(message) +      assert {:reject, "[AntiFollowbotPolicy]" <> _} = AntiFollowbotPolicy.filter(message)      end    end diff --git a/test/web/activity_pub/mrf/ensure_re_prepended_test.exs b/test/web/activity_pub/mrf/ensure_re_prepended_test.exs index 38ddec5bb..9a283f27d 100644 --- a/test/web/activity_pub/mrf/ensure_re_prepended_test.exs +++ b/test/web/activity_pub/mrf/ensure_re_prepended_test.exs @@ -78,5 +78,15 @@ defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrependedTest do        assert {:ok, res} = EnsureRePrepended.filter(message)        assert res == message      end + +    test "it skips if the object is only a reference" do +      message = %{ +        "type" => "Create", +        "object" => "somereference" +      } + +      assert {:ok, res} = EnsureRePrepended.filter(message) +      assert res == message +    end    end  end diff --git a/test/web/activity_pub/mrf/hellthread_policy_test.exs b/test/web/activity_pub/mrf/hellthread_policy_test.exs index 6e9daa7f9..26f5bcdaa 100644 --- a/test/web/activity_pub/mrf/hellthread_policy_test.exs +++ b/test/web/activity_pub/mrf/hellthread_policy_test.exs @@ -50,7 +50,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicyTest do      } do        Pleroma.Config.put([:mrf_hellthread], %{delist_threshold: 0, reject_threshold: 2}) -      {:reject, nil} = filter(message) +      assert {:reject, "[HellthreadPolicy] 3 recipients is over the limit of 2"} == +               filter(message)      end      test "does not reject the message if the recipient count is below reject_threshold", %{ diff --git a/test/web/activity_pub/mrf/keyword_policy_test.exs b/test/web/activity_pub/mrf/keyword_policy_test.exs index fd1f7aec8..b3d0f3d90 100644 --- a/test/web/activity_pub/mrf/keyword_policy_test.exs +++ b/test/web/activity_pub/mrf/keyword_policy_test.exs @@ -25,7 +25,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do          }        } -      assert {:reject, nil} == KeywordPolicy.filter(message) +      assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} = +               KeywordPolicy.filter(message)      end      test "rejects if string matches in summary" do @@ -39,7 +40,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do          }        } -      assert {:reject, nil} == KeywordPolicy.filter(message) +      assert {:reject, "[KeywordPolicy] Matches with rejected keyword"} = +               KeywordPolicy.filter(message)      end      test "rejects if regex matches in content" do @@ -55,7 +57,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do                     }                   } -                 {:reject, nil} == KeywordPolicy.filter(message) +                 {:reject, "[KeywordPolicy] Matches with rejected keyword"} == +                   KeywordPolicy.filter(message)                 end)      end @@ -72,7 +75,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicyTest do                     }                   } -                 {:reject, nil} == KeywordPolicy.filter(message) +                 {:reject, "[KeywordPolicy] Matches with rejected keyword"} == +                   KeywordPolicy.filter(message)                 end)      end    end diff --git a/test/web/activity_pub/mrf/mention_policy_test.exs b/test/web/activity_pub/mrf/mention_policy_test.exs index aa003bef5..220309cc9 100644 --- a/test/web/activity_pub/mrf/mention_policy_test.exs +++ b/test/web/activity_pub/mrf/mention_policy_test.exs @@ -76,7 +76,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.MentionPolicyTest do          "to" => ["https://example.com/blocked"]        } -      assert MentionPolicy.filter(message) == {:reject, nil} +      assert MentionPolicy.filter(message) == +               {:reject, "[MentionPolicy] Rejected for mention of https://example.com/blocked"}      end      test "cc" do @@ -88,7 +89,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.MentionPolicyTest do          "cc" => ["https://example.com/blocked"]        } -      assert MentionPolicy.filter(message) == {:reject, nil} +      assert MentionPolicy.filter(message) == +               {:reject, "[MentionPolicy] Rejected for mention of https://example.com/blocked"}      end    end  end diff --git a/test/web/activity_pub/mrf/object_age_policy_test.exs b/test/web/activity_pub/mrf/object_age_policy_test.exs index b0fb753bd..cf6acc9a2 100644 --- a/test/web/activity_pub/mrf/object_age_policy_test.exs +++ b/test/web/activity_pub/mrf/object_age_policy_test.exs @@ -38,6 +38,17 @@ defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicyTest do    end    describe "with reject action" do +    test "works with objects with empty to or cc fields" do +      Config.put([:mrf_object_age, :actions], [:reject]) + +      data = +        get_old_message() +        |> Map.put("cc", nil) +        |> Map.put("to", nil) + +      assert match?({:reject, _}, ObjectAgePolicy.filter(data)) +    end +      test "it rejects an old post" do        Config.put([:mrf_object_age, :actions], [:reject]) @@ -56,6 +67,21 @@ defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicyTest do    end    describe "with delist action" do +    test "works with objects with empty to or cc fields" do +      Config.put([:mrf_object_age, :actions], [:delist]) + +      data = +        get_old_message() +        |> Map.put("cc", nil) +        |> Map.put("to", nil) + +      {:ok, _u} = User.get_or_fetch_by_ap_id(data["actor"]) + +      {:ok, data} = ObjectAgePolicy.filter(data) + +      assert Visibility.get_visibility(%{data: data}) == "unlisted" +    end +      test "it delists an old post" do        Config.put([:mrf_object_age, :actions], [:delist]) @@ -80,6 +106,22 @@ defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicyTest do    end    describe "with strip_followers action" do +    test "works with objects with empty to or cc fields" do +      Config.put([:mrf_object_age, :actions], [:strip_followers]) + +      data = +        get_old_message() +        |> Map.put("cc", nil) +        |> Map.put("to", nil) + +      {:ok, user} = User.get_or_fetch_by_ap_id(data["actor"]) + +      {:ok, data} = ObjectAgePolicy.filter(data) + +      refute user.follower_address in data["to"] +      refute user.follower_address in data["cc"] +    end +      test "it strips followers collections from an old post" do        Config.put([:mrf_object_age, :actions], [:strip_followers]) diff --git a/test/web/activity_pub/mrf/reject_non_public_test.exs b/test/web/activity_pub/mrf/reject_non_public_test.exs index f36299b86..58b46b9a2 100644 --- a/test/web/activity_pub/mrf/reject_non_public_test.exs +++ b/test/web/activity_pub/mrf/reject_non_public_test.exs @@ -64,7 +64,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.RejectNonPublicTest do        }        Pleroma.Config.put([:mrf_rejectnonpublic, :allow_followersonly], false) -      assert {:reject, nil} = RejectNonPublic.filter(message) +      assert {:reject, _} = RejectNonPublic.filter(message)      end    end @@ -94,7 +94,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.RejectNonPublicTest do        }        Pleroma.Config.put([:mrf_rejectnonpublic, :allow_direct], false) -      assert {:reject, nil} = RejectNonPublic.filter(message) +      assert {:reject, _} = RejectNonPublic.filter(message)      end    end  end diff --git a/test/web/activity_pub/mrf/simple_policy_test.exs b/test/web/activity_pub/mrf/simple_policy_test.exs index b7b9bc6a2..d7dde62c4 100644 --- a/test/web/activity_pub/mrf/simple_policy_test.exs +++ b/test/web/activity_pub/mrf/simple_policy_test.exs @@ -7,6 +7,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do    import Pleroma.Factory    alias Pleroma.Config    alias Pleroma.Web.ActivityPub.MRF.SimplePolicy +  alias Pleroma.Web.CommonAPI    setup do:            clear_config(:mrf_simple, @@ -15,6 +16,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do              federated_timeline_removal: [],              report_removal: [],              reject: [], +            followers_only: [],              accept: [],              avatar_removal: [],              banner_removal: [], @@ -124,7 +126,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do        report_message = build_report_message()        local_message = build_local_message() -      assert SimplePolicy.filter(report_message) == {:reject, nil} +      assert {:reject, _} = SimplePolicy.filter(report_message)        assert SimplePolicy.filter(local_message) == {:ok, local_message}      end @@ -133,7 +135,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do        report_message = build_report_message()        local_message = build_local_message() -      assert SimplePolicy.filter(report_message) == {:reject, nil} +      assert {:reject, _} = SimplePolicy.filter(report_message)        assert SimplePolicy.filter(local_message) == {:ok, local_message}      end    end @@ -241,7 +243,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do        remote_message = build_remote_message() -      assert SimplePolicy.filter(remote_message) == {:reject, nil} +      assert {:reject, _} = SimplePolicy.filter(remote_message)      end      test "activity matches with wildcard domain" do @@ -249,7 +251,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do        remote_message = build_remote_message() -      assert SimplePolicy.filter(remote_message) == {:reject, nil} +      assert {:reject, _} = SimplePolicy.filter(remote_message)      end      test "actor has a matching host" do @@ -257,7 +259,65 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do        remote_user = build_remote_user() -      assert SimplePolicy.filter(remote_user) == {:reject, nil} +      assert {:reject, _} = SimplePolicy.filter(remote_user) +    end +  end + +  describe "when :followers_only" do +    test "is empty" do +      Config.put([:mrf_simple, :followers_only], []) +      {_, ftl_message} = build_ftl_actor_and_message() +      local_message = build_local_message() + +      assert SimplePolicy.filter(ftl_message) == {:ok, ftl_message} +      assert SimplePolicy.filter(local_message) == {:ok, local_message} +    end + +    test "has a matching host" do +      actor = insert(:user) +      following_user = insert(:user) +      non_following_user = insert(:user) + +      {:ok, _, _, _} = CommonAPI.follow(following_user, actor) + +      activity = %{ +        "actor" => actor.ap_id, +        "to" => [ +          "https://www.w3.org/ns/activitystreams#Public", +          following_user.ap_id, +          non_following_user.ap_id +        ], +        "cc" => [actor.follower_address, "http://foo.bar/qux"] +      } + +      dm_activity = %{ +        "actor" => actor.ap_id, +        "to" => [ +          following_user.ap_id, +          non_following_user.ap_id +        ], +        "cc" => [] +      } + +      actor_domain = +        activity +        |> Map.fetch!("actor") +        |> URI.parse() +        |> Map.fetch!(:host) + +      Config.put([:mrf_simple, :followers_only], [actor_domain]) + +      assert {:ok, new_activity} = SimplePolicy.filter(activity) +      assert actor.follower_address in new_activity["cc"] +      assert following_user.ap_id in new_activity["to"] +      refute "https://www.w3.org/ns/activitystreams#Public" in new_activity["to"] +      refute "https://www.w3.org/ns/activitystreams#Public" in new_activity["cc"] +      refute non_following_user.ap_id in new_activity["to"] +      refute non_following_user.ap_id in new_activity["cc"] + +      assert {:ok, new_dm_activity} = SimplePolicy.filter(dm_activity) +      assert new_dm_activity["to"] == [following_user.ap_id] +      assert new_dm_activity["cc"] == []      end    end @@ -279,7 +339,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do        remote_message = build_remote_message()        assert SimplePolicy.filter(local_message) == {:ok, local_message} -      assert SimplePolicy.filter(remote_message) == {:reject, nil} +      assert {:reject, _} = SimplePolicy.filter(remote_message)      end      test "activity has a matching host" do @@ -429,7 +489,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do      test "it rejects the deletion" do        deletion_message = build_remote_deletion_message() -      assert SimplePolicy.filter(deletion_message) == {:reject, nil} +      assert {:reject, _} = SimplePolicy.filter(deletion_message)      end    end @@ -439,7 +499,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicyTest do      test "it rejects the deletion" do        deletion_message = build_remote_deletion_message() -      assert SimplePolicy.filter(deletion_message) == {:reject, nil} +      assert {:reject, _} = SimplePolicy.filter(deletion_message)      end    end diff --git a/test/web/activity_pub/mrf/tag_policy_test.exs b/test/web/activity_pub/mrf/tag_policy_test.exs index e7793641a..6ff71d640 100644 --- a/test/web/activity_pub/mrf/tag_policy_test.exs +++ b/test/web/activity_pub/mrf/tag_policy_test.exs @@ -12,8 +12,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicyTest do    describe "mrf_tag:disable-any-subscription" do      test "rejects message" do        actor = insert(:user, tags: ["mrf_tag:disable-any-subscription"]) -      message = %{"object" => actor.ap_id, "type" => "Follow"} -      assert {:reject, nil} = TagPolicy.filter(message) +      message = %{"object" => actor.ap_id, "type" => "Follow", "actor" => actor.ap_id} +      assert {:reject, _} = TagPolicy.filter(message)      end    end @@ -22,7 +22,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.TagPolicyTest do        actor = insert(:user, tags: ["mrf_tag:disable-remote-subscription"])        follower = insert(:user, tags: ["mrf_tag:disable-remote-subscription"], local: false)        message = %{"object" => actor.ap_id, "type" => "Follow", "actor" => follower.ap_id} -      assert {:reject, nil} = TagPolicy.filter(message) +      assert {:reject, _} = TagPolicy.filter(message)      end      test "allows non-local follow requests" do diff --git a/test/web/activity_pub/mrf/user_allowlist_policy_test.exs b/test/web/activity_pub/mrf/user_allowlist_policy_test.exs index ba1b69658..8e1ad5bc8 100644 --- a/test/web/activity_pub/mrf/user_allowlist_policy_test.exs +++ b/test/web/activity_pub/mrf/user_allowlist_policy_test.exs @@ -26,6 +26,6 @@ defmodule Pleroma.Web.ActivityPub.MRF.UserAllowListPolicyTest do      actor = insert(:user)      Pleroma.Config.put([:mrf_user_allowlist], %{"localhost" => ["test-ap-id"]})      message = %{"actor" => actor.ap_id} -    assert UserAllowListPolicy.filter(message) == {:reject, nil} +    assert {:reject, _} = UserAllowListPolicy.filter(message)    end  end diff --git a/test/web/activity_pub/mrf/vocabulary_policy_test.exs b/test/web/activity_pub/mrf/vocabulary_policy_test.exs index 69f22bb77..2bceb67ee 100644 --- a/test/web/activity_pub/mrf/vocabulary_policy_test.exs +++ b/test/web/activity_pub/mrf/vocabulary_policy_test.exs @@ -46,7 +46,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.VocabularyPolicyTest do          }        } -      {:reject, nil} = VocabularyPolicy.filter(message) +      {:reject, _} = VocabularyPolicy.filter(message)      end      test "it does not accept disallowed parent types" do @@ -60,7 +60,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.VocabularyPolicyTest do          }        } -      {:reject, nil} = VocabularyPolicy.filter(message) +      {:reject, _} = VocabularyPolicy.filter(message)      end    end @@ -75,7 +75,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.VocabularyPolicyTest do          "object" => "whatever"        } -      {:reject, nil} = VocabularyPolicy.filter(message) +      {:reject, _} = VocabularyPolicy.filter(message)      end      test "it rejects based on child object type" do @@ -89,7 +89,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.VocabularyPolicyTest do          }        } -      {:reject, nil} = VocabularyPolicy.filter(message) +      {:reject, _} = VocabularyPolicy.filter(message)      end      test "it passes through objects that aren't disallowed" do diff --git a/test/web/activity_pub/pipeline_test.exs b/test/web/activity_pub/pipeline_test.exs index 8deb64501..f2a231eaf 100644 --- a/test/web/activity_pub/pipeline_test.exs +++ b/test/web/activity_pub/pipeline_test.exs @@ -14,6 +14,51 @@ defmodule Pleroma.Web.ActivityPub.PipelineTest do        :ok      end +    test "when given an `object_data` in meta, Federation will receive a the original activity with the `object` field set to this embedded object" do +      activity = insert(:note_activity) +      object = %{"id" => "1", "type" => "Love"} +      meta = [local: true, object_data: object] + +      activity_with_object = %{activity | data: Map.put(activity.data, "object", object)} + +      with_mocks([ +        {Pleroma.Web.ActivityPub.ObjectValidator, [], [validate: fn o, m -> {:ok, o, m} end]}, +        { +          Pleroma.Web.ActivityPub.MRF, +          [], +          [filter: fn o -> {:ok, o} end] +        }, +        { +          Pleroma.Web.ActivityPub.ActivityPub, +          [], +          [persist: fn o, m -> {:ok, o, m} end] +        }, +        { +          Pleroma.Web.ActivityPub.SideEffects, +          [], +          [ +            handle: fn o, m -> {:ok, o, m} end, +            handle_after_transaction: fn m -> m end +          ] +        }, +        { +          Pleroma.Web.Federator, +          [], +          [publish: fn _o -> :ok end] +        } +      ]) do +        assert {:ok, ^activity, ^meta} = +                 Pleroma.Web.ActivityPub.Pipeline.common_pipeline(activity, meta) + +        assert_called(Pleroma.Web.ActivityPub.ObjectValidator.validate(activity, meta)) +        assert_called(Pleroma.Web.ActivityPub.MRF.filter(activity)) +        assert_called(Pleroma.Web.ActivityPub.ActivityPub.persist(activity, meta)) +        assert_called(Pleroma.Web.ActivityPub.SideEffects.handle(activity, meta)) +        refute called(Pleroma.Web.Federator.publish(activity)) +        assert_called(Pleroma.Web.Federator.publish(activity_with_object)) +      end +    end +      test "it goes through validation, filtering, persisting, side effects and federation for local activities" do        activity = insert(:note_activity)        meta = [local: true] diff --git a/test/web/activity_pub/publisher_test.exs b/test/web/activity_pub/publisher_test.exs index c2bc38d52..b9388b966 100644 --- a/test/web/activity_pub/publisher_test.exs +++ b/test/web/activity_pub/publisher_test.exs @@ -123,6 +123,39 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do    end    describe "publish_one/1" do +    test "publish to url with with different ports" do +      inbox80 = "http://42.site/users/nick1/inbox" +      inbox42 = "http://42.site:42/users/nick1/inbox" + +      mock(fn +        %{method: :post, url: "http://42.site:42/users/nick1/inbox"} -> +          {:ok, %Tesla.Env{status: 200, body: "port 42"}} + +        %{method: :post, url: "http://42.site/users/nick1/inbox"} -> +          {:ok, %Tesla.Env{status: 200, body: "port 80"}} +      end) + +      actor = insert(:user) + +      assert {:ok, %{body: "port 42"}} = +               Publisher.publish_one(%{ +                 inbox: inbox42, +                 json: "{}", +                 actor: actor, +                 id: 1, +                 unreachable_since: true +               }) + +      assert {:ok, %{body: "port 80"}} = +               Publisher.publish_one(%{ +                 inbox: inbox80, +                 json: "{}", +                 actor: actor, +                 id: 1, +                 unreachable_since: true +               }) +    end +      test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",                     Instances,                     [:passthrough], @@ -131,7 +164,6 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do        inbox = "http://200.site/users/nick1/inbox"        assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) -        assert called(Instances.set_reachable(inbox))      end diff --git a/test/web/activity_pub/side_effects_test.exs b/test/web/activity_pub/side_effects_test.exs index 2649b060a..4a08eb7ee 100644 --- a/test/web/activity_pub/side_effects_test.exs +++ b/test/web/activity_pub/side_effects_test.exs @@ -312,8 +312,12 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do        }      end -    test "deletes the original block", %{block_undo: block_undo, block: block} do -      {:ok, _block_undo, _} = SideEffects.handle(block_undo) +    test "deletes the original block", %{ +      block_undo: block_undo, +      block: block +    } do +      {:ok, _block_undo, _meta} = SideEffects.handle(block_undo) +        refute Activity.get_by_id(block.id)      end diff --git a/test/web/activity_pub/transmogrifier/chat_message_test.exs b/test/web/activity_pub/transmogrifier/chat_message_test.exs index d6736dc3e..31274c067 100644 --- a/test/web/activity_pub/transmogrifier/chat_message_test.exs +++ b/test/web/activity_pub/transmogrifier/chat_message_test.exs @@ -124,6 +124,24 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageTest do        {:ok, %Activity{} = _activity} = Transmogrifier.handle_incoming(data)      end +    test "it doesn't work for deactivated users" do +      data = +        File.read!("test/fixtures/create-chat-message.json") +        |> Poison.decode!() + +      _author = +        insert(:user, +          ap_id: data["actor"], +          local: false, +          last_refreshed_at: DateTime.utc_now(), +          deactivated: true +        ) + +      _recipient = insert(:user, ap_id: List.first(data["to"]), local: true) + +      assert {:error, _} = Transmogrifier.handle_incoming(data) +    end +      test "it inserts it and creates a chat" do        data =          File.read!("test/fixtures/create-chat-message.json") diff --git a/test/web/activity_pub/transmogrifier_test.exs b/test/web/activity_pub/transmogrifier_test.exs index f7b7d1a9f..828964a36 100644 --- a/test/web/activity_pub/transmogrifier_test.exs +++ b/test/web/activity_pub/transmogrifier_test.exs @@ -160,7 +160,15 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert capture_log(fn ->                 {:ok, _returned_activity} = Transmogrifier.handle_incoming(data) -             end) =~ "[error] Couldn't fetch \"https://404.site/whatever\", error: nil" +             end) =~ "[warn] Couldn't fetch \"https://404.site/whatever\", error: nil" +    end + +    test "it does not work for deactivated users" do +      data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!() + +      insert(:user, ap_id: data["actor"], deactivated: true) + +      assert {:error, _} = Transmogrifier.handle_incoming(data)      end      test "it works for incoming notices" do @@ -710,7 +718,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do          "id" => activity.data["id"],          "content" => "test post",          "published" => object.data["published"], -        "actor" => AccountView.render("show.json", %{user: user}) +        "actor" => AccountView.render("show.json", %{user: user, skip_visibility_check: true})        }        message = %{ @@ -774,6 +782,29 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do        assert [user.follower_address] == activity.data["to"]      end +    test "it correctly processes messages with weirdness in address fields" do +      user = insert(:user) + +      message = %{ +        "@context" => "https://www.w3.org/ns/activitystreams", +        "to" => [nil, user.follower_address], +        "cc" => ["https://www.w3.org/ns/activitystreams#Public", ["¿"]], +        "type" => "Create", +        "object" => %{ +          "content" => "…", +          "type" => "Note", +          "attributedTo" => user.ap_id, +          "inReplyTo" => nil +        }, +        "actor" => user.ap_id +      } + +      assert {:ok, activity} = Transmogrifier.handle_incoming(message) + +      assert ["https://www.w3.org/ns/activitystreams#Public"] == activity.data["cc"] +      assert [user.follower_address] == activity.data["to"] +    end +      test "it accepts Move activities" do        old_user = insert(:user)        new_user = insert(:user) diff --git a/test/web/activity_pub/utils_test.exs b/test/web/activity_pub/utils_test.exs index 361dc5a41..d50213545 100644 --- a/test/web/activity_pub/utils_test.exs +++ b/test/web/activity_pub/utils_test.exs @@ -482,7 +482,8 @@ defmodule Pleroma.Web.ActivityPub.UtilsTest do          "id" => activity_ap_id,          "content" => content,          "published" => activity.object.data["published"], -        "actor" => AccountView.render("show.json", %{user: target_account}) +        "actor" => +          AccountView.render("show.json", %{user: target_account, skip_visibility_check: true})        }        assert %{ diff --git a/test/web/admin_api/controllers/admin_api_controller_test.exs b/test/web/admin_api/controllers/admin_api_controller_test.exs index c2433f23c..b5d5bd8c7 100644 --- a/test/web/admin_api/controllers/admin_api_controller_test.exs +++ b/test/web/admin_api/controllers/admin_api_controller_test.exs @@ -9,6 +9,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    import ExUnit.CaptureLog    import Mock    import Pleroma.Factory +  import Swoosh.TestAssertions    alias Pleroma.Activity    alias Pleroma.Config @@ -41,6 +42,16 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do      {:ok, %{admin: admin, token: token, conn: conn}}    end +  test "with valid `admin_token` query parameter, skips OAuth scopes check" do +    clear_config([:admin_token], "password123") + +    user = insert(:user) + +    conn = get(build_conn(), "/api/pleroma/admin/users/#{user.nickname}?admin_token=password123") + +    assert json_response(conn, 200) +  end +    describe "with [:auth, :enforce_oauth_admin_scope_usage]," do      setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], true) @@ -338,7 +349,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do          "avatar" => User.avatar_url(user) |> MediaProxy.url(),          "display_name" => HTML.strip_tags(user.name || user.nickname),          "confirmation_pending" => false, -        "url" => user.ap_id +        "approval_pending" => false, +        "url" => user.ap_id, +        "registration_reason" => nil        }        assert expected == json_response(conn, 200) @@ -602,6 +615,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do    describe "GET /api/pleroma/admin/users" do      test "renders users array for the first page", %{conn: conn, admin: admin} do        user = insert(:user, local: false, tags: ["foo", "bar"]) +      user2 = insert(:user, approval_pending: true, registration_reason: "I'm a chill dude") +        conn = get(conn, "/api/pleroma/admin/users?page=1")        users = @@ -616,7 +631,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(admin) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(admin.name || admin.nickname),              "confirmation_pending" => false, -            "url" => admin.ap_id +            "approval_pending" => false, +            "url" => admin.ap_id, +            "registration_reason" => nil            },            %{              "deactivated" => user.deactivated, @@ -628,13 +645,29 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(user) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(user.name || user.nickname),              "confirmation_pending" => false, -            "url" => user.ap_id +            "approval_pending" => false, +            "url" => user.ap_id, +            "registration_reason" => nil +          }, +          %{ +            "deactivated" => user2.deactivated, +            "id" => user2.id, +            "nickname" => user2.nickname, +            "roles" => %{"admin" => false, "moderator" => false}, +            "local" => true, +            "tags" => [], +            "avatar" => User.avatar_url(user2) |> MediaProxy.url(), +            "display_name" => HTML.strip_tags(user2.name || user2.nickname), +            "confirmation_pending" => false, +            "approval_pending" => true, +            "url" => user2.ap_id, +            "registration_reason" => "I'm a chill dude"            }          ]          |> Enum.sort_by(& &1["nickname"])        assert json_response(conn, 200) == %{ -               "count" => 2, +               "count" => 3,                 "page_size" => 50,                 "users" => users               } @@ -701,7 +734,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user.name || user.nickname),                     "confirmation_pending" => false, -                   "url" => user.ap_id +                   "approval_pending" => false, +                   "url" => user.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -727,7 +762,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user.name || user.nickname),                     "confirmation_pending" => false, -                   "url" => user.ap_id +                   "approval_pending" => false, +                   "url" => user.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -753,7 +790,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user.name || user.nickname),                     "confirmation_pending" => false, -                   "url" => user.ap_id +                   "approval_pending" => false, +                   "url" => user.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -779,7 +818,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user.name || user.nickname),                     "confirmation_pending" => false, -                   "url" => user.ap_id +                   "approval_pending" => false, +                   "url" => user.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -805,7 +846,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user.name || user.nickname),                     "confirmation_pending" => false, -                   "url" => user.ap_id +                   "approval_pending" => false, +                   "url" => user.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -831,7 +874,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user.name || user.nickname),                     "confirmation_pending" => false, -                   "url" => user.ap_id +                   "approval_pending" => false, +                   "url" => user.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -852,7 +897,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user2) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user2.name || user2.nickname),                     "confirmation_pending" => false, -                   "url" => user2.ap_id +                   "approval_pending" => false, +                   "url" => user2.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -885,7 +932,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user.name || user.nickname),                     "confirmation_pending" => false, -                   "url" => user.ap_id +                   "approval_pending" => false, +                   "url" => user.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -911,7 +960,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(user) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(user.name || user.nickname),              "confirmation_pending" => false, -            "url" => user.ap_id +            "approval_pending" => false, +            "url" => user.ap_id, +            "registration_reason" => nil            },            %{              "deactivated" => admin.deactivated, @@ -923,7 +974,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(admin) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(admin.name || admin.nickname),              "confirmation_pending" => false, -            "url" => admin.ap_id +            "approval_pending" => false, +            "url" => admin.ap_id, +            "registration_reason" => nil            },            %{              "deactivated" => false, @@ -935,7 +988,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(old_admin) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(old_admin.name || old_admin.nickname),              "confirmation_pending" => false, -            "url" => old_admin.ap_id +            "approval_pending" => false, +            "url" => old_admin.ap_id, +            "registration_reason" => nil            }          ]          |> Enum.sort_by(& &1["nickname"]) @@ -947,6 +1002,44 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do               }      end +    test "only unapproved users", %{conn: conn} do +      user = +        insert(:user, +          nickname: "sadboy", +          approval_pending: true, +          registration_reason: "Plz let me in!" +        ) + +      insert(:user, nickname: "happyboy", approval_pending: false) + +      conn = get(conn, "/api/pleroma/admin/users?filters=need_approval") + +      users = +        [ +          %{ +            "deactivated" => user.deactivated, +            "id" => user.id, +            "nickname" => user.nickname, +            "roles" => %{"admin" => false, "moderator" => false}, +            "local" => true, +            "tags" => [], +            "avatar" => User.avatar_url(user) |> MediaProxy.url(), +            "display_name" => HTML.strip_tags(user.name || user.nickname), +            "confirmation_pending" => false, +            "approval_pending" => true, +            "url" => user.ap_id, +            "registration_reason" => "Plz let me in!" +          } +        ] +        |> Enum.sort_by(& &1["nickname"]) + +      assert json_response(conn, 200) == %{ +               "count" => 1, +               "page_size" => 50, +               "users" => users +             } +    end +      test "load only admins", %{conn: conn, admin: admin} do        second_admin = insert(:user, is_admin: true)        insert(:user) @@ -966,7 +1059,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(admin) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(admin.name || admin.nickname),              "confirmation_pending" => false, -            "url" => admin.ap_id +            "approval_pending" => false, +            "url" => admin.ap_id, +            "registration_reason" => nil            },            %{              "deactivated" => false, @@ -978,7 +1073,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(second_admin) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(second_admin.name || second_admin.nickname),              "confirmation_pending" => false, -            "url" => second_admin.ap_id +            "approval_pending" => false, +            "url" => second_admin.ap_id, +            "registration_reason" => nil            }          ]          |> Enum.sort_by(& &1["nickname"]) @@ -1011,7 +1108,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(moderator) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(moderator.name || moderator.nickname),                     "confirmation_pending" => false, -                   "url" => moderator.ap_id +                   "approval_pending" => false, +                   "url" => moderator.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -1037,7 +1136,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(user1) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(user1.name || user1.nickname),              "confirmation_pending" => false, -            "url" => user1.ap_id +            "approval_pending" => false, +            "url" => user1.ap_id, +            "registration_reason" => nil            },            %{              "deactivated" => false, @@ -1049,7 +1150,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do              "avatar" => User.avatar_url(user2) |> MediaProxy.url(),              "display_name" => HTML.strip_tags(user2.name || user2.nickname),              "confirmation_pending" => false, -            "url" => user2.ap_id +            "approval_pending" => false, +            "url" => user2.ap_id, +            "registration_reason" => nil            }          ]          |> Enum.sort_by(& &1["nickname"]) @@ -1089,7 +1192,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(user) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(user.name || user.nickname),                     "confirmation_pending" => false, -                   "url" => user.ap_id +                   "approval_pending" => false, +                   "url" => user.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -1114,7 +1219,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                     "avatar" => User.avatar_url(admin) |> MediaProxy.url(),                     "display_name" => HTML.strip_tags(admin.name || admin.nickname),                     "confirmation_pending" => false, -                   "url" => admin.ap_id +                   "approval_pending" => false, +                   "url" => admin.ap_id, +                   "registration_reason" => nil                   }                 ]               } @@ -1161,6 +1268,26 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do               "@#{admin.nickname} deactivated users: @#{user_one.nickname}, @#{user_two.nickname}"    end +  test "PATCH /api/pleroma/admin/users/approve", %{admin: admin, conn: conn} do +    user_one = insert(:user, approval_pending: true) +    user_two = insert(:user, approval_pending: true) + +    conn = +      patch( +        conn, +        "/api/pleroma/admin/users/approve", +        %{nicknames: [user_one.nickname, user_two.nickname]} +      ) + +    response = json_response(conn, 200) +    assert Enum.map(response["users"], & &1["approval_pending"]) == [false, false] + +    log_entry = Repo.one(ModerationLog) + +    assert ModerationLog.get_log_entry_message(log_entry) == +             "@#{admin.nickname} approved users: @#{user_one.nickname}, @#{user_two.nickname}" +  end +    test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admin, conn: conn} do      user = insert(:user) @@ -1177,7 +1304,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                 "avatar" => User.avatar_url(user) |> MediaProxy.url(),                 "display_name" => HTML.strip_tags(user.name || user.nickname),                 "confirmation_pending" => false, -               "url" => user.ap_id +               "approval_pending" => false, +               "url" => user.ap_id, +               "registration_reason" => nil               }      log_entry = Repo.one(ModerationLog) @@ -1721,6 +1850,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do                 "@#{admin.nickname} re-sent confirmation email for users: @#{first_user.nickname}, @#{                   second_user.nickname                 }" + +      ObanHelpers.perform_all() +      assert_email_sent(Pleroma.Emails.UserEmail.account_confirmation_email(first_user))      end    end diff --git a/test/web/admin_api/controllers/config_controller_test.exs b/test/web/admin_api/controllers/config_controller_test.exs index 064ef9bc7..61bc9fd39 100644 --- a/test/web/admin_api/controllers/config_controller_test.exs +++ b/test/web/admin_api/controllers/config_controller_test.exs @@ -152,6 +152,14 @@ defmodule Pleroma.Web.AdminAPI.ConfigControllerTest do        assert emoji_val[:groups] == [a: 1, b: 2]        assert assets_val[:mascots] == [a: 1, b: 2]      end + +    test "with valid `admin_token` query parameter, skips OAuth scopes check" do +      clear_config([:admin_token], "password123") + +      build_conn() +      |> get("/api/pleroma/admin/config?admin_token=password123") +      |> json_response_and_validate_schema(200) +    end    end    test "POST /api/pleroma/admin/config error", %{conn: conn} do diff --git a/test/web/admin_api/controllers/report_controller_test.exs b/test/web/admin_api/controllers/report_controller_test.exs index 940bce340..57946e6bb 100644 --- a/test/web/admin_api/controllers/report_controller_test.exs +++ b/test/web/admin_api/controllers/report_controller_test.exs @@ -204,7 +204,7 @@ defmodule Pleroma.Web.AdminAPI.ReportControllerTest do      test "returns empty response when no reports created", %{conn: conn} do        response =          conn -        |> get("/api/pleroma/admin/reports") +        |> get(report_path(conn, :index))          |> json_response_and_validate_schema(:ok)        assert Enum.empty?(response["reports"]) @@ -224,7 +224,7 @@ defmodule Pleroma.Web.AdminAPI.ReportControllerTest do        response =          conn -        |> get("/api/pleroma/admin/reports") +        |> get(report_path(conn, :index))          |> json_response_and_validate_schema(:ok)        [report] = response["reports"] @@ -256,7 +256,7 @@ defmodule Pleroma.Web.AdminAPI.ReportControllerTest do        response =          conn -        |> get("/api/pleroma/admin/reports?state=open") +        |> get(report_path(conn, :index, %{state: "open"}))          |> json_response_and_validate_schema(:ok)        assert [open_report] = response["reports"] @@ -268,7 +268,7 @@ defmodule Pleroma.Web.AdminAPI.ReportControllerTest do        response =          conn -        |> get("/api/pleroma/admin/reports?state=closed") +        |> get(report_path(conn, :index, %{state: "closed"}))          |> json_response_and_validate_schema(:ok)        assert [closed_report] = response["reports"] @@ -280,9 +280,7 @@ defmodule Pleroma.Web.AdminAPI.ReportControllerTest do        assert %{"total" => 0, "reports" => []} ==                 conn -               |> get("/api/pleroma/admin/reports?state=resolved", %{ -                 "" => "" -               }) +               |> get(report_path(conn, :index, %{state: "resolved"}))                 |> json_response_and_validate_schema(:ok)      end @@ -297,7 +295,7 @@ defmodule Pleroma.Web.AdminAPI.ReportControllerTest do          |> get("/api/pleroma/admin/reports")        assert json_response(conn, :forbidden) == -               %{"error" => "User is not an admin or OAuth admin scope is not granted."} +               %{"error" => "User is not an admin."}      end      test "returns 403 when requested by anonymous" do diff --git a/test/web/admin_api/search_test.exs b/test/web/admin_api/search_test.exs index e0e3d4153..b974cedd5 100644 --- a/test/web/admin_api/search_test.exs +++ b/test/web/admin_api/search_test.exs @@ -166,5 +166,16 @@ defmodule Pleroma.Web.AdminAPI.SearchTest do        assert total == 3        assert count == 1      end + +    test "it returns unapproved user" do +      unapproved = insert(:user, approval_pending: true) +      insert(:user) +      insert(:user) + +      {:ok, _results, total} = Search.user() +      {:ok, [^unapproved], count} = Search.user(%{need_approval: true}) +      assert total == 3 +      assert count == 1 +    end    end  end diff --git a/test/web/admin_api/views/report_view_test.exs b/test/web/admin_api/views/report_view_test.exs index f00b0afb2..5a02292be 100644 --- a/test/web/admin_api/views/report_view_test.exs +++ b/test/web/admin_api/views/report_view_test.exs @@ -4,11 +4,14 @@  defmodule Pleroma.Web.AdminAPI.ReportViewTest do    use Pleroma.DataCase +    import Pleroma.Factory + +  alias Pleroma.Web.AdminAPI    alias Pleroma.Web.AdminAPI.Report    alias Pleroma.Web.AdminAPI.ReportView    alias Pleroma.Web.CommonAPI -  alias Pleroma.Web.MastodonAPI.AccountView +  alias Pleroma.Web.MastodonAPI    alias Pleroma.Web.MastodonAPI.StatusView    test "renders a report" do @@ -21,13 +24,16 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do        content: nil,        actor:          Map.merge( -          AccountView.render("show.json", %{user: user}), -          Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: user}) +          MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true}), +          AdminAPI.AccountView.render("show.json", %{user: user})          ),        account:          Map.merge( -          AccountView.render("show.json", %{user: other_user}), -          Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: other_user}) +          MastodonAPI.AccountView.render("show.json", %{ +            user: other_user, +            skip_visibility_check: true +          }), +          AdminAPI.AccountView.render("show.json", %{user: other_user})          ),        statuses: [],        notes: [], @@ -56,13 +62,16 @@ defmodule Pleroma.Web.AdminAPI.ReportViewTest do        content: nil,        actor:          Map.merge( -          AccountView.render("show.json", %{user: user}), -          Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: user}) +          MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true}), +          AdminAPI.AccountView.render("show.json", %{user: user})          ),        account:          Map.merge( -          AccountView.render("show.json", %{user: other_user}), -          Pleroma.Web.AdminAPI.AccountView.render("show.json", %{user: other_user}) +          MastodonAPI.AccountView.render("show.json", %{ +            user: other_user, +            skip_visibility_check: true +          }), +          AdminAPI.AccountView.render("show.json", %{user: other_user})          ),        statuses: [StatusView.render("show.json", %{activity: activity})],        state: "open", diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 7e11fede3..4ba6232dc 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -458,6 +458,11 @@ defmodule Pleroma.Web.CommonAPITest do    end    describe "posting" do +    test "deactivated users can't post" do +      user = insert(:user, deactivated: true) +      assert {:error, _} = CommonAPI.post(user, %{status: "ye"}) +    end +      test "it supports explicit addressing" do        user = insert(:user)        user_two = insert(:user) @@ -624,14 +629,27 @@ defmodule Pleroma.Web.CommonAPITest do        user = insert(:user)        other_user = insert(:user) -      {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"}) -      {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍") +      clear_config([:instance, :federating], true) + +      with_mock Pleroma.Web.Federator, +        publish: fn _ -> nil end do +        {:ok, activity} = CommonAPI.post(other_user, %{status: "cofe"}) +        {:ok, reaction} = CommonAPI.react_with_emoji(activity.id, user, "👍") + +        {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍") -      {:ok, unreaction} = CommonAPI.unreact_with_emoji(activity.id, user, "👍") +        assert unreaction.data["type"] == "Undo" +        assert unreaction.data["object"] == reaction.data["id"] +        assert unreaction.local -      assert unreaction.data["type"] == "Undo" -      assert unreaction.data["object"] == reaction.data["id"] -      assert unreaction.local +        # On federation, it contains the undone (and deleted) object +        unreaction_with_object = %{ +          unreaction +          | data: Map.put(unreaction.data, "object", reaction.data) +        } + +        assert called(Pleroma.Web.Federator.publish(unreaction_with_object)) +      end      end      test "repeating a status" do diff --git a/test/web/feed/user_controller_test.exs b/test/web/feed/user_controller_test.exs index fa2ed1ea5..0d2a61967 100644 --- a/test/web/feed/user_controller_test.exs +++ b/test/web/feed/user_controller_test.exs @@ -181,6 +181,17 @@ defmodule Pleroma.Web.Feed.UserControllerTest do        assert activity_titles == ['public', 'unlisted']      end + +    test "returns 404 when the user is remote", %{conn: conn} do +      user = insert(:user, local: false) + +      {:ok, _} = CommonAPI.post(user, %{status: "test"}) + +      assert conn +             |> put_req_header("accept", "application/atom+xml") +             |> get(user_feed_path(conn, :feed, user.nickname)) +             |> response(404) +    end    end    # Note: see ActivityPubControllerTest for JSON format tests diff --git a/test/web/mastodon_api/controllers/account_controller/update_credentials_test.exs b/test/web/mastodon_api/controllers/account_controller/update_credentials_test.exs index 638626b45..b888e4c71 100644 --- a/test/web/mastodon_api/controllers/account_controller/update_credentials_test.exs +++ b/test/web/mastodon_api/controllers/account_controller/update_credentials_test.exs @@ -351,6 +351,30 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do               ]      end +    test "emojis in fields labels", %{conn: conn} do +      fields = [ +        %{"name" => ":firefox:", "value" => "is best 2hu"}, +        %{"name" => "they wins", "value" => ":blank:"} +      ] + +      account_data = +        conn +        |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields}) +        |> json_response_and_validate_schema(200) + +      assert account_data["fields"] == [ +               %{"name" => ":firefox:", "value" => "is best 2hu"}, +               %{"name" => "they wins", "value" => ":blank:"} +             ] + +      assert account_data["source"]["fields"] == [ +               %{"name" => ":firefox:", "value" => "is best 2hu"}, +               %{"name" => "they wins", "value" => ":blank:"} +             ] + +      assert [%{"shortcode" => "blank"}, %{"shortcode" => "firefox"}] = account_data["emojis"] +    end +      test "update fields via x-www-form-urlencoded", %{conn: conn} do        fields =          [ diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index 9c7b5e9b2..17a1e7d66 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -5,7 +5,6 @@  defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do    use Pleroma.Web.ConnCase -  alias Pleroma.Config    alias Pleroma.Repo    alias Pleroma.User    alias Pleroma.Web.ActivityPub.ActivityPub @@ -16,8 +15,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do    import Pleroma.Factory    describe "account fetching" do -    setup do: clear_config([:instance, :limit_to_local_content]) -      test "works by id" do        %User{id: user_id} = insert(:user) @@ -42,7 +39,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do      end      test "works by nickname for remote users" do -      Config.put([:instance, :limit_to_local_content], false) +      clear_config([:instance, :limit_to_local_content], false)        user = insert(:user, nickname: "user@example.com", local: false) @@ -53,7 +50,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do      end      test "respects limit_to_local_content == :all for remote user nicknames" do -      Config.put([:instance, :limit_to_local_content], :all) +      clear_config([:instance, :limit_to_local_content], :all)        user = insert(:user, nickname: "user@example.com", local: false) @@ -63,7 +60,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do      end      test "respects limit_to_local_content == :unauthenticated for remote user nicknames" do -      Config.put([:instance, :limit_to_local_content], :unauthenticated) +      clear_config([:instance, :limit_to_local_content], :unauthenticated)        user = insert(:user, nickname: "user@example.com", local: false)        reading_user = insert(:user) @@ -583,6 +580,15 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do                 |> get("/api/v1/accounts/#{user.id}/followers?max_id=#{follower3_id}")                 |> json_response_and_validate_schema(200) +      assert [%{"id" => ^follower2_id}, %{"id" => ^follower1_id}] = +               conn +               |> get( +                 "/api/v1/accounts/#{user.id}/followers?id=#{user.id}&limit=20&max_id=#{ +                   follower3_id +                 }" +               ) +               |> json_response_and_validate_schema(200) +        res_conn = get(conn, "/api/v1/accounts/#{user.id}/followers?limit=1&max_id=#{follower3_id}")        assert [%{"id" => ^follower2_id}] = json_response_and_validate_schema(res_conn, 200) @@ -655,6 +661,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        assert id1 == following1.id        res_conn = +        get( +          conn, +          "/api/v1/accounts/#{user.id}/following?id=#{user.id}&limit=20&max_id=#{following3.id}" +        ) + +      assert [%{"id" => id2}, %{"id" => id1}] = json_response_and_validate_schema(res_conn, 200) +      assert id2 == following2.id +      assert id1 == following1.id + +      res_conn =          get(conn, "/api/v1/accounts/#{user.id}/following?limit=1&max_id=#{following3.id}")        assert [%{"id" => id2}] = json_response_and_validate_schema(res_conn, 200) @@ -884,9 +900,93 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        [valid_params: valid_params]      end -    setup do: clear_config([:instance, :account_activation_required]) +    test "registers and logs in without :account_activation_required / :account_approval_required", +         %{conn: conn} do +      clear_config([:instance, :account_activation_required], false) +      clear_config([:instance, :account_approval_required], false) + +      conn = +        conn +        |> put_req_header("content-type", "application/json") +        |> post("/api/v1/apps", %{ +          client_name: "client_name", +          redirect_uris: "urn:ietf:wg:oauth:2.0:oob", +          scopes: "read, write, follow" +        }) + +      assert %{ +               "client_id" => client_id, +               "client_secret" => client_secret, +               "id" => _, +               "name" => "client_name", +               "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob", +               "vapid_key" => _, +               "website" => nil +             } = json_response_and_validate_schema(conn, 200) + +      conn = +        post(conn, "/oauth/token", %{ +          grant_type: "client_credentials", +          client_id: client_id, +          client_secret: client_secret +        }) + +      assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} = +               json_response(conn, 200) + +      assert token +      token_from_db = Repo.get_by(Token, token: token) +      assert token_from_db +      assert refresh +      assert scope == "read write follow" + +      clear_config([User, :email_blacklist], ["example.org"]) + +      params = %{ +        username: "lain", +        email: "lain@example.org", +        password: "PlzDontHackLain", +        bio: "Test Bio", +        agreement: true +      } + +      conn = +        build_conn() +        |> put_req_header("content-type", "multipart/form-data") +        |> put_req_header("authorization", "Bearer " <> token) +        |> post("/api/v1/accounts", params) + +      assert %{"error" => "{\"email\":[\"Invalid email\"]}"} = +               json_response_and_validate_schema(conn, 400) + +      Pleroma.Config.put([User, :email_blacklist], []) + +      conn = +        build_conn() +        |> put_req_header("content-type", "multipart/form-data") +        |> put_req_header("authorization", "Bearer " <> token) +        |> post("/api/v1/accounts", params) + +      %{ +        "access_token" => token, +        "created_at" => _created_at, +        "scope" => ^scope, +        "token_type" => "Bearer" +      } = json_response_and_validate_schema(conn, 200) + +      token_from_db = Repo.get_by(Token, token: token) +      assert token_from_db +      user = Repo.preload(token_from_db, :user).user + +      assert user +      refute user.confirmation_pending +      refute user.approval_pending +    end + +    test "registers but does not log in with :account_activation_required", %{conn: conn} do +      clear_config([:instance, :account_activation_required], true) +      clear_config([:instance, :account_approval_required], false) -    test "Account registration via Application", %{conn: conn} do        conn =          conn          |> put_req_header("content-type", "application/json") @@ -934,19 +1034,76 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do            agreement: true          }) -      %{ -        "access_token" => token, -        "created_at" => _created_at, -        "scope" => ^scope, -        "token_type" => "Bearer" -      } = json_response_and_validate_schema(conn, 200) +      response = json_response_and_validate_schema(conn, 200) +      assert %{"identifier" => "missing_confirmed_email"} = response +      refute response["access_token"] +      refute response["token_type"] +      user = Repo.get_by(User, email: "lain@example.org") +      assert user.confirmation_pending +    end + +    test "registers but does not log in with :account_approval_required", %{conn: conn} do +      clear_config([:instance, :account_approval_required], true) +      clear_config([:instance, :account_activation_required], false) + +      conn = +        conn +        |> put_req_header("content-type", "application/json") +        |> post("/api/v1/apps", %{ +          client_name: "client_name", +          redirect_uris: "urn:ietf:wg:oauth:2.0:oob", +          scopes: "read, write, follow" +        }) + +      assert %{ +               "client_id" => client_id, +               "client_secret" => client_secret, +               "id" => _, +               "name" => "client_name", +               "redirect_uri" => "urn:ietf:wg:oauth:2.0:oob", +               "vapid_key" => _, +               "website" => nil +             } = json_response_and_validate_schema(conn, 200) + +      conn = +        post(conn, "/oauth/token", %{ +          grant_type: "client_credentials", +          client_id: client_id, +          client_secret: client_secret +        }) + +      assert %{"access_token" => token, "refresh_token" => refresh, "scope" => scope} = +               json_response(conn, 200) + +      assert token        token_from_db = Repo.get_by(Token, token: token)        assert token_from_db -      token_from_db = Repo.preload(token_from_db, :user) -      assert token_from_db.user +      assert refresh +      assert scope == "read write follow" -      assert token_from_db.user.confirmation_pending +      conn = +        build_conn() +        |> put_req_header("content-type", "multipart/form-data") +        |> put_req_header("authorization", "Bearer " <> token) +        |> post("/api/v1/accounts", %{ +          username: "lain", +          email: "lain@example.org", +          password: "PlzDontHackLain", +          bio: "Test Bio", +          agreement: true, +          reason: "I'm a cool dude, bro" +        }) + +      response = json_response_and_validate_schema(conn, 200) +      assert %{"identifier" => "awaiting_approval"} = response +      refute response["access_token"] +      refute response["token_type"] + +      user = Repo.get_by(User, email: "lain@example.org") + +      assert user.approval_pending +      assert user.registration_reason == "I'm a cool dude, bro"      end      test "returns error when user already registred", %{conn: conn, valid_params: valid_params} do @@ -1000,11 +1157,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do        end)      end -    setup do: clear_config([:instance, :account_activation_required]) -      test "returns bad_request if missing email params when :account_activation_required is enabled",           %{conn: conn, valid_params: valid_params} do -      Pleroma.Config.put([:instance, :account_activation_required], true) +      clear_config([:instance, :account_activation_required], true)        app_token = insert(:oauth_token, user: nil) @@ -1169,8 +1324,6 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do          assert token_from_db          token_from_db = Repo.preload(token_from_db, :user)          assert token_from_db.user - -        assert token_from_db.user.confirmation_pending        end        conn = diff --git a/test/web/mastodon_api/controllers/domain_block_controller_test.exs b/test/web/mastodon_api/controllers/domain_block_controller_test.exs index 01a24afcf..664654500 100644 --- a/test/web/mastodon_api/controllers/domain_block_controller_test.exs +++ b/test/web/mastodon_api/controllers/domain_block_controller_test.exs @@ -32,6 +32,38 @@ defmodule Pleroma.Web.MastodonAPI.DomainBlockControllerTest do      refute User.blocks?(user, other_user)    end +  test "blocking a domain via query params" do +    %{user: user, conn: conn} = oauth_access(["write:blocks"]) +    other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"}) + +    ret_conn = +      conn +      |> put_req_header("content-type", "application/json") +      |> post("/api/v1/domain_blocks?domain=dogwhistle.zone") + +    assert %{} == json_response_and_validate_schema(ret_conn, 200) +    user = User.get_cached_by_ap_id(user.ap_id) +    assert User.blocks?(user, other_user) +  end + +  test "unblocking a domain via query params" do +    %{user: user, conn: conn} = oauth_access(["write:blocks"]) +    other_user = insert(:user, %{ap_id: "https://dogwhistle.zone/@pundit"}) + +    User.block_domain(user, "dogwhistle.zone") +    user = refresh_record(user) +    assert User.blocks?(user, other_user) + +    ret_conn = +      conn +      |> put_req_header("content-type", "application/json") +      |> delete("/api/v1/domain_blocks?domain=dogwhistle.zone") + +    assert %{} == json_response_and_validate_schema(ret_conn, 200) +    user = User.get_cached_by_ap_id(user.ap_id) +    refute User.blocks?(user, other_user) +  end +    test "getting a list of domain blocks" do      %{user: user, conn: conn} = oauth_access(["read:blocks"]) diff --git a/test/web/mastodon_api/controllers/instance_controller_test.exs b/test/web/mastodon_api/controllers/instance_controller_test.exs index cc880d82c..6a9ccd979 100644 --- a/test/web/mastodon_api/controllers/instance_controller_test.exs +++ b/test/web/mastodon_api/controllers/instance_controller_test.exs @@ -27,6 +27,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do               "thumbnail" => _,               "languages" => _,               "registrations" => _, +             "approval_required" => _,               "poll_limits" => _,               "upload_limit" => _,               "avatar_upload_limit" => _, diff --git a/test/web/mastodon_api/controllers/status_controller_test.exs b/test/web/mastodon_api/controllers/status_controller_test.exs index fd2de8d80..5955d8334 100644 --- a/test/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/web/mastodon_api/controllers/status_controller_test.exs @@ -22,6 +22,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do    setup do: clear_config([:instance, :federating])    setup do: clear_config([:instance, :allow_relay])    setup do: clear_config([:rich_media, :enabled]) +  setup do: clear_config([:mrf, :policies]) +  setup do: clear_config([:mrf_keyword, :reject])    describe "posting statuses" do      setup do: oauth_access(["write:statuses"]) @@ -157,6 +159,17 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do                 |> json_response_and_validate_schema(422)      end +    test "Get MRF reason when posting a status is rejected by one", %{conn: conn} do +      Pleroma.Config.put([:mrf_keyword, :reject], ["GNO"]) +      Pleroma.Config.put([:mrf, :policies], [Pleroma.Web.ActivityPub.MRF.KeywordPolicy]) + +      assert %{"error" => "[KeywordPolicy] Matches with rejected keyword"} = +               conn +               |> put_req_header("content-type", "application/json") +               |> post("api/v1/statuses", %{"status" => "GNO/Linux"}) +               |> json_response_and_validate_schema(422) +    end +      test "posting an undefined status with an attachment", %{user: user, conn: conn} do        file = %Plug.Upload{          content_type: "image/jpg", @@ -1419,6 +1432,20 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do        [%{"id" => id}] = response        assert id == other_user.id      end + +    test "returns empty array when :show_reactions is disabled", %{conn: conn, activity: activity} do +      clear_config([:instance, :show_reactions], false) + +      other_user = insert(:user) +      {:ok, _} = CommonAPI.favorite(other_user, activity.id) + +      response = +        conn +        |> get("/api/v1/statuses/#{activity.id}/favourited_by") +        |> json_response_and_validate_schema(:ok) + +      assert Enum.empty?(response) +    end    end    describe "GET /api/v1/statuses/:id/reblogged_by" do diff --git a/test/web/mastodon_api/mastodon_api_test.exs b/test/web/mastodon_api/mastodon_api_test.exs index c08be37d4..0c5a38bf6 100644 --- a/test/web/mastodon_api/mastodon_api_test.exs +++ b/test/web/mastodon_api/mastodon_api_test.exs @@ -17,8 +17,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPITest do      test "returns error when followed user is deactivated" do        follower = insert(:user)        user = insert(:user, local: true, deactivated: true) -      {:error, error} = MastodonAPI.follow(follower, user) -      assert error == :rejected +      assert {:error, _error} = MastodonAPI.follow(follower, user)      end      test "following for user" do diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index 17f035add..8f37efa3c 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -95,7 +95,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        }      } -    assert expected == AccountView.render("show.json", %{user: user}) +    assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})    end    test "Favicon is nil when :instances_favicons is disabled" do @@ -108,22 +108,20 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do                 favicon:                   "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"               } -           } = AccountView.render("show.json", %{user: user}) +           } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})      Config.put([:instances_favicons, :enabled], false) -    assert %{pleroma: %{favicon: nil}} = AccountView.render("show.json", %{user: user}) +    assert %{pleroma: %{favicon: nil}} = +             AccountView.render("show.json", %{user: user, skip_visibility_check: true})    end    test "Represent the user account for the account owner" do      user = insert(:user)      notification_settings = %{ -      followers: true, -      follows: true, -      non_followers: true, -      non_follows: true, -      privacy_option: false +      block_from_strangers: false, +      hide_notification_contents: false      }      privacy = user.default_scope @@ -192,7 +190,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        }      } -    assert expected == AccountView.render("show.json", %{user: user}) +    assert expected == AccountView.render("show.json", %{user: user, skip_visibility_check: true})    end    test "Represent a Funkwhale channel" do @@ -201,7 +199,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          "https://channels.tests.funkwhale.audio/federation/actors/compositions"        ) -    assert represented = AccountView.render("show.json", %{user: user}) +    assert represented = +             AccountView.render("show.json", %{user: user, skip_visibility_check: true}) +      assert represented.acct == "compositions@channels.tests.funkwhale.audio"      assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions"    end @@ -226,6 +226,23 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do      assert expected == AccountView.render("mention.json", %{user: user})    end +  test "demands :for or :skip_visibility_check option for account rendering" do +    clear_config([:restrict_unauthenticated, :profiles, :local], false) + +    user = insert(:user) +    user_id = user.id + +    assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: nil}) +    assert %{id: ^user_id} = AccountView.render("show.json", %{user: user, for: user}) + +    assert %{id: ^user_id} = +             AccountView.render("show.json", %{user: user, skip_visibility_check: true}) + +    assert_raise RuntimeError, ~r/:skip_visibility_check or :for option is required/, fn -> +      AccountView.render("show.json", %{user: user}) +    end +  end +    describe "relationship" do      defp test_relationship_rendering(user, other_user, expected_result) do        opts = %{user: user, target: other_user, relationships: nil} @@ -339,7 +356,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do      assert result.pleroma.settings_store == %{:fe => "test"} -    result = AccountView.render("show.json", %{user: user, with_pleroma_settings: true}) +    result = AccountView.render("show.json", %{user: user, for: nil, with_pleroma_settings: true})      assert result.pleroma[:settings_store] == nil      result = AccountView.render("show.json", %{user: user, for: user}) @@ -348,13 +365,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do    test "doesn't sanitize display names" do      user = insert(:user, name: "<marquee> username </marquee>") -    result = AccountView.render("show.json", %{user: user}) +    result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})      assert result.display_name == "<marquee> username </marquee>"    end    test "never display nil user follow counts" do      user = insert(:user, following_count: 0, follower_count: 0) -    result = AccountView.render("show.json", %{user: user}) +    result = AccountView.render("show.json", %{user: user, skip_visibility_check: true})      assert result.following_count == 0      assert result.followers_count == 0 @@ -378,7 +395,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do                 followers_count: 0,                 following_count: 0,                 pleroma: %{hide_follows_count: true, hide_followers_count: true} -             } = AccountView.render("show.json", %{user: user}) +             } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})      end      test "shows when follows/followers are hidden" do @@ -391,7 +408,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do                 followers_count: 1,                 following_count: 1,                 pleroma: %{hide_follows: true, hide_followers: true} -             } = AccountView.render("show.json", %{user: user}) +             } = AccountView.render("show.json", %{user: user, skip_visibility_check: true})      end      test "shows actual follower/following count to the account owner" do @@ -534,7 +551,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do          emoji: %{"joker_smile" => "https://evil.website/society.png"}        ) -    AccountView.render("show.json", %{user: user}) +    AccountView.render("show.json", %{user: user, skip_visibility_check: true})      |> Enum.all?(fn        {key, url} when key in [:avatar, :avatar_static, :header, :header_static] ->          String.starts_with?(url, Pleroma.Web.base_url()) diff --git a/test/web/mastodon_api/views/status_view_test.exs b/test/web/mastodon_api/views/status_view_test.exs index fa26b3129..8703d5ba7 100644 --- a/test/web/mastodon_api/views/status_view_test.exs +++ b/test/web/mastodon_api/views/status_view_test.exs @@ -56,6 +56,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do             ]    end +  test "works correctly with badly formatted emojis" do +    user = insert(:user) +    {:ok, activity} = CommonAPI.post(user, %{status: "yo"}) + +    activity +    |> Object.normalize(false) +    |> Object.update_data(%{"reactions" => %{"☕" => [user.ap_id], "x" => 1}}) + +    activity = Activity.get_by_id(activity.id) + +    status = StatusView.render("show.json", activity: activity, for: user) + +    assert status[:pleroma][:emoji_reactions] == [ +             %{name: "☕", count: 1, me: true} +           ] +  end +    test "loads and returns the direct conversation id when given the `with_direct_conversation_id` option" do      user = insert(:user) @@ -177,7 +194,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do        id: to_string(note.id),        uri: object_data["id"],        url: Pleroma.Web.Router.Helpers.o_status_url(Pleroma.Web.Endpoint, :notice, note), -      account: AccountView.render("show.json", %{user: user}), +      account: AccountView.render("show.json", %{user: user, skip_visibility_check: true}),        in_reply_to_id: nil,        in_reply_to_account_id: nil,        card: nil, diff --git a/test/web/media_proxy/media_proxy_controller_test.exs b/test/web/media_proxy/media_proxy_controller_test.exs index d61cef83b..d4db44c63 100644 --- a/test/web/media_proxy/media_proxy_controller_test.exs +++ b/test/web/media_proxy/media_proxy_controller_test.exs @@ -4,82 +4,118 @@  defmodule Pleroma.Web.MediaProxy.MediaProxyControllerTest do    use Pleroma.Web.ConnCase +    import Mock -  alias Pleroma.Config -  setup do: clear_config(:media_proxy) -  setup do: clear_config([Pleroma.Web.Endpoint, :secret_key_base]) +  alias Pleroma.Web.MediaProxy +  alias Pleroma.Web.MediaProxy.MediaProxyController +  alias Plug.Conn    setup do      on_exit(fn -> Cachex.clear(:banned_urls_cache) end)    end    test "it returns 404 when MediaProxy disabled", %{conn: conn} do -    Config.put([:media_proxy, :enabled], false) +    clear_config([:media_proxy, :enabled], false) -    assert %Plug.Conn{ +    assert %Conn{               status: 404,               resp_body: "Not Found"             } = get(conn, "/proxy/hhgfh/eeeee") -    assert %Plug.Conn{ +    assert %Conn{               status: 404,               resp_body: "Not Found"             } = get(conn, "/proxy/hhgfh/eeee/fff")    end -  test "it returns 403 when signature invalidated", %{conn: conn} do -    Config.put([:media_proxy, :enabled], true) -    Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000") -    path = URI.parse(Pleroma.Web.MediaProxy.encode_url("https://google.fn")).path -    Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000") - -    assert %Plug.Conn{ -             status: 403, -             resp_body: "Forbidden" -           } = get(conn, path) - -    assert %Plug.Conn{ -             status: 403, -             resp_body: "Forbidden" -           } = get(conn, "/proxy/hhgfh/eeee") - -    assert %Plug.Conn{ -             status: 403, -             resp_body: "Forbidden" -           } = get(conn, "/proxy/hhgfh/eeee/fff") -  end +  describe "" do +    setup do +      clear_config([:media_proxy, :enabled], true) +      clear_config([Pleroma.Web.Endpoint, :secret_key_base], "00000000000") +      [url: MediaProxy.encode_url("https://google.fn/test.png")] +    end -  test "redirects on valid url when filename invalidated", %{conn: conn} do -    Config.put([:media_proxy, :enabled], true) -    Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000") -    url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png") -    invalid_url = String.replace(url, "test.png", "test-file.png") -    response = get(conn, invalid_url) -    assert response.status == 302 -    assert redirected_to(response) == url -  end +    test "it returns 403 for invalid signature", %{conn: conn, url: url} do +      Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], "000") +      %{path: path} = URI.parse(url) + +      assert %Conn{ +               status: 403, +               resp_body: "Forbidden" +             } = get(conn, path) + +      assert %Conn{ +               status: 403, +               resp_body: "Forbidden" +             } = get(conn, "/proxy/hhgfh/eeee") + +      assert %Conn{ +               status: 403, +               resp_body: "Forbidden" +             } = get(conn, "/proxy/hhgfh/eeee/fff") +    end -  test "it performs ReverseProxy.call when signature valid", %{conn: conn} do -    Config.put([:media_proxy, :enabled], true) -    Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000") -    url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png") +    test "redirects on valid url when filename is invalidated", %{conn: conn, url: url} do +      invalid_url = String.replace(url, "test.png", "test-file.png") +      response = get(conn, invalid_url) +      assert response.status == 302 +      assert redirected_to(response) == url +    end -    with_mock Pleroma.ReverseProxy, -      call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do -      assert %Plug.Conn{status: :success} = get(conn, url) +    test "it performs ReverseProxy.call with valid signature", %{conn: conn, url: url} do +      with_mock Pleroma.ReverseProxy, +        call: fn _conn, _url, _opts -> %Conn{status: :success} end do +        assert %Conn{status: :success} = get(conn, url) +      end +    end + +    test "it returns 404 when url is in banned_urls cache", %{conn: conn, url: url} do +      MediaProxy.put_in_banned_urls("https://google.fn/test.png") + +      with_mock Pleroma.ReverseProxy, +        call: fn _conn, _url, _opts -> %Conn{status: :success} end do +        assert %Conn{status: 404, resp_body: "Not Found"} = get(conn, url) +      end      end    end -  test "it returns 404 when url contains in banned_urls cache", %{conn: conn} do -    Config.put([:media_proxy, :enabled], true) -    Config.put([Pleroma.Web.Endpoint, :secret_key_base], "00000000000") -    url = Pleroma.Web.MediaProxy.encode_url("https://google.fn/test.png") -    Pleroma.Web.MediaProxy.put_in_banned_urls("https://google.fn/test.png") +  describe "filename_matches/3" do +    test "preserves the encoded or decoded path" do +      assert MediaProxyController.filename_matches( +               %{"filename" => "/Hello world.jpg"}, +               "/Hello world.jpg", +               "http://pleroma.social/Hello world.jpg" +             ) == :ok + +      assert MediaProxyController.filename_matches( +               %{"filename" => "/Hello%20world.jpg"}, +               "/Hello%20world.jpg", +               "http://pleroma.social/Hello%20world.jpg" +             ) == :ok + +      assert MediaProxyController.filename_matches( +               %{"filename" => "/my%2Flong%2Furl%2F2019%2F07%2FS.jpg"}, +               "/my%2Flong%2Furl%2F2019%2F07%2FS.jpg", +               "http://pleroma.social/my%2Flong%2Furl%2F2019%2F07%2FS.jpg" +             ) == :ok + +      assert MediaProxyController.filename_matches( +               %{"filename" => "/my%2Flong%2Furl%2F2019%2F07%2FS.jp"}, +               "/my%2Flong%2Furl%2F2019%2F07%2FS.jp", +               "http://pleroma.social/my%2Flong%2Furl%2F2019%2F07%2FS.jpg" +             ) == {:wrong_filename, "my%2Flong%2Furl%2F2019%2F07%2FS.jpg"} +    end + +    test "encoded url are tried to match for proxy as `conn.request_path` encodes the url" do +      # conn.request_path will return encoded url +      request_path = "/ANALYSE-DAI-_-LE-STABLECOIN-100-D%C3%89CENTRALIS%C3%89-BQ.jpg" -    with_mock Pleroma.ReverseProxy, -      call: fn _conn, _url, _opts -> %Plug.Conn{status: :success} end do -      assert %Plug.Conn{status: 404, resp_body: "Not Found"} = get(conn, url) +      assert MediaProxyController.filename_matches( +               true, +               request_path, +               "https://mydomain.com/uploads/2019/07/ANALYSE-DAI-_-LE-STABLECOIN-100-DÉCENTRALISÉ-BQ.jpg" +             ) == :ok      end    end  end diff --git a/test/web/media_proxy/media_proxy_test.exs b/test/web/media_proxy/media_proxy_test.exs index 69d2a71a6..72885cfdd 100644 --- a/test/web/media_proxy/media_proxy_test.exs +++ b/test/web/media_proxy/media_proxy_test.exs @@ -5,38 +5,33 @@  defmodule Pleroma.Web.MediaProxyTest do    use ExUnit.Case    use Pleroma.Tests.Helpers -  import Pleroma.Web.MediaProxy -  alias Pleroma.Web.MediaProxy.MediaProxyController -  setup do: clear_config([:media_proxy, :enabled]) -  setup do: clear_config(Pleroma.Upload) +  alias Pleroma.Web.Endpoint +  alias Pleroma.Web.MediaProxy    describe "when enabled" do -    setup do -      Pleroma.Config.put([:media_proxy, :enabled], true) -      :ok -    end +    setup do: clear_config([:media_proxy, :enabled], true)      test "ignores invalid url" do -      assert url(nil) == nil -      assert url("") == nil +      assert MediaProxy.url(nil) == nil +      assert MediaProxy.url("") == nil      end      test "ignores relative url" do -      assert url("/local") == "/local" -      assert url("/") == "/" +      assert MediaProxy.url("/local") == "/local" +      assert MediaProxy.url("/") == "/"      end      test "ignores local url" do -      local_url = Pleroma.Web.Endpoint.url() <> "/hello" -      local_root = Pleroma.Web.Endpoint.url() -      assert url(local_url) == local_url -      assert url(local_root) == local_root +      local_url = Endpoint.url() <> "/hello" +      local_root = Endpoint.url() +      assert MediaProxy.url(local_url) == local_url +      assert MediaProxy.url(local_root) == local_root      end      test "encodes and decodes URL" do        url = "https://pleroma.soykaf.com/static/logo.png" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert String.starts_with?(                 encoded, @@ -50,86 +45,44 @@ defmodule Pleroma.Web.MediaProxyTest do      test "encodes and decodes URL without a path" do        url = "https://pleroma.soykaf.com" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert decode_result(encoded) == url      end      test "encodes and decodes URL without an extension" do        url = "https://pleroma.soykaf.com/path/" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert String.ends_with?(encoded, "/path")        assert decode_result(encoded) == url      end      test "encodes and decodes URL and ignores query params for the path" do        url = "https://pleroma.soykaf.com/static/logo.png?93939393939&bunny=true" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert String.ends_with?(encoded, "/logo.png")        assert decode_result(encoded) == url      end      test "validates signature" do -      secret_key_base = Pleroma.Config.get([Pleroma.Web.Endpoint, :secret_key_base]) - -      on_exit(fn -> -        Pleroma.Config.put([Pleroma.Web.Endpoint, :secret_key_base], secret_key_base) -      end) - -      encoded = url("https://pleroma.social") +      encoded = MediaProxy.url("https://pleroma.social") -      Pleroma.Config.put( -        [Pleroma.Web.Endpoint, :secret_key_base], +      clear_config( +        [Endpoint, :secret_key_base],          "00000000000000000000000000000000000000000000000"        )        [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/") -      assert decode_url(sig, base64) == {:error, :invalid_signature} -    end - -    test "filename_matches preserves the encoded or decoded path" do -      assert MediaProxyController.filename_matches( -               %{"filename" => "/Hello world.jpg"}, -               "/Hello world.jpg", -               "http://pleroma.social/Hello world.jpg" -             ) == :ok - -      assert MediaProxyController.filename_matches( -               %{"filename" => "/Hello%20world.jpg"}, -               "/Hello%20world.jpg", -               "http://pleroma.social/Hello%20world.jpg" -             ) == :ok - -      assert MediaProxyController.filename_matches( -               %{"filename" => "/my%2Flong%2Furl%2F2019%2F07%2FS.jpg"}, -               "/my%2Flong%2Furl%2F2019%2F07%2FS.jpg", -               "http://pleroma.social/my%2Flong%2Furl%2F2019%2F07%2FS.jpg" -             ) == :ok - -      assert MediaProxyController.filename_matches( -               %{"filename" => "/my%2Flong%2Furl%2F2019%2F07%2FS.jp"}, -               "/my%2Flong%2Furl%2F2019%2F07%2FS.jp", -               "http://pleroma.social/my%2Flong%2Furl%2F2019%2F07%2FS.jpg" -             ) == {:wrong_filename, "my%2Flong%2Furl%2F2019%2F07%2FS.jpg"} -    end - -    test "encoded url are tried to match for proxy as `conn.request_path` encodes the url" do -      # conn.request_path will return encoded url -      request_path = "/ANALYSE-DAI-_-LE-STABLECOIN-100-D%C3%89CENTRALIS%C3%89-BQ.jpg" - -      assert MediaProxyController.filename_matches( -               true, -               request_path, -               "https://mydomain.com/uploads/2019/07/ANALYSE-DAI-_-LE-STABLECOIN-100-DÉCENTRALISÉ-BQ.jpg" -             ) == :ok +      assert MediaProxy.decode_url(sig, base64) == {:error, :invalid_signature}      end      test "uses the configured base_url" do -      clear_config([:media_proxy, :base_url], "https://cache.pleroma.social") +      base_url = "https://cache.pleroma.social" +      clear_config([:media_proxy, :base_url], base_url)        url = "https://pleroma.soykaf.com/static/logo.png" -      encoded = url(url) +      encoded = MediaProxy.url(url) -      assert String.starts_with?(encoded, Pleroma.Config.get([:media_proxy, :base_url])) +      assert String.starts_with?(encoded, base_url)      end      # Some sites expect ASCII encoded characters in the URL to be preserved even if @@ -140,7 +93,7 @@ defmodule Pleroma.Web.MediaProxyTest do        url =          "https://pleroma.com/%20/%21/%22/%23/%24/%25/%26/%27/%28/%29/%2A/%2B/%2C/%2D/%2E/%2F/%30/%31/%32/%33/%34/%35/%36/%37/%38/%39/%3A/%3B/%3C/%3D/%3E/%3F/%40/%41/%42/%43/%44/%45/%46/%47/%48/%49/%4A/%4B/%4C/%4D/%4E/%4F/%50/%51/%52/%53/%54/%55/%56/%57/%58/%59/%5A/%5B/%5C/%5D/%5E/%5F/%60/%61/%62/%63/%64/%65/%66/%67/%68/%69/%6A/%6B/%6C/%6D/%6E/%6F/%70/%71/%72/%73/%74/%75/%76/%77/%78/%79/%7A/%7B/%7C/%7D/%7E/%7F/%80/%81/%82/%83/%84/%85/%86/%87/%88/%89/%8A/%8B/%8C/%8D/%8E/%8F/%90/%91/%92/%93/%94/%95/%96/%97/%98/%99/%9A/%9B/%9C/%9D/%9E/%9F/%C2%A0/%A1/%A2/%A3/%A4/%A5/%A6/%A7/%A8/%A9/%AA/%AB/%AC/%C2%AD/%AE/%AF/%B0/%B1/%B2/%B3/%B4/%B5/%B6/%B7/%B8/%B9/%BA/%BB/%BC/%BD/%BE/%BF/%C0/%C1/%C2/%C3/%C4/%C5/%C6/%C7/%C8/%C9/%CA/%CB/%CC/%CD/%CE/%CF/%D0/%D1/%D2/%D3/%D4/%D5/%D6/%D7/%D8/%D9/%DA/%DB/%DC/%DD/%DE/%DF/%E0/%E1/%E2/%E3/%E4/%E5/%E6/%E7/%E8/%E9/%EA/%EB/%EC/%ED/%EE/%EF/%F0/%F1/%F2/%F3/%F4/%F5/%F6/%F7/%F8/%F9/%FA/%FB/%FC/%FD/%FE/%FF" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert decode_result(encoded) == url      end @@ -151,56 +104,49 @@ defmodule Pleroma.Web.MediaProxyTest do        url =          "https://pleroma.com/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-._~:/?#[]@!$&'()*+,;=|^`{}" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert decode_result(encoded) == url      end      test "preserve unicode characters" do        url = "https://ko.wikipedia.org/wiki/위키백과:대문" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert decode_result(encoded) == url      end    end    describe "when disabled" do -    setup do -      enabled = Pleroma.Config.get([:media_proxy, :enabled]) - -      if enabled do -        Pleroma.Config.put([:media_proxy, :enabled], false) - -        on_exit(fn -> -          Pleroma.Config.put([:media_proxy, :enabled], enabled) -          :ok -        end) -      end - -      :ok -    end +    setup do: clear_config([:media_proxy, :enabled], false)      test "does not encode remote urls" do -      assert url("https://google.fr") == "https://google.fr" +      assert MediaProxy.url("https://google.fr") == "https://google.fr"      end    end    defp decode_result(encoded) do      [_, "proxy", sig, base64 | _] = URI.parse(encoded).path |> String.split("/") -    {:ok, decoded} = decode_url(sig, base64) +    {:ok, decoded} = MediaProxy.decode_url(sig, base64)      decoded    end    describe "whitelist" do -    setup do -      Pleroma.Config.put([:media_proxy, :enabled], true) -      :ok -    end +    setup do: clear_config([:media_proxy, :enabled], true)      test "mediaproxy whitelist" do -      Pleroma.Config.put([:media_proxy, :whitelist], ["google.com", "feld.me"]) +      clear_config([:media_proxy, :whitelist], ["https://google.com", "https://feld.me"]) +      url = "https://feld.me/foo.png" + +      unencoded = MediaProxy.url(url) +      assert unencoded == url +    end + +    # TODO: delete after removing support bare domains for media proxy whitelist +    test "mediaproxy whitelist bare domains whitelist (deprecated)" do +      clear_config([:media_proxy, :whitelist], ["google.com", "feld.me"])        url = "https://feld.me/foo.png" -      unencoded = url(url) +      unencoded = MediaProxy.url(url)        assert unencoded == url      end @@ -211,17 +157,17 @@ defmodule Pleroma.Web.MediaProxyTest do        media_url = "https://mycdn.akamai.com"        url = "#{media_url}/static/logo.png" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert String.starts_with?(encoded, media_url)      end      test "ensure Pleroma.Upload base_url is always whitelisted" do        media_url = "https://media.pleroma.social" -      Pleroma.Config.put([Pleroma.Upload, :base_url], media_url) +      clear_config([Pleroma.Upload, :base_url], media_url)        url = "#{media_url}/static/logo.png" -      encoded = url(url) +      encoded = MediaProxy.url(url)        assert String.starts_with?(encoded, media_url)      end diff --git a/test/web/oauth/app_test.exs b/test/web/oauth/app_test.exs index 899af648e..993a490e0 100644 --- a/test/web/oauth/app_test.exs +++ b/test/web/oauth/app_test.exs @@ -29,5 +29,16 @@ defmodule Pleroma.Web.OAuth.AppTest do        assert exist_app.id == app.id        assert exist_app.scopes == ["read", "write", "follow", "push"]      end + +    test "has unique client_id" do +      insert(:oauth_app, client_name: "", redirect_uris: "", client_id: "boop") + +      error = +        catch_error(insert(:oauth_app, client_name: "", redirect_uris: "", client_id: "boop")) + +      assert %Ecto.ConstraintError{} = error +      assert error.constraint == "apps_client_id_index" +      assert error.type == :unique +    end    end  end diff --git a/test/web/oauth/oauth_controller_test.exs b/test/web/oauth/oauth_controller_test.exs index d389e4ce0..1200126b8 100644 --- a/test/web/oauth/oauth_controller_test.exs +++ b/test/web/oauth/oauth_controller_test.exs @@ -19,7 +19,10 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do      key: "_test",      signing_salt: "cooldude"    ] -  setup do: clear_config([:instance, :account_activation_required]) +  setup do +    clear_config([:instance, :account_activation_required]) +    clear_config([:instance, :account_approval_required]) +  end    describe "in OAuth consumer mode, " do      setup do @@ -995,6 +998,30 @@ defmodule Pleroma.Web.OAuth.OAuthControllerTest do               }      end +    test "rejects token exchange for valid credentials belonging to an unapproved user" do +      password = "testpassword" + +      user = insert(:user, password_hash: Pbkdf2.hash_pwd_salt(password), approval_pending: true) + +      refute Pleroma.User.account_status(user) == :active + +      app = insert(:oauth_app) + +      conn = +        build_conn() +        |> post("/oauth/token", %{ +          "grant_type" => "password", +          "username" => user.nickname, +          "password" => password, +          "client_id" => app.client_id, +          "client_secret" => app.client_secret +        }) + +      assert resp = json_response(conn, 403) +      assert %{"error" => _} = resp +      refute Map.has_key?(resp, "access_token") +    end +      test "rejects an invalid authorization code" do        app = insert(:oauth_app) diff --git a/test/web/pleroma_api/controllers/chat_controller_test.exs b/test/web/pleroma_api/controllers/chat_controller_test.exs index 82e16741d..d71e80d03 100644 --- a/test/web/pleroma_api/controllers/chat_controller_test.exs +++ b/test/web/pleroma_api/controllers/chat_controller_test.exs @@ -332,5 +332,27 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do                 chat_1.id |> to_string()               ]      end + +    test "it is not affected by :restrict_unauthenticated setting (issue #1973)", %{ +      conn: conn, +      user: user +    } do +      clear_config([:restrict_unauthenticated, :profiles, :local], true) +      clear_config([:restrict_unauthenticated, :profiles, :remote], true) + +      user2 = insert(:user) +      user3 = insert(:user, local: false) + +      {:ok, _chat_12} = Chat.get_or_create(user.id, user2.ap_id) +      {:ok, _chat_13} = Chat.get_or_create(user.id, user3.ap_id) + +      result = +        conn +        |> get("/api/v1/pleroma/chats") +        |> json_response_and_validate_schema(200) + +      account_ids = Enum.map(result, &get_in(&1, ["account", "id"])) +      assert Enum.sort(account_ids) == Enum.sort([user2.id, user3.id]) +    end    end  end diff --git a/test/web/pleroma_api/controllers/emoji_pack_controller_test.exs b/test/web/pleroma_api/controllers/emoji_pack_controller_test.exs index df58a5eb6..e113bb15f 100644 --- a/test/web/pleroma_api/controllers/emoji_pack_controller_test.exs +++ b/test/web/pleroma_api/controllers/emoji_pack_controller_test.exs @@ -14,6 +14,8 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do                )    setup do: clear_config([:auth, :enforce_oauth_admin_scope_usage], false) +  setup do: clear_config([:instance, :public], true) +    setup do      admin = insert(:user, is_admin: true)      token = insert(:oauth_admin_token, user: admin) @@ -27,6 +29,11 @@ defmodule Pleroma.Web.PleromaAPI.EmojiPackControllerTest do      {:ok, %{admin_conn: admin_conn}}    end +  test "GET /api/pleroma/emoji/packs when :public: false", %{conn: conn} do +    Config.put([:instance, :public], false) +    conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200) +  end +    test "GET /api/pleroma/emoji/packs", %{conn: conn} do      resp = conn |> get("/api/pleroma/emoji/packs") |> json_response_and_validate_schema(200) diff --git a/test/web/pleroma_api/controllers/emoji_reaction_controller_test.exs b/test/web/pleroma_api/controllers/emoji_reaction_controller_test.exs index e1bb5ebfe..3deab30d1 100644 --- a/test/web/pleroma_api/controllers/emoji_reaction_controller_test.exs +++ b/test/web/pleroma_api/controllers/emoji_reaction_controller_test.exs @@ -106,6 +106,23 @@ defmodule Pleroma.Web.PleromaAPI.EmojiReactionControllerTest do               result    end +  test "GET /api/v1/pleroma/statuses/:id/reactions with :show_reactions disabled", %{conn: conn} do +    clear_config([:instance, :show_reactions], false) + +    user = insert(:user) +    other_user = insert(:user) + +    {:ok, activity} = CommonAPI.post(user, %{status: "#cofe"}) +    {:ok, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅") + +    result = +      conn +      |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions") +      |> json_response_and_validate_schema(200) + +    assert result == [] +  end +    test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do      user = insert(:user)      other_user = insert(:user) diff --git a/test/web/pleroma_api/views/chat/message_reference_view_test.exs b/test/web/pleroma_api/views/chat/message_reference_view_test.exs index e5b165255..40dbae3cd 100644 --- a/test/web/pleroma_api/views/chat/message_reference_view_test.exs +++ b/test/web/pleroma_api/views/chat/message_reference_view_test.exs @@ -43,7 +43,17 @@ defmodule Pleroma.Web.PleromaAPI.Chat.MessageReferenceViewTest do      assert chat_message[:unread] == false      assert match?([%{shortcode: "firefox"}], chat_message[:emojis]) -    {:ok, activity} = CommonAPI.post_chat_message(recipient, user, "gkgkgk", media_id: upload.id) +    clear_config([:rich_media, :enabled], true) + +    Tesla.Mock.mock(fn +      %{url: "https://example.com/ogp"} -> +        %Tesla.Env{status: 200, body: File.read!("test/fixtures/rich_media/ogp.html")} +    end) + +    {:ok, activity} = +      CommonAPI.post_chat_message(recipient, user, "gkgkgk https://example.com/ogp", +        media_id: upload.id +      )      object = Object.normalize(activity) @@ -52,10 +62,11 @@ defmodule Pleroma.Web.PleromaAPI.Chat.MessageReferenceViewTest do      chat_message_two = MessageReferenceView.render("show.json", chat_message_reference: cm_ref)      assert chat_message_two[:id] == cm_ref.id -    assert chat_message_two[:content] == "gkgkgk" +    assert chat_message_two[:content] == object.data["content"]      assert chat_message_two[:account_id] == recipient.id      assert chat_message_two[:chat_id] == chat_message[:chat_id]      assert chat_message_two[:attachment]      assert chat_message_two[:unread] == true +    assert chat_message_two[:card]    end  end diff --git a/test/web/pleroma_api/views/chat_view_test.exs b/test/web/pleroma_api/views/chat_view_test.exs index 14eecb1bd..02484b705 100644 --- a/test/web/pleroma_api/views/chat_view_test.exs +++ b/test/web/pleroma_api/views/chat_view_test.exs @@ -26,7 +26,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatViewTest do      assert represented_chat == %{               id: "#{chat.id}", -             account: AccountView.render("show.json", user: recipient), +             account: +               AccountView.render("show.json", user: recipient, skip_visibility_check: true),               unread: 0,               last_message: nil,               updated_at: Utils.to_masto_date(chat.updated_at) diff --git a/test/web/push/impl_test.exs b/test/web/push/impl_test.exs index b48952b29..aeb5c1fbd 100644 --- a/test/web/push/impl_test.exs +++ b/test/web/push/impl_test.exs @@ -238,9 +238,11 @@ defmodule Pleroma.Web.Push.ImplTest do               }      end -    test "hides details for notifications when privacy option enabled" do +    test "hides contents of notifications when option enabled" do        user = insert(:user, nickname: "Bob") -      user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: true}) + +      user2 = +        insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})        {:ok, activity} =          CommonAPI.post(user, %{ @@ -284,9 +286,11 @@ defmodule Pleroma.Web.Push.ImplTest do               }      end -    test "returns regular content for notifications with privacy option disabled" do +    test "returns regular content when hiding contents option disabled" do        user = insert(:user, nickname: "Bob") -      user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: false}) + +      user2 = +        insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})        {:ok, activity} =          CommonAPI.post(user, %{ diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 368533292..20a45cb6f 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -4,11 +4,11 @@  defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do    use Pleroma.DataCase +  import Pleroma.Factory    alias Pleroma.Repo    alias Pleroma.Tests.ObanHelpers    alias Pleroma.User    alias Pleroma.UserInviteToken -  alias Pleroma.Web.MastodonAPI.AccountView    alias Pleroma.Web.TwitterAPI.TwitterAPI    setup_all do @@ -27,13 +27,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do      {:ok, user} = TwitterAPI.register_user(data) -    fetched_user = User.get_cached_by_nickname("lain") - -    assert AccountView.render("show.json", %{user: user}) == -             AccountView.render("show.json", %{user: fetched_user}) +    assert user == User.get_cached_by_nickname("lain")    end -  test "it registers a new user with empty string in bio and returns the user." do +  test "it registers a new user with empty string in bio and returns the user" do      data = %{        :username => "lain",        :email => "lain@wired.jp", @@ -45,10 +42,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do      {:ok, user} = TwitterAPI.register_user(data) -    fetched_user = User.get_cached_by_nickname("lain") - -    assert AccountView.render("show.json", %{user: user}) == -             AccountView.render("show.json", %{user: fetched_user}) +    assert user == User.get_cached_by_nickname("lain")    end    test "it sends confirmation email if :account_activation_required is specified in instance config" do @@ -85,6 +79,42 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do      )    end +  test "it sends an admin email if :account_approval_required is specified in instance config" do +    admin = insert(:user, is_admin: true) +    setting = Pleroma.Config.get([:instance, :account_approval_required]) + +    unless setting do +      Pleroma.Config.put([:instance, :account_approval_required], true) +      on_exit(fn -> Pleroma.Config.put([:instance, :account_approval_required], setting) end) +    end + +    data = %{ +      :username => "lain", +      :email => "lain@wired.jp", +      :fullname => "lain iwakura", +      :bio => "", +      :password => "bear", +      :confirm => "bear", +      :reason => "I love anime" +    } + +    {:ok, user} = TwitterAPI.register_user(data) +    ObanHelpers.perform_all() + +    assert user.approval_pending + +    email = Pleroma.Emails.AdminEmail.new_unapproved_registration(admin, user) + +    notify_email = Pleroma.Config.get([:instance, :notify_email]) +    instance_name = Pleroma.Config.get([:instance, :name]) + +    Swoosh.TestAssertions.assert_email_sent( +      from: {instance_name, notify_email}, +      to: {admin.name, admin.email}, +      html_body: email.html_body +    ) +  end +    test "it registers a new user and parses mentions in the bio" do      data1 = %{        :username => "john", @@ -134,13 +164,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do        {:ok, user} = TwitterAPI.register_user(data) -      fetched_user = User.get_cached_by_nickname("vinny") -      invite = Repo.get_by(UserInviteToken, token: invite.token) +      assert user == User.get_cached_by_nickname("vinny") +      invite = Repo.get_by(UserInviteToken, token: invite.token)        assert invite.used == true - -      assert AccountView.render("show.json", %{user: user}) == -               AccountView.render("show.json", %{user: fetched_user})      end      test "returns error on invalid token" do @@ -197,10 +224,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do        check_fn = fn invite ->          data = Map.put(data, :token, invite.token)          {:ok, user} = TwitterAPI.register_user(data) -        fetched_user = User.get_cached_by_nickname("vinny") -        assert AccountView.render("show.json", %{user: user}) == -                 AccountView.render("show.json", %{user: fetched_user}) +        assert user == User.get_cached_by_nickname("vinny")        end        {:ok, data: data, check_fn: check_fn} @@ -260,14 +285,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do        }        {:ok, user} = TwitterAPI.register_user(data) -      fetched_user = User.get_cached_by_nickname("vinny") -      invite = Repo.get_by(UserInviteToken, token: invite.token) +      assert user == User.get_cached_by_nickname("vinny") +      invite = Repo.get_by(UserInviteToken, token: invite.token)        assert invite.used == true -      assert AccountView.render("show.json", %{user: user}) == -               AccountView.render("show.json", %{user: fetched_user}) -        data = %{          :username => "GrimReaper",          :email => "death@reapers.afterlife", @@ -302,13 +324,10 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do        }        {:ok, user} = TwitterAPI.register_user(data) -      fetched_user = User.get_cached_by_nickname("vinny") -      invite = Repo.get_by(UserInviteToken, token: invite.token) +      assert user == User.get_cached_by_nickname("vinny") +      invite = Repo.get_by(UserInviteToken, token: invite.token)        refute invite.used - -      assert AccountView.render("show.json", %{user: user}) == -               AccountView.render("show.json", %{user: fetched_user})      end      test "error after max uses" do @@ -327,13 +346,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do        }        {:ok, user} = TwitterAPI.register_user(data) -      fetched_user = User.get_cached_by_nickname("vinny") +      assert user == User.get_cached_by_nickname("vinny") +        invite = Repo.get_by(UserInviteToken, token: invite.token)        assert invite.used == true -      assert AccountView.render("show.json", %{user: user}) == -               AccountView.render("show.json", %{user: fetched_user}) -        data = %{          :username => "GrimReaper",          :email => "death@reapers.afterlife", diff --git a/test/web/twitter_api/util_controller_test.exs b/test/web/twitter_api/util_controller_test.exs index 76e9369f7..109c1e637 100644 --- a/test/web/twitter_api/util_controller_test.exs +++ b/test/web/twitter_api/util_controller_test.exs @@ -191,7 +191,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do      test "it updates notification settings", %{user: user, conn: conn} do        conn        |> put("/api/pleroma/notification_settings", %{ -        "followers" => false, +        "block_from_strangers" => true,          "bar" => 1        })        |> json_response(:ok) @@ -199,27 +199,21 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do        user = refresh_record(user)        assert %Pleroma.User.NotificationSetting{ -               followers: false, -               follows: true, -               non_follows: true, -               non_followers: true, -               privacy_option: false +               block_from_strangers: true, +               hide_notification_contents: false               } == user.notification_settings      end -    test "it updates notification privacy option", %{user: user, conn: conn} do +    test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do        conn -      |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"}) +      |> put("/api/pleroma/notification_settings", %{"hide_notification_contents" => "1"})        |> json_response(:ok)        user = refresh_record(user)        assert %Pleroma.User.NotificationSetting{ -               followers: true, -               follows: true, -               non_follows: true, -               non_followers: true, -               privacy_option: true +               block_from_strangers: false, +               hide_notification_contents: true               } == user.notification_settings      end    end  | 
