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.MFA.TOTP
12 alias Pleroma.Web.CommonAPI
14 import ExUnit.CaptureLog
15 import Pleroma.Factory
19 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
23 setup_all do: clear_config([:instance, :federating], true)
24 setup do: clear_config([:instance])
25 setup do: clear_config([:frontend_configurations, :pleroma_fe])
26 setup do: 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 with enabled Two-Factor Auth " do
167 test "render the MFA login form", %{conn: conn} do
168 otp_secret = TOTP.generate_secret()
172 multi_factor_authentication_settings: %MFA.Settings{
174 totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
178 user2 = insert(:user)
182 |> post(remote_follow_path(conn, :do_follow), %{
183 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
187 mfa_token = Pleroma.Repo.one(from(q in Pleroma.MFA.Token, where: q.user_id == ^user.id))
189 assert response =~ "Two-factor authentication"
190 assert response =~ "Authentication code"
191 assert response =~ mfa_token.token
192 refute user2.follower_address in User.following(user)
195 test "returns error when password is incorrect", %{conn: conn} do
196 otp_secret = TOTP.generate_secret()
200 multi_factor_authentication_settings: %MFA.Settings{
202 totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
206 user2 = insert(:user)
210 |> post(remote_follow_path(conn, :do_follow), %{
211 "authorization" => %{"name" => user.nickname, "password" => "test1", "id" => user2.id}
215 assert response =~ "Wrong username or password"
216 refute user2.follower_address in User.following(user)
219 test "follows", %{conn: conn} do
220 otp_secret = TOTP.generate_secret()
224 multi_factor_authentication_settings: %MFA.Settings{
226 totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
230 {:ok, %{token: token}} = MFA.Token.create(user)
232 user2 = insert(:user)
233 otp_token = TOTP.generate_token(otp_secret)
238 remote_follow_path(conn, :do_follow),
240 "mfa" => %{"code" => otp_token, "token" => token, "id" => user2.id}
244 assert redirected_to(conn) == "/users/#{user2.id}"
245 assert user2.follower_address in User.following(user)
248 test "returns error when auth code is incorrect", %{conn: conn} do
249 otp_secret = TOTP.generate_secret()
253 multi_factor_authentication_settings: %MFA.Settings{
255 totp: %MFA.Settings.TOTP{secret: otp_secret, confirmed: true}
259 {:ok, %{token: token}} = MFA.Token.create(user)
261 user2 = insert(:user)
262 otp_token = TOTP.generate_token(TOTP.generate_secret())
267 remote_follow_path(conn, :do_follow),
269 "mfa" => %{"code" => otp_token, "token" => token, "id" => user2.id}
274 assert response =~ "Wrong authentication code"
275 refute user2.follower_address in User.following(user)
279 describe "POST /ostatus_subscribe - follow/2 without assigned user " do
280 test "follows", %{conn: conn} do
282 user2 = insert(:user)
286 |> post(remote_follow_path(conn, :do_follow), %{
287 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
290 assert redirected_to(conn) == "/users/#{user2.id}"
291 assert user2.follower_address in User.following(user)
294 test "returns error when followee not found", %{conn: conn} do
299 |> post(remote_follow_path(conn, :do_follow), %{
300 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
304 assert response =~ "Error following account"
307 test "returns error when login invalid", %{conn: conn} do
312 |> post(remote_follow_path(conn, :do_follow), %{
313 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
317 assert response =~ "Wrong username or password"
320 test "returns error when password invalid", %{conn: conn} do
322 user2 = insert(:user)
326 |> post(remote_follow_path(conn, :do_follow), %{
327 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
331 assert response =~ "Wrong username or password"
334 test "returns error when user is blocked", %{conn: conn} do
335 Pleroma.Config.put([:user, :deny_follow_blocked], true)
337 user2 = insert(:user)
338 {:ok, _user_block} = Pleroma.User.block(user2, user)
342 |> post(remote_follow_path(conn, :do_follow), %{
343 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
347 assert response =~ "Error following account"