activity_pub_test.exs 37.5 KB
Newer Older
kaniini's avatar
kaniini committed
1
# Pleroma: A lightweight social networking server
minibikini's avatar
Reports  
minibikini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
kaniini's avatar
kaniini committed
3 4
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5 6
defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
  use Pleroma.DataCase
Haelwenn's avatar
Haelwenn committed
7
  alias Pleroma.Activity
8 9
  alias Pleroma.Builders.ActivityBuilder
  alias Pleroma.Instances
Haelwenn's avatar
Haelwenn committed
10 11
  alias Pleroma.Object
  alias Pleroma.User
12 13 14
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Web.ActivityPub.Utils
  alias Pleroma.Web.CommonAPI
lain's avatar
lain committed
15

lain's avatar
lain committed
16
  import Pleroma.Factory
Maksim's avatar
Maksim committed
17
  import Tesla.Mock
18
  import Mock
Maksim's avatar
Maksim committed
19 20 21 22 23

  setup do
    mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end
lain's avatar
lain committed
24

lain's avatar
lain committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  describe "fetching restricted by visibility" do
    test "it restricts by the appropriate visibility" do
      user = insert(:user)

      {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})

      {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})

      {:ok, unlisted_activity} =
        CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})

      {:ok, private_activity} =
        CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})

      activities =
        ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})

      assert activities == [direct_activity]

      activities =
        ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})

      assert activities == [unlisted_activity]

      activities =
        ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})

      assert activities == [private_activity]

      activities =
        ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})

      assert activities == [public_activity]
58 59 60 61 62 63 64 65

      activities =
        ActivityPub.fetch_activities([], %{
          :visibility => ~w[private public],
          "actor_id" => user.ap_id
        })

      assert activities == [public_activity, private_activity]
lain's avatar
lain committed
66 67 68
    end
  end

lain's avatar
lain committed
69 70 71 72 73 74
  describe "building a user from his ap id" do
    test "it returns a user" do
      user_id = "http://mastodon.example.org/users/admin"
      {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
      assert user.ap_id == user_id
      assert user.nickname == "admin@mastodon.example.org"
lain's avatar
lain committed
75 76
      assert user.info.source_data
      assert user.info.ap_enabled
lain's avatar
lain committed
77 78
      assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
    end
79 80 81 82 83 84 85 86

    test "it fetches the appropriate tag-restricted posts" do
      user = insert(:user)

      {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
      {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
      {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})

87 88 89 90
      fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})

      fetch_two =
        ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
91 92 93

      fetch_three =
        ActivityPub.fetch_activities([], %{
94
          "type" => "Create",
95 96 97 98
          "tag" => ["test", "essais"],
          "tag_reject" => ["reject"]
        })

Haelwenn's avatar
Haelwenn committed
99 100
      fetch_four =
        ActivityPub.fetch_activities([], %{
101
          "type" => "Create",
Haelwenn's avatar
Haelwenn committed
102 103 104 105
          "tag" => ["test"],
          "tag_all" => ["test", "reject"]
        })

106 107 108
      assert fetch_one == [status_one, status_three]
      assert fetch_two == [status_one, status_two, status_three]
      assert fetch_three == [status_one, status_two]
Haelwenn's avatar
Haelwenn committed
109
      assert fetch_four == [status_three]
110
    end
lain's avatar
lain committed
111 112
  end

lain's avatar
lain committed
113
  describe "insertion" do
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    test "drops activities beyond a certain limit" do
      limit = Pleroma.Config.get([:instance, :remote_limit])

      random_text =
        :crypto.strong_rand_bytes(limit + 1)
        |> Base.encode64()
        |> binary_part(0, limit + 1)

      data = %{
        "ok" => true,
        "object" => %{
          "content" => random_text
        }
      }

      assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
    end

132
    test "doesn't drop activities with content being null" do
lain's avatar
lain committed
133 134
      user = insert(:user)

135
      data = %{
lain's avatar
lain committed
136 137
        "actor" => user.ap_id,
        "to" => [],
138
        "object" => %{
lain's avatar
lain committed
139 140 141
          "actor" => user.ap_id,
          "to" => [],
          "type" => "Note",
142 143 144 145 146 147 148
          "content" => nil
        }
      }

      assert {:ok, _} = ActivityPub.insert(data)
    end

lain's avatar
lain committed
149 150
    test "returns the activity if one with the same id is already in" do
      activity = insert(:note_activity)
lain's avatar
lain committed
151
      {:ok, new_activity} = ActivityPub.insert(activity.data)
lain's avatar
lain committed
152

