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