Merge branch 'develop' into feature/database-compaction
[akkoma] / test / web / ostatus / ostatus_test.exs
index 3e7cf01556e764fd41742e7b5acad336c25db079..50467c71fbf1d8b49bd7210d0643ca0eae32332b 100644 (file)
@@ -1,11 +1,24 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
 defmodule Pleroma.Web.OStatusTest do
   use Pleroma.DataCase
+  alias Pleroma.Activity
+  alias Pleroma.Instances
+  alias Pleroma.Object
+  alias Pleroma.Repo
+  alias Pleroma.User
   alias Pleroma.Web.OStatus
   alias Pleroma.Web.XML
-  alias Pleroma.{Object, Repo, User, Activity}
   import Pleroma.Factory
   import ExUnit.CaptureLog
 
+  setup_all do
+    Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
+    :ok
+  end
+
   test "don't insert create notes twice" do
     incoming = File.read!("test/fixtures/incoming_note_activity.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
@@ -15,34 +28,35 @@ defmodule Pleroma.Web.OStatusTest do
   test "handle incoming note - GS, Salmon" do
     incoming = File.read!("test/fixtures/incoming_note_activity.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
 
     user = User.get_by_ap_id(activity.data["actor"])
-    assert user.info["note_count"] == 1
+    assert user.info.note_count == 1
     assert activity.data["type"] == "Create"
-    assert activity.data["object"]["type"] == "Note"
+    assert object.data["type"] == "Note"
 
-    assert activity.data["object"]["id"] ==
-             "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note"
+    assert object.data["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note"
 
     assert activity.data["published"] == "2017-04-23T14:51:03+00:00"
-    assert activity.data["object"]["published"] == "2017-04-23T14:51:03+00:00"
+    assert object.data["published"] == "2017-04-23T14:51:03+00:00"
 
     assert activity.data["context"] ==
              "tag:gs.example.org:4040,2017-04-23:objectType=thread:nonce=f09e22f58abd5c7b"
 
     assert "http://pleroma.example.org:4000/users/lain3" in activity.data["to"]
-    assert activity.data["object"]["emoji"] == %{"marko" => "marko.png", "reimu" => "reimu.png"}
+    assert object.data["emoji"] == %{"marko" => "marko.png", "reimu" => "reimu.png"}
     assert activity.local == false
   end
 
   test "handle incoming notes - GS, subscription" do
     incoming = File.read!("test/fixtures/ostatus_incoming_post.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
 
     assert activity.data["type"] == "Create"
-    assert activity.data["object"]["type"] == "Note"
-    assert activity.data["object"]["actor"] == "https://social.heldscal.la/user/23211"
-    assert activity.data["object"]["content"] == "Will it blend?"
+    assert object.data["type"] == "Note"
+    assert object.data["actor"] == "https://social.heldscal.la/user/23211"
+    assert object.data["content"] == "Will it blend?"
     user = User.get_cached_by_ap_id(activity.data["actor"])
     assert User.ap_followers(user) in activity.data["to"]
     assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
@@ -51,20 +65,22 @@ defmodule Pleroma.Web.OStatusTest do
   test "handle incoming notes with attachments - GS, subscription" do
     incoming = File.read!("test/fixtures/incoming_websub_gnusocial_attachments.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
 
     assert activity.data["type"] == "Create"
-    assert activity.data["object"]["type"] == "Note"
-    assert activity.data["object"]["actor"] == "https://social.heldscal.la/user/23211"
-    assert activity.data["object"]["attachment"] |> length == 2
-    assert activity.data["object"]["external_url"] == "https://social.heldscal.la/notice/2020923"
+    assert object.data["type"] == "Note"
+    assert object.data["actor"] == "https://social.heldscal.la/user/23211"
+    assert object.data["attachment"] |> length == 2
+    assert object.data["external_url"] == "https://social.heldscal.la/notice/2020923"
     assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
   end
 
   test "handle incoming notes with tags" do
     incoming = File.read!("test/fixtures/ostatus_incoming_post_tag.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
 
-    assert activity.data["object"]["tag"] == ["nsfw"]
+    assert object.data["tag"] == ["nsfw"]
     assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
   end
 
@@ -79,10 +95,11 @@ defmodule Pleroma.Web.OStatusTest do
 
     incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
 
     assert activity.data["type"] == "Create"
-    assert activity.data["object"]["type"] == "Note"
-    assert activity.data["object"]["actor"] == "https://mastodon.social/users/lambadalambda"
+    assert object.data["type"] == "Note"
+    assert object.data["actor"] == "https://mastodon.social/users/lambadalambda"
     assert activity.data["context"] == "2hu"
     assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
   end
@@ -90,42 +107,47 @@ defmodule Pleroma.Web.OStatusTest do
   test "handle incoming notes - Mastodon, with CW" do
     incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
 
     assert activity.data["type"] == "Create"
-    assert activity.data["object"]["type"] == "Note"
-    assert activity.data["object"]["actor"] == "https://mastodon.social/users/lambadalambda"
-    assert activity.data["object"]["summary"] == "technologic"
+    assert object.data["type"] == "Note"
+    assert object.data["actor"] == "https://mastodon.social/users/lambadalambda"
+    assert object.data["summary"] == "technologic"
     assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
   end
 
   test "handle incoming unlisted messages, put public into cc" do
     incoming = File.read!("test/fixtures/mastodon-note-unlisted.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
+
     refute "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
     assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["cc"]
-    refute "https://www.w3.org/ns/activitystreams#Public" in activity.data["object"]["to"]
-    assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["object"]["cc"]
+    refute "https://www.w3.org/ns/activitystreams#Public" in object.data["to"]
+    assert "https://www.w3.org/ns/activitystreams#Public" in object.data["cc"]
   end
 
   test "handle incoming retweets - Mastodon, with CW" do
     incoming = File.read!("test/fixtures/cw_retweet.xml")
     {:ok, [[_activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
+    retweeted_object = Object.normalize(retweeted_activity.data["object"])
 
-    assert retweeted_activity.data["object"]["summary"] == "Hey."
+    assert retweeted_object.data["summary"] == "Hey."
   end
 
   test "handle incoming notes - GS, subscription, reply" do
     incoming = File.read!("test/fixtures/ostatus_incoming_reply.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
 
     assert activity.data["type"] == "Create"
-    assert activity.data["object"]["type"] == "Note"
-    assert activity.data["object"]["actor"] == "https://social.heldscal.la/user/23211"
+    assert object.data["type"] == "Note"
+    assert object.data["actor"] == "https://social.heldscal.la/user/23211"
 
-    assert activity.data["object"]["content"] ==
+    assert object.data["content"] ==
              "@<a href=\"https://gs.archae.me/user/4687\" class=\"h-card u-url p-nickname mention\" title=\"shpbot\">shpbot</a> why not indeed."
 
-    assert activity.data["object"]["inReplyTo"] ==
+    assert object.data["inReplyTo"] ==
              "tag:gs.archae.me,2017-04-30:noticeId=778260:objectType=note"
 
     assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
@@ -137,17 +159,18 @@ defmodule Pleroma.Web.OStatusTest do
 
     assert activity.data["type"] == "Announce"
     assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
-    assert activity.data["object"] == retweeted_activity.data["object"]["id"]
+    assert activity.data["object"] == retweeted_activity.data["object"]
     assert "https://pleroma.soykaf.com/users/lain" in activity.data["to"]
     refute activity.local
 
-    retweeted_activity = Repo.get(Activity, retweeted_activity.id)
+    retweeted_activity = Activity.get_by_id(retweeted_activity.id)
+    retweeted_object = Object.normalize(retweeted_activity.data["object"])
     assert retweeted_activity.data["type"] == "Create"
     assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
     refute retweeted_activity.local
-    assert retweeted_activity.data["object"]["announcement_count"] == 1
-    assert String.contains?(retweeted_activity.data["object"]["content"], "mastodon")
-    refute String.contains?(retweeted_activity.data["object"]["content"], "Test account")
+    assert retweeted_object.data["announcement_count"] == 1
+    assert String.contains?(retweeted_object.data["content"], "mastodon")
+    refute String.contains?(retweeted_object.data["content"], "Test account")
   end
 
   test "handle incoming retweets - GS, subscription - local message" do
@@ -168,7 +191,7 @@ defmodule Pleroma.Web.OStatusTest do
     assert user.ap_id in activity.data["to"]
     refute activity.local
 
-    retweeted_activity = Repo.get(Activity, retweeted_activity.id)
+    retweeted_activity = Activity.get_by_id(retweeted_activity.id)
     assert note_activity.id == retweeted_activity.id
     assert retweeted_activity.data["type"] == "Create"
     assert retweeted_activity.data["actor"] == user.ap_id
@@ -179,10 +202,11 @@ defmodule Pleroma.Web.OStatusTest do
   test "handle incoming retweets - Mastodon, salmon" do
     incoming = File.read!("test/fixtures/share.xml")
     {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)
+    retweeted_object = Object.normalize(retweeted_activity.data["object"])
 
     assert activity.data["type"] == "Announce"
     assert activity.data["actor"] == "https://mastodon.social/users/lambadalambda"
-    assert activity.data["object"] == retweeted_activity.data["object"]["id"]
+    assert activity.data["object"] == retweeted_activity.data["object"]
 
     assert activity.data["id"] ==
              "tag:mastodon.social,2017-05-03:objectId=4934452:objectType=Status"
@@ -191,7 +215,7 @@ defmodule Pleroma.Web.OStatusTest do
     assert retweeted_activity.data["type"] == "Create"
     assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
     refute retweeted_activity.local
-    refute String.contains?(retweeted_activity.data["object"]["content"], "Test account")
+    refute String.contains?(retweeted_object.data["content"], "Test account")
   end
 
   test "handle incoming favorites - GS, websub" do
@@ -201,7 +225,7 @@ defmodule Pleroma.Web.OStatusTest do
 
       assert activity.data["type"] == "Like"
       assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
-      assert activity.data["object"] == favorited_activity.data["object"]["id"]
+      assert activity.data["object"] == favorited_activity.data["object"]
 
       assert activity.data["id"] ==
                "tag:social.heldscal.la,2017-05-05:fave:23211:comment:2061643:2017-05-05T09:12:50+00:00"
@@ -210,7 +234,7 @@ defmodule Pleroma.Web.OStatusTest do
       assert favorited_activity.data["type"] == "Create"
       assert favorited_activity.data["actor"] == "https://shitposter.club/user/1"
 
-      assert favorited_activity.data["object"]["id"] ==
+      assert favorited_activity.data["object"] ==
                "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
 
       refute favorited_activity.local
@@ -245,17 +269,17 @@ defmodule Pleroma.Web.OStatusTest do
   test "handle incoming replies" do
     incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
+    object = Object.normalize(activity.data["object"])
 
     assert activity.data["type"] == "Create"
-    assert activity.data["object"]["type"] == "Note"
+    assert object.data["type"] == "Note"
 
-    assert activity.data["object"]["inReplyTo"] ==
+    assert object.data["inReplyTo"] ==
              "http://pleroma.example.org:4000/objects/55bce8fc-b423-46b1-af71-3759ab4670bc"
 
     assert "http://pleroma.example.org:4000/users/lain5" in activity.data["to"]
 
-    assert activity.data["object"]["id"] ==
-             "tag:gs.example.org:4040,2017-04-25:noticeId=55:objectType=note"
+    assert object.data["id"] == "tag:gs.example.org:4040,2017-04-25:noticeId=55:objectType=note"
 
     assert "https://www.w3.org/ns/activitystreams#Public" in activity.data["to"]
   end
@@ -278,6 +302,46 @@ defmodule Pleroma.Web.OStatusTest do
     assert User.following?(follower, followed)
   end
 
+  test "handle incoming unfollows with existing follow" do
+    incoming_follow = File.read!("test/fixtures/follow.xml")
+    {:ok, [_activity]} = OStatus.handle_incoming(incoming_follow)
+
+    incoming = File.read!("test/fixtures/unfollow.xml")
+    {:ok, [activity]} = OStatus.handle_incoming(incoming)
+
+    assert activity.data["type"] == "Undo"
+
+    assert activity.data["id"] ==
+             "undo:tag:social.heldscal.la,2017-05-07:subscription:23211:person:44803:2017-05-07T09:54:48+00:00"
+
+    assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
+    assert is_map(activity.data["object"])
+    assert activity.data["object"]["type"] == "Follow"
+    assert activity.data["object"]["object"] == "https://pawoo.net/users/pekorino"
+    refute activity.local
+
+    follower = User.get_by_ap_id(activity.data["actor"])
+    followed = User.get_by_ap_id(activity.data["object"]["object"])
+
+    refute User.following?(follower, followed)
+  end
+
+  test "it clears `unreachable` federation status of the sender" do
+    incoming_reaction_xml = File.read!("test/fixtures/share-gs.xml")
+    doc = XML.parse_document(incoming_reaction_xml)
+    actor_uri = XML.string_from_xpath("//author/uri[1]", doc)
+    reacted_to_author_uri = XML.string_from_xpath("//author/uri[2]", doc)
+
+    Instances.set_consistently_unreachable(actor_uri)
+    Instances.set_consistently_unreachable(reacted_to_author_uri)
+    refute Instances.reachable?(actor_uri)
+    refute Instances.reachable?(reacted_to_author_uri)
+
+    {:ok, _} = OStatus.handle_incoming(incoming_reaction_xml)
+    assert Instances.reachable?(actor_uri)
+    refute Instances.reachable?(reacted_to_author_uri)
+  end
+
   describe "new remote user creation" do
     test "returns local users" do
       local_user = insert(:user)
@@ -291,11 +355,11 @@ defmodule Pleroma.Web.OStatusTest do
 
       {:ok, user} = OStatus.find_or_make_user(uri)
 
-      user = Repo.get(Pleroma.User, user.id)
+      user = Pleroma.User.get_by_id(user.id)
       assert user.name == "Constance Variable"
       assert user.nickname == "lambadalambda@social.heldscal.la"
       assert user.local == false
-      assert user.info["uri"] == uri
+      assert user.info.uri == uri
       assert user.ap_id == uri
       assert user.bio == "Call me Deacon Blues."
       assert user.avatar["type"] == "Image"
@@ -305,6 +369,38 @@ defmodule Pleroma.Web.OStatusTest do
       assert user == user_again
     end
 
+    test "find_or_make_user sets all the nessary input fields" do
+      uri = "https://social.heldscal.la/user/23211"
+      {:ok, user} = OStatus.find_or_make_user(uri)
+
+      assert user.info ==
+               %Pleroma.User.Info{
+                 id: user.info.id,
+                 ap_enabled: false,
+                 background: %{},
+                 banner: %{},
+                 blocks: [],
+                 deactivated: false,
+                 default_scope: "public",
+                 domain_blocks: [],
+                 follower_count: 0,
+                 is_admin: false,
+                 is_moderator: false,
+                 keys: nil,
+                 locked: false,
+                 no_rich_text: false,
+                 note_count: 0,
+                 settings: nil,
+                 source_data: %{},
+                 hub: "https://social.heldscal.la/main/push/hub",
+                 magic_key:
+                   "RSA.uzg6r1peZU0vXGADWxGJ0PE34WvmhjUmydbX5YYdOiXfODVLwCMi1umGoqUDm-mRu4vNEdFBVJU1CpFA7dKzWgIsqsa501i2XqElmEveXRLvNRWFB6nG03Q5OUY2as8eE54BJm0p20GkMfIJGwP6TSFb-ICp3QjzbatuSPJ6xCE=.AQAB",
+                 salmon: "https://social.heldscal.la/main/salmon/user/23211",
+                 topic: "https://social.heldscal.la/api/statuses/user_timeline/23211.atom",
+                 uri: "https://social.heldscal.la/user/23211"
+               }
+    end
+
     test "find_make_or_update_user takes an author element and returns an updated user" do
       uri = "https://social.heldscal.la/user/23211"
 
@@ -410,7 +506,7 @@ defmodule Pleroma.Web.OStatusTest do
 
         assert activity.data["actor"] == "https://shitposter.club/user/1"
 
-        assert activity.data["object"]["id"] ==
+        assert activity.data["object"] ==
                  "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
       end)
     end
@@ -419,11 +515,11 @@ defmodule Pleroma.Web.OStatusTest do
       url = "https://social.sakamoto.gq/objects/0ccc1a2c-66b0-4305-b23a-7f7f2b040056"
       {:ok, [activity]} = OStatus.fetch_activity_from_url(url)
       assert activity.data["actor"] == "https://social.sakamoto.gq/users/eal"
-      assert activity.data["object"]["id"] == url
+      assert activity.data["object"] == url
     end
   end
 
-  test "it doesn't add nil in the do field" do
+  test "it doesn't add nil in the to field" do
     incoming = File.read!("test/fixtures/nil_mention_entry.xml")
     {:ok, [activity]} = OStatus.handle_incoming(incoming)
 
@@ -432,4 +528,30 @@ defmodule Pleroma.Web.OStatusTest do
              "https://www.w3.org/ns/activitystreams#Public"
            ]
   end
+
+  describe "is_representable?" do
+    test "Note objects are representable" do
+      note_activity = insert(:note_activity)
+
+      assert OStatus.is_representable?(note_activity)
+    end
+
+    test "Article objects are not representable" do
+      note_activity = insert(:note_activity)
+
+      note_object = Object.normalize(note_activity.data["object"])
+
+      note_data =
+        note_object.data
+        |> Map.put("type", "Article")
+
+      Cachex.clear(:object_cache)
+
+      cs = Object.change(note_object, %{data: note_data})
+      {:ok, _article_object} = Repo.update(cs)
+
+      # the underlying object is now an Article instead of a note, so this should fail
+      refute OStatus.is_representable?(note_activity)
+    end
+  end
 end