153
      assert activity.id == new_activity.id
lain's avatar
lain committed
154 155
    end

lain's avatar
lain committed
156
    test "inserts a given map into the activity database, giving it an id if it has none." do
lain's avatar
lain committed
157 158
      user = insert(:user)

lain's avatar
lain committed
159
      data = %{
lain's avatar
lain committed
160 161 162 163 164 165 166 167
        "actor" => user.ap_id,
        "to" => [],
        "object" => %{
          "actor" => user.ap_id,
          "to" => [],
          "type" => "Note",
          "content" => "hey"
        }
lain's avatar
lain committed
168 169 170
      }

      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
lain's avatar
lain committed
171 172 173 174
      assert activity.data["ok"] == data["ok"]
      assert is_binary(activity.data["id"])

      given_id = "bla"
lain's avatar
lain committed
175

lain's avatar
lain committed
176
      data = %{
177
        "id" => given_id,
lain's avatar
lain committed
178 179 180 181 182 183 184 185 186
        "actor" => user.ap_id,
        "to" => [],
        "context" => "blabla",
        "object" => %{
          "actor" => user.ap_id,
          "to" => [],
          "type" => "Note",
          "content" => "hey"
        }
lain's avatar
lain committed
187 188 189 190 191
      }

      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
      assert activity.data["ok"] == data["ok"]
      assert activity.data["id"] == given_id
192 193 194 195 196
      assert activity.data["context"] == "blabla"
      assert activity.data["context_id"]
    end

    test "adds a context when none is there" do
lain's avatar
lain committed
197 198
      user = insert(:user)

199
      data = %{
lain's avatar
lain committed
200 201
        "actor" => user.ap_id,
        "to" => [],
202
        "object" => %{
lain's avatar
lain committed
203 204 205 206
          "actor" => user.ap_id,
          "to" => [],
          "type" => "Note",
          "content" => "hey"
lain's avatar
lain committed
207
        }
208 209 210
      }

      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
Roman Chvanikov's avatar
Roman Chvanikov committed
211
      object = Pleroma.Object.normalize(activity)
212 213

      assert is_binary(activity.data["context"])
Roman Chvanikov's avatar
Roman Chvanikov committed
214
      assert is_binary(object.data["context"])
215
      assert activity.data["context_id"]
Roman Chvanikov's avatar
Roman Chvanikov committed
216
      assert object.data["context_id"]
lain's avatar
lain committed
217
    end
lain's avatar
lain committed
218

lain's avatar
lain committed
219
    test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
lain's avatar
lain committed
220 221
      user = insert(:user)

lain's avatar
lain committed
222
      data = %{
lain's avatar
lain committed
223 224
        "actor" => user.ap_id,
        "to" => [],
lain's avatar
lain committed
225
        "object" => %{
lain's avatar
lain committed
226 227
          "actor" => user.ap_id,
          "to" => [],
lain's avatar
lain committed
228
          "type" => "Note",
lain's avatar
lain committed
229
          "content" => "hey"
lain's avatar
lain committed
230 231 232 233
        }
      }

      {:ok, %Activity{} = activity} = ActivityPub.insert(data)
kaniini's avatar
kaniini committed
234 235 236 237
      object = Object.normalize(activity.data["object"])

      assert is_binary(object.data["id"])
      assert %Object{} = Object.get_by_ap_id(activity.data["object"])
lain's avatar
lain committed
238
    end
lain's avatar
lain committed
239 240
  end

lain's avatar
lain committed
241 242
  describe "create activities" do
    test "removes doubled 'to' recipients" do
eal's avatar
eal committed
243 244
      user = insert(:user)

lain's avatar
lain committed
245 246 247
      {:ok, activity} =
        ActivityPub.create(%{
          to: ["user1", "user1", "user2"],
eal's avatar
eal committed
248
          actor: user,
lain's avatar
lain committed
249
          context: "",
250 251 252 253 254
          object: %{
            "to" => ["user1", "user1", "user2"],
            "type" => "Note",
            "content" => "testing"
          }
lain's avatar
lain committed
255 256
        })

lain's avatar
lain committed
257
      assert activity.data["to"] == ["user1", "user2"]
eal's avatar
eal committed
258
      assert activity.actor == user.ap_id
Maxim Filippov's avatar
Maxim Filippov committed
259
      assert activity.recipients == ["user1", "user2", user.ap_id]
lain's avatar
lain committed
260
    end
261 262 263 264 265

    test "increases user note count only for public activities" do
      user = insert(:user)

      {:ok, _} =
