Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[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 conn =
61 conn
62 |> assign(:user, user)
63 |> assign(:token, insert(:oauth_token, user: user, scopes: ["read:statuses"]))
64
65 result =
66 conn
67 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
68 |> json_response(200)
69
70 assert result == %{}
71
72 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
73
74 result =
75 conn
76 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
77 |> json_response(200)
78
79 [represented_user] = result["🎅"]
80 assert represented_user["id"] == other_user.id
81 end
82
83 test "/api/v1/pleroma/conversations/:id" do
84 user = insert(:user)
85 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
86
87 {:ok, _activity} =
88 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
89
90 [participation] = Participation.for_user(other_user)
91
92 result =
93 conn
94 |> get("/api/v1/pleroma/conversations/#{participation.id}")
95 |> json_response(200)
96
97 assert result["id"] == participation.id |> to_string()
98 end
99
100 test "/api/v1/pleroma/conversations/:id/statuses" do
101 user = insert(:user)
102 %{user: other_user, conn: conn} = oauth_access(["read:statuses"])
103 third_user = insert(:user)
104
105 {:ok, _activity} =
106 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
107
108 {:ok, activity} =
109 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
110
111 [participation] = Participation.for_user(other_user)
112
113 {:ok, activity_two} =
114 CommonAPI.post(other_user, %{
115 "status" => "Hi!",
116 "in_reply_to_status_id" => activity.id,
117 "in_reply_to_conversation_id" => participation.id
118 })
119
120 result =
121 conn
122 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
123 |> json_response(200)
124
125 assert length(result) == 2
126
127 id_one = activity.id
128 id_two = activity_two.id
129 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
130 end
131
132 test "PATCH /api/v1/pleroma/conversations/:id" do
133 %{user: user, conn: conn} = oauth_access(["write:conversations"])
134 other_user = insert(:user)
135
136 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
137
138 [participation] = Participation.for_user(user)
139
140 participation = Repo.preload(participation, :recipients)
141
142 user = User.get_cached_by_id(user.id)
143 assert [user] == participation.recipients
144 assert other_user not in participation.recipients
145
146 result =
147 conn
148 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
149 "recipients" => [user.id, other_user.id]
150 })
151 |> json_response(200)
152
153 assert result["id"] == participation.id |> to_string
154
155 [participation] = Participation.for_user(user)
156 participation = Repo.preload(participation, :recipients)
157
158 assert user in participation.recipients
159 assert other_user in participation.recipients
160 end
161
162 test "POST /api/v1/pleroma/conversations/read" do
163 user = insert(:user)
164 %{user: other_user, conn: conn} = oauth_access(["write:notifications"])
165
166 {:ok, _activity} =
167 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
168
169 {:ok, _activity} =
170 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
171
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
176
177 [%{"unread" => false}, %{"unread" => false}] =
178 conn
179 |> post("/api/v1/pleroma/conversations/read", %{})
180 |> json_response(200)
181
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
186 end
187
188 describe "POST /api/v1/pleroma/notifications/read" do
189 setup do: oauth_access(["write:notifications"])
190
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)
197
198 response =
199 conn
200 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
201 |> json_response(:ok)
202
203 assert %{"pleroma" => %{"is_seen" => true}} = response
204 assert Repo.get(Notification, notification1.id).seen
205 refute Repo.get(Notification, notification2.id).seen
206 end
207
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}"})
213
214 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
215
216 [response1, response2] =
217 conn
218 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
219 |> json_response(:ok)
220
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
226 end
227
228 test "it returns error when notification not found", %{conn: conn} do
229 response =
230 conn
231 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
232 |> json_response(:bad_request)
233
234 assert response == %{"error" => "Cannot get notification"}
235 end
236 end
237 end