Merge remote-tracking branch 'origin/develop' into reactions
[akkoma] / test / web / pleroma_api / 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.Repo
11 alias Pleroma.Web.CommonAPI
12
13 import Pleroma.Factory
14
15 test "POST /api/v1/pleroma/statuses/:id/react_with_emoji", %{conn: conn} do
16 user = insert(:user)
17 other_user = insert(:user)
18
19 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
20
21 result =
22 conn
23 |> assign(:user, other_user)
24 |> post("/api/v1/pleroma/statuses/#{activity.id}/react_with_emoji", %{"emoji" => "☕"})
25
26 assert %{"id" => id} = json_response(result, 200)
27 assert to_string(activity.id) == id
28 end
29
30 test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
31 user = insert(:user)
32 other_user = insert(:user)
33
34 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
35
36 result =
37 conn
38 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
39 |> json_response(200)
40
41 assert result == %{}
42
43 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
44
45 result =
46 conn
47 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
48 |> json_response(200)
49
50 [represented_user] = result["🎅"]
51 assert represented_user["id"] == other_user.id
52 end
53
54 test "/api/v1/pleroma/conversations/:id", %{conn: conn} do
55 user = insert(:user)
56 other_user = insert(:user)
57
58 {:ok, _activity} =
59 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
60
61 [participation] = Participation.for_user(other_user)
62
63 result =
64 conn
65 |> assign(:user, other_user)
66 |> get("/api/v1/pleroma/conversations/#{participation.id}")
67 |> json_response(200)
68
69 assert result["id"] == participation.id |> to_string()
70 end
71
72 test "/api/v1/pleroma/conversations/:id/statuses", %{conn: conn} do
73 user = insert(:user)
74 other_user = insert(:user)
75 third_user = insert(:user)
76
77 {:ok, _activity} =
78 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
79
80 {:ok, activity} =
81 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
82
83 [participation] = Participation.for_user(other_user)
84
85 {:ok, activity_two} =
86 CommonAPI.post(other_user, %{
87 "status" => "Hi!",
88 "in_reply_to_status_id" => activity.id,
89 "in_reply_to_conversation_id" => participation.id
90 })
91
92 result =
93 conn
94 |> assign(:user, other_user)
95 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
96 |> json_response(200)
97
98 assert length(result) == 2
99
100 id_one = activity.id
101 id_two = activity_two.id
102 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
103 end
104
105 test "PATCH /api/v1/pleroma/conversations/:id", %{conn: conn} do
106 user = insert(:user)
107 other_user = insert(:user)
108
109 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
110
111 [participation] = Participation.for_user(user)
112
113 participation = Repo.preload(participation, :recipients)
114
115 assert [user] == participation.recipients
116 assert other_user not in participation.recipients
117
118 result =
119 conn
120 |> assign(:user, user)
121 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
122 "recipients" => [user.id, other_user.id]
123 })
124 |> json_response(200)
125
126 assert result["id"] == participation.id |> to_string
127
128 [participation] = Participation.for_user(user)
129 participation = Repo.preload(participation, :recipients)
130
131 assert user in participation.recipients
132 assert other_user in participation.recipients
133 end
134
135 describe "POST /api/v1/pleroma/notifications/read" do
136 test "it marks a single notification as read", %{conn: conn} do
137 user1 = insert(:user)
138 user2 = insert(:user)
139 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
140 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
141 {:ok, [notification1]} = Notification.create_notifications(activity1)
142 {:ok, [notification2]} = Notification.create_notifications(activity2)
143
144 response =
145 conn
146 |> assign(:user, user1)
147 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
148 |> json_response(:ok)
149
150 assert %{"pleroma" => %{"is_seen" => true}} = response
151 assert Repo.get(Notification, notification1.id).seen
152 refute Repo.get(Notification, notification2.id).seen
153 end
154
155 test "it marks multiple notifications as read", %{conn: conn} do
156 user1 = insert(:user)
157 user2 = insert(:user)
158 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
159 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
160 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
161
162 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
163
164 [response1, response2] =
165 conn
166 |> assign(:user, user1)
167 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
168 |> json_response(:ok)
169
170 assert %{"pleroma" => %{"is_seen" => true}} = response1
171 assert %{"pleroma" => %{"is_seen" => true}} = response2
172 assert Repo.get(Notification, notification1.id).seen
173 assert Repo.get(Notification, notification2.id).seen
174 refute Repo.get(Notification, notification3.id).seen
175 end
176
177 test "it returns error when notification not found", %{conn: conn} do
178 user1 = insert(:user)
179
180 response =
181 conn
182 |> assign(:user, user1)
183 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
184 |> json_response(:bad_request)
185
186 assert response == %{"error" => "Cannot get notification"}
187 end
188 end
189 end