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 Pleroma.Web.ConnCase
8 alias Pleroma.Conversation.Participation
9 alias Pleroma.Notification
13 alias Pleroma.Web.CommonAPI
15 import Pleroma.Factory
17 test "PUT /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
19 other_user = insert(:user)
21 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
25 |> assign(:user, other_user)
26 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
27 |> put("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
30 # We return the status, but this our implementation detail.
31 assert %{"id" => id} = result
32 assert to_string(activity.id) == id
34 assert result["pleroma"]["emoji_reactions"] == [
35 %{"name" => "☕", "count" => 1, "me" => true}
39 test "DELETE /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
41 other_user = insert(:user)
43 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
44 {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
48 |> assign(:user, other_user)
49 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
50 |> delete("/api/v1/pleroma/statuses/#{activity.id}/reactions/☕")
52 assert %{"id" => id} = json_response(result, 200)
53 assert to_string(activity.id) == id
55 object = Object.normalize(activity)
57 assert object.data["reaction_count"] == 0
60 test "GET /api/v1/pleroma/statuses/:id/reactions", %{conn: conn} do
62 other_user = insert(:user)
63 doomed_user = insert(:user)
65 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
69 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
74 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
75 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, doomed_user, "🎅")
77 User.perform(:delete, doomed_user)
81 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions")
84 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
86 assert represented_user["id"] == other_user.id
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}/reactions")
95 assert [%{"name" => "🎅", "count" => 1, "accounts" => [_represented_user], "me" => true}] =
99 test "GET /api/v1/pleroma/statuses/:id/reactions/:emoji", %{conn: conn} do
101 other_user = insert(:user)
103 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
107 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
108 |> json_response(200)
112 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
113 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
117 |> get("/api/v1/pleroma/statuses/#{activity.id}/reactions/🎅")
118 |> json_response(200)
120 [%{"name" => "🎅", "count" => 1, "accounts" => [represented_user], "me" => false}] = result
122 assert represented_user["id"] == other_user.id
125 test "/api/v1/pleroma/conversations/:id" do
127 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
130 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
132 [participation] = Participation.for_user(other_user)
136 |> get("/api/v1/pleroma/conversations/#{participation.id}")
137 |> json_response(200)
139 assert result["id"] == participation.id |> to_string()
142 test "/api/v1/pleroma/conversations/:id/statuses" do
144 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
145 third_user = insert(:user)
148 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
151 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
153 [participation] = Participation.for_user(other_user)
155 {:ok, activity_two} =
156 CommonAPI.post(other_user, %{
158 "in_reply_to_status_id" => activity.id,
159 "in_reply_to_conversation_id" => participation.id
164 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
165 |> json_response(200)
167 assert length(result) == 2
170 id_two = activity_two.id
171 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
173 {:ok, %{id: id_three}} =
174 CommonAPI.post(other_user, %{
176 "in_reply_to_status_id" => activity.id,
177 "in_reply_to_conversation_id" => participation.id
180 assert [%{"id" => ^id_two}, %{"id" => ^id_three}] =
182 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?limit=2")
183 |> json_response(:ok)
185 assert [%{"id" => ^id_three}] =
187 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses?min_id=#{id_two}")
188 |> json_response(:ok)
191 test "PATCH /api/v1/pleroma/conversations/:id" do
192 %{user: user, conn: conn} = oauth_access(["write:conversations"])
193 other_user = insert(:user)
195 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
197 [participation] = Participation.for_user(user)
199 participation = Repo.preload(participation, :recipients)
201 user = User.get_cached_by_id(user.id)
202 assert [user] == participation.recipients
203 assert other_user not in participation.recipients
207 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
208 "recipients" => [user.id, other_user.id]
210 |> json_response(200)
212 assert result["id"] == participation.id |> to_string
214 [participation] = Participation.for_user(user)
215 participation = Repo.preload(participation, :recipients)
217 assert user in participation.recipients
218 assert other_user in participation.recipients
221 test "POST /api/v1/pleroma/conversations/read" do
223 %{user: other_user, conn: conn} = oauth_access(["write:conversations"])
226 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
229 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
231 [participation2, participation1] = Participation.for_user(other_user)
232 assert Participation.get(participation2.id).read == false
233 assert Participation.get(participation1.id).read == false
234 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
236 [%{"unread" => false}, %{"unread" => false}] =
238 |> post("/api/v1/pleroma/conversations/read", %{})
239 |> json_response(200)
241 [participation2, participation1] = Participation.for_user(other_user)
242 assert Participation.get(participation2.id).read == true
243 assert Participation.get(participation1.id).read == true
244 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
247 describe "POST /api/v1/pleroma/notifications/read" do
248 setup do: oauth_access(["write:notifications"])
250 test "it marks a single notification as read", %{user: user1, conn: conn} do
251 user2 = insert(:user)
252 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
253 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
254 {:ok, [notification1]} = Notification.create_notifications(activity1)
255 {:ok, [notification2]} = Notification.create_notifications(activity2)
259 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
260 |> json_response(:ok)
262 assert %{"pleroma" => %{"is_seen" => true}} = response
263 assert Repo.get(Notification, notification1.id).seen
264 refute Repo.get(Notification, notification2.id).seen
267 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
268 user2 = insert(:user)
269 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
270 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
271 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
273 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
275 [response1, response2] =
277 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
278 |> json_response(:ok)
280 assert %{"pleroma" => %{"is_seen" => true}} = response1
281 assert %{"pleroma" => %{"is_seen" => true}} = response2
282 assert Repo.get(Notification, notification1.id).seen
283 assert Repo.get(Notification, notification2.id).seen
284 refute Repo.get(Notification, notification3.id).seen
287 test "it returns error when notification not found", %{conn: conn} do
290 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
291 |> json_response(:bad_request)
293 assert response == %{"error" => "Cannot get notification"}