Add tests for fetch_follow_information_for_user and check object type
authorrinpatch <rinpatch@sdf.org>
Sat, 13 Jul 2019 21:56:02 +0000 (00:56 +0300)
committerrinpatch <rinpatch@sdf.org>
Sat, 13 Jul 2019 21:56:02 +0000 (00:56 +0300)
when fetching the page

lib/pleroma/web/activity_pub/activity_pub.ex
test/web/activity_pub/activity_pub_test.exs

index df4155d211f00d45931411d84a87843e9528084a..c821ba45ff08cdd81b1c0e73bea1f1c12c264c8a 100644 (file)
@@ -1063,7 +1063,8 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
          data["first"]["type"] in ["CollectionPage", "OrderedCollectionPage"] do
       {:ok, false}
     else
-      with {:ok, _data} <- Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do
+      with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
+             Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do
         {:ok, false}
       else
         {:error, {:ok, %{status: code}}} when code in [401, 403] ->
index 59d56f3a758324633b0d70953ebd076f5117f3ca..448ffbf5477eb09446b0440b95ae52af9c32fd59 100644 (file)
@@ -1222,4 +1222,85 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       assert result.id == activity.id
     end
   end
+
+  describe "fetch_follow_information_for_user" do
+    test "syncronizes following/followers counters" do
+      user =
+        insert(:user,
+          local: false,
+          follower_address: "http://localhost:4001/users/fuser2/followers",
+          following_address: "http://localhost:4001/users/fuser2/following"
+        )
+
+      {:ok, user} = ActivityPub.fetch_follow_information_for_user(user)
+      assert user.info.follower_count == 527
+      assert user.info.following_count == 267
+    end
+
+    test "detects hidden followers" do
+      mock(fn env ->
+        case env.url 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
+      end)
+
+      user =
+        insert(:user,
+          local: false,
+          follower_address: "http://localhost:4001/users/masto_closed/followers",
+          following_address: "http://localhost:4001/users/masto_closed/following"
+        )
+
+      {:ok, user} = ActivityPub.fetch_follow_information_for_user(user)
+      assert user.info.hide_followers == true
+      assert user.info.hide_follows == false
+    end
+
+    test "detects hidden follows" do
+      mock(fn env ->
+        case env.url 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
+      end)
+
+      user =
+        insert(:user,
+          local: false,
+          follower_address: "http://localhost:4001/users/masto_closed/followers",
+          following_address: "http://localhost:4001/users/masto_closed/following"
+        )
+
+      {:ok, user} = ActivityPub.fetch_follow_information_for_user(user)
+      assert user.info.hide_followers == false
+      assert user.info.hide_follows == true
+    end
+  end
 end