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
12 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
16 test "/api/v1/pleroma/conversations/:id", %{conn: conn} do
18 other_user = insert(:user)
21 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
23 [participation] = Participation.for_user(other_user)
27 |> assign(:user, other_user)
28 |> get("/api/v1/pleroma/conversations/#{participation.id}")
31 assert result["id"] == participation.id |> to_string()
34 test "/api/v1/pleroma/conversations/:id/statuses", %{conn: conn} do
36 other_user = insert(:user)
37 third_user = insert(:user)
40 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
43 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
45 [participation] = Participation.for_user(other_user)
48 CommonAPI.post(other_user, %{
50 "in_reply_to_status_id" => activity.id,
51 "in_reply_to_conversation_id" => participation.id
56 |> assign(:user, other_user)
57 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
60 assert length(result) == 2
63 id_two = activity_two.id
64 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
67 test "PATCH /api/v1/pleroma/conversations/:id", %{conn: conn} do
69 other_user = insert(:user)
71 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
73 [participation] = Participation.for_user(user)
75 participation = Repo.preload(participation, :recipients)
77 user = User.get_cached_by_id(user.id)
78 assert [user] == participation.recipients
79 assert other_user not in participation.recipients
83 |> assign(:user, user)
84 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
85 "recipients" => [user.id, other_user.id]
89 assert result["id"] == participation.id |> to_string
91 [participation] = Participation.for_user(user)
92 participation = Repo.preload(participation, :recipients)
94 assert user in participation.recipients
95 assert other_user in participation.recipients
98 test "POST /api/v1/pleroma/conversations/read", %{conn: conn} do
100 other_user = insert(:user)
103 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
106 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
108 [participation2, participation1] = Participation.for_user(other_user)
109 assert Participation.get(participation2.id).read == false
110 assert Participation.get(participation1.id).read == false
111 assert User.get_cached_by_id(other_user.id).info.unread_conversation_count == 2
113 [%{"unread" => false}, %{"unread" => false}] =
115 |> assign(:user, other_user)
116 |> post("/api/v1/pleroma/conversations/read", %{})
117 |> json_response(200)
119 [participation2, participation1] = Participation.for_user(other_user)
120 assert Participation.get(participation2.id).read == true
121 assert Participation.get(participation1.id).read == true
122 assert User.get_cached_by_id(other_user.id).info.unread_conversation_count == 0
125 describe "POST /api/v1/pleroma/notifications/read" do
126 test "it marks a single notification as read", %{conn: conn} do
127 user1 = insert(:user)
128 user2 = insert(:user)
129 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
130 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
131 {:ok, [notification1]} = Notification.create_notifications(activity1)
132 {:ok, [notification2]} = Notification.create_notifications(activity2)
136 |> assign(:user, user1)
137 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
138 |> json_response(:ok)
140 assert %{"pleroma" => %{"is_seen" => true}} = response
141 assert Repo.get(Notification, notification1.id).seen
142 refute Repo.get(Notification, notification2.id).seen
145 test "it marks multiple notifications as read", %{conn: conn} do
146 user1 = insert(:user)
147 user2 = insert(:user)
148 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
149 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
150 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
152 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
154 [response1, response2] =
156 |> assign(:user, user1)
157 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
158 |> json_response(:ok)
160 assert %{"pleroma" => %{"is_seen" => true}} = response1
161 assert %{"pleroma" => %{"is_seen" => true}} = response2
162 assert Repo.get(Notification, notification1.id).seen
163 assert Repo.get(Notification, notification2.id).seen
164 refute Repo.get(Notification, notification3.id).seen
167 test "it returns error when notification not found", %{conn: conn} do
168 user1 = insert(:user)
172 |> assign(:user, user1)
173 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
174 |> json_response(:bad_request)
176 assert response == %{"error" => "Cannot get notification"}