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], true)
22 clear_config([:instance])
23 clear_config([:frontend_configurations, :pleroma_fe])
24 clear_config([:user, :deny_follow_blocked])
26 describe "GET /ostatus_subscribe - remote_follow/2" do
27 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
30 remote_follow_path(conn, :follow, %{
31 acct: "https://mastodon.social/users/emelie/statuses/101849165031453009"
34 |> redirected_to() =~ "/notice/"
37 test "show follow account page if the `acct` is a account link", %{conn: conn} do
40 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
43 assert response =~ "Log in to follow"
46 test "show follow page if the `acct` is a account link", %{conn: conn} do
51 |> assign(:user, user)
52 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
55 assert response =~ "Remote follow"
58 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
61 assert capture_log(fn ->
64 |> assign(:user, user)
66 remote_follow_path(conn, :follow, %{
67 acct: "https://mastodon.social/users/not_found"
72 assert response =~ "Error fetching user"
73 end) =~ "Object has been deleted"
77 describe "POST /ostatus_subscribe - do_follow/2 with assigned user " do
78 test "required `follow | write:follows` scope", %{conn: conn} do
81 read_token = insert(:oauth_token, user: user, scopes: ["read"])
83 assert capture_log(fn ->
86 |> assign(:user, user)
87 |> assign(:token, read_token)
88 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
91 assert response =~ "Error following account"
92 end) =~ "Insufficient permissions: follow | write:follows."
95 test "follows user", %{conn: conn} do
101 |> assign(:user, user)
102 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:follows"]))
103 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
105 assert redirected_to(conn) == "/users/#{user2.id}"
108 test "returns error when user is deactivated", %{conn: conn} do
109 user = insert(:user, deactivated: true)
110 user2 = insert(:user)
114 |> assign(:user, user)
115 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
118 assert response =~ "Error following account"
121 test "returns error when user is blocked", %{conn: conn} do
122 Pleroma.Config.put([:user, :deny_follow_blocked], true)
124 user2 = insert(:user)
126 {:ok, _user_block} = Pleroma.User.block(user2, user)
130 |> assign(:user, user)
131 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
134 assert response =~ "Error following account"
137 test "returns error when followee not found", %{conn: conn} do
142 |> assign(:user, user)
143 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => "jimm"}})
146 assert response =~ "Error following account"
149 test "returns success result when user already in followers", %{conn: conn} do
151 user2 = insert(:user)
152 {:ok, _, _, _} = CommonAPI.follow(user, user2)
156 |> assign(:user, refresh_record(user))
157 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:follows"]))
158 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
160 assert redirected_to(conn) == "/users/#{user2.id}"
164 describe "POST /ostatus_subscribe - follow/2 without assigned user " do
165 test "follows", %{conn: conn} do
167 user2 = insert(:user)
171 |> post(remote_follow_path(conn, :do_follow), %{
172 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
175 assert redirected_to(conn) == "/users/#{user2.id}"
176 assert user2.follower_address in User.following(user)
179 test "returns error when followee not found", %{conn: conn} do
184 |> post(remote_follow_path(conn, :do_follow), %{
185 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
189 assert response =~ "Error following account"
192 test "returns error when login invalid", %{conn: conn} do
197 |> post(remote_follow_path(conn, :do_follow), %{
198 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
202 assert response =~ "Wrong username or password"
205 test "returns error when password invalid", %{conn: conn} do
207 user2 = insert(:user)
211 |> post(remote_follow_path(conn, :do_follow), %{
212 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
216 assert response =~ "Wrong username or password"
219 test "returns error when user is blocked", %{conn: conn} do
220 Pleroma.Config.put([:user, :deny_follow_blocked], true)
222 user2 = insert(:user)
223 {:ok, _user_block} = Pleroma.User.block(user2, user)
227 |> post(remote_follow_path(conn, :do_follow), %{
228 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
232 assert response =~ "Error following account"