ostatus_test.exs 13.7 KB
Newer Older
1 2 3
defmodule Pleroma.Web.OStatusTest do
  use Pleroma.DataCase
  alias Pleroma.Web.OStatus
4
  alias Pleroma.Web.XML
5
  alias Pleroma.{Object, Repo, User, Activity}
6
  import Pleroma.Factory
7

lain's avatar
lain committed
8 9
  test "don't insert create notes twice" do
    incoming = File.read!("test/fixtures/incoming_note_activity.xml")
10 11
    {:ok, [activity]} = OStatus.handle_incoming(incoming)
    assert {:ok, [activity]} == OStatus.handle_incoming(incoming)
lain's avatar
lain committed
12 13
  end

lain's avatar
lain committed
14
  test "handle incoming note - GS, Salmon" do
15
    incoming = File.read!("test/fixtures/incoming_note_activity.xml")
lain's avatar
lain committed
16
    {:ok, [activity]} = OStatus.handle_incoming(incoming)
17 18 19

    assert activity.data["type"] == "Create"
    assert activity.data["object"]["type"] == "Note"
20
    assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-23:noticeId=29:objectType=note"
21
    assert activity.data["published"] == "2017-04-23T14:51:03+00:00"
lain's avatar
lain committed
22
    assert activity.data["context"] == "tag:gs.example.org:4040,2017-04-23:objectType=thread:nonce=f09e22f58abd5c7b"
lain's avatar
lain committed
23
    assert "http://pleroma.example.org:4000/users/lain3" in activity.data["to"]
lain's avatar
lain committed
24
    assert activity.local == false
25 26
  end

lain's avatar
lain committed
27 28 29 30 31 32 33 34
  test "handle incoming notes - GS, subscription" do
    incoming = File.read!("test/fixtures/ostatus_incoming_post.xml")
    {:ok, [activity]} = OStatus.handle_incoming(incoming)

    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?"
lain's avatar
lain committed
35 36
    user = User.get_cached_by_ap_id(activity.data["actor"])
    assert User.ap_followers(user) in activity.data["to"]
lain's avatar
lain committed
37 38
  end

39 40 41 42 43 44 45 46 47 48
  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)

    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
  end

lain's avatar
lain committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  test "handle incoming notes - Mastodon, salmon, reply" do
    # It uses the context of the replied to object
    Repo.insert!(%Object{
          data: %{
            "id" => "https://pleroma.soykaf.com/objects/c237d966-ac75-4fe3-a87a-d89d71a3a7a4",
            "context" => "2hu"
          }})
    incoming = File.read!("test/fixtures/incoming_reply_mastodon.xml")
    {:ok, [activity]} = OStatus.handle_incoming(incoming)

    assert activity.data["type"] == "Create"
    assert activity.data["object"]["type"] == "Note"
    assert activity.data["object"]["actor"] == "https://mastodon.social/users/lambadalambda"
    assert activity.data["context"] == "2hu"
  end

lain's avatar
lain committed
65 66 67 68 69 70 71 72 73 74
  test "handle incoming notes - Mastodon, with CW" do
    incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
    {:ok, [activity]} = OStatus.handle_incoming(incoming)

    assert activity.data["type"] == "Create"
    assert activity.data["object"]["type"] == "Note"
    assert activity.data["object"]["actor"] == "https://mastodon.social/users/lambadalambda"
    assert String.contains?(activity.data["object"]["content"], "technologic")
  end

lain's avatar
lain committed
75 76 77 78 79 80 81
  test "handle incoming retweets - Mastodon, with CW" do
    incoming = File.read!("test/fixtures/cw_retweet.xml")
    {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)

    assert String.contains?(retweeted_activity.data["object"]["content"], "Hey.")
  end

lain's avatar
lain committed
82 83 84 85 86 87 88 89 90 91 92
  test "handle incoming notes - GS, subscription, reply" do
    incoming = File.read!("test/fixtures/ostatus_incoming_reply.xml")
    {:ok, [activity]} = OStatus.handle_incoming(incoming)

    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"] == "@<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"] == "tag:gs.archae.me,2017-04-30:noticeId=778260:objectType=note"
  end

lain's avatar
lain committed
93 94 95 96 97 98 99
  test "handle incoming retweets - GS, subscription" do
    incoming = File.read!("test/fixtures/share-gs.xml")
    {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)

    assert activity.data["type"] == "Announce"
    assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
    assert activity.data["object"] == retweeted_activity.data["object"]["id"]
100
    assert "https://pleroma.soykaf.com/users/lain" in activity.data["to"]
lain's avatar
lain committed
101
    refute activity.local
102 103

    retweeted_activity = Repo.get(Activity, retweeted_activity.id)
