Merge branch 'develop' into feature/polls-2-electric-boogalo
[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 "followers" => false,
106 "bar" => 1
107 })
108 |> json_response(:ok)
109
110 user = Repo.get(User, user.id)
111
112 assert %{
113 "followers" => false,
114 "follows" => true,
115 "non_follows" => true,
116 "non_followers" => true
117 } == user.info.notification_settings
118 end
119 end
120
121 describe "GET /api/statusnet/config.json" do
122 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
123 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
124 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
125
126 response =
127 conn
128 |> get("/api/statusnet/config.json")
129 |> json_response(:ok)
130
131 assert response["site"]["safeDMMentionsEnabled"] == "1"
132
133 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
134
135 response =
136 conn
137 |> get("/api/statusnet/config.json")
138 |> json_response(:ok)
139
140 assert response["site"]["safeDMMentionsEnabled"] == "0"
141
142 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
143 end
144
145 test "it returns the managed config", %{conn: conn} do
146 Pleroma.Config.put([:instance, :managed_config], false)
147 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
148
149 response =
150 conn
151 |> get("/api/statusnet/config.json")
152 |> json_response(:ok)
153
154 refute response["site"]["pleromafe"]
155
156 Pleroma.Config.put([:instance, :managed_config], true)
157
158 response =
159 conn
160 |> get("/api/statusnet/config.json")
161 |> json_response(:ok)
162
163 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
164 end
165 end
166
167 describe "GET /api/pleroma/frontend_configurations" do
168 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
169 config = [
170 frontend_a: %{
171 x: 1,
172 y: 2
173 },
174 frontend_b: %{
175 z: 3
176 }
177 ]
178
179 Pleroma.Config.put(:frontend_configurations, config)
180
181 response =
182 conn
183 |> get("/api/pleroma/frontend_configurations")
184 |> json_response(:ok)
185
186 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
187 end
188 end
189
190 describe "/api/pleroma/emoji" do
191 test "returns json with custom emoji with tags", %{conn: conn} do
192 emoji =
193 conn
194 |> get("/api/pleroma/emoji")
195 |> json_response(200)
196
197 assert Enum.all?(emoji, fn
198 {_key,
199 %{
200 "image_url" => url,
201 "tags" => tags
202 }} ->
203 is_binary(url) and is_list(tags)
204 end)
205 end
206 end
207
208 describe "GET /ostatus_subscribe?acct=...." do
209 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
210 conn =
211 get(
212 conn,
213 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
214 )
215
216 assert redirected_to(conn) =~ "/notice/"
217 end
218
219 test "show follow account page if the `acct` is a account link", %{conn: conn} do
220 response =
221 get(
222 conn,
223 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
224 )
225
226 assert html_response(response, 200) =~ "Log in to follow"
227 end
228 end
229
230 test "GET /api/pleroma/healthcheck", %{conn: conn} do
231 conn = get(conn, "/api/pleroma/healthcheck")
232
233 assert conn.status in [200, 503]
234 end
235
236 describe "POST /api/pleroma/disable_account" do
237 test "it returns HTTP 200", %{conn: conn} do
238 user = insert(:user)
239
240 response =
241 conn
242 |> assign(:user, user)
243 |> post("/api/pleroma/disable_account", %{"password" => "test"})
244 |> json_response(:ok)
245
246 assert response == %{"status" => "success"}
247
248 user = User.get_cached_by_id(user.id)
249
250 assert user.info.deactivated == true
251 end
252 end
253 end