diff options
| author | rinpatch <rinpatch@sdf.org> | 2019-04-17 12:22:32 +0300 | 
|---|---|---|
| committer | rinpatch <rinpatch@sdf.org> | 2019-04-17 12:22:32 +0300 | 
| commit | 627e5a0a4992cc19fc65a7e93a09c470c8e2bf33 (patch) | |
| tree | 0f38b475e8554863a1cbbd7750c19d4cd1336eb1 /test/web/common_api | |
| parent | d6ab701a14f7c9fb4d59953648c425e04725fc62 (diff) | |
| parent | 73df3046e014ae16e03f16a9c82921652cefcb54 (diff) | |
| download | pleroma-627e5a0a4992cc19fc65a7e93a09c470c8e2bf33.tar.gz pleroma-627e5a0a4992cc19fc65a7e93a09c470c8e2bf33.zip | |
Merge branch 'develop' into feature/database-compaction
Diffstat (limited to 'test/web/common_api')
| -rw-r--r-- | test/web/common_api/common_api_test.exs | 214 | ||||
| -rw-r--r-- | test/web/common_api/common_api_utils_test.exs | 139 | 
2 files changed, 350 insertions, 3 deletions
| diff --git a/test/web/common_api/common_api_test.exs b/test/web/common_api/common_api_test.exs index 3dc5f6f84..b9ed088e4 100644 --- a/test/web/common_api/common_api_test.exs +++ b/test/web/common_api/common_api_test.exs @@ -1,10 +1,34 @@ -defmodule Pleroma.Web.CommonAPI.Test do +# Pleroma: A lightweight social networking server +# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.Web.CommonAPITest do    use Pleroma.DataCase +  alias Pleroma.Activity +  alias Pleroma.User +  alias Pleroma.Object    alias Pleroma.Web.CommonAPI -  alias Pleroma.{User, Object}    import Pleroma.Factory +  test "with the safe_dm_mention option set, it does not mention people beyond the initial tags" do +    har = insert(:user) +    jafnhar = insert(:user) +    tridi = insert(:user) +    option = Pleroma.Config.get([:instance, :safe_dm_mentions]) +    Pleroma.Config.put([:instance, :safe_dm_mentions], true) + +    {:ok, activity} = +      CommonAPI.post(har, %{ +        "status" => "@#{jafnhar.nickname} hey, i never want to see @#{tridi.nickname} again", +        "visibility" => "direct" +      }) + +    refute tridi.ap_id in activity.recipients +    assert jafnhar.ap_id in activity.recipients +    Pleroma.Config.put([:instance, :safe_dm_mentions], option) +  end +    test "it de-duplicates tags" do      user = insert(:user)      {:ok, activity} = CommonAPI.post(user, %{"status" => "#2hu #2HU"}) @@ -14,6 +38,13 @@ defmodule Pleroma.Web.CommonAPI.Test do      assert object.data["tag"] == ["2hu"]    end +  test "it adds emoji in the object" do +    user = insert(:user) +    {:ok, activity} = CommonAPI.post(user, %{"status" => ":moominmamma:"}) + +    assert activity.data["object"]["emoji"]["moominmamma"] +  end +    test "it adds emoji when updating profiles" do      user = insert(:user, %{name: ":karjalanpiirakka:"}) @@ -57,4 +88,183 @@ defmodule Pleroma.Web.CommonAPI.Test do        assert object.data["content"] == "<p><b>2hu</b></p>alert('xss')"      end    end + +  describe "reactions" do +    test "repeating a status" do +      user = insert(:user) +      other_user = insert(:user) + +      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"}) + +      {:ok, %Activity{}, _} = CommonAPI.repeat(activity.id, user) +    end + +    test "favoriting a status" do +      user = insert(:user) +      other_user = insert(:user) + +      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"}) + +      {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user) +    end + +    test "retweeting a status twice returns an error" do +      user = insert(:user) +      other_user = insert(:user) + +      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"}) +      {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user) +      {:error, _} = CommonAPI.repeat(activity.id, user) +    end + +    test "favoriting a status twice returns an error" do +      user = insert(:user) +      other_user = insert(:user) + +      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"}) +      {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user) +      {:error, _} = CommonAPI.favorite(activity.id, user) +    end +  end + +  describe "pinned statuses" do +    setup do +      Pleroma.Config.put([:instance, :max_pinned_statuses], 1) + +      user = insert(:user) +      {:ok, activity} = CommonAPI.post(user, %{"status" => "HI!!!"}) + +      [user: user, activity: activity] +    end + +    test "pin status", %{user: user, activity: activity} do +      assert {:ok, ^activity} = CommonAPI.pin(activity.id, user) + +      id = activity.id +      user = refresh_record(user) + +      assert %User{info: %{pinned_activities: [^id]}} = user +    end + +    test "only self-authored can be pinned", %{activity: activity} do +      user = insert(:user) + +      assert {:error, "Could not pin"} = CommonAPI.pin(activity.id, user) +    end + +    test "max pinned statuses", %{user: user, activity: activity_one} do +      {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"}) + +      assert {:ok, ^activity_one} = CommonAPI.pin(activity_one.id, user) + +      user = refresh_record(user) + +      assert {:error, "You have already pinned the maximum number of statuses"} = +               CommonAPI.pin(activity_two.id, user) +    end + +    test "unpin status", %{user: user, activity: activity} do +      {:ok, activity} = CommonAPI.pin(activity.id, user) + +      user = refresh_record(user) + +      assert {:ok, ^activity} = CommonAPI.unpin(activity.id, user) + +      user = refresh_record(user) + +      assert %User{info: %{pinned_activities: []}} = user +    end + +    test "should unpin when deleting a status", %{user: user, activity: activity} do +      {:ok, activity} = CommonAPI.pin(activity.id, user) + +      user = refresh_record(user) + +      assert {:ok, _} = CommonAPI.delete(activity.id, user) + +      user = refresh_record(user) + +      assert %User{info: %{pinned_activities: []}} = user +    end +  end + +  describe "mute tests" do +    setup do +      user = insert(:user) + +      activity = insert(:note_activity) + +      [user: user, activity: activity] +    end + +    test "add mute", %{user: user, activity: activity} do +      {:ok, _} = CommonAPI.add_mute(user, activity) +      assert CommonAPI.thread_muted?(user, activity) +    end + +    test "remove mute", %{user: user, activity: activity} do +      CommonAPI.add_mute(user, activity) +      {:ok, _} = CommonAPI.remove_mute(user, activity) +      refute CommonAPI.thread_muted?(user, activity) +    end + +    test "check that mutes can't be duplicate", %{user: user, activity: activity} do +      CommonAPI.add_mute(user, activity) +      {:error, _} = CommonAPI.add_mute(user, activity) +    end +  end + +  describe "reports" do +    test "creates a report" do +      reporter = insert(:user) +      target_user = insert(:user) + +      {:ok, activity} = CommonAPI.post(target_user, %{"status" => "foobar"}) + +      reporter_ap_id = reporter.ap_id +      target_ap_id = target_user.ap_id +      activity_ap_id = activity.data["id"] +      comment = "foobar" + +      report_data = %{ +        "account_id" => target_user.id, +        "comment" => comment, +        "status_ids" => [activity.id] +      } + +      assert {:ok, flag_activity} = CommonAPI.report(reporter, report_data) + +      assert %Activity{ +               actor: ^reporter_ap_id, +               data: %{ +                 "type" => "Flag", +                 "content" => ^comment, +                 "object" => [^target_ap_id, ^activity_ap_id] +               } +             } = flag_activity +    end +  end + +  describe "reblog muting" do +    setup do +      muter = insert(:user) + +      muted = insert(:user) + +      [muter: muter, muted: muted] +    end + +    test "add a reblog mute", %{muter: muter, muted: muted} do +      {:ok, muter} = CommonAPI.hide_reblogs(muter, muted) + +      assert Pleroma.User.showing_reblogs?(muter, muted) == false +    end + +    test "remove a reblog mute", %{muter: muter, muted: muted} do +      {:ok, muter} = CommonAPI.hide_reblogs(muter, muted) +      {:ok, muter} = CommonAPI.show_reblogs(muter, muted) + +      assert Pleroma.User.showing_reblogs?(muter, muted) == true +    end +  end  end diff --git a/test/web/common_api/common_api_utils_test.exs b/test/web/common_api/common_api_utils_test.exs index b01ce04f8..f0c59d5c3 100644 --- a/test/web/common_api/common_api_utils_test.exs +++ b/test/web/common_api/common_api_utils_test.exs @@ -1,7 +1,12 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/> +# SPDX-License-Identifier: AGPL-3.0-only +  defmodule Pleroma.Web.CommonAPI.UtilsTest do +  alias Pleroma.Builders.UserBuilder +  alias Pleroma.Object    alias Pleroma.Web.CommonAPI.Utils    alias Pleroma.Web.Endpoint -  alias Pleroma.Builders.{UserBuilder}    use Pleroma.DataCase    test "it adds attachment links to a given text and attachment set" do @@ -52,4 +57,136 @@ defmodule Pleroma.Web.CommonAPI.UtilsTest do      assert expected == Utils.emoji_from_profile(user)    end + +  describe "format_input/3" do +    test "works for bare text/plain" do +      text = "hello world!" +      expected = "hello world!" + +      {output, [], []} = Utils.format_input(text, "text/plain") + +      assert output == expected + +      text = "hello world!\n\nsecond paragraph!" +      expected = "hello world!<br><br>second paragraph!" + +      {output, [], []} = Utils.format_input(text, "text/plain") + +      assert output == expected +    end + +    test "works for bare text/html" do +      text = "<p>hello world!</p>" +      expected = "<p>hello world!</p>" + +      {output, [], []} = Utils.format_input(text, "text/html") + +      assert output == expected + +      text = "<p>hello world!</p>\n\n<p>second paragraph</p>" +      expected = "<p>hello world!</p>\n\n<p>second paragraph</p>" + +      {output, [], []} = Utils.format_input(text, "text/html") + +      assert output == expected +    end + +    test "works for bare text/markdown" do +      text = "**hello world**" +      expected = "<p><strong>hello world</strong></p>\n" + +      {output, [], []} = Utils.format_input(text, "text/markdown") + +      assert output == expected + +      text = "**hello world**\n\n*another paragraph*" +      expected = "<p><strong>hello world</strong></p>\n<p><em>another paragraph</em></p>\n" + +      {output, [], []} = Utils.format_input(text, "text/markdown") + +      assert output == expected + +      text = """ +      > cool quote + +      by someone +      """ + +      expected = "<blockquote><p>cool quote</p>\n</blockquote>\n<p>by someone</p>\n" + +      {output, [], []} = Utils.format_input(text, "text/markdown") + +      assert output == expected +    end + +    test "works for text/markdown with mentions" do +      {:ok, user} = +        UserBuilder.insert(%{nickname: "user__test", ap_id: "http://foo.com/user__test"}) + +      text = "**hello world**\n\n*another @user__test and @user__test google.com paragraph*" + +      expected = +        "<p><strong>hello world</strong></p>\n<p><em>another <span class=\"h-card\"><a data-user=\"#{ +          user.id +        }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> and <span class=\"h-card\"><a data-user=\"#{ +          user.id +        }\" class=\"u-url mention\" href=\"http://foo.com/user__test\">@<span>user__test</span></a></span> <a href=\"http://google.com\">google.com</a> paragraph</em></p>\n" + +      {output, _, _} = Utils.format_input(text, "text/markdown") + +      assert output == expected +    end +  end + +  describe "context_to_conversation_id" do +    test "creates a mapping object" do +      conversation_id = Utils.context_to_conversation_id("random context") +      object = Object.get_by_ap_id("random context") + +      assert conversation_id == object.id +    end + +    test "returns an existing mapping for an existing object" do +      {:ok, object} = Object.context_mapping("random context") |> Repo.insert() +      conversation_id = Utils.context_to_conversation_id("random context") + +      assert conversation_id == object.id +    end +  end + +  describe "formats date to asctime" do +    test "when date is in ISO 8601 format" do +      date = DateTime.utc_now() |> DateTime.to_iso8601() + +      expected = +        date +        |> DateTime.from_iso8601() +        |> elem(1) +        |> Calendar.Strftime.strftime!("%a %b %d %H:%M:%S %z %Y") + +      assert Utils.date_to_asctime(date) == expected +    end + +    test "when date is a binary in wrong format" do +      date = DateTime.utc_now() + +      expected = "" + +      assert Utils.date_to_asctime(date) == expected +    end + +    test "when date is a Unix timestamp" do +      date = DateTime.utc_now() |> DateTime.to_unix() + +      expected = "" + +      assert Utils.date_to_asctime(date) == expected +    end + +    test "when date is nil" do +      expected = "" + +      assert Utils.date_to_asctime(nil) == expected +    end +  end  end | 