minibikini's avatar
minibikini committed
266 267 268 269
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "1",
          "visibility" => "public"
        })
270 271

      {:ok, _} =
minibikini's avatar
minibikini committed
272 273 274 275
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "2",
          "visibility" => "unlisted"
        })
276 277

      {:ok, _} =
minibikini's avatar
minibikini committed
278 279 280 281
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "2",
          "visibility" => "private"
        })
282 283

      {:ok, _} =
minibikini's avatar
minibikini committed
284 285 286 287
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "3",
          "visibility" => "direct"
        })
288

minibikini's avatar
minibikini committed
289
      user = User.get_cached_by_id(user.id)
290 291
      assert user.info.note_count == 2
    end
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320

    test "increases replies count" do
      user = insert(:user)
      user2 = insert(:user)

      {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
      ap_id = activity.data["id"]
      reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}

      # public
      {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 1

      # unlisted
      {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2

      # private
      {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2

      # direct
      {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2
    end
lain's avatar
lain committed
321 322
  end

lain's avatar
lain committed
323 324 325 326
  describe "fetch activities for recipients" do
    test "retrieve the activities for certain recipients" do
      {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
      {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
lain's avatar
lain committed
327
      {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
lain's avatar
lain committed
328 329 330 331 332 333 334

      activities = ActivityPub.fetch_activities(["someone", "someone_else"])
      assert length(activities) == 2
      assert activities == [activity_one, activity_two]
    end
  end

lain's avatar
lain committed
335 336
  describe "fetch activities in context" do
    test "retrieves activities that have a given context" do
337 338 339 340
      {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
      {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
      {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
      {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
341 342
      activity_five = insert(:note_activity)
      user = insert(:user)
lain's avatar
lain committed
343

344
      {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
lain's avatar
lain committed
345

346
      activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
347
      assert activities == [activity_two, activity]
lain's avatar
lain committed
348 349 350
    end
  end

351 352 353
  test "doesn't return blocked activities" do
    activity_one = insert(:note_activity)
    activity_two = insert(:note_activity)
354
    activity_three = insert(:note_activity)
355
    user = insert(:user)
356
    booster = insert(:user)
357 358
    {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})

359 360
    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
361 362

    assert Enum.member?(activities, activity_two)
363
    assert Enum.member?(activities, activity_three)
364 365 366 367
    refute Enum.member?(activities, activity_one)

    {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})

368 369
    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
370 371

    assert Enum.member?(activities, activity_two)
372 373 374 375 376
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, activity_one)

    {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
    {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
377
    %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
378
    activity_three = Activity.get_by_id(activity_three.id)
379

380 381
    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
382 383 384 385

    assert Enum.member?(activities, activity_two)
    refute Enum.member?(activities, activity_three)
    refute Enum.member?(activities, boost_activity)
386
    assert Enum.member?(activities, activity_one)
lain's avatar
lain committed
387

388 389
    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
lain's avatar
lain committed
390 391

    assert Enum.member?(activities, activity_two)
392 393
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, boost_activity)
lain's avatar
lain committed
394
    assert Enum.member?(activities, activity_one)
395 396
  end

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
  test "doesn't return transitive interactions concerning blocked users" do
    blocker = insert(:user)
    blockee = insert(:user)
    friend = insert(:user)

    {:ok, blocker} = User.block(blocker, blockee)

    {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})

    {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})

    {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})

    {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})

    activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})

    assert Enum.member?(activities, activity_one)
    refute Enum.member?(activities, activity_two)
    refute Enum.member?(activities, activity_three)
    refute Enum.member?(activities, activity_four)
  end

420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
  test "doesn't return announce activities concerning blocked users" do
    blocker = insert(:user)
    blockee = insert(:user)
    friend = insert(:user)

    {:ok, blocker} = User.block(blocker, blockee)

    {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})

    {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})

    {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)

    activities =
      ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
      |> Enum.map(fn act -> act.id end)

    assert Enum.member?(activities, activity_one.id)
    refute Enum.member?(activities, activity_two.id)
    refute Enum.member?(activities, activity_three.id)
  end

lain's avatar
lain committed
442 443 444 445 446 447 448 449
  test "doesn't return muted activities" do
    activity_one = insert(:note_activity)
    activity_two = insert(:note_activity)
    activity_three = insert(:note_activity)
    user = insert(:user)
    booster = insert(:user)
    {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})

450 451
    activities =
      ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
lain's avatar
lain committed
452 453 454 455 456

    assert Enum.member?(activities, activity_two)
    assert Enum.member?(activities, activity_three)
    refute Enum.member?(activities, activity_one)

lain's avatar
lain committed
457
    # Calling with 'with_muted' will deliver muted activities, too.
458 459 460 461 462 463
    activities =
      ActivityPub.fetch_activities([], %{
        "muting_user" => user,
        "with_muted" => true,
        "skip_preload" => true
      })
lain's avatar
lain committed
464 465 466 467 468

    assert Enum.member?(activities, activity_two)
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, activity_one)

