Merge branch 'mastoapi-non-html-strings' into 'develop'
[akkoma] / test / web / pleroma_api / controllers / pleroma_api_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Conversation.Participation
9 alias Pleroma.Notification
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.Web.CommonAPI
14
15 import Pleroma.Factory
16
17 test "POST /api/v1/pleroma/statuses/:id/react_with_emoji", %{conn: conn} do
18 user = insert(:user)
19 other_user = insert(:user)
20
21 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
22
23 result =
24 conn
25 |> assign(:user, other_user)
26 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
27 |> post("/api/v1/pleroma/statuses/#{activity.id}/react_with_emoji", %{"emoji" => "☕"})
28 |> json_response(200)
29
30 assert %{"id" => id} = result
31 assert to_string(activity.id) == id
32
33 assert result["pleroma"]["emoji_reactions"] == [
34 %{"emoji" => "☕", "count" => 1, "reacted" => true}
35 ]
36 end
37
38 test "POST /api/v1/pleroma/statuses/:id/unreact_with_emoji", %{conn: conn} do
39 user = insert(:user)
40 other_user = insert(:user)
41
42 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
43 {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
44
45 result =
46 conn
47 |> assign(:user, other_user)
48 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
49 |> post("/api/v1/pleroma/statuses/#{activity.id}/unreact_with_emoji", %{"emoji" => "☕"})
50
51 assert %{"id" => id} = json_response(result, 200)
52 assert to_string(activity.id) == id
53
54 object = Object.normalize(activity)
55
56 assert object.data["reaction_count"] == 0
57 end
58
59 test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
60 user = insert(:user)
61 other_user = insert(:user)
62 doomed_user = insert(:user)
63
64 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
65
66 result =
67 conn
68 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
69 |> json_response(200)
70
71 assert result == []
72
73 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
74 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
75
76 User.perform(:delete, doomed_user)
77
78 result =
79 conn
80 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
81 |> json_response(200)
82
83 [%{"emoji" => "🎅", "count" => 1, "accounts" => [represented_user], "reacted" => false}] =
84 result
85
86 assert represented_user["id"] == other_user.id
87
88 result =
89 conn
90 |> assign(:user, other_user)
91 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
92 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
93 |> json_response(200)
94
95 assert [%{"emoji" => "🎅", "count" => 1, "accounts" => [_represented_user], "reacted" => true}] =
96 result
97 end
98
99 test "/api/v1/pleroma/conversations/:id" do
100 user = insert(:user)
101 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
102
103 {:ok, _activity} =
104 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
105
106 [participation] = Participation.for_user(other_user)
107
108 result =
109 conn
110 |> get("/api/v1/pleroma/conversations/#{participation.id}")
111 |> json_response(200)
112
113 assert result["id"] == participation.id |> to_string()
114 end
115
116 test "/api/v1/pleroma/conversations/:id/statuses" do
117 user = insert(:user)
118 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
119 third_user = insert(:user)
120
121 {:ok, _activity} =
122 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
123
124 {:ok, activity} =
125 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
126
127 [participation] = Participation.for_user(other_user)
128
129 {:ok, activity_two} =
130 CommonAPI.post(other_user, %{
131 "status" => "Hi!",
132 "in_reply_to_status_id" => activity.id,
133 "in_reply_to_conversation_id" => participation.id
134 })
135
136 result =
137 conn
138 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
139 |> json_response(200)
140
141 assert length(result) == 2
142
143 id_one = activity.id
144 id_two = activity_two.id
145 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
146 end
147
148 test "PATCH /api/v1/pleroma/conversations/:id" do
149 %{user: user, conn: conn} = oauth_access(["write:conversations"])
150 other_user = insert(:user)
151
152 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
153
154 [participation] = Participation.for_user(user)
155
156 participation = Repo.preload(participation, :recipients)
157
158 user = User.get_cached_by_id(user.id)
159 assert [user] == participation.recipients
160 assert other_user not in participation.recipients
161
162 result =
163 conn
164 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
165 "recipients" => [user.id, other_user.id]
166 })
167 |> json_response(200)
168
169 assert result["id"] == participation.id |> to_string
170
171 [participation] = Participation.for_user(user)
172 participation = Repo.preload(participation, :recipients)
173
174 assert user in participation.recipients
175 assert other_user in participation.recipients
176 end
177
178 test "POST /api/v1/pleroma/conversations/read" do
179 user = insert(:user)
180 %{user: other_user, conn: conn} = oauth_access(["write:notifications"])
181
182 {:ok, _activity} =
183 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
184
185 {:ok, _activity} =
186 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
187
188 [participation2, participation1] = Participation.for_user(other_user)
189 assert Participation.get(participation2.id).read == false
190 assert Participation.get(participation1.id).read == false
191 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
192
193 [%{"unread" => false}, %{"unread" => false}] =
194 conn
195 |> post("/api/v1/pleroma/conversations/read", %{})
196 |> json_response(200)
197
198 [participation2, participation1] = Participation.for_user(other_user)
199 assert Participation.get(participation2.id).read == true
200 assert Participation.get(participation1.id).read == true
201 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
202 end
203
204 describe "POST /api/v1/pleroma/notifications/read" do
205 setup do: oauth_access(["write:notifications"])
206
207 test "it marks a single notification as read", %{user: user1, conn: conn} do
208 user2 = insert(:user)
209 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
210 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
211 {:ok, [notification1]} = Notification.create_notifications(activity1)
212 {:ok, [notification2]} = Notification.create_notifications(activity2)
213
214 response =
215 conn
216 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
217 |> json_response(:ok)
218
219 assert %{"pleroma" => %{"is_seen" => true}} = response
220 assert Repo.get(Notification, notification1.id).seen
221 refute Repo.get(Notification, notification2.id).seen
222 end
223
224 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
225 user2 = insert(:user)
226 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
227 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
228 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
229
230 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
231
232 [response1, response2] =
233 conn
234 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
235 |> json_response(:ok)
236
237 assert %{"pleroma" => %{"is_seen" => true}} = response1
238 assert %{"pleroma" => %{"is_seen" => true}} = response2
239 assert Repo.get(Notification, notification1.id).seen
240 assert Repo.get(Notification, notification2.id).seen
241 refute Repo.get(Notification, notification3.id).seen
242 end
243
244 test "it returns error when notification not found", %{conn: conn} do
245 response =
246 conn
247 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
248 |> json_response(:bad_request)
249
250 assert response == %{"error" => "Cannot get notification"}
251 end
252 end
253 end