Strip status data from Flag (when federating or closing/resolving report)
[akkoma] / test / web / pleroma_api / controllers / 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.User
12 alias Pleroma.Web.CommonAPI
13
14 import Pleroma.Factory
15
16 test "/api/v1/pleroma/conversations/:id", %{conn: conn} do
17 user = insert(:user)
18 other_user = insert(:user)
19
20 {:ok, _activity} =
21 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
22
23 [participation] = Participation.for_user(other_user)
24
25 result =
26 conn
27 |> assign(:user, other_user)
28 |> get("/api/v1/pleroma/conversations/#{participation.id}")
29 |> json_response(200)
30
31 assert result["id"] == participation.id |> to_string()
32 end
33
34 test "/api/v1/pleroma/conversations/:id/statuses", %{conn: conn} do
35 user = insert(:user)
36 other_user = insert(:user)
37 third_user = insert(:user)
38
39 {:ok, _activity} =
40 CommonAPI.post(user, %{"status" => "Hi @#{third_user.nickname}!", "visibility" => "direct"})
41
42 {:ok, activity} =
43 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}!", "visibility" => "direct"})
44
45 [participation] = Participation.for_user(other_user)
46
47 {:ok, activity_two} =
48 CommonAPI.post(other_user, %{
49 "status" => "Hi!",
50 "in_reply_to_status_id" => activity.id,
51 "in_reply_to_conversation_id" => participation.id
52 })
53
54 result =
55 conn
56 |> assign(:user, other_user)
57 |> get("/api/v1/pleroma/conversations/#{participation.id}/statuses")
58 |> json_response(200)
59
60 assert length(result) == 2
61
62 id_one = activity.id
63 id_two = activity_two.id
64 assert [%{"id" => ^id_one}, %{"id" => ^id_two}] = result
65 end
66
67 test "PATCH /api/v1/pleroma/conversations/:id", %{conn: conn} do
68 user = insert(:user)
69 other_user = insert(:user)
70
71 {:ok, _activity} = CommonAPI.post(user, %{"status" => "Hi", "visibility" => "direct"})
72
73 [participation] = Participation.for_user(user)
74
75 participation = Repo.preload(participation, :recipients)
76
77 user = User.get_cached_by_id(user.id)
78 assert [user] == participation.recipients
79 assert other_user not in participation.recipients
80
81 result =
82 conn
83 |> assign(:user, user)
84 |> patch("/api/v1/pleroma/conversations/#{participation.id}", %{
85 "recipients" => [user.id, other_user.id]
86 })
87 |> json_response(200)
88
89 assert result["id"] == participation.id |> to_string
90
91 [participation] = Participation.for_user(user)
92 participation = Repo.preload(participation, :recipients)
93
94 assert user in participation.recipients
95 assert other_user in participation.recipients
96 end
97
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)
106
107 response =
108 conn
109 |> assign(:user, user1)
110 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
111 |> json_response(:ok)
112
113 assert %{"pleroma" => %{"is_seen" => true}} = response
114 assert Repo.get(Notification, notification1.id).seen
115 refute Repo.get(Notification, notification2.id).seen
116 end
117
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}"})
124
125 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
126
127 [response1, response2] =
128 conn
129 |> assign(:user, user1)
130 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
131 |> json_response(:ok)
132
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
138 end
139
140 test "it returns error when notification not found", %{conn: conn} do
141 user1 = insert(:user)
142
143 response =
144 conn
145 |> assign(:user, user1)
146 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
147 |> json_response(:bad_request)
148
149 assert response == %{"error" => "Cannot get notification"}
150 end
151 end
152 end