lain's avatar
lain committed
469 470
    {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})

471 472
    activities =
      ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
lain's avatar
lain committed
473 474 475 476 477 478 479 480

    assert Enum.member?(activities, activity_two)
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, activity_one)

    {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
    {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
    %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
481
    activity_three = Activity.get_by_id(activity_three.id)
lain's avatar
lain committed
482

483 484
    activities =
      ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
lain's avatar
lain committed
485 486 487 488 489 490

    assert Enum.member?(activities, activity_two)
    refute Enum.member?(activities, activity_three)
    refute Enum.member?(activities, boost_activity)
    assert Enum.member?(activities, activity_one)

491
    activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
lain's avatar
lain committed
492 493 494 495 496 497 498

    assert Enum.member?(activities, activity_two)
    assert Enum.member?(activities, activity_three)
    assert Enum.member?(activities, boost_activity)
    assert Enum.member?(activities, activity_one)
  end

499 500 501 502 503 504 505 506 507 508 509 510 511 512
  test "does include announces on request" do
    activity_three = insert(:note_activity)
    user = insert(:user)
    booster = insert(:user)

    {:ok, user} = User.follow(user, booster)

    {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)

    [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])

    assert announce_activity.id == announce.id
  end

513 514 515 516 517 518 519 520 521 522
  test "excludes reblogs on request" do
    user = insert(:user)
    {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
    {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})

    [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})

    assert activity == expected_activity
  end

lain's avatar
lain committed
523
  describe "public fetch activities" do
lain's avatar
lain committed
524 525
    test "doesn't retrieve unlisted activities" do
      user = insert(:user)
lain's avatar
lain committed
526

Maksim's avatar
Maksim committed
527
      {:ok, _unlisted_activity} =
lain's avatar
lain committed
528 529
        CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})

lain's avatar
lain committed
530 531 532 533 534 535 536
      {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})

      [activity] = ActivityPub.fetch_public_activities()

      assert activity == listed_activity
    end

lain's avatar
lain committed
537
    test "retrieves public activities" do
lain's avatar
lain committed
538
      _activities = ActivityPub.fetch_public_activities()
lain's avatar
lain committed
539

lain's avatar
lain committed
540
      %{public: public} = ActivityBuilder.public_and_non_public()
lain's avatar
lain committed
541

lain's avatar
lain committed
542
      activities = ActivityPub.fetch_public_activities()
lain's avatar
lain committed
543 544 545
      assert length(activities) == 1
      assert Enum.at(activities, 0) == public
    end
lain's avatar
lain committed
546 547 548 549 550

    test "retrieves a maximum of 20 activities" do
      activities = ActivityBuilder.insert_list(30)
      last_expected = List.last(activities)

lain's avatar
lain committed
551
      activities = ActivityPub.fetch_public_activities()
lain's avatar
lain committed
552 553 554 555 556
      last = List.last(activities)

      assert length(activities) == 20
      assert last == last_expected
    end
lain's avatar
lain committed
557 558 559 560 561 562 563

    test "retrieves ids starting from a since_id" do
      activities = ActivityBuilder.insert_list(30)
      later_activities = ActivityBuilder.insert_list(10)
      since_id = List.last(activities).id
      last_expected = List.last(later_activities)

lain's avatar
lain committed
564
      activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
lain's avatar
lain committed
565 566 567 568 569
      last = List.last(activities)

      assert length(activities) == 10
      assert last == last_expected
    end
570 571 572 573 574 575 576 577 578 579 580 581 582 583

    test "retrieves ids up to max_id" do
      _first_activities = ActivityBuilder.insert_list(10)
      activities = ActivityBuilder.insert_list(20)
      later_activities = ActivityBuilder.insert_list(10)
      max_id = List.first(later_activities).id
      last_expected = List.last(activities)

      activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
      last = List.last(activities)

      assert length(activities) == 20
      assert last == last_expected
    end
584 585 586 587 588 589 590 591 592 593 594

    test "doesn't return reblogs for users for whom reblogs have been muted" do
      activity = insert(:note_activity)
      user = insert(:user)
      booster = insert(:user)
      {:ok, user} = CommonAPI.hide_reblogs(user, booster)

      {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)

      activities = ActivityPub.fetch_activities([], %{"muting_user" => user})

