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 "/api/v1/pleroma/conversations/:id", %{conn: conn} do
17 other_user = insert(:user)
20 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
22 [participation] = Participation.for_user(other_user)
26 |> assign(:user, other_user)
27 |> get("/api/v1/pleroma/conversations/#{participation.id}")
30 assert result["id"] == participation.id |> to_string()
33 test "/api/v1/pleroma/conversations/:id/statuses", %{conn: conn} do
35 other_user = insert(:user)
36 third_user = insert(:user)
39 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
42 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
44 [participation] = Participation.for_user(other_user)
47 CommonAPI.post(other_user, %{
49 "in_reply_to_status_id" => activity.id,
50 "in_reply_to_conversation_id" => participation.id
55 |> assign(:user, other_user)
56 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
59 assert length(result) == 2
62 id_two = activity_two.id
63 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
66 test "PATCH /api/v1/pleroma/conversations/:id", %{conn: conn} do
68 other_user = insert(:user)
70 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
72 [participation] = Participation.for_user(user)
74 participation = Repo.preload(participation, :recipients)
76 assert [user] == participation.recipients
77 assert other_user not in participation.recipients
81 |> assign(:user, user)
82 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
83 "recipients" => [user.id, other_user.id]
87 assert result["id"] == participation.id |> to_string
89 [participation] = Participation.for_user(user)
90 participation = Repo.preload(participation, :recipients)
92 assert user in participation.recipients
93 assert other_user in participation.recipients
96 describe "POST /api/v1/pleroma/notifications/read" do
97 test "it marks a single notification as read", %{conn: conn} do
100 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
101 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
102 {:ok, [notification1]} = Notification.create_notifications(activity1)
103 {:ok, [notification2]} = Notification.create_notifications(activity2)
107 |> assign(:user, user1)
108 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
109 |> json_response(:ok)
111 assert %{"pleroma" => %{"is_seen" => true}} = response
112 assert Repo.get(Notification, notification1.id).seen
113 refute Repo.get(Notification, notification2.id).seen
116 test "it marks multiple notifications as read", %{conn: conn} do
117 user1 = insert(:user)
118 user2 = insert(:user)
119 {:ok, _activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
120 {:ok, _activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
121 {:ok, _activity3} = CommonAPI.post(user2, %{"status" => "HIE @#{user1.nickname}"})
123 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
125 [response1, response2] =
127 |> assign(:user, user1)
128 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
129 |> json_response(:ok)
131 assert %{"pleroma" => %{"is_seen" => true}} = response1
132 assert %{"pleroma" => %{"is_seen" => true}} = response2
133 assert Repo.get(Notification, notification1.id).seen
134 assert Repo.get(Notification, notification2.id).seen
135 refute Repo.get(Notification, notification3.id).seen
138 test "it returns error when notification not found", %{conn: conn} do
139 user1 = insert(:user)
143 |> assign(:user, user1)
144 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
145 |> json_response(:bad_request)
147 assert response == %{"error" => "Cannot get notification"}