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