rinpatch's avatar
rinpatch committed
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
      refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
    end

    test "returns reblogs for users for whom reblogs have not been muted" do
      activity = insert(:note_activity)
      user = insert(:user)
      booster = insert(:user)
      {:ok, user} = CommonAPI.hide_reblogs(user, booster)
      {:ok, user} = CommonAPI.show_reblogs(user, booster)

      {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)

      activities = ActivityPub.fetch_activities([], %{"muting_user" => user})

      assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
610
    end
lain's avatar
lain committed
611
  end
lain's avatar
lain committed
612

lain's avatar
lain committed
613 614 615 616 617 618 619 620 621 622 623 624
  describe "like an object" do
    test "adds a like activity to the db" do
      note_activity = insert(:note_activity)
      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
      user = insert(:user)
      user_two = insert(:user)

      {:ok, like_activity, object} = ActivityPub.like(user, object)

      assert like_activity.data["actor"] == user.ap_id
      assert like_activity.data["type"] == "Like"
      assert like_activity.data["object"] == object.data["id"]
625
      assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
lain's avatar
lain committed
626
      assert like_activity.data["context"] == object.data["context"]
lain's avatar
lain committed
627
      assert object.data["like_count"] == 1
lain's avatar
lain committed
628 629 630 631 632 633 634
      assert object.data["likes"] == [user.ap_id]

      # Just return the original activity if the user already liked it.
      {:ok, same_like_activity, object} = ActivityPub.like(user, object)

      assert like_activity == same_like_activity
      assert object.data["likes"] == [user.ap_id]
lain's avatar
lain committed
635

