Add tests for counters being updated on follow
authorrinpatch <rinpatch@sdf.org>
Wed, 31 Jul 2019 16:37:55 +0000 (19:37 +0300)
committerrinpatch <rinpatch@sdf.org>
Wed, 31 Jul 2019 18:09:13 +0000 (21:09 +0300)
lib/pleroma/user.ex
test/fixtures/users_mock/masto_closed_followers_page.json [new file with mode: 0644]
test/fixtures/users_mock/masto_closed_following_page.json [new file with mode: 0644]
test/support/http_request_mock.ex
test/user_test.exs
test/web/activity_pub/activity_pub_test.exs

index 7acf1e53cbf8192c334d1cd01b321a52298e3fe9..69835f3dd0d7e664203fecf708049dda0686d83f 100644 (file)
@@ -741,7 +741,7 @@ defmodule Pleroma.User do
   end
 
   def update_follower_count(%User{} = user) do
-    unless !user.local and Pleroma.Config.get([:instance, :external_user_synchronization]) do
+    if user.local or !Pleroma.Config.get([:instance, :external_user_synchronization]) do
       follower_count_query =
         User.Query.build(%{followers: user, deactivated: false})
         |> select([u], %{count: count(u.id)})
diff --git a/test/fixtures/users_mock/masto_closed_followers_page.json b/test/fixtures/users_mock/masto_closed_followers_page.json
new file mode 100644 (file)
index 0000000..04ab0c4
--- /dev/null
@@ -0,0 +1 @@
+{"@context":"https://www.w3.org/ns/activitystreams","id":"http://localhost:4001/users/masto_closed/followers?page=1","type":"OrderedCollectionPage","totalItems":437,"next":"http://localhost:4001/users/masto_closed/followers?page=2","partOf":"http://localhost:4001/users/masto_closed/followers","orderedItems":["https://testing.uguu.ltd/users/rin","https://patch.cx/users/rin","https://letsalllovela.in/users/xoxo","https://pleroma.site/users/crushv","https://aria.company/users/boris","https://kawen.space/users/crushv","https://freespeech.host/users/cvcvcv","https://pleroma.site/users/picpub","https://pixelfed.social/users/nosleep","https://boopsnoot.gq/users/5c1896d162f7d337f90492a3","https://pikachu.rocks/users/waifu","https://royal.crablettesare.life/users/crablettes"]}
diff --git a/test/fixtures/users_mock/masto_closed_following_page.json b/test/fixtures/users_mock/masto_closed_following_page.json
new file mode 100644 (file)
index 0000000..8d83246
--- /dev/null
@@ -0,0 +1 @@
+{"@context":"https://www.w3.org/ns/activitystreams","id":"http://localhost:4001/users/masto_closed/following?page=1","type":"OrderedCollectionPage","totalItems":152,"next":"http://localhost:4001/users/masto_closed/following?page=2","partOf":"http://localhost:4001/users/masto_closed/following","orderedItems":["https://testing.uguu.ltd/users/rin","https://patch.cx/users/rin","https://letsalllovela.in/users/xoxo","https://pleroma.site/users/crushv","https://aria.company/users/boris","https://kawen.space/users/crushv","https://freespeech.host/users/cvcvcv","https://pleroma.site/users/picpub","https://pixelfed.social/users/nosleep","https://boopsnoot.gq/users/5c1896d162f7d337f90492a3","https://pikachu.rocks/users/waifu","https://royal.crablettesare.life/users/crablettes"]}
index 2ed5f504204cfbc7f1979f41576ea392fe17e096..bdfe43b28e6ac28b7a55c6bfa1c7dca92264d9c3 100644 (file)
@@ -792,6 +792,14 @@ defmodule HttpRequestMock do
      }}
   end
 
+  def get("http://localhost:4001/users/masto_closed/followers?page=1", _, _, _) do
+    {:ok,
+     %Tesla.Env{
+       status: 200,
+       body: File.read!("test/fixtures/users_mock/masto_closed_followers_page.json")
+     }}
+  end
+
   def get("http://localhost:4001/users/masto_closed/following", _, _, _) do
     {:ok,
      %Tesla.Env{
@@ -800,6 +808,14 @@ defmodule HttpRequestMock do
      }}
   end
 
