1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
6 use Pleroma.Web.ConnCase
7 use Oban.Testing, repo: Pleroma.Repo
9 alias Pleroma.Notification
11 alias Pleroma.Tests.ObanHelpers
13 alias Pleroma.Web.CommonAPI
14 import Pleroma.Factory
18 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 instance_config = Pleroma.Config.get([:instance])
21 pleroma_fe = Pleroma.Config.get([:frontend_configurations, :pleroma_fe])
22 deny_follow_blocked = Pleroma.Config.get([:user, :deny_follow_blocked])
25 Pleroma.Config.put([:instance], instance_config)
26 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], pleroma_fe)
27 Pleroma.Config.put([:user, :deny_follow_blocked], deny_follow_blocked)
33 describe "POST /api/pleroma/follow_import" do
34 test "it returns HTTP 200", %{conn: conn} do
40 |> assign(:user, user1)
41 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
44 assert response == "job started"
47 test "it imports follow lists from file", %{conn: conn} do
53 read!: fn "follow_list.txt" ->
54 "Account address,Show boosts\n#{user2.ap_id},true"
59 |> assign(:user, user1)
60 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
63 assert response == "job started"
65 assert ObanHelpers.member?(
67 "op" => "follow_import",
68 "follower_id" => user1.id,
69 "followed_identifiers" => [user2.ap_id]
71 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
76 test "it imports new-style mastodon follow lists", %{conn: conn} do
82 |> assign(:user, user1)
83 |> post("/api/pleroma/follow_import", %{
84 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
88 assert response == "job started"
91 test "requires 'follow' permission", %{conn: conn} do
92 token1 = insert(:oauth_token, scopes: ["read", "write"])
93 token2 = insert(:oauth_token, scopes: ["follow"])
94 another_user = insert(:user)
96 for token <- [token1, token2] do
99 |> put_req_header("authorization", "Bearer #{token.token}")
100 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
102 if token == token1 do
103 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
105 assert json_response(conn, 200)
111 describe "POST /api/pleroma/blocks_import" do
112 test "it returns HTTP 200", %{conn: conn} do
113 user1 = insert(:user)
114 user2 = insert(:user)
118 |> assign(:user, user1)
119 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
120 |> json_response(:ok)
122 assert response == "job started"
125 test "it imports blocks users from file", %{conn: conn} do
126 user1 = insert(:user)
127 user2 = insert(:user)
128 user3 = insert(:user)
131 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
135 |> assign(:user, user1)
136 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
137 |> json_response(:ok)
139 assert response == "job started"
141 assert ObanHelpers.member?(
143 "op" => "blocks_import",
144 "blocker_id" => user1.id,
145 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
147 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
153 describe "POST /api/pleroma/notifications/read" do
154 test "it marks a single notification as read", %{conn: conn} do
155 user1 = insert(:user)
156 user2 = insert(:user)
157 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
158 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
159 {:ok, [notification1]} = Notification.create_notifications(activity1)
160 {:ok, [notification2]} = Notification.create_notifications(activity2)
163 |> assign(:user, user1)
164 |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
165 |> json_response(:ok)
167 assert Repo.get(Notification, notification1.id).seen
168 refute Repo.get(Notification, notification2.id).seen
171 test "it returns error when notification not found", %{conn: conn} do
172 user1 = insert(:user)
176 |> assign(:user, user1)
177 |> post("/api/pleroma/notifications/read", %{"id" => "22222222222222"})
178 |> json_response(403)
180 assert response == %{"error" => "Cannot get notification"}
184 describe "PUT /api/pleroma/notification_settings" do
185 test "it updates notification settings", %{conn: conn} do
189 |> assign(:user, user)
190 |> put("/api/pleroma/notification_settings", %{
191 "followers" => false,
194 |> json_response(:ok)
196 user = Repo.get(User, user.id)
199 "followers" => false,
201 "non_follows" => true,
202 "non_followers" => true
203 } == user.info.notification_settings
207 describe "GET /api/statusnet/config" do
208 test "it returns config in xml format", %{conn: conn} do
209 instance = Pleroma.Config.get(:instance)
213 |> put_req_header("accept", "application/xml")
214 |> get("/api/statusnet/config")
218 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
219 Pleroma.Web.base_url()
220 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
221 !Keyword.get(instance, :registrations_open)
222 }</closed>\n</site>\n</config>\n"
225 test "it returns config in json format", %{conn: conn} do
226 instance = Pleroma.Config.get(:instance)
227 Pleroma.Config.put([:instance, :managed_config], true)
228 Pleroma.Config.put([:instance, :registrations_open], false)
229 Pleroma.Config.put([:instance, :invites_enabled], true)
230 Pleroma.Config.put([:instance, :public], false)
231 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
235 |> put_req_header("accept", "application/json")
236 |> get("/api/statusnet/config")
237 |> json_response(:ok)
241 "accountActivationRequired" => "0",
243 "description" => Keyword.get(instance, :description),
244 "invitesEnabled" => "1",
245 "name" => Keyword.get(instance, :name),
246 "pleromafe" => %{"theme" => "asuka-hospital"},
248 "safeDMMentionsEnabled" => "0",
249 "server" => Pleroma.Web.base_url(),
250 "textlimit" => to_string(Keyword.get(instance, :limit)),
252 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
253 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
254 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
255 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
257 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
261 assert response == expected_data
264 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
265 option = Pleroma.Config.get([:instance, :safe_dm_mentions])
266 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
270 |> get("/api/statusnet/config.json")
271 |> json_response(:ok)
273 assert response["site"]["safeDMMentionsEnabled"] == "1"
275 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
279 |> get("/api/statusnet/config.json")
280 |> json_response(:ok)
282 assert response["site"]["safeDMMentionsEnabled"] == "0"
284 Pleroma.Config.put([:instance, :safe_dm_mentions], option)
287 test "it returns the managed config", %{conn: conn} do
288 Pleroma.Config.put([:instance, :managed_config], false)
289 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
293 |> get("/api/statusnet/config.json")
294 |> json_response(:ok)
296 refute response["site"]["pleromafe"]
298 Pleroma.Config.put([:instance, :managed_config], true)
302 |> get("/api/statusnet/config.json")
303 |> json_response(:ok)
305 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
309 describe "GET /api/pleroma/frontend_configurations" do
310 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
321 Pleroma.Config.put(:frontend_configurations, config)
325 |> get("/api/pleroma/frontend_configurations")
326 |> json_response(:ok)
328 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
332 describe "/api/pleroma/emoji" do
333 test "returns json with custom emoji with tags", %{conn: conn} do
336 |> get("/api/pleroma/emoji")
337 |> json_response(200)
339 assert Enum.all?(emoji, fn
345 is_binary(url) and is_list(tags)
350 describe "GET /ostatus_subscribe - remote_follow/2" do
351 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
355 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
358 assert redirected_to(conn) =~ "/notice/"
361 test "show follow account page if the `acct` is a account link", %{conn: conn} do
365 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
368 assert html_response(response, 200) =~ "Log in to follow"
371 test "show follow page if the `acct` is a account link", %{conn: conn} do
376 |> assign(:user, user)
377 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
379 assert html_response(response, 200) =~ "Remote follow"
382 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
387 |> assign(:user, user)
388 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
390 assert html_response(response, 200) =~ "Error fetching user"
394 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
395 test "follows user", %{conn: conn} do
397 user2 = insert(:user)
401 |> assign(:user, user)
402 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
405 assert response =~ "Account followed!"
406 assert user2.follower_address in refresh_record(user).following
409 test "returns error when user is deactivated", %{conn: conn} do
410 user = insert(:user, info: %{deactivated: true})
411 user2 = insert(:user)
415 |> assign(:user, user)
416 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
419 assert response =~ "Error following account"
422 test "returns error when user is blocked", %{conn: conn} do
423 Pleroma.Config.put([:user, :deny_follow_blocked], true)
425 user2 = insert(:user)
427 {:ok, _user} = Pleroma.User.block(user2, user)
431 |> assign(:user, user)
432 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
435 assert response =~ "Error following account"
438 test "returns error when followee not found", %{conn: conn} do
443 |> assign(:user, user)
444 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
447 assert response =~ "Error following account"
450 test "returns success result when user already in followers", %{conn: conn} do
452 user2 = insert(:user)
453 {:ok, _, _, _} = CommonAPI.follow(user, user2)
457 |> assign(:user, refresh_record(user))
458 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
461 assert response =~ "Account followed!"
465 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
466 test "follows", %{conn: conn} do
468 user2 = insert(:user)
472 |> post("/ostatus_subscribe", %{
473 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
477 assert response =~ "Account followed!"
478 assert user2.follower_address in refresh_record(user).following
481 test "returns error when followee not found", %{conn: conn} do
486 |> post("/ostatus_subscribe", %{
487 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
491 assert response =~ "Error following account"
494 test "returns error when login invalid", %{conn: conn} do
499 |> post("/ostatus_subscribe", %{
500 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
504 assert response =~ "Wrong username or password"
507 test "returns error when password invalid", %{conn: conn} do
509 user2 = insert(:user)
513 |> post("/ostatus_subscribe", %{
514 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
518 assert response =~ "Wrong username or password"
521 test "returns error when user is blocked", %{conn: conn} do
522 Pleroma.Config.put([:user, :deny_follow_blocked], true)
524 user2 = insert(:user)
525 {:ok, _user} = Pleroma.User.block(user2, user)
529 |> post("/ostatus_subscribe", %{
530 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
534 assert response =~ "Error following account"
538 describe "GET /api/pleroma/healthcheck" do
540 config_healthcheck = Pleroma.Config.get([:instance, :healthcheck])
543 Pleroma.Config.put([:instance, :healthcheck], config_healthcheck)
549 test "returns 503 when healthcheck disabled", %{conn: conn} do
550 Pleroma.Config.put([:instance, :healthcheck], false)
554 |> get("/api/pleroma/healthcheck")
555 |> json_response(503)
557 assert response == %{}
560 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
561 Pleroma.Config.put([:instance, :healthcheck], true)
563 with_mock Pleroma.Healthcheck,
564 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
567 |> get("/api/pleroma/healthcheck")
568 |> json_response(200)
580 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
581 Pleroma.Config.put([:instance, :healthcheck], true)
583 with_mock Pleroma.Healthcheck,
584 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
587 |> get("/api/pleroma/healthcheck")
588 |> json_response(503)
601 describe "POST /api/pleroma/disable_account" do
602 test "it returns HTTP 200", %{conn: conn} do
607 |> assign(:user, user)
608 |> post("/api/pleroma/disable_account", %{"password" => "test"})
609 |> json_response(:ok)
611 assert response == %{"status" => "success"}
612 ObanHelpers.perform_all()
614 user = User.get_cached_by_id(user.id)
616 assert user.info.deactivated == true
619 test "it returns returns when password invalid", %{conn: conn} do
624 |> assign(:user, user)
625 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
626 |> json_response(:ok)
628 assert response == %{"error" => "Invalid password."}
629 user = User.get_cached_by_id(user.id)
631 refute user.info.deactivated
635 describe "GET /api/statusnet/version" do
636 test "it returns version in xml format", %{conn: conn} do
639 |> put_req_header("accept", "application/xml")
640 |> get("/api/statusnet/version")
643 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
646 test "it returns version in json format", %{conn: conn} do
649 |> put_req_header("accept", "application/json")
650 |> get("/api/statusnet/version")
651 |> json_response(:ok)
653 assert response == "#{Pleroma.Application.named_version()}"
657 describe "POST /main/ostatus - remote_subscribe/2" do
658 test "renders subscribe form", %{conn: conn} do
663 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
666 refute response =~ "Could not find user"
667 assert response =~ "Remotely follow #{user.nickname}"
670 test "renders subscribe form with error when user not found", %{conn: conn} do
673 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
676 assert response =~ "Could not find user"
677 refute response =~ "Remotely follow"
680 test "it redirect to webfinger url", %{conn: conn} do
682 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
686 |> post("/main/ostatus", %{
687 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
690 assert redirected_to(conn) ==
691 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
694 test "it renders form with error when use not found", %{conn: conn} do
695 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
699 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
702 assert response =~ "Something went wrong."
706 test "it returns new captcha", %{conn: conn} do
707 with_mock Pleroma.Captcha,
708 new: fn -> "test_captcha" end do
711 |> get("/api/pleroma/captcha")
714 assert resp == "\"test_captcha\""
715 assert called(Pleroma.Captcha.new())