Feature/826 healthcheck endpoint
[akkoma] / test / web / twitter_api / util_controller_test.exs
1 defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
2 use Pleroma.Web.ConnCase
3
4 alias Pleroma.Notification
5 alias Pleroma.Repo
6 alias Pleroma.User
7 alias Pleroma.Web.CommonAPI
8 import Pleroma.Factory
9
10 setup do
11 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
12 :ok
13 end
14
15 describe "POST /api/pleroma/follow_import" do
16 test "it returns HTTP 200", %{conn: conn} do
17 user1 = insert(:user)
18 user2 = insert(:user)
19
20 response =
21 conn
22 |> assign(:user, user1)
23 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
24 |> json_response(:ok)
25
26 assert response == "job started"
27 end
28
29 test "it imports new-style mastodon follow lists", %{conn: conn} do
30 user1 = insert(:user)
31 user2 = insert(:user)
32
33 response =
34 conn
35 |> assign(:user, user1)
36 |> post("/api/pleroma/follow_import", %{
37 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
38 })
39 |> json_response(:ok)
40
41 assert response == "job started"
42 end
43
44 test "requires 'follow' permission", %{conn: conn} do
45 token1 = insert(:oauth_token, scopes: ["read", "write"])
46 token2 = insert(:oauth_token, scopes: ["follow"])
47 another_user = insert(:user)
48
49 for token <- [token1, token2] do
50 conn =
51 conn
52 |> put_req_header("authorization", "Bearer #{token.token}")
53 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
54
55 if token == token1 do
56 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
57 else
58 assert json_response(conn, 200)
59 end
60 end
61 end
62 end
63
64 describe "POST /api/pleroma/blocks_import" do
65 test "it returns HTTP 200", %{conn: conn} do
66 user1 = insert(:user)
67 user2 = insert(:user)
68
69 response =
70 conn
71 |> assign(:user, user1)
72 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
73 |> json_response(:ok)
74
75 assert response == "job started"
76 end
77 end
78
79 describe "POST /api/pleroma/notifications/read" do
80 test "it marks a single notification as read", %{conn: conn} do
81 user1 = insert(:user)
82 user2 = insert(:user)
83 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
84 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
85 {:ok, [notification1]} = Notification.create_notifications(activity1)
86 {:ok, [notification2]} = Notification.create_notifications(activity2)
87
88 conn
89 |> assign(:user, user1)
90 |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
91 |> json_response(:ok)
92
93 assert Repo.get(Notification, notification1.id).seen
94 refute Repo.get(Notification, notification2.id).seen
95 end
96 end
97
98 describe "PUT /api/pleroma/notification_settings" do
99 test "it updates notification settings", %{conn: conn} do
100 user = insert(:user)
101
102 conn
103 |> assign(:user, user)
104 |> put("/api/pleroma/notification_settings", %{
105 "remote" => false,
106 "followers" => false,
107 "bar" => 1
108 })
109 |> json_response(:ok)
110
111 user = Repo.get(User, user.id)
112
113 assert %{"remote" => false, "local" => true, "followers" => false, "follows" => true} ==
114 user.info.notification_settings
115 end
116 end
117
118 describe "GET /api/statusnet/config.json" do
119 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
120 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
121 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
122
123 response =
124 conn
125 |> get("/api/statusnet/config.json")
126 |> json_response(:ok)
127
128 assert response["site"]["safeDMMentionsEnabled"] == "1"
129
130 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
131
132 response =
133 conn
134 |> get("/api/statusnet/config.json")
135 |> json_response(:ok)
136
137 assert response["site"]["safeDMMentionsEnabled"] == "0"
138
139 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
140 end
141
142 test "it returns the managed config", %{conn: conn} do
143 Pleroma.Config.put([:instance, :managed_config], false)
144 Pleroma.Config.put([:fe], theme: "rei-ayanami-towel")
145
146 response =
147 conn
148 |> get("/api/statusnet/config.json")
149 |> json_response(:ok)
150
151 refute response["site"]["pleromafe"]
152
153 Pleroma.Config.put([:instance, :managed_config], true)
154
155 response =
156 conn
157 |> get("/api/statusnet/config.json")
158 |> json_response(:ok)
159
160 assert response["site"]["pleromafe"]
161 end
162
163 test "if :pleroma, :fe is false, it returns the new style config settings", %{conn: conn} do
164 Pleroma.Config.put([:instance, :managed_config], true)
165 Pleroma.Config.put([:fe, :theme], "rei-ayanami-towel")
166 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
167
168 response =
169 conn
170 |> get("/api/statusnet/config.json")
171 |> json_response(:ok)
172
173 assert response["site"]["pleromafe"]["theme"] == "rei-ayanami-towel"
174
175 Pleroma.Config.put([:fe], false)
176
177 response =
178 conn
179 |> get("/api/statusnet/config.json")
180 |> json_response(:ok)
181
182 assert response["site"]["pleromafe"]["theme"] == "asuka-hospital"
183 end
184 end
185
186 describe "GET /api/pleroma/frontend_configurations" do
187 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
188 config = [
189 frontend_a: %{
190 x: 1,
191 y: 2
192 },
193 frontend_b: %{
194 z: 3
195 }
196 ]
197
198 Pleroma.Config.put(:frontend_configurations, config)
199
200 response =
201 conn
202 |> get("/api/pleroma/frontend_configurations")
203 |> json_response(:ok)
204
205 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
206 end
207 end
208
209 describe "/api/pleroma/emoji" do
210 test "returns json with custom emoji with tags", %{conn: conn} do
211 emoji =
212 conn
213 |> get("/api/pleroma/emoji")
214 |> json_response(200)
215
216 assert Enum.all?(emoji, fn
217 {_key,
218 %{
219 "image_url" => url,
220 "tags" => tags
221 }} ->
222 is_binary(url) and is_list(tags)
223 end)
224 end
225 end
226
227 describe "GET /ostatus_subscribe?acct=...." do
228 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
229 conn =
230 get(
231 conn,
232 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
233 )
234
235 assert redirected_to(conn) =~ "/notice/"
236 end
237
238 test "show follow account page if the `acct` is a account link", %{conn: conn} do
239 response =
240 get(
241 conn,
242 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
243 )
244
245 assert html_response(response, 200) =~ "Log in to follow"
246 end
247 end
248
249 test "GET /api/pleroma/healthcheck", %{conn: conn} do
250 conn = get(conn, "/api/pleroma/healthcheck")
251
252 assert conn.status in [200, 503]
253 end
254 end