+  def get("http://localhost:4001/users/masto_closed/following?page=1", _, _, _) do
+    {:ok,
+     %Tesla.Env{
+       status: 200,
+       body: File.read!("test/fixtures/users_mock/masto_closed_following_page.json")
+     }}
+  end
+
   def get("http://localhost:4001/users/fuser2/followers", _, _, _) do
     {:ok,
      %Tesla.Env{
index 556df45fd0f75a14276d8df6ddb54eb2207a0e14..7ec241c2505176b2aef1ad2f0a7c6ef16d192248 100644 (file)
@@ -1393,4 +1393,78 @@ defmodule Pleroma.UserTest do
       assert %User{bio: "test-bio"} = User.get_cached_by_ap_id(user.ap_id)
     end
   end
+
+  describe "following/followers synchronization" do
+    setup do
+      sync = Pleroma.Config.get([:instance, :external_user_synchronization])
+      on_exit(fn -> Pleroma.Config.put([:instance, :external_user_synchronization], sync) end)
+    end
+
+    test "updates the counters normally on following/getting a follow when disabled" do
+      Pleroma.Config.put([:instance, :external_user_synchronization], false)
+      user = insert(:user)
+
+      other_user =
+        insert(:user,
+          local: false,
+          follower_address: "http://localhost:4001/users/masto_closed/followers",
+          following_address: "http://localhost:4001/users/masto_closed/following",
+          info: %{ap_enabled: true}
+        )
+
+      assert User.user_info(other_user).following_count == 0
+      assert User.user_info(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
+    end
+
+    test "syncronizes the counters with the remote instance for the followed when enabled" do
+      Pleroma.Config.put([:instance, :external_user_synchronization], false)
+
+      user = insert(:user)
+
+      other_user =
+        insert(:user,
+          local: false,
+          follower_address: "http://localhost:4001/users/masto_closed/followers",
+          following_address: "http://localhost:4001/users/masto_closed/following",
+          info: %{ap_enabled: true}
+        )
+
+      assert User.user_info(other_user).following_count == 0
+      assert User.user_info(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
+    end
+
+    test "syncronizes the counters with the remote instance for the follower when enabled" do
+      Pleroma.Config.put([:instance, :external_user_synchronization], false)
+
+      user = insert(:user)
+
+      other_user =
+        insert(:user,
+          local: false,
+          follower_address: "http://localhost:4001/users/masto_closed/followers",
+          following_address: "http://localhost:4001/users/masto_closed/following",
+          info: %{ap_enabled: true}
+        )
+
+      assert User.user_info(other_user).following_count == 0
+      assert User.user_info(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
+    end
+  end
 end
index 853c93ab517719b75119c1317ad75a7d2a067e09..3d9a678dd0f380b6c7e92fe6b86dfc218b046237 100644 (file)
@@ -1149,16 +1149,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
           "http://localhost:4001/users/masto_closed/followers?page=1" ->
             %Tesla.Env{status: 403, body: ""}
 
-          "http://localhost:4001/users/masto_closed/following?page=1" ->
-            %Tesla.Env{
-              status: 200,
-              body:
-                Jason.encode!(%{
-                  "id" => "http://localhost:4001/users/masto_closed/following?page=1",
-                  "type" => "OrderedCollectionPage"
-                })
-            }
-
           _ ->
             apply(HttpRequestMock, :request, [env])
         end
@@ -1182,16 +1172,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
           "http://localhost:4001/users/masto_closed/following?page=1" ->
             %Tesla.Env{status: 403, body: ""}
 
-          "http://localhost:4001/users/masto_closed/followers?page=1" ->
-            %Tesla.Env{
-              status: 200,
-              body:
-                Jason.encode!(%{
-                  "id" => "http://localhost:4001/users/masto_closed/followers?page=1",
-                  "type" => "OrderedCollectionPage"
-                })
-            }
-
           _ ->
             apply(HttpRequestMock, :request, [env])
         end