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

5 6 7
defmodule Pleroma.Web.StreamerTest do
  use Pleroma.DataCase

8 9
  import Pleroma.Factory

10
  alias Pleroma.Conversation.Participation
11 12
  alias Pleroma.List
  alias Pleroma.User
13
  alias Pleroma.Web.CommonAPI
14
  alias Pleroma.Web.Streamer
15 16
  alias Pleroma.Web.Streamer.StreamerSocket
  alias Pleroma.Web.Streamer.Worker
17

minibikini's avatar
minibikini committed
18
  @moduletag needs_streamer: true, capture_log: true
19
  clear_config_all([:instance, :skip_thread_containment])
Maksim's avatar
Maksim committed
20

Maksim's avatar
Maksim committed
21 22 23 24 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
  describe "user streams" do
    setup do
      user = insert(:user)
      notify = insert(:notification, user: user, activity: build(:note_activity))
      {:ok, %{user: user, notify: notify}}
    end

    test "it sends notify to in the 'user' stream", %{user: user, notify: notify} do
      task =
        Task.async(fn ->
          assert_receive {:text, _}, 4_000
        end)

      Streamer.add_socket(
        "user",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      Streamer.stream("user", notify)
      Task.await(task)
    end

    test "it sends notify to in the 'user:notification' stream", %{user: user, notify: notify} do
      task =
        Task.async(fn ->
          assert_receive {:text, _}, 4_000
        end)

      Streamer.add_socket(
        "user:notification",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      Streamer.stream("user:notification", notify)
      Task.await(task)
    end
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    test "it doesn't send notify to the 'user:notification' stream when a user is blocked", %{
      user: user
    } do
      blocked = insert(:user)
      {:ok, user} = User.block(user, blocked)

      task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)

      Streamer.add_socket(
        "user:notification",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      {:ok, activity} = CommonAPI.post(user, %{"status" => ":("})
      {:ok, notif, _} = CommonAPI.favorite(activity.id, blocked)

      Streamer.stream("user:notification", notif)
      Task.await(task)
    end

    test "it doesn't send notify to the 'user:notification' stream when a thread is muted", %{
      user: user
    } do
      user2 = insert(:user)
      task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)

      Streamer.add_socket(
        "user:notification",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
      {:ok, activity} = CommonAPI.add_mute(user, activity)
      {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
      Streamer.stream("user:notification", notif)
      Task.await(task)
    end
95 96 97 98 99 100 101 102 103 104 105 106 107

    test "it doesn't send notify to the 'user:notification' stream' when a domain is blocked", %{
      user: user
    } do
      user2 = insert(:user, %{ap_id: "https://hecking-lewd-place.com/user/meanie"})
      task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)

      Streamer.add_socket(
        "user:notification",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      {:ok, user} = User.block_domain(user, "hecking-lewd-place.com")
108
      {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})
109
      {:ok, notif, _} = CommonAPI.favorite(activity.id, user2)
110

111 112 113
      Streamer.stream("user:notification", notif)
      Task.await(task)
    end
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

    test "it sends follow activities to the 'user:notification' stream", %{
      user: user
    } do
      user2 = insert(:user)
      task = Task.async(fn -> assert_receive {:text, _}, 4_000 end)

      Streamer.add_socket(
        "user:notification",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      {:ok, _follower, _followed, _activity} = CommonAPI.follow(user2, user)

      # We don't directly pipe the notification to the streamer as it's already
      # generated as a side effect of CommonAPI.follow().
      Task.await(task)
    end
Maksim's avatar
Maksim committed
132 133
  end

134 135 136 137 138 139 140 141 142
  test "it sends to public" do
    user = insert(:user)
    other_user = insert(:user)

    task =
      Task.async(fn ->
        assert_receive {:text, _}, 4_000
      end)

143
    fake_socket = %StreamerSocket{
144
      transport_pid: task.pid,
145
      user: user
146 147 148 149 150 151 152 153
    }

    {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})

    topics = %{
      "public" => [fake_socket]
    }

154
    Worker.push_to_socket(topics, "public", activity)
155 156

    Task.await(task)
157 158 159

    task =
      Task.async(fn ->
160 161 162 163 164 165 166 167 168
        expected_event =
          %{
            "event" => "delete",
            "payload" => activity.id
          }
          |> Jason.encode!()

        assert_receive {:text, received_event}, 4_000
        assert received_event == expected_event
169 170
      end)

171
    fake_socket = %StreamerSocket{
172
      transport_pid: task.pid,
173
      user: user
174 175 176 177 178 179 180 181
    }

    {:ok, activity} = CommonAPI.delete(activity.id, other_user)

    topics = %{
      "public" => [fake_socket]
    }

182
    Worker.push_to_socket(topics, "public", activity)
183 184

    Task.await(task)
185 186
  end

Maksim's avatar
Maksim committed
187 188 189 190
  describe "thread_containment" do
    test "it doesn't send to user if recipients invalid and thread containment is enabled" do
      Pleroma.Config.put([:instance, :skip_thread_containment], false)
      author = insert(:user)
191 192
      user = insert(:user)
      User.follow(user, author, "accept")
Maksim's avatar
Maksim committed
193 194 195 196 197 198 199 200 201 202 203

      activity =
        insert(:note_activity,
          note:
            insert(:note,
              user: author,
              data: %{"to" => ["TEST-FFF"]}
            )
        )

      task = Task.async(fn -> refute_receive {:text, _}, 1_000 end)
204
      fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
Maksim's avatar
Maksim committed
205
      topics = %{"public" => [fake_socket]}
206
      Worker.push_to_socket(topics, "public", activity)
Maksim's avatar
Maksim committed
207 208 209 210 211 212 213

      Task.await(task)
    end

    test "it sends message if recipients invalid and thread containment is disabled" do
      Pleroma.Config.put([:instance, :skip_thread_containment], true)
      author = insert(:user)
214 215
      user = insert(:user)
      User.follow(user, author, "accept")
Maksim's avatar
Maksim committed
216 217 218 219 220 221 222 223 224 225 226

      activity =
        insert(:note_activity,
          note:
            insert(:note,
              user: author,
              data: %{"to" => ["TEST-FFF"]}
            )
        )

      task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
227
      fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
Maksim's avatar
Maksim committed
228
      topics = %{"public" => [fake_socket]}
229
      Worker.push_to_socket(topics, "public", activity)
Maksim's avatar
Maksim committed
230 231 232 233 234 235 236

      Task.await(task)
    end

    test "it sends message if recipients invalid and thread containment is enabled but user's thread containment is disabled" do
      Pleroma.Config.put([:instance, :skip_thread_containment], false)
      author = insert(:user)
237
      user = insert(:user, skip_thread_containment: true)
238
      User.follow(user, author, "accept")
Maksim's avatar
Maksim committed
239 240 241 242 243 244 245 246 247 248 249

      activity =
        insert(:note_activity,
          note:
            insert(:note,
              user: author,
              data: %{"to" => ["TEST-FFF"]}
            )
        )

      task = Task.async(fn -> assert_receive {:text, _}, 1_000 end)
250
      fake_socket = %StreamerSocket{transport_pid: task.pid, user: user}
Maksim's avatar
Maksim committed
251
      topics = %{"public" => [fake_socket]}
252
      Worker.push_to_socket(topics, "public", activity)
Maksim's avatar
Maksim committed
253 254 255 256 257

      Task.await(task)
    end
  end

258 259 260 261 262
  describe "blocks" do
    test "it doesn't send messages involving blocked users" do
      user = insert(:user)
      blocked_user = insert(:user)
      {:ok, user} = User.block(user, blocked_user)
263

264 265 266 267
      task =
        Task.async(fn ->
          refute_receive {:text, _}, 1_000
        end)
268

269 270 271 272
      fake_socket = %StreamerSocket{
        transport_pid: task.pid,
        user: user
      }
273

274
      {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
275

276 277 278
      topics = %{
        "public" => [fake_socket]
      }
279

280
      Worker.push_to_socket(topics, "public", activity)
281

282 283 284 285 286 287 288 289 290 291 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
      Task.await(task)
    end

    test "it doesn't send messages transitively involving blocked users" do
      blocker = insert(:user)
      blockee = insert(:user)
      friend = insert(:user)

      task =
        Task.async(fn ->
          refute_receive {:text, _}, 1_000
        end)

      fake_socket = %StreamerSocket{
        transport_pid: task.pid,
        user: blocker
      }

      topics = %{
        "public" => [fake_socket]
      }

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

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

      Worker.push_to_socket(topics, "public", activity_one)

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

      Worker.push_to_socket(topics, "public", activity_two)

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

      Worker.push_to_socket(topics, "public", activity_three)

      Task.await(task)
    end
320
  end
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

  test "it doesn't send unwanted DMs to list" do
    user_a = insert(:user)
    user_b = insert(:user)
    user_c = insert(:user)

    {:ok, user_a} = User.follow(user_a, user_b)

    {:ok, list} = List.create("Test", user_a)
    {:ok, list} = List.follow(list, user_b)

    task =
      Task.async(fn ->
        refute_receive {:text, _}, 1_000
      end)

337
    fake_socket = %StreamerSocket{
338
      transport_pid: task.pid,
339
      user: user_a
340 341 342 343 344 345 346 347 348 349 350 351
    }

    {:ok, activity} =
      CommonAPI.post(user_b, %{
        "status" => "@#{user_c.nickname} Test",
        "visibility" => "direct"
      })

    topics = %{
      "list:#{list.id}" => [fake_socket]
    }

352
    Worker.handle_call({:stream, "list", activity}, self(), topics)
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368

    Task.await(task)
  end

  test "it doesn't send unwanted private posts to list" do
    user_a = insert(:user)
    user_b = insert(:user)

    {:ok, list} = List.create("Test", user_a)
    {:ok, list} = List.follow(list, user_b)

    task =
      Task.async(fn ->
        refute_receive {:text, _}, 1_000
      end)

369
    fake_socket = %StreamerSocket{
370
      transport_pid: task.pid,
371
      user: user_a
372 373 374 375 376 377 378 379 380 381 382 383
    }

    {:ok, activity} =
      CommonAPI.post(user_b, %{
        "status" => "Test",
        "visibility" => "private"
      })

    topics = %{
      "list:#{list.id}" => [fake_socket]
    }

384
    Worker.handle_call({:stream, "list", activity}, self(), topics)
385 386 387 388

    Task.await(task)
  end

389
  test "it sends wanted private posts to list" do
390 391 392 393 394 395 396 397 398 399 400 401 402
    user_a = insert(:user)
    user_b = insert(:user)

    {:ok, user_a} = User.follow(user_a, user_b)

    {:ok, list} = List.create("Test", user_a)
    {:ok, list} = List.follow(list, user_b)

    task =
      Task.async(fn ->
        assert_receive {:text, _}, 1_000
      end)

403
    fake_socket = %StreamerSocket{
404
      transport_pid: task.pid,
405
      user: user_a
406 407 408 409 410 411 412 413
    }

    {:ok, activity} =
      CommonAPI.post(user_b, %{
        "status" => "Test",
        "visibility" => "private"
      })

414 415 416 417
    Streamer.add_socket(
      "list:#{list.id}",
      fake_socket
    )
418

419
    Worker.handle_call({:stream, "list", activity}, self(), %{})
420 421 422

    Task.await(task)
  end
423 424 425 426 427

  test "it doesn't send muted reblogs" do
    user1 = insert(:user)
    user2 = insert(:user)
    user3 = insert(:user)
Karen Konou's avatar
Karen Konou committed
428
    CommonAPI.hide_reblogs(user1, user2)
429 430 431 432 433 434

    task =
      Task.async(fn ->
        refute_receive {:text, _}, 1_000
      end)

435
    fake_socket = %StreamerSocket{
436
      transport_pid: task.pid,
437
      user: user1
438 439 440 441 442 443 444 445 446
    }

    {:ok, create_activity} = CommonAPI.post(user3, %{"status" => "I'm kawen"})
    {:ok, announce_activity, _} = CommonAPI.repeat(create_activity.id, user2)

    topics = %{
      "public" => [fake_socket]
    }

447
    Worker.push_to_socket(topics, "public", announce_activity)
448 449 450

    Task.await(task)
  end
451

452 453 454 455 456 457 458 459 460 461 462
  test "it doesn't send posts from muted threads" do
    user = insert(:user)
    user2 = insert(:user)
    {:ok, user2, user, _activity} = CommonAPI.follow(user2, user)

    {:ok, activity} = CommonAPI.post(user, %{"status" => "super hot take"})

    {:ok, activity} = CommonAPI.add_mute(user2, activity)

    task = Task.async(fn -> refute_receive {:text, _}, 4_000 end)

463 464
    Process.sleep(4000)

465 466 467 468 469 470 471 472 473
    Streamer.add_socket(
      "user",
      %{transport_pid: task.pid, assigns: %{user: user2}}
    )

    Streamer.stream("user", activity)
    Task.await(task)
  end

474 475 476 477 478 479 480 481 482 483 484
  describe "direct streams" do
    setup do
      :ok
    end

    test "it sends conversation update to the 'direct' stream", %{} do
      user = insert(:user)
      another_user = insert(:user)

      task =
        Task.async(fn ->
485 486 487 488 489 490 491 492
          assert_receive {:text, received_event}, 4_000

          assert %{"event" => "conversation", "payload" => received_payload} =
                   Jason.decode!(received_event)

          assert %{"last_status" => last_status} = Jason.decode!(received_payload)
          [participation] = Participation.for_user(user)
          assert last_status["pleroma"]["direct_conversation_id"] == participation.id
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
        end)

      Streamer.add_socket(
        "direct",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      {:ok, _create_activity} =
        CommonAPI.post(another_user, %{
          "status" => "hey @#{user.nickname}",
          "visibility" => "direct"
        })

      Task.await(task)
    end

509
    test "it doesn't send conversation update to the 'direct' stream when the last message in the conversation is deleted" do
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
      user = insert(:user)
      another_user = insert(:user)

      {:ok, create_activity} =
        CommonAPI.post(another_user, %{
          "status" => "hi @#{user.nickname}",
          "visibility" => "direct"
        })

      task =
        Task.async(fn ->
          assert_receive {:text, received_event}, 4_000
          assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)

          refute_receive {:text, _}, 4_000
        end)

527 528
      Process.sleep(1000)

529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
      Streamer.add_socket(
        "direct",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      {:ok, _} = CommonAPI.delete(create_activity.id, another_user)

      Task.await(task)
    end

    test "it sends conversation update to the 'direct' stream when a message is deleted" do
      user = insert(:user)
      another_user = insert(:user)

      {:ok, create_activity} =
        CommonAPI.post(another_user, %{
          "status" => "hi @#{user.nickname}",
          "visibility" => "direct"
        })

      {:ok, create_activity2} =
        CommonAPI.post(another_user, %{
          "status" => "hi @#{user.nickname}",
          "in_reply_to_status_id" => create_activity.id,
          "visibility" => "direct"
        })

      task =
        Task.async(fn ->
          assert_receive {:text, received_event}, 4_000
          assert %{"event" => "delete", "payload" => _} = Jason.decode!(received_event)

          assert_receive {:text, received_event}, 4_000

          assert %{"event" => "conversation", "payload" => received_payload} =
                   Jason.decode!(received_event)

          assert %{"last_status" => last_status} = Jason.decode!(received_payload)
          assert last_status["id"] == to_string(create_activity.id)
        end)

570 571
      Process.sleep(1000)

572 573 574 575 576 577 578 579 580 581
      Streamer.add_socket(
        "direct",
        %{transport_pid: task.pid, assigns: %{user: user}}
      )

      {:ok, _} = CommonAPI.delete(create_activity2.id, another_user)

      Task.await(task)
    end
  end
582
end