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 describe "POST /api/v1/pleroma/notifications/read" do
99 test "it marks a single notification as read", %{conn: conn} do
100 user1 = insert(:user)
101 user2 = insert(:user)
102 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
103 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
104 {:ok, [notification1]} = Notification.create_notifications(activity1)
105 {:ok, [notification2]} = Notification.create_notifications(activity2)
109 |> assign(:user, user1)
110 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
111 |> json_response(:ok)
113 assert %{"pleroma" => %{"is_seen" => true}} = response
114 assert Repo.get(Notification, notification1.id).seen
115 refute Repo.get(Notification, notification2.id).seen
118 test "it marks multiple notifications as read", %{conn: conn} do
119 user1 = insert(:user)
120 user2 = insert(:user)
121 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
122 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
123 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
125 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
127 [response1, response2] =
129 |> assign(:user, user1)
130 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
131 |> json_response(:ok)
133 assert %{"pleroma" => %{"is_seen" => true}} = response1
134 assert %{"pleroma" => %{"is_seen" => true}} = response2
135 assert Repo.get(Notification, notification1.id).seen
136 assert Repo.get(Notification, notification2.id).seen
137 refute Repo.get(Notification, notification3.id).seen
140 test "it returns error when notification not found", %{conn: conn} do
141 user1 = insert(:user)
145 |> assign(:user, user1)
146 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
147 |> json_response(:bad_request)
149 assert response == %{"error" => "Cannot get notification"}