lain's avatar
lain committed
104 105 106
    assert retweeted_activity.data["type"] == "Create"
    assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
    refute retweeted_activity.local
107
    assert retweeted_activity.data["object"]["announcement_count"] == 1
lain's avatar
lain committed
108
    assert String.contains?(retweeted_activity.data["object"]["content"], "mastodon")
lain's avatar
lain committed
109
    refute String.contains?(retweeted_activity.data["object"]["content"], "Test account")
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
  end

  test "handle incoming retweets - GS, subscription - local message" do
    incoming = File.read!("test/fixtures/share-gs-local.xml")
    note_activity = insert(:note_activity)
    user = User.get_cached_by_ap_id(note_activity.data["actor"])
    incoming = incoming
    |> String.replace("LOCAL_ID", note_activity.data["object"]["id"])
    |> String.replace("LOCAL_USER", user.ap_id)

    {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)

    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 user.ap_id in activity.data["to"]
    refute activity.local

    retweeted_activity = Repo.get(Activity, retweeted_activity.id)
    assert note_activity.id == retweeted_activity.id
    assert retweeted_activity.data["type"] == "Create"
    assert retweeted_activity.data["actor"] == user.ap_id
    assert retweeted_activity.local
    assert retweeted_activity.data["object"]["announcement_count"] == 1
lain's avatar
lain committed
134 135 136 137 138 139 140 141 142
  end

  test "handle incoming retweets - Mastodon, salmon" do
    incoming = File.read!("test/fixtures/share.xml")
    {:ok, [[activity, retweeted_activity]]} = OStatus.handle_incoming(incoming)

    assert activity.data["type"] == "Announce"
    assert activity.data["actor"] == "https://mastodon.social/users/lambadalambda"
    assert activity.data["object"] == retweeted_activity.data["object"]["id"]
lain's avatar
lain committed
143 144
    assert activity.data["id"] == "tag:mastodon.social,2017-05-03:objectId=4934452:objectType=Status"

lain's avatar
lain committed
145 146 147 148
    refute activity.local
    assert retweeted_activity.data["type"] == "Create"
    assert retweeted_activity.data["actor"] == "https://pleroma.soykaf.com/users/lain"
    refute retweeted_activity.local
lain's avatar
lain committed
149
    refute String.contains?(retweeted_activity.data["object"]["content"], "Test account")
lain's avatar
lain committed
150 151
  end

152 153
  test "handle incoming favorites - GS, websub" do
    incoming = File.read!("test/fixtures/favorite.xml")
154
    {:ok, [[activity, favorited_activity]]} = OStatus.handle_incoming(incoming)
155 156 157

    assert activity.data["type"] == "Like"
    assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
158
    assert activity.data["object"] == favorited_activity.data["object"]["id"]
lain's avatar
lain committed
159 160
    assert activity.data["id"] == "tag:social.heldscal.la,2017-05-05:fave:23211:comment:2061643:2017-05-05T09:12:50+00:00"

