Merge branch 'develop' into admin-be
[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
29 assert %{"id" => id} = json_response(result, 200)
30 assert to_string(activity.id) == id
31 end
32
33 test "POST /api/v1/pleroma/statuses/:id/unreact_with_emoji", %{conn: conn} do
34 user = insert(:user)
35 other_user = insert(:user)
36
37 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
38 {:ok, activity, _object} = CommonAPI.react_with_emoji(activity.id, other_user, "☕")
39
40 result =
41 conn
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" => "☕"})
45
46 assert %{"id" => id} = json_response(result, 200)
47 assert to_string(activity.id) == id
48
49 object = Object.normalize(activity)
50
51 assert object.data["reaction_count"] == 0
52 end
53
54 test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
55 user = insert(:user)
56 other_user = insert(:user)
57
58 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
59
60 result =
61 conn
62 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
63 |> json_response(200)
64
65 assert result == %{}
66
67 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
68
69 result =
70 conn
71 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
72 |> json_response(200)
73
74 [represented_user] = result["🎅"]
75 assert represented_user["id"] == other_user.id
76 end
77
78 test "/api/v1/pleroma/conversations/:id" do
79 user = insert(:user)
80 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
81
82 {:ok, _activity} =
83 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
84
85 [participation] = Participation.for_user(other_user)
86
87 result =
88 conn
89 |> get("/api/v1/pleroma/conversations/#{participation.id}")
90 |> json_response(200)
91
92 assert result["id"] == participation.id |> to_string()
93 end
94
95 test "/api/v1/pleroma/conversations/:id/statuses" do
96 user = insert(:user)
97 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
98 third_user = insert(:user)
99
100 {:ok, _activity} =
101 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
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 {:ok, activity_two} =
109 CommonAPI.post(other_user, %{
110 "status" => "Hi!",
111 "in_reply_to_status_id" => activity.id,
112 "in_reply_to_conversation_id" => participation.id
113 })
114
115 result =
116 conn
117 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
118 |> json_response(200)
119
120 assert length(result) == 2
121
122 id_one = activity.id
123 id_two = activity_two.id
124 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
125 end
126
127 test "PATCH /api/v1/pleroma/conversations/:id" do
128 %{user: user, conn: conn} = oauth_access(["write:conversations"])
129 other_user = insert(:user)
130
131 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
132
133 [participation] = Participation.for_user(user)
134
135 participation = Repo.preload(participation, :recipients)
136
137 user = User.get_cached_by_id(user.id)
138 assert [user] == participation.recipients
139 assert other_user not in participation.recipients
140
141 result =
142 conn
143 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
144 "recipients" => [user.id, other_user.id]
145 })
146 |> json_response(200)
147
148 assert result["id"] == participation.id |> to_string
149
150 [participation] = Participation.for_user(user)
151 participation = Repo.preload(participation, :recipients)
152
153 assert user in participation.recipients
154 assert other_user in participation.recipients
155 end
156
157 test "POST /api/v1/pleroma/conversations/read" do
158 user = insert(:user)
159 %{user: other_user, conn: conn} = oauth_access(["write:notifications"])
160
161 {:ok, _activity} =
162 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
163
164 {:ok, _activity} =
165 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
166
167 [participation2, participation1] = Participation.for_user(other_user)
168 assert Participation.get(participation2.id).read == false
169 assert Participation.get(participation1.id).read == false
170 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 2
171
172 [%{"unread" => false}, %{"unread" => false}] =
173 conn
174 |> post("/api/v1/pleroma/conversations/read", %{})
175 |> json_response(200)
176
177 [participation2, participation1] = Participation.for_user(other_user)
178 assert Participation.get(participation2.id).read == true
179 assert Participation.get(participation1.id).read == true
180 assert User.get_cached_by_id(other_user.id).unread_conversation_count == 0
181 end
182
183 describe "POST /api/v1/pleroma/notifications/read" do
184 setup do: oauth_access(["write:notifications"])
185
186 test "it marks a single notification as read", %{user: user1, conn: conn} do
187 user2 = insert(:user)
188 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
189 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
190 {:ok, [notification1]} = Notification.create_notifications(activity1)
191 {:ok, [notification2]} = Notification.create_notifications(activity2)
192
193 response =
194 conn
195 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
196 |> json_response(:ok)
197
198 assert %{"pleroma" => %{"is_seen" => true}} = response
199 assert Repo.get(Notification, notification1.id).seen
200 refute Repo.get(Notification, notification2.id).seen
201 end
202
203 test "it marks multiple notifications as read", %{user: user1, conn: conn} do
204 user2 = insert(:user)
205 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
206 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
207 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
208
209 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
210
211 [response1, response2] =
212 conn
213 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
214 |> json_response(:ok)
215
216 assert %{"pleroma" => %{"is_seen" => true}} = response1
217 assert %{"pleroma" => %{"is_seen" => true}} = response2
218 assert Repo.get(Notification, notification1.id).seen
219 assert Repo.get(Notification, notification2.id).seen
220 refute Repo.get(Notification, notification3.id).seen
221 end
222
223 test "it returns error when notification not found", %{conn: conn} do
224 response =
225 conn
226 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
227 |> json_response(:bad_request)
228
229 assert response == %{"error" => "Cannot get notification"}
230 end
231 end
232 end