Improved in-test `clear_config/n` applicability (setup / setup_all / in-test usage).
[akkoma] / test / web / twitter_api / remote_follow_controller_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.TwitterAPI.RemoteFollowControllerTest do
6 use Pleroma.Web.ConnCase
7
8 alias Pleroma.Config
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI
11
12 import ExUnit.CaptureLog
13 import Pleroma.Factory
14
15 setup do
16 Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
17 :ok
18 end
19
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])
24
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
27 assert conn
28 |> get(
29 remote_follow_path(conn, :follow, %{
30 acct: "https://mastodon.social/users/emelie/statuses/101849165031453009"
31 })
32 )
33 |> redirected_to() =~ "/notice/"
34 end
35
36 test "show follow account page if the `acct` is a account link", %{conn: conn} do
37 response =
38 conn
39 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
40 |> html_response(200)
41
42 assert response =~ "Log in to follow"
43 end
44
45 test "show follow page if the `acct` is a account link", %{conn: conn} do
46 user = insert(:user)
47
48 response =
49 conn
50 |> assign(:user, user)
51 |> get(remote_follow_path(conn, :follow, %{acct: "https://mastodon.social/users/emelie"}))
52 |> html_response(200)
53
54 assert response =~ "Remote follow"
55 end
56
57 test "show follow page with error when user cannot fecth by `acct` link", %{conn: conn} do
58 user = insert(:user)
59
60 assert capture_log(fn ->
61 response =
62 conn
63 |> assign(:user, user)
64 |> get(
65 remote_follow_path(conn, :follow, %{
66 acct: "https://mastodon.social/users/not_found"
67 })
68 )
69 |> html_response(200)
70
71 assert response =~ "Error fetching user"
72 end) =~ "Object has been deleted"
73 end
74 end
75
76 describe "POST /ostatus_subscribe - do_follow/2 with assigned user " do
77 test "required `follow | write:follows` scope", %{conn: conn} do
78 user = insert(:user)
79 user2 = insert(:user)
80 read_token = insert(:oauth_token, user: user, scopes: ["read"])
81
82 assert capture_log(fn ->
83 response =
84 conn
85 |> assign(:user, user)
86 |> assign(:token, read_token)
87 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
88 |> response(200)
89
90 assert response =~ "Error following account"
91 end) =~ "Insufficient permissions: follow | write:follows."
92 end
93
94 test "follows user", %{conn: conn} do
95 user = insert(:user)
96 user2 = insert(:user)
97
98 conn =
99 conn
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}})
103
104 assert redirected_to(conn) == "/users/#{user2.id}"
105 end
106
107 test "returns error when user is deactivated", %{conn: conn} do
108 user = insert(:user, deactivated: true)
109 user2 = insert(:user)
110
111 response =
112 conn
113 |> assign(:user, user)
114 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
115 |> response(200)
116
117 assert response =~ "Error following account"
118 end
119
120 test "returns error when user is blocked", %{conn: conn} do
121 Pleroma.Config.put([:user, :deny_follow_blocked], true)
122 user = insert(:user)
123 user2 = insert(:user)
124
125 {:ok, _user_block} = Pleroma.User.block(user2, user)
126
127 response =
128 conn
129 |> assign(:user, user)
130 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => user2.id}})
131 |> response(200)
132
133 assert response =~ "Error following account"
134 end
135
136 test "returns error when followee not found", %{conn: conn} do
137 user = insert(:user)
138
139 response =
140 conn
141 |> assign(:user, user)
142 |> post(remote_follow_path(conn, :do_follow), %{"user" => %{"id" => "jimm"}})
143 |> response(200)
144
145 assert response =~ "Error following account"
146 end
147
148 test "returns success result when user already in followers", %{conn: conn} do
149 user = insert(:user)
150 user2 = insert(:user)
151 {:ok, _, _, _} = CommonAPI.follow(user, user2)
152
153 conn =
154 conn
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}})
158
159 assert redirected_to(conn) == "/users/#{user2.id}"
160 end
161 end
162
163 describe "POST /ostatus_subscribe - follow/2 without assigned user " do
164 test "follows", %{conn: conn} do
165 user = insert(:user)
166 user2 = insert(:user)
167
168 conn =
169 conn
170 |> post(remote_follow_path(conn, :do_follow), %{
171 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
172 })
173
174 assert redirected_to(conn) == "/users/#{user2.id}"
175 assert user2.follower_address in User.following(user)
176 end
177
178 test "returns error when followee not found", %{conn: conn} do
179 user = insert(:user)
180
181 response =
182 conn
183 |> post(remote_follow_path(conn, :do_follow), %{
184 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => "jimm"}
185 })
186 |> response(200)
187
188 assert response =~ "Error following account"
189 end
190
191 test "returns error when login invalid", %{conn: conn} do
192 user = insert(:user)
193
194 response =
195 conn
196 |> post(remote_follow_path(conn, :do_follow), %{
197 "authorization" => %{"name" => "jimm", "password" => "test", "id" => user.id}
198 })
199 |> response(200)
200
201 assert response =~ "Wrong username or password"
202 end
203
204 test "returns error when password invalid", %{conn: conn} do
205 user = insert(:user)
206 user2 = insert(:user)
207
208 response =
209 conn
210 |> post(remote_follow_path(conn, :do_follow), %{
211 "authorization" => %{"name" => user.nickname, "password" => "42", "id" => user2.id}
212 })
213 |> response(200)
214
215 assert response =~ "Wrong username or password"
216 end
217
218 test "returns error when user is blocked", %{conn: conn} do
219 Pleroma.Config.put([:user, :deny_follow_blocked], true)
220 user = insert(:user)
221 user2 = insert(:user)
222 {:ok, _user_block} = Pleroma.User.block(user2, user)
223
224 response =
225 conn
226 |> post(remote_follow_path(conn, :do_follow), %{
227 "authorization" => %{"name" => user.nickname, "password" => "test", "id" => user2.id}
228 })
229 |> response(200)
230
231 assert response =~ "Error following account"
232 end
233 end
234 end