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
11 alias Pleroma.Web.CommonAPI
13 import Pleroma.Factory
15 test "POST /api/v1/pleroma/statuses/:id/react_with_emoji", %{conn: conn} do
17 other_user = insert(:user)
19 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
23 |> assign(:user, other_user)
24 |> post("/api/v1/pleroma/statuses/#{activity.id}/react_with_emoji", %{"emoji" => "☕"})
26 assert %{"id" => id} = json_response(result, 200)
27 assert to_string(activity.id) == id
30 test "GET /api/v1/pleroma/statuses/:id/emoji_reactions_by", %{conn: conn} do
32 other_user = insert(:user)
34 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe"})
38 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
43 {:ok, _, _} = CommonAPI.react_with_emoji(activity.id, other_user, "🎅")
47 |> get("/api/v1/pleroma/statuses/#{activity.id}/emoji_reactions_by")
50 [represented_user] = result["🎅"]
51 assert represented_user["id"] == other_user.id
54 test "/api/v1/pleroma/conversations/:id", %{conn: conn} do
56 other_user = insert(:user)
59 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
61 [participation] = Participation.for_user(other_user)
65 |> assign(:user, other_user)
66 |> get("/api/v1/pleroma/conversations/#{participation.id}")
69 assert result["id"] == participation.id |> to_string()
72 test "/api/v1/pleroma/conversations/:id/statuses", %{conn: conn} do
74 other_user = insert(:user)
75 third_user = insert(:user)
78 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
81 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
83 [participation] = Participation.for_user(other_user)
86 CommonAPI.post(other_user, %{
88 "in_reply_to_status_id" => activity.id,
89 "in_reply_to_conversation_id" => participation.id
94 |> assign(:user, other_user)
95 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
98 assert length(result) == 2
101 id_two = activity_two.id
102 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
105 test "PATCH /api/v1/pleroma/conversations/:id", %{conn: conn} do
107 other_user = insert(:user)
109 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
111 [participation] = Participation.for_user(user)
113 participation = Repo.preload(participation, :recipients)
115 assert [user] == participation.recipients
116 assert other_user not in participation.recipients
120 |> assign(:user, user)
121 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
122 "recipients" => [user.id, other_user.id]
124 |> json_response(200)
126 assert result["id"] == participation.id |> to_string
128 [participation] = Participation.for_user(user)
129 participation = Repo.preload(participation, :recipients)
131 assert user in participation.recipients
132 assert other_user in participation.recipients
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)
146 |> assign(:user, user1)
147 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
148 |> json_response(:ok)
150 assert %{"pleroma" => %{"is_seen" => true}} = response
151 assert Repo.get(Notification, notification1.id).seen
152 refute Repo.get(Notification, notification2.id).seen
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}"})
162 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
164 [response1, response2] =
166 |> assign(:user, user1)
167 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
168 |> json_response(:ok)
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
177 test "it returns error when notification not found", %{conn: conn} do
178 user1 = insert(:user)
182 |> assign(:user, user1)
183 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
184 |> json_response(:bad_request)
186 assert response == %{"error" => "Cannot get notification"}