1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.TwitterAPI.RemoteFollowControllerTest do
6 use Pleroma.Web.ConnCase
10 alias Pleroma.Web.CommonAPI
12 import ExUnit.CaptureLog
13 import Pleroma.Factory
16 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
20 setup_all do: clear_config([:instance, :federating], true)
21 setup do: clear_config([:instance])
22 setup do: clear_config([:frontend_configurations, :pleroma_fe])
23 setup do: clear_config([:user, :deny_follow_blocked])
25 describe "GET /ostatus_subscribe - remote_follow/2" do
26 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
29 remote_follow_path(conn, :follow, %{
30 acct: "https://mastodon.social/users/emelie/statuses/101849165031453009"
33 |> redirected_to() =~ "/notice/"
36 test "show follow account page if the `acct` is a account link", %{conn: conn} do
39 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
42 assert response =~ "Log in to follow"
45 test "show follow page if the `acct` is a account link", %{conn: conn} do
50 |> assign(:user, user)
51 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
54 assert response =~ "Remote follow"
57 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
60 assert capture_log(fn ->
63 |> assign(:user, user)
65 remote_follow_path(conn, :follow, %{
66 acct: "https://mastodon.social/users/not_found"
71 assert response =~ "Error fetching user"
72 end) =~ "Object has been deleted"
76 describe "POST /ostatus_subscribe - do_follow/2 with assigned user " do
77 test "required `follow | write:follows` scope", %{conn: conn} do
80 read_token = insert(:oauth_token, user: user, scopes: ["read"])
82 assert capture_log(fn ->
85 |> assign(:user, user)
86 |> assign(:token, read_token)
87 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
90 assert response =~ "Error following account"
91 end) =~ "Insufficient permissions: follow | write:follows."
94 test "follows user", %{conn: conn} do
100 |> assign(:user, user)
101 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:follows"]))
102 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
104 assert redirected_to(conn) == "/users/#{user2.id}"
107 test "returns error when user is deactivated", %{conn: conn} do
108 user = insert(:user, deactivated: true)
109 user2 = insert(:user)
113 |> assign(:user, user)
114 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
117 assert response =~ "Error following account"
120 test "returns error when user is blocked", %{conn: conn} do
121 Pleroma.Config.put([:user, :deny_follow_blocked], true)
123 user2 = insert(:user)
125 {:ok, _user_block} = Pleroma.User.block(user2, user)
129 |> assign(:user, user)
130 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
133 assert response =~ "Error following account"
136 test "returns error when followee not found", %{conn: conn} do
141 |> assign(:user, user)
142 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => "jimm"}})
145 assert response =~ "Error following account"
148 test "returns success result when user already in followers", %{conn: conn} do
150 user2 = insert(:user)
151 {:ok, _, _, _} = CommonAPI.follow(user, user2)
155 |> assign(:user, refresh_record(user))
156 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:follows"]))
157 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
159 assert redirected_to(conn) == "/users/#{user2.id}"
163 describe "POST /ostatus_subscribe - follow/2 without assigned user " do
164 test "follows", %{conn: conn} do
166 user2 = insert(:user)
170 |> post(remote_follow_path(conn, :do_follow), %{
171 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
174 assert redirected_to(conn) == "/users/#{user2.id}"
175 assert user2.follower_address in User.following(user)
178 test "returns error when followee not found", %{conn: conn} do
183 |> post(remote_follow_path(conn, :do_follow), %{
184 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
188 assert response =~ "Error following account"
191 test "returns error when login invalid", %{conn: conn} do
196 |> post(remote_follow_path(conn, :do_follow), %{
197 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
201 assert response =~ "Wrong username or password"
204 test "returns error when password invalid", %{conn: conn} do
206 user2 = insert(:user)
210 |> post(remote_follow_path(conn, :do_follow), %{
211 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
215 assert response =~ "Wrong username or password"
218 test "returns error when user is blocked", %{conn: conn} do
219 Pleroma.Config.put([:user, :deny_follow_blocked], true)
221 user2 = insert(:user)
222 {:ok, _user_block} = Pleroma.User.block(user2, user)
226 |> post(remote_follow_path(conn, :do_follow), %{
227 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
231 assert response =~ "Error following account"