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)
22 clear_config([:instance])
23 clear_config([:frontend_configurations, :pleroma_fe])
24 clear_config([:user, :deny_follow_blocked])
26 describe "POST /api/pleroma/follow_import" do
27 test "it returns HTTP 200", %{conn: conn} do
33 |> assign(:user, user1)
34 |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
37 assert response == "job started"
40 test "it imports follow lists from file", %{conn: conn} do
46 read!: fn "follow_list.txt" ->
47 "Account address,Show boosts\n#{user2.ap_id},true"
52 |> assign(:user, user1)
53 |> post("/api/pleroma/follow_import", %{"list" => %Plug.Upload{path: "follow_list.txt"}})
56 assert response == "job started"
58 assert ObanHelpers.member?(
60 "op" => "follow_import",
61 "follower_id" => user1.id,
62 "followed_identifiers" => [user2.ap_id]
64 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
69 test "it imports new-style mastodon follow lists", %{conn: conn} do
75 |> assign(:user, user1)
76 |> post("/api/pleroma/follow_import", %{
77 "list" => "Account address,Show boosts\n#{user2.ap_id},true"
81 assert response == "job started"
84 test "requires 'follow' permission", %{conn: conn} do
85 token1 = insert(:oauth_token, scopes: ["read", "write"])
86 token2 = insert(:oauth_token, scopes: ["follow"])
87 another_user = insert(:user)
89 for token <- [token1, token2] do
92 |> put_req_header("authorization", "Bearer #{token.token}")
93 |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
96 assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
98 assert json_response(conn, 200)
104 describe "POST /api/pleroma/blocks_import" do
105 test "it returns HTTP 200", %{conn: conn} do
106 user1 = insert(:user)
107 user2 = insert(:user)
111 |> assign(:user, user1)
112 |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
113 |> json_response(:ok)
115 assert response == "job started"
118 test "it imports blocks users from file", %{conn: conn} do
119 user1 = insert(:user)
120 user2 = insert(:user)
121 user3 = insert(:user)
124 {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
128 |> assign(:user, user1)
129 |> post("/api/pleroma/blocks_import", %{"list" => %Plug.Upload{path: "blocks_list.txt"}})
130 |> json_response(:ok)
132 assert response == "job started"
134 assert ObanHelpers.member?(
136 "op" => "blocks_import",
137 "blocker_id" => user1.id,
138 "blocked_identifiers" => [user2.ap_id, user3.ap_id]
140 all_enqueued(worker: Pleroma.Workers.BackgroundWorker)
146 describe "POST /api/pleroma/notifications/read" do
147 test "it marks a single notification as read", %{conn: conn} do
148 user1 = insert(:user)
149 user2 = insert(:user)
150 {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
151 {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
152 {:ok, [notification1]} = Notification.create_notifications(activity1)
153 {:ok, [notification2]} = Notification.create_notifications(activity2)
156 |> assign(:user, user1)
157 |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
158 |> json_response(:ok)
160 assert Repo.get(Notification, notification1.id).seen
161 refute Repo.get(Notification, notification2.id).seen
164 test "it returns error when notification not found", %{conn: conn} do
165 user1 = insert(:user)
169 |> assign(:user, user1)
170 |> post("/api/pleroma/notifications/read", %{"id" => "22222222222222"})
171 |> json_response(403)
173 assert response == %{"error" => "Cannot get notification"}
177 describe "PUT /api/pleroma/notification_settings" do
178 test "it updates notification settings", %{conn: conn} do
182 |> assign(:user, user)
183 |> put("/api/pleroma/notification_settings", %{
184 "followers" => false,
187 |> json_response(:ok)
189 user = Repo.get(User, user.id)
192 "followers" => false,
194 "non_follows" => true,
195 "non_followers" => true
196 } == user.info.notification_settings
200 describe "GET /api/statusnet/config" do
201 test "it returns config in xml format", %{conn: conn} do
202 instance = Pleroma.Config.get(:instance)
206 |> put_req_header("accept", "application/xml")
207 |> get("/api/statusnet/config")
211 "<config>\n<site>\n<name>#{Keyword.get(instance, :name)}</name>\n<site>#{
212 Pleroma.Web.base_url()
213 }</site>\n<textlimit>#{Keyword.get(instance, :limit)}</textlimit>\n<closed>#{
214 !Keyword.get(instance, :registrations_open)
215 }</closed>\n</site>\n</config>\n"
218 test "it returns config in json format", %{conn: conn} do
219 instance = Pleroma.Config.get(:instance)
220 Pleroma.Config.put([:instance, :managed_config], true)
221 Pleroma.Config.put([:instance, :registrations_open], false)
222 Pleroma.Config.put([:instance, :invites_enabled], true)
223 Pleroma.Config.put([:instance, :public], false)
224 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
228 |> put_req_header("accept", "application/json")
229 |> get("/api/statusnet/config")
230 |> json_response(:ok)
234 "accountActivationRequired" => "0",
236 "description" => Keyword.get(instance, :description),
237 "invitesEnabled" => "1",
238 "name" => Keyword.get(instance, :name),
239 "pleromafe" => %{"theme" => "asuka-hospital"},
241 "safeDMMentionsEnabled" => "0",
242 "server" => Pleroma.Web.base_url(),
243 "textlimit" => to_string(Keyword.get(instance, :limit)),
245 "avatarlimit" => to_string(Keyword.get(instance, :avatar_upload_limit)),
246 "backgroundlimit" => to_string(Keyword.get(instance, :background_upload_limit)),
247 "bannerlimit" => to_string(Keyword.get(instance, :banner_upload_limit)),
248 "uploadlimit" => to_string(Keyword.get(instance, :upload_limit))
250 "vapidPublicKey" => Keyword.get(Pleroma.Web.Push.vapid_config(), :public_key)
254 assert response == expected_data
257 test "returns the state of safe_dm_mentions flag", %{conn: conn} do
258 Pleroma.Config.put([:instance, :safe_dm_mentions], true)
262 |> get("/api/statusnet/config.json")
263 |> json_response(:ok)
265 assert response["site"]["safeDMMentionsEnabled"] == "1"
267 Pleroma.Config.put([:instance, :safe_dm_mentions], false)
271 |> get("/api/statusnet/config.json")
272 |> json_response(:ok)
274 assert response["site"]["safeDMMentionsEnabled"] == "0"
277 test "it returns the managed config", %{conn: conn} do
278 Pleroma.Config.put([:instance, :managed_config], false)
279 Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
283 |> get("/api/statusnet/config.json")
284 |> json_response(:ok)
286 refute response["site"]["pleromafe"]
288 Pleroma.Config.put([:instance, :managed_config], true)
292 |> get("/api/statusnet/config.json")
293 |> json_response(:ok)
295 assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
299 describe "GET /api/pleroma/frontend_configurations" do
300 test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
311 Pleroma.Config.put(:frontend_configurations, config)
315 |> get("/api/pleroma/frontend_configurations")
316 |> json_response(:ok)
318 assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
322 describe "/api/pleroma/emoji" do
323 test "returns json with custom emoji with tags", %{conn: conn} do
326 |> get("/api/pleroma/emoji")
327 |> json_response(200)
329 assert Enum.all?(emoji, fn
335 is_binary(url) and is_list(tags)
340 describe "GET /ostatus_subscribe - remote_follow/2" do
341 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
345 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
348 assert redirected_to(conn) =~ "/notice/"
351 test "show follow account page if the `acct` is a account link", %{conn: conn} do
355 "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
358 assert html_response(response, 200) =~ "Log in to follow"
361 test "show follow page if the `acct` is a account link", %{conn: conn} do
366 |> assign(:user, user)
367 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/emelie")
369 assert html_response(response, 200) =~ "Remote follow"
372 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
377 |> assign(:user, user)
378 |> get("/ostatus_subscribe?acct=https://mastodon.social/users/not_found")
380 assert html_response(response, 200) =~ "Error fetching user"
384 describe "POST /ostatus_subscribe - do_remote_follow/2 with assigned user " do
385 test "follows user", %{conn: conn} do
387 user2 = insert(:user)
391 |> assign(:user, user)
392 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
395 assert response =~ "Account followed!"
396 assert user2.follower_address in refresh_record(user).following
399 test "returns error when user is deactivated", %{conn: conn} do
400 user = insert(:user, info: %{deactivated: true})
401 user2 = insert(:user)
405 |> assign(:user, user)
406 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
409 assert response =~ "Error following account"
412 test "returns error when user is blocked", %{conn: conn} do
413 Pleroma.Config.put([:user, :deny_follow_blocked], true)
415 user2 = insert(:user)
417 {:ok, _user} = Pleroma.User.block(user2, user)
421 |> assign(:user, user)
422 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
425 assert response =~ "Error following account"
428 test "returns error when followee not found", %{conn: conn} do
433 |> assign(:user, user)
434 |> post("/ostatus_subscribe", %{"user" => %{"id" => "jimm"}})
437 assert response =~ "Error following account"
440 test "returns success result when user already in followers", %{conn: conn} do
442 user2 = insert(:user)
443 {:ok, _, _, _} = CommonAPI.follow(user, user2)
447 |> assign(:user, refresh_record(user))
448 |> post("/ostatus_subscribe", %{"user" => %{"id" => user2.id}})
451 assert response =~ "Account followed!"
455 describe "POST /ostatus_subscribe - do_remote_follow/2 without assigned user " do
456 test "follows", %{conn: conn} do
458 user2 = insert(:user)
462 |> post("/ostatus_subscribe", %{
463 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
467 assert response =~ "Account followed!"
468 assert user2.follower_address in refresh_record(user).following
471 test "returns error when followee not found", %{conn: conn} do
476 |> post("/ostatus_subscribe", %{
477 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
481 assert response =~ "Error following account"
484 test "returns error when login invalid", %{conn: conn} do
489 |> post("/ostatus_subscribe", %{
490 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
494 assert response =~ "Wrong username or password"
497 test "returns error when password invalid", %{conn: conn} do
499 user2 = insert(:user)
503 |> post("/ostatus_subscribe", %{
504 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
508 assert response =~ "Wrong username or password"
511 test "returns error when user is blocked", %{conn: conn} do
512 Pleroma.Config.put([:user, :deny_follow_blocked], true)
514 user2 = insert(:user)
515 {:ok, _user} = Pleroma.User.block(user2, user)
519 |> post("/ostatus_subscribe", %{
520 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
524 assert response =~ "Error following account"
528 describe "GET /api/pleroma/healthcheck" do
529 clear_config([:instance, :healthcheck])
531 test "returns 503 when healthcheck disabled", %{conn: conn} do
532 Pleroma.Config.put([:instance, :healthcheck], false)
536 |> get("/api/pleroma/healthcheck")
537 |> json_response(503)
539 assert response == %{}
542 test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
543 Pleroma.Config.put([:instance, :healthcheck], true)
545 with_mock Pleroma.Healthcheck,
546 system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
549 |> get("/api/pleroma/healthcheck")
550 |> json_response(200)
562 test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
563 Pleroma.Config.put([:instance, :healthcheck], true)
565 with_mock Pleroma.Healthcheck,
566 system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
569 |> get("/api/pleroma/healthcheck")
570 |> json_response(503)
583 describe "POST /api/pleroma/disable_account" do
584 test "it returns HTTP 200", %{conn: conn} do
589 |> assign(:user, user)
590 |> post("/api/pleroma/disable_account", %{"password" => "test"})
591 |> json_response(:ok)
593 assert response == %{"status" => "success"}
594 ObanHelpers.perform_all()
596 user = User.get_cached_by_id(user.id)
598 assert user.info.deactivated == true
601 test "it returns returns when password invalid", %{conn: conn} do
606 |> assign(:user, user)
607 |> post("/api/pleroma/disable_account", %{"password" => "test1"})
608 |> json_response(:ok)
610 assert response == %{"error" => "Invalid password."}
611 user = User.get_cached_by_id(user.id)
613 refute user.info.deactivated
617 describe "GET /api/statusnet/version" do
618 test "it returns version in xml format", %{conn: conn} do
621 |> put_req_header("accept", "application/xml")
622 |> get("/api/statusnet/version")
625 assert response == "<version>#{Pleroma.Application.named_version()}</version>"
628 test "it returns version in json format", %{conn: conn} do
631 |> put_req_header("accept", "application/json")
632 |> get("/api/statusnet/version")
633 |> json_response(:ok)
635 assert response == "#{Pleroma.Application.named_version()}"
639 describe "POST /main/ostatus - remote_subscribe/2" do
640 test "renders subscribe form", %{conn: conn} do
645 |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
648 refute response =~ "Could not find user"
649 assert response =~ "Remotely follow #{user.nickname}"
652 test "renders subscribe form with error when user not found", %{conn: conn} do
655 |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
658 assert response =~ "Could not find user"
659 refute response =~ "Remotely follow"
662 test "it redirect to webfinger url", %{conn: conn} do
664 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
668 |> post("/main/ostatus", %{
669 "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
672 assert redirected_to(conn) ==
673 "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
676 test "it renders form with error when use not found", %{conn: conn} do
677 user2 = insert(:user, ap_id: "shp@social.heldscal.la")
681 |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
684 assert response =~ "Something went wrong."
688 test "it returns new captcha", %{conn: conn} do
689 with_mock Pleroma.Captcha,
690 new: fn -> "test_captcha" end do
693 |> get("/api/pleroma/captcha")
696 assert resp == "\"test_captcha\""
697 assert called(Pleroma.Captcha.new())