Merge branch 'fix/emoji-api-nonsense' into 'develop'
[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 "requires 'follow' permission", %{conn: conn} do
30 token1 = insert(:oauth_token, scopes: ["read", "write"])
31 token2 = insert(:oauth_token, scopes: ["follow"])
32 another_user = insert(:user)
33
34 for token <- [token1, token2] do
35 conn =
36 conn
37 |> put_req_header("authorization", "Bearer #{token.token}")
38 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
39
40 if token == token1 do
41 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
42 else
43 assert json_response(conn, 200)
44 end
45 end
46 end
47 end
48
49 describe "POST /api/pleroma/blocks_import" do
50 test "it returns HTTP 200", %{conn: conn} do
51 user1 = insert(:user)
52 user2 = insert(:user)
53
54 response =
55 conn
56 |> assign(:user, user1)
57 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
58 |> json_response(:ok)
59
60 assert response == "job started"
61 end
62 end
63
64 describe "POST /api/pleroma/notifications/read" do
65 test "it marks a single notification as read", %{conn: conn} do
66 user1 = insert(:user)
67 user2 = insert(:user)
68 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
69 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
70 {:ok, [notification1]} = Notification.create_notifications(activity1)
71 {:ok, [notification2]} = Notification.create_notifications(activity2)
72
73 conn
74 |> assign(:user, user1)
75 |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
76 |> json_response(:ok)
77
78 assert Repo.get(Notification, notification1.id).seen
79 refute Repo.get(Notification, notification2.id).seen
80 end
81 end
82
83 describe "PUT /api/pleroma/notification_settings" do
84 test "it updates notification settings", %{conn: conn} do
85 user = insert(:user)
86
87 conn
88 |> assign(:user, user)
89 |> put("/api/pleroma/notification_settings", %{
90 "remote" => false,
91 "followers" => false,
92 "bar" => 1
93 })
94 |> json_response(:ok)
95
96 user = Repo.get(User, user.id)
97
98 assert %{"remote" => false, "local" => true, "followers" => false, "follows" => true} ==
99 user.info.notification_settings
100 end
101 end
102
103 describe "GET /api/statusnet/config.json" do
104 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
105 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
106 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
107
108 response =
109 conn
110 |> get("/api/statusnet/config.json")
111 |> json_response(:ok)
112
113 assert response["site"]["safeDMMentionsEnabled"] == "1"
114
115 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
116
117 response =
118 conn
119 |> get("/api/statusnet/config.json")
120 |> json_response(:ok)
121
122 assert response["site"]["safeDMMentionsEnabled"] == "0"
123
124 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
125 end
126
127 test "it returns the managed config", %{conn: conn} do
128 Pleroma.Config.put([:instance, :managed_config], false)
129 Pleroma.Config.put([:fe], theme: "rei-ayanami-towel")
130
131 response =
132 conn
133 |> get("/api/statusnet/config.json")
134 |> json_response(:ok)
135
136 refute response["site"]["pleromafe"]
137
138 Pleroma.Config.put([:instance, :managed_config], true)
139
140 response =
141 conn
142 |> get("/api/statusnet/config.json")
143 |> json_response(:ok)
144
145 assert response["site"]["pleromafe"]
146 end
147
148 test "if :pleroma, :fe is false, it returns the new style config settings", %{conn: conn} do
149 Pleroma.Config.put([:instance, :managed_config], true)
150 Pleroma.Config.put([:fe, :theme], "rei-ayanami-towel")
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 assert response["site"]["pleromafe"]["theme"] == "rei-ayanami-towel"
159
160 Pleroma.Config.put([:fe], false)
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 end