Merge branch 'fed-testing-updates' into 'develop'
authorlain <lain@soykaf.club>
Thu, 28 Nov 2019 14:34:07 +0000 (14:34 +0000)
committerlain <lain@soykaf.club>
Thu, 28 Nov 2019 14:34:07 +0000 (14:34 +0000)
Add federated test infrastructure

See merge request pleroma/pleroma!2014

lib/pleroma/activity.ex
lib/pleroma/user.ex
lib/pleroma/web/mastodon_api/views/account_view.ex
lib/pleroma/web/mastodon_api/views/status_view.ex
lib/pleroma/web/push/impl.ex
test/user_test.exs
test/web/activity_pub/transmogrifier_test.exs
test/web/admin_api/admin_api_controller_test.exs
test/web/mastodon_api/views/account_view_test.exs
test/web/streamer/streamer_test.exs

index 7e283df32c01aeae7ac4eb981a670aef66e09da0..cd7a5aae9aba85848274103c9de1e95f757a78b5 100644 (file)
@@ -303,4 +303,17 @@ defmodule Pleroma.Activity do
   end
 
   defdelegate search(user, query, options \\ []), to: Pleroma.Activity.Search
+
+  def direct_conversation_id(activity, for_user) do
+    alias Pleroma.Conversation.Participation
+
+    with %{data: %{"context" => context}} when is_binary(context) <- activity,
+         %Pleroma.Conversation{} = conversation <- Pleroma.Conversation.get_for_ap_id(context),
+         %Participation{id: participation_id} <-
+           Participation.for_user_and_conversation(for_user, conversation) do
+      participation_id
+    else
+      _ -> nil
+    end
+  end
 end
index 3010fe87fd46976784a3cd1e24229fc344869c4a..b18a4c6a5099a4d381ec72e51d78033323eb474d 100644 (file)
@@ -177,20 +177,6 @@ defmodule Pleroma.User do
   def ap_following(%User{following_address: fa}) when is_binary(fa), do: fa
   def ap_following(%User{} = user), do: "#{ap_id(user)}/following"
 
-  def user_info(%User{} = user, args \\ %{}) do
-    following_count = Map.get(args, :following_count, user.following_count)
-    follower_count = Map.get(args, :follower_count, user.follower_count)
-
-    %{
-      note_count: user.note_count,
-      locked: user.locked,
-      confirmation_pending: user.confirmation_pending,
-      default_scope: user.default_scope,
-      follower_count: follower_count,
-      following_count: following_count
-    }
-  end
-
   def follow_state(%User{} = user, %User{} = target) do
     case Utils.fetch_latest_follow(user, target) do
       %{data: %{"state" => state}} -> state
@@ -209,10 +195,6 @@ defmodule Pleroma.User do
     Cachex.put(:user_cache, "follow_state:#{user_ap_id}|#{target_ap_id}", state)
   end
 
-  def set_info_cache(user, args) do
-    Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user, args))
-  end
-
   @spec restrict_deactivated(Ecto.Query.t()) :: Ecto.Query.t()
   def restrict_deactivated(query) do
     from(u in query, where: u.deactivated != ^true)
@@ -614,7 +596,6 @@ defmodule Pleroma.User do
   def set_cache(%User{} = user) do
     Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
     Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
-    Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
     {:ok, user}
   end
 
@@ -633,7 +614,6 @@ defmodule Pleroma.User do
   def invalidate_cache(user) do
     Cachex.del(:user_cache, "ap_id:#{user.ap_id}")
     Cachex.del(:user_cache, "nickname:#{user.nickname}")
-    Cachex.del(:user_cache, "user_info:#{user.id}")
   end
 
   def get_cached_by_ap_id(ap_id) do
@@ -701,11 +681,6 @@ defmodule Pleroma.User do
     get_by_nickname(nickname_or_email) || get_by_email(nickname_or_email)
   end
 
-  def get_cached_user_info(user) do
-    key = "user_info:#{user.id}"
-    Cachex.fetch!(:user_cache, key, fn -> user_info(user) end)
-  end
-
   def fetch_by_nickname(nickname), do: ActivityPub.make_user_from_nickname(nickname)
 
   def get_or_fetch_by_nickname(nickname) do
