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 clear_config_all([:instance, :federating]) do
21 Config.put([:instance, :federating], true)
24 clear_config([:instance])
25 clear_config([:frontend_configurations, :pleroma_fe])
26 clear_config([:user, :deny_follow_blocked])
28 describe "GET /ostatus_subscribe - remote_follow/2" do
29 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
32 remote_follow_path(conn, :follow, %{
33 acct: "https://mastodon.social/users/emelie/statuses/101849165031453009"
36 |> redirected_to() =~ "/notice/"
39 test "show follow account page if the `acct` is a account link", %{conn: conn} do
42 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
45 assert response =~ "Log in to follow"
48 test "show follow page if the `acct` is a account link", %{conn: conn} do
53 |> assign(:user, user)
54 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
57 assert response =~ "Remote follow"
60 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
63 assert capture_log(fn ->
66 |> assign(:user, user)
68 remote_follow_path(conn, :follow, %{
69 acct: "https://mastodon.social/users/not_found"
74 assert response =~ "Error fetching user"
75 end) =~ "Object has been deleted"
79 describe "POST /ostatus_subscribe - do_follow/2 with assigned user " do
80 test "required `follow | write:follows` scope", %{conn: conn} do
83 read_token = insert(:oauth_token, user: user, scopes: ["read"])
85 assert capture_log(fn ->
88 |> assign(:user, user)
89 |> assign(:token, read_token)
90 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
93 assert response =~ "Error following account"
94 end) =~ "Insufficient permissions: follow | write:follows."
97 test "follows user", %{conn: conn} do
103 |> assign(:user, user)
104 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:follows"]))
105 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
107 assert redirected_to(conn) == "/users/#{user2.id}"
110 test "returns error when user is deactivated", %{conn: conn} do
111 user = insert(:user, deactivated: true)
112 user2 = insert(:user)
116 |> assign(:user, user)
117 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
120 assert response =~ "Error following account"
123 test "returns error when user is blocked", %{conn: conn} do
124 Pleroma.Config.put([:user, :deny_follow_blocked], true)
126 user2 = insert(:user)
128 {:ok, _user_block} = Pleroma.User.block(user2, user)
132 |> assign(:user, user)
133 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
136 assert response =~ "Error following account"
139 test "returns error when followee not found", %{conn: conn} do
144 |> assign(:user, user)
145 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => "jimm"}})
148 assert response =~ "Error following account"
151 test "returns success result when user already in followers", %{conn: conn} do
153 user2 = insert(:user)
154 {:ok, _, _, _} = CommonAPI.follow(user, user2)
158 |> assign(:user, refresh_record(user))
159 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:follows"]))
160 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
162 assert redirected_to(conn) == "/users/#{user2.id}"
166 describe "POST /ostatus_subscribe - follow/2 without assigned user " do
167 test "follows", %{conn: conn} do
169 user2 = insert(:user)
173 |> post(remote_follow_path(conn, :do_follow), %{
174 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
177 assert redirected_to(conn) == "/users/#{user2.id}"
178 assert user2.follower_address in User.following(user)
181 test "returns error when followee not found", %{conn: conn} do
186 |> post(remote_follow_path(conn, :do_follow), %{
187 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
191 assert response =~ "Error following account"
194 test "returns error when login invalid", %{conn: conn} do
199 |> post(remote_follow_path(conn, :do_follow), %{
200 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
204 assert response =~ "Wrong username or password"
207 test "returns error when password invalid", %{conn: conn} do
209 user2 = insert(:user)
213 |> post(remote_follow_path(conn, :do_follow), %{
214 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
218 assert response =~ "Wrong username or password"
221 test "returns error when user is blocked", %{conn: conn} do
222 Pleroma.Config.put([:user, :deny_follow_blocked], true)
224 user2 = insert(:user)
225 {:ok, _user_block} = Pleroma.User.block(user2, user)
229 |> post(remote_follow_path(conn, :do_follow), %{
230 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
234 assert response =~ "Error following account"