df58441f0f62474e439ddbc67d2ec92aff422118
[akkoma] / test / web / streamer_test.exs
1 defmodule Pleroma.Web.StreamerTest do
2 use Pleroma.DataCase
3
4 alias Pleroma.Web.Streamer
5 alias Pleroma.{List, User}
6 alias Pleroma.Web.CommonAPI
7 import Pleroma.Factory
8
9 test "it sends to public" do
10 user = insert(:user)
11 other_user = insert(:user)
12
13 task =
14 Task.async(fn ->
15 assert_receive {:text, _}, 4_000
16 end)
17
18 fake_socket = %{
19 transport_pid: task.pid,
20 assigns: %{
21 user: user
22 }
23 }
24
25 {:ok, activity} = CommonAPI.post(other_user, %{"status" => "Test"})
26
27 topics = %{
28 "public" => [fake_socket]
29 }
30
31 Streamer.push_to_socket(topics, "public", activity)
32
33 Task.await(task)
34 end
35
36 test "it doesn't send to blocked users" do
37 user = insert(:user)
38 blocked_user = insert(:user)
39 {:ok, user} = User.block(user, blocked_user)
40
41 task =
42 Task.async(fn ->
43 refute_receive {:text, _}, 1_000
44 end)
45
46 fake_socket = %{
47 transport_pid: task.pid,
48 assigns: %{
49 user: user
50 }
51 }
52
53 {:ok, activity} = CommonAPI.post(blocked_user, %{"status" => "Test"})
54
55 topics = %{
56 "public" => [fake_socket]
57 }
58
59 Streamer.push_to_socket(topics, "public", activity)
60
61 Task.await(task)
62 end
63
64 test "it doesn't send unwanted DMs to list" do
65 user_a = insert(:user)
66 user_b = insert(:user)
67 user_c = insert(:user)
68
69 {:ok, user_a} = User.follow(user_a, user_b)
70
71 {:ok, list} = List.create("Test", user_a)
72 {:ok, list} = List.follow(list, user_b)
73
74 task =
75 Task.async(fn ->
76 refute_receive {:text, _}, 1_000
77 end)
78
79 fake_socket = %{
80 transport_pid: task.pid,
81 assigns: %{
82 user: user_a
83 }
84 }
85
86 {:ok, activity} =
87 CommonAPI.post(user_b, %{
88 "status" => "@#{user_c.nickname} Test",
89 "visibility" => "direct"
90 })
91
92 topics = %{
93 "list:#{list.id}" => [fake_socket]
94 }
95
96 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
97
98 Task.await(task)
99 end
100
101 test "it doesn't send unwanted private posts to list" do
102 user_a = insert(:user)
103 user_b = insert(:user)
104
105 {:ok, list} = List.create("Test", user_a)
106 {:ok, list} = List.follow(list, user_b)
107
108 task =
109 Task.async(fn ->
110 refute_receive {:text, _}, 1_000
111 end)
112
113 fake_socket = %{
114 transport_pid: task.pid,
115 assigns: %{
116 user: user_a
117 }
118 }
119
120 {:ok, activity} =
121 CommonAPI.post(user_b, %{
122 "status" => "Test",
123 "visibility" => "private"
124 })
125
126 topics = %{
127 "list:#{list.id}" => [fake_socket]
128 }
129
130 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
131
132 Task.await(task)
133 end
134
135 test "it send wanted private posts to list" do
136 user_a = insert(:user)
137 user_b = insert(:user)
138
139 {:ok, user_a} = User.follow(user_a, user_b)
140
141 {:ok, list} = List.create("Test", user_a)
142 {:ok, list} = List.follow(list, user_b)
143
144 task =
145 Task.async(fn ->
146 assert_receive {:text, _}, 1_000
147 end)
148
149 fake_socket = %{
150 transport_pid: task.pid,
151 assigns: %{
152 user: user_a
153 }
154 }
155
156 {:ok, activity} =
157 CommonAPI.post(user_b, %{
158 "status" => "Test",
159 "visibility" => "private"
160 })
161
162 topics = %{
163 "list:#{list.id}" => [fake_socket]
164 }
165
166 Streamer.handle_cast(%{action: :stream, topic: "list", item: activity}, topics)
167
168 Task.await(task)
169 end
170 end