index e30fed6102b7b16a491cfc0f6010df1570371025..1068f8823f0ecd464963f84976220f91c5f9a9ff 100644 (file)
@@ -71,18 +71,17 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
     image = User.avatar_url(user) |> MediaProxy.url()
     header = User.banner_url(user) |> MediaProxy.url()
-    user_info = User.get_cached_user_info(user)
 
     following_count =
       if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
-        user_info.following_count
+        user.following_count || 0
       else
         0
       end
 
     followers_count =
       if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
-        user_info.follower_count
+        user.follower_count || 0
       else
         0
       end
@@ -144,7 +143,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
 
       # Pleroma extension
       pleroma: %{
-        confirmation_pending: user_info.confirmation_pending,
+        confirmation_pending: user.confirmation_pending,
         tags: user.tags,
         hide_followers_count: user.hide_followers_count,
         hide_follows_count: user.hide_follows_count,
@@ -157,7 +156,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
       }
     }
     |> maybe_put_role(user, opts[:for])
-    |> maybe_put_settings(user, opts[:for], user_info)
+    |> maybe_put_settings(user, opts[:for], opts)
     |> maybe_put_notification_settings(user, opts[:for])
     |> maybe_put_settings_store(user, opts[:for], opts)
     |> maybe_put_chat_token(user, opts[:for], opts)
@@ -191,7 +190,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
          data,
          %User{id: user_id} = user,
          %User{id: user_id},
-         _user_info
+         _opts
        ) do
     data
     |> Kernel.put_in([:source, :privacy], user.default_scope)
index baff5415113a7d8230dce1d40d23a5dc16511b4c..a0257dfa6a0ee1705edf57ecb549e5270476aeba 100644 (file)
@@ -9,8 +9,6 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
 
   alias Pleroma.Activity
   alias Pleroma.ActivityExpiration
-  alias Pleroma.Conversation
-  alias Pleroma.Conversation.Participation
   alias Pleroma.HTML
   alias Pleroma.Object
   alias Pleroma.Repo
@@ -245,12 +243,8 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
     direct_conversation_id =
       with {_, nil} <- {:direct_conversation_id, opts[:direct_conversation_id]},
            {_, true} <- {:include_id, opts[:with_direct_conversation_id]},
-           {_, %User{} = for_user} <- {:for_user, opts[:for]},
-           %{data: %{"context" => context}} when is_binary(context) <- activity,
-           %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
-           %Participation{id: participation_id} <-
-             Participation.for_user_and_conversation(for_user, conversation) do
-        participation_id
+           {_, %User{} = for_user} <- {:for_user, opts[:for]} do
+        Activity.direct_conversation_id(activity, for_user)
       else
         {:direct_conversation_id, participation_id} when is_integer(participation_id) ->
           participation_id
index dd445e8bfe2d0877ebbe850b304b5ef6952f2979..3de7af7086ca8f1c7814529defa538d6fd1d0da5 100644 (file)
@@ -33,6 +33,8 @@ defmodule Pleroma.Web.Push.Impl do
     gcm_api_key = Application.get_env(:web_push_encryption, :gcm_api_key)
     avatar_url = User.avatar_url(actor)
     object = Object.normalize(activity)
+    user = User.get_cached_by_id(user_id)
+    direct_conversation_id = Activity.direct_conversation_id(activity, user)
 
     for subscription <- fetch_subsriptions(user_id),
         get_in(subscription.data, ["alerts", type]) do
@@ -45,7 +47,8 @@ defmodule Pleroma.Web.Push.Impl do
         icon: avatar_url,
         preferred_locale: "en",
         pleroma: %{
-          activity_id: activity_id
+          activity_id: activity_id,
+          direct_conversation_id: direct_conversation_id
         }
       }
       |> Jason.encode!()
index e6302b5255c87d04dcb6ce12f7d83fac51193d4a..82e338e7599f6ae956f733c31be4674c488adb25 100644 (file)
@@ -961,9 +961,9 @@ defmodule Pleroma.UserTest do
       {:ok, user} = User.follow(user, user2)
       {:ok, _user} = User.deactivate(user)
 
