Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel
[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 test "POST /api/v1/pleroma/conversations/read", %{conn: conn} do
99 user = insert(:user)
100 other_user = insert(:user)
101
102 {:ok, _activity} =
103 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
104
105 {:ok, _activity} =
106 CommonAPI.post(user, %{"status" => "Hi @#{other_user.nickname}", "visibility" => "direct"})
107
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).unread_conversation_count == 2
112
113 [%{"unread" => false}, %{"unread" => false}] =
114 conn
115 |> assign(:user, other_user)
116 |> post("/api/v1/pleroma/conversations/read", %{})
117 |> json_response(200)
118
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).unread_conversation_count == 0
123 end
124
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)
133
134 response =
135 conn
136 |> assign(:user, user1)
137 |> post("/api/v1/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
138 |> json_response(:ok)
139
140 assert %{"pleroma" => %{"is_seen" => true}} = response
141 assert Repo.get(Notification, notification1.id).seen
142 refute Repo.get(Notification, notification2.id).seen
143 end
144
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}"})
151
152 [notification3, notification2, notification1] = Notification.for_user(user1, %{limit: 3})
153
154 [response1, response2] =
155 conn
156 |> assign(:user, user1)
157 |> post("/api/v1/pleroma/notifications/read", %{"max_id" => "#{notification2.id}"})
158 |> json_response(:ok)
159
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
165 end
166
167 test "it returns error when notification not found", %{conn: conn} do
168 user1 = insert(:user)
169
170 response =
171 conn
172 |> assign(:user, user1)
173 |> post("/api/v1/pleroma/notifications/read", %{"id" => "22222222222222"})
174 |> json_response(:bad_request)
175
176 assert response == %{"error" => "Cannot get notification"}
177 end
178 end
179 end