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.RemoteFollowControllerTest do
6 use Pleroma.Web.ConnCase
9 alias Pleroma.Web.CommonAPI
10 import ExUnit.CaptureLog
11 import Pleroma.Factory
14 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
18 clear_config([:instance])
19 clear_config([:frontend_configurations, :pleroma_fe])
20 clear_config([:user, :deny_follow_blocked])
22 describe "GET /ostatus_subscribe - remote_follow/2" do
23 test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
26 remote_follow_path(conn, :follow, %{
27 acct: "https://mastodon.social/users/emelie/statuses/101849165031453009"
30 |> redirected_to() =~ "/notice/"
33 test "show follow account page if the `acct` is a account link", %{conn: conn} do
36 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
39 assert response =~ "Log in to follow"
42 test "show follow page if the `acct` is a account link", %{conn: conn} do
47 |> assign(:user, user)
48 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
51 assert response =~ "Remote follow"
54 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
57 assert capture_log(fn ->
60 |> assign(:user, user)
62 remote_follow_path(conn, :follow, %{
63 acct: "https://mastodon.social/users/not_found"
68 assert response =~ "Error fetching user"
69 end) =~ "Object has been deleted"
73 describe "POST /ostatus_subscribe - do_follow/2 with assigned user " do
74 test "required `follow | write:follows` scope", %{conn: conn} do
77 read_token = insert(:oauth_token, user: user, scopes: ["read"])
79 assert capture_log(fn ->
82 |> assign(:user, user)
83 |> assign(:token, read_token)
84 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
87 assert response =~ "Error following account"
88 end) =~ "Insufficient permissions: follow | write:follows."
91 test "follows user", %{conn: conn} do
97 |> assign(:user, user)
98 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:follows"]))
99 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
102 assert response =~ "Account followed!"
103 assert user2.follower_address in User.following(user)
106 test "returns error when user is deactivated", %{conn: conn} do
107 user = insert(:user, deactivated: true)
108 user2 = insert(:user)
112 |> assign(:user, user)
113 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
116 assert response =~ "Error following account"
119 test "returns error when user is blocked", %{conn: conn} do
120 Pleroma.Config.put([:user, :deny_follow_blocked], true)
122 user2 = insert(:user)
124 {:ok, _user_block} = Pleroma.User.block(user2, user)
128 |> assign(:user, user)
129 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
132 assert response =~ "Error following account"
135 test "returns error when followee not found", %{conn: conn} do
140 |> assign(:user, user)
141 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => "jimm"}})
144 assert response =~ "Error following account"
147 test "returns success result when user already in followers", %{conn: conn} do
149 user2 = insert(:user)
150 {:ok, _, _, _} = CommonAPI.follow(user, user2)
154 |> assign(:user, refresh_record(user))
155 |> assign(:token, insert(:oauth_token, user: user, scopes: ["write:follows"]))
156 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
159 assert response =~ "Account followed!"
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}
175 assert response =~ "Account followed!"
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"