-      info = User.get_cached_user_info(user2)
+      user2 = User.get_cached_by_id(user2.id)
 
-      assert info.follower_count == 0
+      assert user2.follower_count == 0
       assert [] = User.get_followers(user2)
     end
 
@@ -977,10 +977,10 @@ defmodule Pleroma.UserTest do
 
       {:ok, _user} = User.deactivate(user)
 
-      info = User.get_cached_user_info(user2)
+      user2 = User.get_cached_by_id(user2.id)
 
       assert refresh_record(user2).following_count == 0
-      assert info.following_count == 0
+      assert user2.following_count == 0
       assert User.following_count(user2) == 0
       assert [] = User.get_friends(user2)
     end
@@ -1182,13 +1182,12 @@ defmodule Pleroma.UserTest do
   describe "caching" do
     test "invalidate_cache works" do
       user = insert(:user)
-      _user_info = User.get_cached_user_info(user)
 
+      User.set_cache(user)
       User.invalidate_cache(user)
 
       {:ok, nil} = Cachex.get(:user_cache, "ap_id:#{user.ap_id}")
       {:ok, nil} = Cachex.get(:user_cache, "nickname:#{user.nickname}")
-      {:ok, nil} = Cachex.get(:user_cache, "user_info:#{user.id}")
     end
 
     test "User.delete() plugs any possible zombie objects" do
@@ -1344,7 +1343,7 @@ defmodule Pleroma.UserTest do
 
     {:ok, user} = User.block(user, follower)
 
-    assert User.user_info(user).follower_count == 2
+    assert user.follower_count == 2
   end
 
   describe "list_inactive_users_query/1" do
@@ -1521,51 +1520,6 @@ defmodule Pleroma.UserTest do
     end
   end
 
-  describe "set_info_cache/2" do
-    setup do
-      user = insert(:user)
-      {:ok, user: user}
-    end
-
-    test "update from args", %{user: user} do
-      User.set_info_cache(user, %{following_count: 15, follower_count: 18})
-
-      %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
-      assert followers == 18
-      assert following == 15
-    end
-
-    test "without args", %{user: user} do
-      User.set_info_cache(user, %{})
-
-      %{follower_count: followers, following_count: following} = User.get_cached_user_info(user)
-      assert followers == 0
-      assert following == 0
-    end
-  end
-
-  describe "user_info/2" do
-    setup do
-      user = insert(:user)
-      {:ok, user: user}
-    end
-
-    test "update from args", %{user: user} do
-      %{follower_count: followers, following_count: following} =
-        User.user_info(user, %{following_count: 15, follower_count: 18})
-
-      assert followers == 18
-      assert following == 15
-    end
-
-    test "without args", %{user: user} do
-      %{follower_count: followers, following_count: following} = User.user_info(user)
-
-      assert followers == 0
-      assert following == 0
-    end
-  end
-
   describe "is_internal_user?/1" do
     test "non-internal user returns false" do
       user = insert(:user)
@@ -1622,14 +1576,14 @@ defmodule Pleroma.UserTest do
           ap_enabled: true
         )
 
-      assert User.user_info(other_user).following_count == 0
-      assert User.user_info(other_user).follower_count == 0
+      assert other_user.following_count == 0
+      assert other_user.follower_count == 0
 
       {:ok, user} = Pleroma.User.follow(user, other_user)
       other_user = Pleroma.User.get_by_id(other_user.id)
 
-      assert User.user_info(user).following_count == 1
-      assert User.user_info(other_user).follower_count == 1
+      assert user.following_count == 1
+      assert other_user.follower_count == 1
     end
 
     test "syncronizes the counters with the remote instance for the followed when enabled" do
@@ -1645,14 +1599,14 @@ defmodule Pleroma.UserTest do
           ap_enabled: true
         )
 
-      assert User.user_info(other_user).following_count == 0
-      assert User.user_info(other_user).follower_count == 0
+      assert other_user.following_count == 0
+      assert other_user.follower_count == 0
 
       Pleroma.Config.put([:instance, :external_user_synchronization], true)
       {:ok, _user} = User.follow(user, other_user)
       other_user = User.get_by_id(other_user.id)
 
