1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.PleromaAPI.PleromaAPIControllerTest do
6 use Oban.Testing, repo: Pleroma.Repo
7 use Pleroma.Web.ConnCase
9 alias Pleroma.Conversation.Participation
10 alias Pleroma.Notification
13 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.Web.CommonAPI
17 import Pleroma.Factory
19 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
21 other_user = insert(:user)
23 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
27 |> assign(:user, other_user)
28 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
29 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
32 # We return the status, but this our implementation detail.
33 assert %{"id" => id} = result
34 assert to_string(activity.id) == id
36 assert result["pleroma"]["emoji_reactions"] == [
37 %{"name" => "☕", "count" => 1, "me" => true}
41 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
43 other_user = insert(:user)
45 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
46 {:ok, _reaction, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
48 ObanHelpers.perform_all()
52 |> assign(:user, other_user)
53 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
54 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
56 assert %{"id" => id} = json_response(result, 200)
57 assert to_string(activity.id) == id
59 ObanHelpers.perform_all()
61 object = Object.get_by_ap_id(activity.data["object"])
63 assert object.data["reaction_count"] == 0
66 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
68 other_user = insert(:user)
69 doomed_user = insert(:user)
71 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
75 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
80 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
81 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
83 User.perform(:delete, doomed_user)
87 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
90 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
92 assert represented_user["id"] == other_user.id
96 |> assign(:user, other_user)
97 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["read:statuses"]))
98 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
101 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
105 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
107 other_user = insert(:user)
109 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
113 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
114 |> json_response(200)
118 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
119 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
123 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
124 |> json_response(200)
126 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
128 assert represented_user["id"] == other_user.id
131 test "/api/v1/pleroma/conversations/:id" do
133 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
136 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
138 [participation] = Participation.for_user(other_user)
142 |> get("/api/v1/pleroma/conversations/#{participation.id}")
143 |> json_response(200)
145 assert result["id"] == participation.id |> to_string()
148 test "/api/v1/pleroma/conversations/:id/statuses" do
150 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
151 third_user = insert(:user)
154 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
157 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
159 [participation] = Participation.for_user(other_user)
161 {:ok, activity_two} =
162 CommonAPI.post(other_user, %{
164 "in_reply_to_status_id" => activity.id,
165 "in_reply_to_conversation_id" => participation.id
170 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
171 |> json_response(200)
173 assert length(result) == 2
176 id_two = activity_two.id
177 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
179 {:ok, %{id: id_three}} =
180 CommonAPI.post(other_user, %{
182 "in_reply_to_status_id" => activity.id,
183 "in_reply_to_conversation_id" => participation.id
186 assert [%{"id" => ^id_two}, %{"id" => ^id_three}] =
188 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?limit=2")
189 |> json_response(:ok)
191 assert [%{"id" => ^id_three}] =
193 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?min_id=#{id_two}")
194 |> json_response(:ok)
197 test "PATCH /api/v1/pleroma/conversations/:id" do
198 %{user: user, conn: conn} = oauth_access(["write:conversations"])
199 other_user = insert(:user)
201 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
203 [participation] = Participation.for_user(user)
205 participation = Repo.preload(participation, :recipients)
207 user = User.get_cached_by_id(user.id)
208 assert [user] == participation.recipients
209 assert other_user not in participation.recipients
213 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
214 "recipients" => [user.id, other_user.id]
216 |> json_response(200)
218 assert result["id"] == participation.id |> to_string
220 [participation] = Participation.for_user(user)
221 participation = Repo.preload(participation, :recipients)
223 assert user in participation.recipients
224 assert other_user in participation.recipients
227 test "POST /api/v1/pleroma/conversations/read" do
229 %{user: other_user, conn: conn} = oauth_access(["write:conversations"])
232 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
235 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
237 [participation2, participation1] = Participation.for_user(other_user)
238 assert Participation.get(participation2.id).read == false
239 assert Participation.get(participation1.id).read == false
240 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
242 [%{"unread" => false}, %{"unread" => false}] =
244 |> post("/api/v1/pleroma/conversations/read", %{})
245 |> json_response(200)
247 [participation2, participation1] = Participation.for_user(other_user)
248 assert Participation.get(participation2.id).read == true
249 assert Participation.get(participation1.id).read == true
250 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
253 describe "POST /api/v1/pleroma/notifications/read" do
254 setup do: oauth_access(["write:notifications"])
256 test "it marks a single notification as read", %{user: user1, conn: conn} do
257 user2 = insert(:user)
258 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
259 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
260 {:ok, [notification1]} = Notification.create_notifications(activity1)
261 {:ok, [notification2]} = Notification.create_notifications(activity2)
265 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
266 |> json_response(:ok)
268 assert %{"pleroma" => %{"is_seen" => true}} = response
269 assert Repo.get(Notification, notification1.id).seen
270 refute Repo.get(Notification, notification2.id).seen
273 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
274 user2 = insert(:user)
275 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
276 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
277 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
279 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
281 [response1, response2] =
283 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
284 |> json_response(:ok)
286 assert %{"pleroma" => %{"is_seen" => true}} = response1
287 assert %{"pleroma" => %{"is_seen" => true}} = response2
288 assert Repo.get(Notification, notification1.id).seen
289 assert Repo.get(Notification, notification2.id).seen
290 refute Repo.get(Notification, notification3.id).seen
293 test "it returns error when notification not found", %{conn: conn} do
296 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
297 |> json_response(:bad_request)
299 assert response == %{"error" => "Cannot get notification"}