diff options
Diffstat (limited to 'test/web/mastodon_api')
9 files changed, 324 insertions, 133 deletions
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 51cebe567..2d256f63c 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 @@ -82,9 +82,9 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do        assert user_data = json_response(conn, 200)        assert user_data["note"] == -               ~s(I drink <a class="hashtag" data-tag="cofe" href="http://localhost:4001/tag/cofe">#cofe</a> with <span class="h-card"><a data-user="#{ +               ~s(I drink <a class="hashtag" data-tag="cofe" href="http://localhost:4001/tag/cofe">#cofe</a> with <span class="h-card"><a class="u-url mention" data-user="#{                   user2.id -               }" class="u-url mention" href="#{user2.ap_id}" rel="ugc">@<span>#{user2.nickname}</span></a></span><br/><br/>suya..) +               }" href="#{user2.ap_id}" rel="ugc">@<span>#{user2.nickname}</span></a></span><br/><br/>suya..)      end      test "updates the user's locking status", %{conn: conn} do @@ -118,6 +118,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do        assert user_data["pleroma"]["hide_followers"] == true      end +    test "updates the user's discoverable status", %{conn: conn} do +      assert %{"source" => %{"pleroma" => %{"discoverable" => true}}} = +               conn +               |> patch("/api/v1/accounts/update_credentials", %{discoverable: "true"}) +               |> json_response(:ok) + +      assert %{"source" => %{"pleroma" => %{"discoverable" => false}}} = +               conn +               |> patch("/api/v1/accounts/update_credentials", %{discoverable: "false"}) +               |> json_response(:ok) +    end +      test "updates the user's hide_followers_count and hide_follows_count", %{conn: conn} do        conn =          patch(conn, "/api/v1/accounts/update_credentials", %{ @@ -261,7 +273,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do      test "update fields", %{conn: conn} do        fields = [          %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "<script>bar</script>"}, -        %{"name" => "link", "value" => "cofe.io"} +        %{"name" => "link.io", "value" => "cofe.io"}        ]        account_data = @@ -271,7 +283,10 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do        assert account_data["fields"] == [                 %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "bar"}, -               %{"name" => "link", "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>)} +               %{ +                 "name" => "link.io", +                 "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>) +               }               ]        assert account_data["source"]["fields"] == [ @@ -279,14 +294,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do                   "name" => "<a href=\"http://google.com\">foo</a>",                   "value" => "<script>bar</script>"                 }, -               %{"name" => "link", "value" => "cofe.io"} +               %{"name" => "link.io", "value" => "cofe.io"}               ] +    end +    test "update fields via x-www-form-urlencoded", %{conn: conn} do        fields =          [            "fields_attributes[1][name]=link", -          "fields_attributes[1][value]=cofe.io", -          "fields_attributes[0][name]=<a href=\"http://google.com\">foo</a>", +          "fields_attributes[1][value]=http://cofe.io", +          "fields_attributes[0][name]=foo",            "fields_attributes[0][value]=bar"          ]          |> Enum.join("&") @@ -298,32 +315,49 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do          |> json_response(200)        assert account["fields"] == [ -               %{"name" => "<a href=\"http://google.com\">foo</a>", "value" => "bar"}, -               %{"name" => "link", "value" => ~S(<a href="http://cofe.io" rel="ugc">cofe.io</a>)} +               %{"name" => "foo", "value" => "bar"}, +               %{ +                 "name" => "link", +                 "value" => ~S(<a href="http://cofe.io" rel="ugc">http://cofe.io</a>) +               }               ]        assert account["source"]["fields"] == [ -               %{ -                 "name" => "<a href=\"http://google.com\">foo</a>", -                 "value" => "bar" -               }, -               %{"name" => "link", "value" => "cofe.io"} +               %{"name" => "foo", "value" => "bar"}, +               %{"name" => "link", "value" => "http://cofe.io"} +             ] +    end + +    test "update fields with empty name", %{conn: conn} do +      fields = [ +        %{"name" => "foo", "value" => ""}, +        %{"name" => "", "value" => "bar"} +      ] + +      account = +        conn +        |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields}) +        |> json_response(200) + +      assert account["fields"] == [ +               %{"name" => "foo", "value" => ""}               ] +    end +    test "update fields when invalid request", %{conn: conn} do        name_limit = Pleroma.Config.get([:instance, :account_field_name_length])        value_limit = Pleroma.Config.get([:instance, :account_field_value_length]) +      long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join()        long_value = Enum.map(0..value_limit, fn _ -> "x" end) |> Enum.join() -      fields = [%{"name" => "<b>foo<b>", "value" => long_value}] +      fields = [%{"name" => "foo", "value" => long_value}]        assert %{"error" => "Invalid request"} ==                 conn                 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})                 |> json_response(403) -      long_name = Enum.map(0..name_limit, fn _ -> "x" end) |> Enum.join() -        fields = [%{"name" => long_name, "value" => "bar"}]        assert %{"error" => "Invalid request"} == @@ -334,7 +368,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do        Pleroma.Config.put([:instance, :max_account_fields], 1)        fields = [ -        %{"name" => "<b>foo<b>", "value" => "<i>bar</i>"}, +        %{"name" => "foo", "value" => "bar"},          %{"name" => "link", "value" => "cofe.io"}        ] @@ -342,20 +376,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController.UpdateCredentialsTest do                 conn                 |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields})                 |> json_response(403) - -      fields = [ -        %{"name" => "foo", "value" => ""}, -        %{"name" => "", "value" => "bar"} -      ] - -      account = -        conn -        |> patch("/api/v1/accounts/update_credentials", %{"fields_attributes" => fields}) -        |> json_response(200) - -      assert account["fields"] == [ -               %{"name" => "foo", "value" => ""} -             ]      end    end  end diff --git a/test/web/mastodon_api/controllers/account_controller_test.exs b/test/web/mastodon_api/controllers/account_controller_test.exs index a9fa0ce48..a450a732c 100644 --- a/test/web/mastodon_api/controllers/account_controller_test.exs +++ b/test/web/mastodon_api/controllers/account_controller_test.exs @@ -794,7 +794,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do      test "Account registration via Application", %{conn: conn} do        conn = -        post(conn, "/api/v1/apps", %{ +        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" diff --git a/test/web/mastodon_api/controllers/app_controller_test.exs b/test/web/mastodon_api/controllers/app_controller_test.exs index 77d234d67..e7b11d14e 100644 --- a/test/web/mastodon_api/controllers/app_controller_test.exs +++ b/test/web/mastodon_api/controllers/app_controller_test.exs @@ -16,8 +16,7 @@ defmodule Pleroma.Web.MastodonAPI.AppControllerTest do      conn =        conn -      |> assign(:user, token.user) -      |> assign(:token, token) +      |> put_req_header("authorization", "Bearer #{token.token}")        |> get("/api/v1/apps/verify_credentials")      app = Repo.preload(token, :app).app @@ -37,6 +36,7 @@ defmodule Pleroma.Web.MastodonAPI.AppControllerTest do      conn =        conn +      |> put_req_header("content-type", "application/json")        |> assign(:user, user)        |> post("/api/v1/apps", %{          client_name: app_attrs.client_name, diff --git a/test/web/mastodon_api/controllers/notification_controller_test.exs b/test/web/mastodon_api/controllers/notification_controller_test.exs index 7a0011646..1557937d8 100644 --- a/test/web/mastodon_api/controllers/notification_controller_test.exs +++ b/test/web/mastodon_api/controllers/notification_controller_test.exs @@ -26,7 +26,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do        |> get("/api/v1/notifications")      expected_response = -      "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{ +      "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{          user.ap_id        }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>" @@ -45,7 +45,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do      conn = get(conn, "/api/v1/notifications/#{notification.id}")      expected_response = -      "hi <span class=\"h-card\"><a data-user=\"#{user.id}\" class=\"u-url mention\" href=\"#{ +      "hi <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{user.id}\" href=\"#{          user.ap_id        }\" rel=\"ugc\">@<span>#{user.nickname}</span></a></span>" @@ -53,7 +53,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do      assert response == expected_response    end -  test "dismissing a single notification" do +  test "dismissing a single notification (deprecated endpoint)" do      %{user: user, conn: conn} = oauth_access(["write:notifications"])      other_user = insert(:user) @@ -69,6 +69,22 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do      assert %{} = json_response(conn, 200)    end +  test "dismissing a single notification" do +    %{user: user, conn: conn} = oauth_access(["write:notifications"]) +    other_user = insert(:user) + +    {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hi @#{user.nickname}"}) + +    {:ok, [notification]} = Notification.create_notifications(activity) + +    conn = +      conn +      |> assign(:user, user) +      |> post("/api/v1/notifications/#{notification.id}/dismiss") + +    assert %{} = json_response(conn, 200) +  end +    test "clearing all notifications" do      %{user: user, conn: conn} = oauth_access(["write:notifications", "read:notifications"])      other_user = insert(:user) @@ -194,10 +210,10 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do        {:ok, private_activity} =          CommonAPI.post(other_user, %{"status" => ".", "visibility" => "private"}) -      {:ok, _, _} = CommonAPI.favorite(public_activity.id, user) -      {:ok, _, _} = CommonAPI.favorite(direct_activity.id, user) -      {:ok, _, _} = CommonAPI.favorite(unlisted_activity.id, user) -      {:ok, _, _} = CommonAPI.favorite(private_activity.id, user) +      {:ok, _} = CommonAPI.favorite(user, public_activity.id) +      {:ok, _} = CommonAPI.favorite(user, direct_activity.id) +      {:ok, _} = CommonAPI.favorite(user, unlisted_activity.id) +      {:ok, _} = CommonAPI.favorite(user, private_activity.id)        activity_ids =          conn @@ -274,7 +290,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do      {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})      {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"}) -    {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user) +    {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)      {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)      {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user) @@ -310,7 +326,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do      {:ok, mention_activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})      {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"}) -    {:ok, favorite_activity, _} = CommonAPI.favorite(create_activity.id, other_user) +    {:ok, favorite_activity} = CommonAPI.favorite(other_user, create_activity.id)      {:ok, reblog_activity, _} = CommonAPI.repeat(create_activity.id, other_user)      {:ok, _, _, follow_activity} = CommonAPI.follow(other_user, user) @@ -452,11 +468,24 @@ defmodule Pleroma.Web.MastodonAPI.NotificationControllerTest do      assert length(json_response(conn, 200)) == 1    end +  @tag capture_log: true    test "see move notifications" do      old_user = insert(:user)      new_user = insert(:user, also_known_as: [old_user.ap_id])      %{user: follower, conn: conn} = oauth_access(["read:notifications"]) +    old_user_url = old_user.ap_id + +    body = +      File.read!("test/fixtures/users_mock/localhost.json") +      |> String.replace("{{nickname}}", old_user.nickname) +      |> Jason.encode!() + +    Tesla.Mock.mock(fn +      %{method: :get, url: ^old_user_url} -> +        %Tesla.Env{status: 200, body: body} +    end) +      User.follow(follower, old_user)      Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)      Pleroma.Tests.ObanHelpers.perform_all() diff --git a/test/web/mastodon_api/controllers/status_controller_test.exs b/test/web/mastodon_api/controllers/status_controller_test.exs index d59974d50..cd9ca4973 100644 --- a/test/web/mastodon_api/controllers/status_controller_test.exs +++ b/test/web/mastodon_api/controllers/status_controller_test.exs @@ -775,7 +775,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do        user1 = insert(:user)        user2 = insert(:user)        user3 = insert(:user) -      CommonAPI.favorite(activity.id, user2) +      {:ok, _} = CommonAPI.favorite(user2, activity.id)        {:ok, _bookmark} = Pleroma.Bookmark.create(user2.id, activity.id)        {:ok, reblog_activity1, _object} = CommonAPI.repeat(activity.id, user1)        {:ok, _, _object} = CommonAPI.repeat(activity.id, user2) @@ -850,11 +850,15 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do        activity = insert(:note_activity)        post(conn, "/api/v1/statuses/#{activity.id}/favourite") -      assert post(conn, "/api/v1/statuses/#{activity.id}/favourite") |> json_response(200) + +      assert post(conn, "/api/v1/statuses/#{activity.id}/favourite") +             |> json_response(200)      end      test "returns 404 error for a wrong id", %{conn: conn} do -      conn = post(conn, "/api/v1/statuses/1/favourite") +      conn = +        conn +        |> post("/api/v1/statuses/1/favourite")        assert json_response(conn, 404) == %{"error" => "Record not found"}      end @@ -866,7 +870,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do      test "unfavorites a status and returns it", %{user: user, conn: conn} do        activity = insert(:note_activity) -      {:ok, _, _} = CommonAPI.favorite(activity.id, user) +      {:ok, _} = CommonAPI.favorite(user, activity.id)        conn = post(conn, "/api/v1/statuses/#{activity.id}/unfavourite") @@ -1176,7 +1180,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do      test "returns users who have favorited the status", %{conn: conn, activity: activity} do        other_user = insert(:user) -      {:ok, _, _} = CommonAPI.favorite(activity.id, other_user) +      {:ok, _} = CommonAPI.favorite(other_user, activity.id)        response =          conn @@ -1207,7 +1211,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do        other_user = insert(:user)        {:ok, _user_relationship} = User.block(user, other_user) -      {:ok, _, _} = CommonAPI.favorite(activity.id, other_user) +      {:ok, _} = CommonAPI.favorite(other_user, activity.id)        response =          conn @@ -1219,7 +1223,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do      test "does not fail on an unauthenticated request", %{activity: activity} do        other_user = insert(:user) -      {:ok, _, _} = CommonAPI.favorite(activity.id, other_user) +      {:ok, _} = CommonAPI.favorite(other_user, activity.id)        response =          build_conn() @@ -1239,7 +1243,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do            "visibility" => "direct"          }) -      {:ok, _, _} = CommonAPI.favorite(activity.id, other_user) +      {:ok, _} = CommonAPI.favorite(other_user, activity.id)        favourited_by_url = "/api/v1/statuses/#{activity.id}/favourited_by" @@ -1399,7 +1403,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do      {:ok, _} = CommonAPI.post(other_user, %{"status" => "bla"})      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "traps are happy"}) -    {:ok, _, _} = CommonAPI.favorite(activity.id, user) +    {:ok, _} = CommonAPI.favorite(user, activity.id)      first_conn = get(conn, "/api/v1/favourites") @@ -1416,7 +1420,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do            "Trees Are Never Sad Look At Them Every Once In Awhile They're Quite Beautiful."        }) -    {:ok, _, _} = CommonAPI.favorite(second_activity.id, user) +    {:ok, _} = CommonAPI.favorite(user, second_activity.id)      last_like = status["id"] diff --git a/test/web/mastodon_api/controllers/timeline_controller_test.exs b/test/web/mastodon_api/controllers/timeline_controller_test.exs index 6fedb4223..97b1c3e66 100644 --- a/test/web/mastodon_api/controllers/timeline_controller_test.exs +++ b/test/web/mastodon_api/controllers/timeline_controller_test.exs @@ -21,9 +21,12 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do      setup do: oauth_access(["read:statuses"])      test "the home timeline", %{user: user, conn: conn} do -      following = insert(:user) +      following = insert(:user, nickname: "followed") +      third_user = insert(:user, nickname: "repeated") -      {:ok, _activity} = CommonAPI.post(following, %{"status" => "test"}) +      {:ok, _activity} = CommonAPI.post(following, %{"status" => "post"}) +      {:ok, activity} = CommonAPI.post(third_user, %{"status" => "repeated post"}) +      {:ok, _, _} = CommonAPI.repeat(activity.id, following)        ret_conn = get(conn, "/api/v1/timelines/home") @@ -31,9 +34,54 @@ defmodule Pleroma.Web.MastodonAPI.TimelineControllerTest do        {:ok, _user} = User.follow(user, following) -      conn = get(conn, "/api/v1/timelines/home") +      ret_conn = get(conn, "/api/v1/timelines/home") -      assert [%{"content" => "test"}] = json_response(conn, :ok) +      assert [ +               %{ +                 "reblog" => %{ +                   "content" => "repeated post", +                   "account" => %{ +                     "pleroma" => %{ +                       "relationship" => %{"following" => false, "followed_by" => false} +                     } +                   } +                 }, +                 "account" => %{"pleroma" => %{"relationship" => %{"following" => true}}} +               }, +               %{ +                 "content" => "post", +                 "account" => %{ +                   "acct" => "followed", +                   "pleroma" => %{"relationship" => %{"following" => true}} +                 } +               } +             ] = json_response(ret_conn, :ok) + +      {:ok, _user} = User.follow(third_user, user) + +      ret_conn = get(conn, "/api/v1/timelines/home") + +      assert [ +               %{ +                 "reblog" => %{ +                   "content" => "repeated post", +                   "account" => %{ +                     "acct" => "repeated", +                     "pleroma" => %{ +                       "relationship" => %{"following" => false, "followed_by" => true} +                     } +                   } +                 }, +                 "account" => %{"pleroma" => %{"relationship" => %{"following" => true}}} +               }, +               %{ +                 "content" => "post", +                 "account" => %{ +                   "acct" => "followed", +                   "pleroma" => %{"relationship" => %{"following" => true}} +                 } +               } +             ] = json_response(ret_conn, :ok)      end      test "the home timeline when the direct messages are excluded", %{user: user, conn: conn} do diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs index 983886c6b..4435f69ff 100644 --- a/test/web/mastodon_api/views/account_view_test.exs +++ b/test/web/mastodon_api/views/account_view_test.exs @@ -4,11 +4,20 @@  defmodule Pleroma.Web.MastodonAPI.AccountViewTest do    use Pleroma.DataCase -  import Pleroma.Factory +    alias Pleroma.User +  alias Pleroma.UserRelationship    alias Pleroma.Web.CommonAPI    alias Pleroma.Web.MastodonAPI.AccountView +  import Pleroma.Factory +  import Tesla.Mock + +  setup do +    mock(fn env -> apply(HttpRequestMock, :request, [env]) end) +    :ok +  end +    test "Represent a user account" do      source_data = %{        "tag" => [ @@ -161,6 +170,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do      assert expected == AccountView.render("show.json", %{user: user})    end +  test "Represent a Funkwhale channel" do +    {:ok, user} = +      User.get_or_fetch_by_ap_id( +        "https://channels.tests.funkwhale.audio/federation/actors/compositions" +      ) + +    assert represented = AccountView.render("show.json", %{user: user}) +    assert represented.acct == "compositions@channels.tests.funkwhale.audio" +    assert represented.url == "https://channels.tests.funkwhale.audio/channels/compositions" +  end +    test "Represent a deactivated user for an admin" do      admin = insert(:user, is_admin: true)      deactivated_user = insert(:user, deactivated: true) @@ -182,6 +202,32 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do    end    describe "relationship" do +    defp test_relationship_rendering(user, other_user, expected_result) do +      opts = %{user: user, target: other_user, relationships: nil} +      assert expected_result == AccountView.render("relationship.json", opts) + +      relationships_opt = UserRelationship.view_relationships_option(user, [other_user]) +      opts = Map.put(opts, :relationships, relationships_opt) +      assert expected_result == AccountView.render("relationship.json", opts) + +      assert [expected_result] == +               AccountView.render("relationships.json", %{user: user, targets: [other_user]}) +    end + +    @blank_response %{ +      following: false, +      followed_by: false, +      blocking: false, +      blocked_by: false, +      muting: false, +      muting_notifications: false, +      subscribing: false, +      requested: false, +      domain_blocking: false, +      showing_reblogs: true, +      endorsed: false +    } +      test "represent a relationship for the following and followed user" do        user = insert(:user)        other_user = insert(:user) @@ -192,23 +238,21 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        {:ok, _user_relationships} = User.mute(user, other_user, true)        {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, other_user) -      expected = %{ -        id: to_string(other_user.id), -        following: true, -        followed_by: true, -        blocking: false, -        blocked_by: false, -        muting: true, -        muting_notifications: true, -        subscribing: true, -        requested: false, -        domain_blocking: false, -        showing_reblogs: false, -        endorsed: false -      } - -      assert expected == -               AccountView.render("relationship.json", %{user: user, target: other_user}) +      expected = +        Map.merge( +          @blank_response, +          %{ +            following: true, +            followed_by: true, +            muting: true, +            muting_notifications: true, +            subscribing: true, +            showing_reblogs: false, +            id: to_string(other_user.id) +          } +        ) + +      test_relationship_rendering(user, other_user, expected)      end      test "represent a relationship for the blocking and blocked user" do @@ -220,23 +264,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        {:ok, _user_relationship} = User.block(user, other_user)        {:ok, _user_relationship} = User.block(other_user, user) -      expected = %{ -        id: to_string(other_user.id), -        following: false, -        followed_by: false, -        blocking: true, -        blocked_by: true, -        muting: false, -        muting_notifications: false, -        subscribing: false, -        requested: false, -        domain_blocking: false, -        showing_reblogs: true, -        endorsed: false -      } +      expected = +        Map.merge( +          @blank_response, +          %{following: false, blocking: true, blocked_by: true, id: to_string(other_user.id)} +        ) -      assert expected == -               AccountView.render("relationship.json", %{user: user, target: other_user}) +      test_relationship_rendering(user, other_user, expected)      end      test "represent a relationship for the user blocking a domain" do @@ -245,8 +279,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        {:ok, user} = User.block_domain(user, "bad.site") -      assert %{domain_blocking: true, blocking: false} = -               AccountView.render("relationship.json", %{user: user, target: other_user}) +      expected = +        Map.merge( +          @blank_response, +          %{domain_blocking: true, blocking: false, id: to_string(other_user.id)} +        ) + +      test_relationship_rendering(user, other_user, expected)      end      test "represent a relationship for the user with a pending follow request" do @@ -257,23 +296,13 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do        user = User.get_cached_by_id(user.id)        other_user = User.get_cached_by_id(other_user.id) -      expected = %{ -        id: to_string(other_user.id), -        following: false, -        followed_by: false, -        blocking: false, -        blocked_by: false, -        muting: false, -        muting_notifications: false, -        subscribing: false, -        requested: true, -        domain_blocking: false, -        showing_reblogs: true, -        endorsed: false -      } +      expected = +        Map.merge( +          @blank_response, +          %{requested: true, following: false, id: to_string(other_user.id)} +        ) -      assert expected == -               AccountView.render("relationship.json", %{user: user, target: other_user}) +      test_relationship_rendering(user, other_user, expected)      end    end diff --git a/test/web/mastodon_api/views/notification_view_test.exs b/test/web/mastodon_api/views/notification_view_test.exs index d04c3022f..c3ec9dfec 100644 --- a/test/web/mastodon_api/views/notification_view_test.exs +++ b/test/web/mastodon_api/views/notification_view_test.exs @@ -16,6 +16,21 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do    alias Pleroma.Web.MastodonAPI.StatusView    import Pleroma.Factory +  defp test_notifications_rendering(notifications, user, expected_result) do +    result = NotificationView.render("index.json", %{notifications: notifications, for: user}) + +    assert expected_result == result + +    result = +      NotificationView.render("index.json", %{ +        notifications: notifications, +        for: user, +        relationships: nil +      }) + +    assert expected_result == result +  end +    test "Mention notification" do      user = insert(:user)      mentioned_user = insert(:user) @@ -32,17 +47,14 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do        created_at: Utils.to_masto_date(notification.inserted_at)      } -    result = -      NotificationView.render("index.json", %{notifications: [notification], for: mentioned_user}) - -    assert [expected] == result +    test_notifications_rendering([notification], mentioned_user, [expected])    end    test "Favourite notification" do      user = insert(:user)      another_user = insert(:user)      {:ok, create_activity} = CommonAPI.post(user, %{"status" => "hey"}) -    {:ok, favorite_activity, _object} = CommonAPI.favorite(create_activity.id, another_user) +    {:ok, favorite_activity} = CommonAPI.favorite(another_user, create_activity.id)      {:ok, [notification]} = Notification.create_notifications(favorite_activity)      create_activity = Activity.get_by_id(create_activity.id) @@ -55,9 +67,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do        created_at: Utils.to_masto_date(notification.inserted_at)      } -    result = NotificationView.render("index.json", %{notifications: [notification], for: user}) - -    assert [expected] == result +    test_notifications_rendering([notification], user, [expected])    end    test "Reblog notification" do @@ -77,9 +87,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do        created_at: Utils.to_masto_date(notification.inserted_at)      } -    result = NotificationView.render("index.json", %{notifications: [notification], for: user}) - -    assert [expected] == result +    test_notifications_rendering([notification], user, [expected])    end    test "Follow notification" do @@ -96,23 +104,32 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do        created_at: Utils.to_masto_date(notification.inserted_at)      } -    result = -      NotificationView.render("index.json", %{notifications: [notification], for: followed}) - -    assert [expected] == result +    test_notifications_rendering([notification], followed, [expected])      User.perform(:delete, follower)      notification = Notification |> Repo.one() |> Repo.preload(:activity) -    assert [] == -             NotificationView.render("index.json", %{notifications: [notification], for: followed}) +    test_notifications_rendering([notification], followed, [])    end +  @tag capture_log: true    test "Move notification" do      old_user = insert(:user)      new_user = insert(:user, also_known_as: [old_user.ap_id])      follower = insert(:user) +    old_user_url = old_user.ap_id + +    body = +      File.read!("test/fixtures/users_mock/localhost.json") +      |> String.replace("{{nickname}}", old_user.nickname) +      |> Jason.encode!() + +    Tesla.Mock.mock(fn +      %{method: :get, url: ^old_user_url} -> +        %Tesla.Env{status: 200, body: body} +    end) +      User.follow(follower, old_user)      Pleroma.Web.ActivityPub.ActivityPub.move(old_user, new_user)      Pleroma.Tests.ObanHelpers.perform_all() @@ -131,8 +148,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do        created_at: Utils.to_masto_date(notification.inserted_at)      } -    assert [expected] == -             NotificationView.render("index.json", %{notifications: [notification], for: follower}) +    test_notifications_rendering([notification], follower, [expected])    end    test "EmojiReact notification" do @@ -158,7 +174,6 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do        created_at: Utils.to_masto_date(notification.inserted_at)      } -    assert expected == -             NotificationView.render("show.json", %{notification: notification, for: user}) +    test_notifications_rendering([notification], user, [expected])    end  end diff --git a/test/web/mastodon_api/views/status_view_test.exs b/test/web/mastodon_api/views/status_view_test.exs index 191895c6f..6791c2fb0 100644 --- a/test/web/mastodon_api/views/status_view_test.exs +++ b/test/web/mastodon_api/views/status_view_test.exs @@ -12,10 +12,12 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do    alias Pleroma.Object    alias Pleroma.Repo    alias Pleroma.User +  alias Pleroma.UserRelationship    alias Pleroma.Web.CommonAPI    alias Pleroma.Web.CommonAPI.Utils    alias Pleroma.Web.MastodonAPI.AccountView    alias Pleroma.Web.MastodonAPI.StatusView +    import Pleroma.Factory    import Tesla.Mock @@ -92,6 +94,23 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do      Repo.delete(user)      Cachex.clear(:user_cache) +    finger_url = +      "https://localhost/.well-known/webfinger?resource=acct:#{user.nickname}@localhost" + +    Tesla.Mock.mock_global(fn +      %{method: :get, url: "http://localhost/.well-known/host-meta"} -> +        %Tesla.Env{status: 404, body: ""} + +      %{method: :get, url: "https://localhost/.well-known/host-meta"} -> +        %Tesla.Env{status: 404, body: ""} + +      %{ +        method: :get, +        url: ^finger_url +      } -> +        %Tesla.Env{status: 404, body: ""} +    end) +      %{account: ms_user} = StatusView.render("show.json", activity: activity)      assert ms_user.acct == "erroruser@example.com" @@ -212,12 +231,21 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do      {:ok, _user_relationships} = User.mute(user, other_user)      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "test"}) -    status = StatusView.render("show.json", %{activity: activity}) +    relationships_opt = UserRelationship.view_relationships_option(user, [other_user]) + +    opts = %{activity: activity} +    status = StatusView.render("show.json", opts)      assert status.muted == false -    status = StatusView.render("show.json", %{activity: activity, for: user}) +    status = StatusView.render("show.json", Map.put(opts, :relationships, relationships_opt)) +    assert status.muted == false + +    for_opts = %{activity: activity, for: user} +    status = StatusView.render("show.json", for_opts) +    assert status.muted == true +    status = StatusView.render("show.json", Map.put(for_opts, :relationships, relationships_opt))      assert status.muted == true    end @@ -420,6 +448,22 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do      assert length(represented[:media_attachments]) == 1    end +  test "funkwhale audio" do +    user = insert(:user) + +    {:ok, object} = +      Pleroma.Object.Fetcher.fetch_object_from_id( +        "https://channels.tests.funkwhale.audio/federation/music/uploads/42342395-0208-4fee-a38d-259a6dae0871" +      ) + +    %Activity{} = activity = Activity.get_create_by_object_ap_id(object.data["id"]) + +    represented = StatusView.render("show.json", %{for: user, activity: activity}) + +    assert represented[:id] == to_string(activity.id) +    assert length(represented[:media_attachments]) == 1 +  end +    test "a Mobilizon event" do      user = insert(:user)  | 