636
      [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
lain's avatar
lain committed
637 638 639 640 641 642 643
      assert note_activity.data["object"]["like_count"] == 1

      {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
      assert object.data["like_count"] == 2
    end
  end

lain's avatar
lain committed
644 645 646 647 648 649 650 651 652 653 654 655 656
  describe "unliking" do
    test "unliking a previously liked object" do
      note_activity = insert(:note_activity)
      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
      user = insert(:user)

      # Unliking something that hasn't been liked does nothing
      {:ok, object} = ActivityPub.unlike(user, object)
      assert object.data["like_count"] == 0

      {:ok, like_activity, object} = ActivityPub.like(user, object)
      assert object.data["like_count"] == 1

Thog's avatar
Thog committed
657
      {:ok, _, _, object} = ActivityPub.unlike(user, object)
lain's avatar
lain committed
658 659
      assert object.data["like_count"] == 0

660
      assert Activity.get_by_id(like_activity.id) == nil
lain's avatar
lain committed
661 662 663
    end
  end

lain's avatar
lain committed
664 665 666 667 668 669 670 671 672
  describe "announcing an object" do
    test "adds an announce activity to the db" do
      note_activity = insert(:note_activity)
      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
      user = insert(:user)

      {:ok, announce_activity, object} = ActivityPub.announce(user, object)
      assert object.data["announcement_count"] == 1
      assert object.data["announcements"] == [user.ap_id]
lain's avatar
lain committed
673 674 675 676 677 678

      assert announce_activity.data["to"] == [
               User.ap_followers(user),
               note_activity.data["actor"]
             ]

lain's avatar
lain committed
679 680
      assert announce_activity.data["object"] == object.data["id"]
      assert announce_activity.data["actor"] == user.ap_id
lain's avatar
lain committed
681
      assert announce_activity.data["context"] == object.data["context"]
lain's avatar
lain committed
682 683 684
    end
  end

normandy's avatar
normandy committed
685 686 687 688 689 690 691
  describe "unannouncing an object" do
    test "unannouncing a previously announced object" do
      note_activity = insert(:note_activity)
      object = Object.get_by_ap_id(note_activity.data["object"]["id"])
      user = insert(:user)

      # Unannouncing an object that is not announced does nothing
normandy's avatar
normandy committed
692 693
      # {:ok, object} = ActivityPub.unannounce(user, object)
      # assert object.data["announcement_count"] == 0
normandy's avatar
normandy committed
694 695 696 697

      {:ok, announce_activity, object} = ActivityPub.announce(user, object)
      assert object.data["announcement_count"] == 1

698
      {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
normandy's avatar
normandy committed
699 700
      assert object.data["announcement_count"] == 0

701 702
      assert unannounce_activity.data["to"] == [
               User.ap_followers(user),
normandy's avatar
normandy committed
703
               announce_activity.data["actor"]
normandy's avatar
normandy committed
704
             ]
normandy's avatar
normandy committed
705

706
      assert unannounce_activity.data["type"] == "Undo"
707
      assert unannounce_activity.data["object"] == announce_activity.data
708
      assert unannounce_activity.data["actor"] == user.ap_id
normandy's avatar
normandy committed
709
      assert unannounce_activity.data["context"] == announce_activity.data["context"]
710

711
      assert Activity.get_by_id(announce_activity.id) == nil
normandy's avatar
normandy committed
712 713 714
    end
  end

lain's avatar
lain committed
715 716
  describe "uploading files" do
    test "copies the file to the configured folder" do
lain's avatar
lain committed
717 718 719 720 721
      file = %Plug.Upload{
        content_type: "image/jpg",
        path: Path.absname("test/fixtures/image.jpg"),
        filename: "an_image.jpg"
      }
lain's avatar
lain committed
722 723 724 725

      {:ok, %Object{} = object} = ActivityPub.upload(file)
      assert object.data["name"] == "an_image.jpg"
    end
lain's avatar
lain committed
726 727 728

    test "works with base64 encoded images" do
      file = %{
lain's avatar
lain committed
729
        "img" => data_uri()
lain's avatar
lain committed
730 731
      }

lain's avatar
lain committed
732
      {:ok, %Object{}} = ActivityPub.upload(file)
lain's avatar
lain committed
733
    end
lain's avatar
lain committed
734
  end
lain's avatar
lain committed
735

dtluna's avatar
dtluna committed
736 737 738 739 740
  describe "fetch the latest Follow" do
    test "fetches the latest Follow activity" do
      %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
      follower = Repo.get_by(User, ap_id: activity.data["actor"])
      followed = Repo.get_by(User, ap_id: activity.data["object"])
741

lain's avatar
lain committed
742
      assert activity == Utils.fetch_latest_follow(follower, followed)
743
    end
744 745
  end

746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
  describe "following / unfollowing" do
    test "creates a follow activity" do
      follower = insert(:user)
      followed = insert(:user)

      {:ok, activity} = ActivityPub.follow(follower, followed)
      assert activity.data["type"] == "Follow"
      assert activity.data["actor"] == follower.ap_id
      assert activity.data["object"] == followed.ap_id
    end

    test "creates an undo activity for the last follow" do
      follower = insert(:user)
      followed = insert(:user)

      {:ok, follow_activity} = ActivityPub.follow(follower, followed)
      {:ok, activity} = ActivityPub.unfollow(follower, followed)

      assert activity.data["type"] == "Undo"
      assert activity.data["actor"] == follower.ap_id
766 767 768 769

      assert is_map(activity.data["object"])
      assert activity.data["object"]["type"] == "Follow"
      assert activity.data["object"]["object"] == followed.ap_id
normandy's avatar
normandy committed
770
      assert activity.data["object"]["id"] == follow_activity.data["id"]
771 772 773
    end
  end

774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802
  describe "blocking / unblocking" do
    test "creates a block activity" do
      blocker = insert(:user)
      blocked = insert(:user)

      {:ok, activity} = ActivityPub.block(blocker, blocked)

      assert activity.data["type"] == "Block"
      assert activity.data["actor"] == blocker.ap_id
      assert activity.data["object"] == blocked.ap_id
    end

    test "creates an undo activity for the last block" do
      blocker = insert(:user)
      blocked = insert(:user)

      {:ok, block_activity} = ActivityPub.block(blocker, blocked)
      {:ok, activity} = ActivityPub.unblock(blocker, blocked)

      assert activity.data["type"] == "Undo"
      assert activity.data["actor"] == blocker.ap_id

      assert is_map(activity.data["object"])
      assert activity.data["object"]["type"] == "Block"
      assert activity.data["object"]["object"] == blocked.ap_id
      assert activity.data["object"]["id"] == block_activity.data["id"]
    end
  end

lain's avatar
lain committed
803 804 805 806 807 808 809 810 811 812
  describe "deletion" do
    test "it creates a delete activity and deletes the original object" do
      note = insert(:note_activity)
      object = Object.get_by_ap_id(note.data["object"]["id"])
      {:ok, delete} = ActivityPub.delete(object)

      assert delete.data["type"] == "Delete"
      assert delete.data["actor"] == note.data["actor"]
      assert delete.data["object"] == note.data["object"]["id"]

813
      assert Activity.get_by_id(delete.id) != nil
lain's avatar
lain committed
814

Maxim Filippov's avatar
Maxim Filippov committed
815
      assert Repo.get(Object, object.id).data["type"] == "Tombstone"
lain's avatar
lain committed
816
    end
817 818 819 820 821

    test "decrements user note count only for public activities" do
      user = insert(:user, info: %{note_count: 10})

      {:ok, a1} =
minibikini's avatar
minibikini committed
822 823 824 825
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "yeah",
          "visibility" => "public"
        })
826 827

      {:ok, a2} =
minibikini's avatar
minibikini committed
828 829 830 831
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "yeah",
          "visibility" => "unlisted"
        })