161
    refute activity.local
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    assert favorited_activity.data["type"] == "Create"
    assert favorited_activity.data["actor"] == "https://shitposter.club/user/1"
    assert favorited_activity.data["object"]["id"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
    refute favorited_activity.local
  end

  test "handle incoming favorites with locally available object - GS, websub" do
    note_activity = insert(:note_activity)

    incoming = File.read!("test/fixtures/favorite_with_local_note.xml")
    |> String.replace("localid", note_activity.data["object"]["id"])

    {:ok, [[activity, favorited_activity]]} = OStatus.handle_incoming(incoming)

    assert activity.data["type"] == "Like"
    assert activity.data["actor"] == "https://social.heldscal.la/user/23211"
    assert activity.data["object"] == favorited_activity.data["object"]["id"]
    refute activity.local
    assert note_activity.id == favorited_activity.id
    assert favorited_activity.local
182 183
  end

lain's avatar
lain committed
184 185
  test "handle incoming replies" do
    incoming = File.read!("test/fixtures/incoming_note_activity_answer.xml")
lain's avatar
lain committed
186
    {:ok, [activity]} = OStatus.handle_incoming(incoming)
lain's avatar
lain committed
187 188 189 190 191

    assert activity.data["type"] == "Create"
    assert activity.data["object"]["type"] == "Note"
    assert activity.data["object"]["inReplyTo"] == "http://pleroma.example.org:4000/objects/55bce8fc-b423-46b1-af71-3759ab4670bc"
    assert "http://pleroma.example.org:4000/users/lain5" in activity.data["to"]
lain's avatar
lain committed
192
    assert activity.data["object"]["id"] == "tag:gs.example.org:4040,2017-04-25:noticeId=55:objectType=note"
lain's avatar
lain committed
193 194
  end

lain's avatar
lain committed
195 196 197 198 199 200 201 202 203
  test "handle incoming follows" do
    incoming = File.read!("test/fixtures/follow.xml")
    {:ok, [activity]} = OStatus.handle_incoming(incoming)
    assert activity.data["type"] == "Follow"
    assert activity.data["id"] == "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 activity.data["object"] == "https://pawoo.net/users/pekorino"
    refute activity.local

lain's avatar
lain committed
204 205
    follower = User.get_by_ap_id(activity.data["actor"])
    followed = User.get_by_ap_id(activity.data["object"])
lain's avatar
lain committed
206 207 208 209

    assert User.following?(follower, followed)
  end

210
  describe "new remote user creation" do
lain's avatar
lain committed
211 212 213 214 215 216 217
    test "returns local users" do
      local_user = insert(:user)
      {:ok, user} = OStatus.find_or_make_user(local_user.ap_id)

      assert user == local_user
    end

218
    test "tries to use the information in poco fields" do
lain's avatar
lain committed
219
      uri = "https://social.heldscal.la/user/23211"
220

lain's avatar
lain committed
221
      {:ok, user} = OStatus.find_or_make_user(uri)
222

lain's avatar
lain committed
223
      user = Repo.get(Pleroma.User, user.id)
224
      assert user.name == "Constance Variable"
lain's avatar
lain committed
225
      assert user.nickname == "lambadalambda@social.heldscal.la"
226
      assert user.local == false
lain's avatar
lain committed
227 228
      assert user.info["uri"] == uri
      assert user.ap_id == uri
lain's avatar
lain committed
229
      assert user.avatar["type"] == "Image"
230

lain's avatar
lain committed
231
      {:ok, user_again} = OStatus.find_or_make_user(uri)
232 233 234

      assert user == user_again
    end
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252

    test "find_make_or_update_user takes an author element and returns an updated user" do
      uri = "https://social.heldscal.la/user/23211"

      {:ok, user} = OStatus.find_or_make_user(uri)
      change = Ecto.Changeset.change(user, %{avatar: nil})

      {:ok, user} = Repo.update(change)
      refute user.avatar

      doc = XML.parse_document(File.read!("test/fixtures/23211.atom"))
      [author] = :xmerl_xpath.string('//author[1]', doc)
      {:ok, user} = OStatus.find_make_or_update_user(author)
      assert user.avatar["type"] == "Image"

      {:ok, user_again} = OStatus.find_make_or_update_user(author)
      assert user_again == user
    end
253
  end
lain's avatar
lain committed
254 255 256 257 258 259 260 261 262

  describe "gathering user info from a user id" do
    test "it returns user info in a hash" do
      user = "shp@social.heldscal.la"

      # TODO: make test local
      {:ok, data} = OStatus.gather_user_info(user)

      expected = %{
263 264 265 266 267 268 269 270 271 272 273
        "hub" => "https://social.heldscal.la/main/push/hub",
        "magic_key" => "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
        "name" => "shp",
        "nickname" => "shp",
        "salmon" => "https://social.heldscal.la/main/salmon/user/29191",
        "subject" => "acct:shp@social.heldscal.la",
        "topic" => "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
        "uri" => "https://social.heldscal.la/user/29191",
        "host" => "social.heldscal.la",
        "fqn" => user,
        "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}
lain's avatar
lain committed
274 275 276
      }
      assert data == expected
    end
lain's avatar
lain committed
277 278 279 280 281 282 283 284

    test "it works with the uri" do
      user = "https://social.heldscal.la/user/29191"

      # TODO: make test local
      {:ok, data} = OStatus.gather_user_info(user)

      expected = %{
285 286 287 288 289 290 291 292 293 294 295
        "hub" => "https://social.heldscal.la/main/push/hub",
        "magic_key" => "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
        "name" => "shp",
        "nickname" => "shp",
        "salmon" => "https://social.heldscal.la/main/salmon/user/29191",
        "subject" => "https://social.heldscal.la/user/29191",
        "topic" => "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
        "uri" => "https://social.heldscal.la/user/29191",
        "host" => "social.heldscal.la",
        "fqn" => user,
        "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}
lain's avatar
lain committed
296 297 298
      }
      assert data == expected
    end
lain's avatar
lain committed
299
  end
300 301 302 303 304 305 306 307

  describe "fetching a status by it's HTML url" do
    test "it builds a missing status from an html url" do
      url = "https://shitposter.club/notice/2827873"
      {:ok, [activity] } = OStatus.fetch_activity_from_html_url(url)

      assert activity.data["actor"] == "https://shitposter.club/user/1"
      assert activity.data["object"]["id"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment"
lain's avatar
lain committed
308
    end
309
  end
310
end