1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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 "/api/v1/pleroma/conversations/:id" do
101 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
104 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
106 [participation] = Participation.for_user(other_user)
110 |> get("/api/v1/pleroma/conversations/#{participation.id}")
111 |> json_response(200)
113 assert result["id"] == participation.id |> to_string()
116 test "/api/v1/pleroma/conversations/:id/statuses" do
118 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
119 third_user = insert(:user)
122 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
125 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
127 [participation] = Participation.for_user(other_user)
129 {:ok, activity_two} =
130 CommonAPI.post(other_user, %{
132 "in_reply_to_status_id" => activity.id,
133 "in_reply_to_conversation_id" => participation.id
138 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
139 |> json_response(200)
141 assert length(result) == 2
144 id_two = activity_two.id
145 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
148 test "PATCH /api/v1/pleroma/conversations/:id" do
149 %{user: user, conn: conn} = oauth_access(["write:conversations"])
150 other_user = insert(:user)
152 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
154 [participation] = Participation.for_user(user)
156 participation = Repo.preload(participation, :recipients)
158 user = User.get_cached_by_id(user.id)
159 assert [user] == participation.recipients
160 assert other_user not in participation.recipients
164 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
165 "recipients" => [user.id, other_user.id]
167 |> json_response(200)
169 assert result["id"] == participation.id |> to_string
171 [participation] = Participation.for_user(user)
172 participation = Repo.preload(participation, :recipients)
174 assert user in participation.recipients
175 assert other_user in participation.recipients
178 test "POST /api/v1/pleroma/conversations/read" do
180 %{user: other_user, conn: conn} = oauth_access(["write:notifications"])
183 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
186 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
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
193 [%{"unread" => false}, %{"unread" => false}] =
195 |> post("/api/v1/pleroma/conversations/read", %{})
196 |> json_response(200)
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
204 describe "POST /api/v1/pleroma/notifications/read" do
205 setup do: oauth_access(["write:notifications"])
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)
216 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
217 |> json_response(:ok)
219 assert %{"pleroma" => %{"is_seen" => true}} = response
220 assert Repo.get(Notification, notification1.id).seen
221 refute Repo.get(Notification, notification2.id).seen
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}"})
230 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
232 [response1, response2] =
234 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
235 |> json_response(:ok)
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
244 test "it returns error when notification not found", %{conn: conn} do
247 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
248 |> json_response(:bad_request)
250 assert response == %{"error" => "Cannot get notification"}