832 833

      {:ok, a3} =
minibikini's avatar
minibikini committed
834 835 836 837
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "yeah",
          "visibility" => "private"
        })
838 839

      {:ok, a4} =
minibikini's avatar
minibikini committed
840 841 842 843
        CommonAPI.post(User.get_cached_by_id(user.id), %{
          "status" => "yeah",
          "visibility" => "direct"
        })
844

rinpatch's avatar
rinpatch committed
845 846 847 848
      {:ok, _} = Object.normalize(a1) |> ActivityPub.delete()
      {:ok, _} = Object.normalize(a2) |> ActivityPub.delete()
      {:ok, _} = Object.normalize(a3) |> ActivityPub.delete()
      {:ok, _} = Object.normalize(a4) |> ActivityPub.delete()
849

minibikini's avatar
minibikini committed
850
      user = User.get_cached_by_id(user.id)
851 852
      assert user.info.note_count == 10
    end
853

Karen Konou's avatar
Karen Konou committed
854 855 856
    test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
      user = insert(:user)
      note = insert(:note_activity)
857 858 859 860 861 862 863 864 865 866 867 868 869

      {:ok, object} =
        Object.get_by_ap_id(note.data["object"]["id"])
        |> Object.change(%{
          data: %{
            "actor" => note.data["object"]["actor"],
            "id" => note.data["object"]["id"],
            "to" => [user.ap_id],
            "type" => "Note"
          }
        })
        |> Object.update_and_set_cache()

Karen Konou's avatar
Karen Konou committed
870 871 872 873
      {:ok, delete} = ActivityPub.delete(object)

      assert user.ap_id in delete.data["to"]
    end
874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903

    test "decreases reply count" do
      user = insert(:user)
      user2 = insert(:user)

      {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
      reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
      ap_id = activity.data["id"]

      {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
      {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
      {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
      {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))

      _ = CommonAPI.delete(direct_reply.id, user2)
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2

      _ = CommonAPI.delete(private_reply.id, user2)
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 2

      _ = CommonAPI.delete(public_reply.id, user2)
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 1

      _ = CommonAPI.delete(unlisted_reply.id, user2)
      assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
      assert object.data["repliesCount"] == 0
    end
lain's avatar
lain committed
904 905
  end

906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922
  describe "timeline post-processing" do
    test "it filters broken threads" do
      user1 = insert(:user)
      user2 = insert(:user)
      user3 = insert(:user)

      {:ok, user1} = User.follow(user1, user3)
      assert User.following?(user1, user3)

      {:ok, user2} = User.follow(user2, user3)
      assert User.following?(user2, user3)

      {:ok, user3} = User.follow(user3, user2)
      assert User.following?(user3, user2)

      {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})

kaniini's avatar
kaniini committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
      {:ok, private_activity_1} =
        CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})

      {:ok, private_activity_2} =
        CommonAPI.post(user2, %{
          "status" => "hi 3",
          "visibility" => "private",
          "in_reply_to_status_id" => private_activity_1.id
        })

      {:ok, private_activity_3} =
        CommonAPI.post(user3, %{
          "status" => "hi 4",
          "visibility" => "private",
          "in_reply_to_status_id" => private_activity_2.id
        })
939

kaniini's avatar
kaniini committed
940
      activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
941

942
      private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
943

944 945 946
      assert [public_activity, private_activity_1, private_activity_3] ==
               activities

947 948 949 950 951 952 953 954 955
      assert length(activities) == 3

      activities = ActivityPub.contain_timeline(activities, user1)

      assert [public_activity, private_activity_1] == activities
      assert length(activities) == 2
    end
  end