-      assert User.user_info(other_user).follower_count == 437
+      assert other_user.follower_count == 437
     end
 
     test "syncronizes the counters with the remote instance for the follower when enabled" do
@@ -1668,13 +1622,13 @@ defmodule Pleroma.UserTest do
           ap_enabled: true
         )
 
-      assert User.user_info(other_user).following_count == 0
-      assert User.user_info(other_user).follower_count == 0
+      assert other_user.following_count == 0
+      assert other_user.follower_count == 0
 
       Pleroma.Config.put([:instance, :external_user_synchronization], true)
       {:ok, other_user} = User.follow(other_user, user)
 
-      assert User.user_info(other_user).following_count == 152
+      assert other_user.following_count == 152
     end
   end
 
index 0bdd514e97384edf5a81c40bcf692185b2a1cbfd..b31c411dc3ea32cb5948bdbd1b2df5183287343a 100644 (file)
@@ -39,6 +39,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       assert activity == returned_activity
     end
 
+    @tag capture_log: true
     test "it fetches replied-to activities if we don't have them" do
       data =
         File.read!("test/fixtures/mastodon-post-activity.json")
@@ -533,6 +534,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       assert object.data["content"] == "this is a private toot"
     end
 
+    @tag capture_log: true
     test "it rejects incoming announces with an inlined activity from another origin" do
       data =
         File.read!("test/fixtures/bogus-mastodon-announce.json")
@@ -814,6 +816,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       assert Activity.get_by_id(activity.id)
     end
 
+    @tag capture_log: true
     test "it works for incoming user deletes" do
       %{ap_id: ap_id} = insert(:user, ap_id: "http://mastodon.example.org/users/admin")
 
@@ -1749,6 +1752,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
       assert modified_object["inReplyToAtomUri"] == ""
     end
 
+    @tag capture_log: true
     test "returns modified object when allowed incoming reply", %{data: data} do
       object_with_reply =
         Map.put(
@@ -1868,6 +1872,7 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
              end) =~ "Unsupported URI scheme"
     end
 
+    @tag capture_log: true
     test "returns {:ok, %Object{}} for success case" do
       assert {:ok, %Object{}} =
                Transmogrifier.get_obj_helper("https://shitposter.club/notice/2827873")
index bb2ca6a62d293ecb7e95ce9845af1b06941f2fde..32577afeebef7ef1419de2be08d1c03435637180 100644 (file)
@@ -1923,6 +1923,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
       Pleroma.Config.put([:instance, :dynamic_configuration], true)
     end
 
+    @tag capture_log: true
     test "create new config setting in db", %{conn: conn} do
       conn =
         post(conn, "/api/pleroma/admin/config", %{
index af88841ed234c5f8ba87a49312a79abe302247fe..d147079ab603f3e2b2592c25dfaa2eda2feb045b 100644 (file)
@@ -350,7 +350,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
       }
     }
 
-    assert expected == AccountView.render("show.json", %{user: user, for: other_user})
+    assert expected ==
+             AccountView.render("show.json", %{user: refresh_record(user), for: other_user})
   end
 
   test "returns the settings store if the requesting user is the represented user and it's requested specifically" do
@@ -374,6 +375,14 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     refute 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})
+
+    assert result.following_count == 0
+    assert result.followers_count == 0
+  end
+
   describe "hiding follows/following" do
     test "shows when follows/followers stats are hidden and sets follow/follower count to 0" do
       user =
index 80a7541b22f3a84bd731737f54ee0d7589e1e914..8265f18dd28e1939d186f5264204aae65d2deac7 100644 (file)
@@ -15,7 +15,7 @@ defmodule Pleroma.Web.StreamerTest do
   alias Pleroma.Web.Streamer.StreamerSocket
   alias Pleroma.Web.Streamer.Worker
 
-  @moduletag needs_streamer: true
+  @moduletag needs_streamer: true, capture_log: true
   clear_config_all([:instance, :skip_thread_containment])
 
   describe "user streams" do