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 "POST /api/v1/pleroma/statuses/:id/react_with_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 |> post("/api/v1/pleroma/statuses/#{activity.id}/react_with_emoji", %{"emoji" => "☕"})
29 assert %{"id" => id} = json_response(result, 200)
30 assert to_string(activity.id) == id
33 test "POST /api/v1/pleroma/statuses/:id/unreact_with_emoji", %{conn: conn} do
35 other_user = insert(:user)
37 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
38 {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
42 |> assign(:user, other_user)
43 |> assign(:token, insert(:oauth_token, user: other_user, scopes: ["write:statuses"]))
44 |> post("/api/v1/pleroma/statuses/#{activity.id}/unreact_with_emoji", %{"emoji" => "☕"})
46 assert %{"id" => id} = json_response(result, 200)
47 assert to_string(activity.id) == id
49 object = Object.normalize(activity)
51 assert object.data["reaction_count"] == 0
54 test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
56 other_user = insert(:user)
58 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
62 |> assign(:user, user)
63 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read:statuses"]))
67 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
72 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
76 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
79 [represented_user] = result["🎅"]
80 assert represented_user["id"] == other_user.id
83 test "/api/v1/pleroma/conversations/:id" do
85 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
88 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
90 [participation] = Participation.for_user(other_user)
94 |> get("/api/v1/pleroma/conversations/#{participation.id}")
97 assert result["id"] == participation.id |> to_string()
100 test "/api/v1/pleroma/conversations/:id/statuses" do
102 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
103 third_user = insert(:user)
106 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
109 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
111 [participation] = Participation.for_user(other_user)
113 {:ok, activity_two} =
114 CommonAPI.post(other_user, %{
116 "in_reply_to_status_id" => activity.id,
117 "in_reply_to_conversation_id" => participation.id
122 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
123 |> json_response(200)
125 assert length(result) == 2
128 id_two = activity_two.id
129 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
132 test "PATCH /api/v1/pleroma/conversations/:id" do
133 %{user: user, conn: conn} = oauth_access(["write:conversations"])
134 other_user = insert(:user)
136 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
138 [participation] = Participation.for_user(user)
140 participation = Repo.preload(participation, :recipients)
142 user = User.get_cached_by_id(user.id)
143 assert [user] == participation.recipients
144 assert other_user not in participation.recipients
148 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
149 "recipients" => [user.id, other_user.id]
151 |> json_response(200)
153 assert result["id"] == participation.id |> to_string
155 [participation] = Participation.for_user(user)
156 participation = Repo.preload(participation, :recipients)
158 assert user in participation.recipients
159 assert other_user in participation.recipients
162 test "POST /api/v1/pleroma/conversations/read" do
164 %{user: other_user, conn: conn} = oauth_access(["write:notifications"])
167 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
170 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
172 [participation2, participation1] = Participation.for_user(other_user)
173 assert Participation.get(participation2.id).read == false
174 assert Participation.get(participation1.id).read == false
175 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
177 [%{"unread" => false}, %{"unread" => false}] =
179 |> post("/api/v1/pleroma/conversations/read", %{})
180 |> json_response(200)
182 [participation2, participation1] = Participation.for_user(other_user)
183 assert Participation.get(participation2.id).read == true
184 assert Participation.get(participation1.id).read == true
185 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
188 describe "POST /api/v1/pleroma/notifications/read" do
189 setup do: oauth_access(["write:notifications"])
191 test "it marks a single notification as read", %{user: user1, conn: conn} do
192 user2 = insert(:user)
193 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
194 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
195 {:ok, [notification1]} = Notification.create_notifications(activity1)
196 {:ok, [notification2]} = Notification.create_notifications(activity2)
200 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
201 |> json_response(:ok)
203 assert %{"pleroma" => %{"is_seen" => true}} = response
204 assert Repo.get(Notification, notification1.id).seen
205 refute Repo.get(Notification, notification2.id).seen
208 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
209 user2 = insert(:user)
210 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
211 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
212 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
214 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
216 [response1, response2] =
218 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
219 |> json_response(:ok)
221 assert %{"pleroma" => %{"is_seen" => true}} = response1
222 assert %{"pleroma" => %{"is_seen" => true}} = response2
223 assert Repo.get(Notification, notification1.id).seen
224 assert Repo.get(Notification, notification2.id).seen
225 refute Repo.get(Notification, notification3.id).seen
228 test "it returns error when notification not found", %{conn: conn} do
231 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
232 |> json_response(:bad_request)
234 assert response == %{"error" => "Cannot get notification"}