lain's avatar
lain committed
956 957 958 959 960
  describe "update" do
    test "it creates an update activity with the new user data" do
      user = insert(:user)
      {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
      user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
lain's avatar
lain committed
961 962 963 964 965 966 967 968

      {:ok, update} =
        ActivityPub.update(%{
          actor: user_data["id"],
          to: [user.follower_address],
          cc: [],
          object: user_data
        })
lain's avatar
lain committed
969 970 971 972 973 974 975 976

      assert update.data["actor"] == user.ap_id
      assert update.data["to"] == [user.follower_address]
      assert update.data["object"]["id"] == user_data["id"]
      assert update.data["object"]["type"] == user_data["type"]
    end
  end

minibikini's avatar
minibikini committed
977 978
  test "returned pinned statuses" do
    Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
minibikini's avatar
minibikini committed
979 980 981 982 983 984 985
    user = insert(:user)

    {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
    {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
    {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})

    CommonAPI.pin(activity_one.id, user)
minibikini's avatar
minibikini committed
986
    user = refresh_record(user)
minibikini's avatar
minibikini committed
987 988

    CommonAPI.pin(activity_two.id, user)
minibikini's avatar
minibikini committed
989
    user = refresh_record(user)
minibikini's avatar
minibikini committed
990 991

    CommonAPI.pin(activity_three.id, user)
minibikini's avatar
minibikini committed
992
    user = refresh_record(user)
minibikini's avatar
minibikini committed
993 994 995 996 997 998

    activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})

    assert 3 = length(activities)
  end

minibikini's avatar
Reports  
minibikini committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
  test "it can create a Flag activity" do
    reporter = insert(:user)
    target_account = insert(:user)
    {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
    context = Utils.generate_context_id()
    content = "foobar"

    reporter_ap_id = reporter.ap_id
    target_ap_id = target_account.ap_id
    activity_ap_id = activity.data["id"]

    assert {:ok, activity} =
             ActivityPub.flag(%{
               actor: reporter,
               context: context,
               account: target_account,
               statuses: [activity],
               content: content
             })

    assert %Activity{
             actor: ^reporter_ap_id,
             data: %{
               "type" => "Flag",
               "content" => ^content,
               "context" => ^context,
               "object" => [^target_ap_id, ^activity_ap_id]
             }
           } = activity
  end

1030
  describe "publish_one/1" do
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
    test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
                   Instances,
                   [:passthrough],
                   [] do
      actor = insert(:user)
      inbox = "http://200.site/users/nick1/inbox"

      assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})

      assert called(Instances.set_reachable(inbox))
    end

    test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
                   Instances,
                   [:passthrough],
                   [] do
      actor = insert(:user)
      inbox = "http://200.site/users/nick1/inbox"

      assert {:ok, _} =
               ActivityPub.publish_one(%{
                 inbox: inbox,
                 json: "{}",
                 actor: actor,
                 id: 1,
                 unreachable_since: NaiveDateTime.utc_now()
               })

      assert called(Instances.set_reachable(inbox))
    end

    test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
                   Instances,
                   [:passthrough],
                   [] do
      actor = insert(:user)
      inbox = "http://200.site/users/nick1/inbox"

      assert {:ok, _} =
               ActivityPub.publish_one(%{
                 inbox: inbox,
                 json: "{}",
                 actor: actor,
                 id: 1,
                 unreachable_since: nil
               })

      refute called(Instances.set_reachable(inbox))
    end

    test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
                   Instances,
                   [:passthrough],
                   [] do
      actor = insert(:user)
      inbox = "http://404.site/users/nick1/inbox"

      assert {:error, _} =
               ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})

      assert called(Instances.set_unreachable(inbox))
    end

    test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
                   Instances,
                   [:passthrough],
                   [] do
      actor = insert(:user)
      inbox = "http://connrefused.site/users/nick1/inbox"

      assert {:error, _} =
               ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})

      assert called(Instances.set_unreachable(inbox))
    end

1107
    test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1108 1109 1110 1111 1112 1113 1114 1115 1116 1117
                   Instances,
                   [:passthrough],
                   [] do
      actor = insert(:user)
      inbox = "http://200.site/users/nick1/inbox"

      assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})

      refute called(Instances.set_unreachable(inbox))
    end
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136

    test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
                   Instances,
                   [:passthrough],
                   [] do
      actor = insert(:user)
      inbox = "http://connrefused.site/users/nick1/inbox"

      assert {:error, _} =
               ActivityPub.publish_one(%{
                 inbox: inbox,
                 json: "{}",
                 actor: actor,
                 id: 1,
                 unreachable_since: NaiveDateTime.utc_now()
               })

      refute called(Instances.set_unreachable(inbox))
    end
1137 1138
  end

lain's avatar
lain committed
1139
  def data_uri do
lain's avatar
lain committed
1140
    File.read!("test/fixtures/avatar_data_uri")
lain's avatar
lain committed
1141
  end
lain's avatar
lain committed
1142
end