logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

user_import_controller_test.exs (7849B)


  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. defmodule Pleroma.Web.PleromaAPI.UserImportControllerTest do
  5. use Pleroma.Web.ConnCase
  6. use Oban.Testing, repo: Pleroma.Repo
  7. alias Pleroma.Config
  8. alias Pleroma.Tests.ObanHelpers
  9. import Pleroma.Factory
  10. import Mock
  11. setup do
  12. Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  13. :ok
  14. end
  15. describe "POST /api/pleroma/follow_import" do
  16. setup do: oauth_access(["follow"])
  17. test "it returns HTTP 200", %{conn: conn} do
  18. user2 = insert(:user)
  19. assert "job started" ==
  20. conn
  21. |> put_req_header("content-type", "application/json")
  22. |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
  23. |> json_response_and_validate_schema(200)
  24. end
  25. test "it imports follow lists from file", %{conn: conn} do
  26. user2 = insert(:user)
  27. with_mocks([
  28. {File, [],
  29. read!: fn "follow_list.txt" ->
  30. "Account address,Show boosts\n#{user2.ap_id},true"
  31. end}
  32. ]) do
  33. assert "job started" ==
  34. conn
  35. |> put_req_header("content-type", "application/json")
  36. |> post("/api/pleroma/follow_import", %{
  37. "list" => %Plug.Upload{path: "follow_list.txt"}
  38. })
  39. |> json_response_and_validate_schema(200)
  40. assert [{:ok, job_result}] = ObanHelpers.perform_all()
  41. assert job_result == [user2]
  42. end
  43. end
  44. test "it imports new-style mastodon follow lists", %{conn: conn} do
  45. user2 = insert(:user)
  46. response =
  47. conn
  48. |> put_req_header("content-type", "application/json")
  49. |> post("/api/pleroma/follow_import", %{
  50. "list" => "Account address,Show boosts\n#{user2.ap_id},true"
  51. })
  52. |> json_response_and_validate_schema(200)
  53. assert response == "job started"
  54. end
  55. test "requires 'follow' or 'write:follows' permissions" do
  56. token1 = insert(:oauth_token, scopes: ["read", "write"])
  57. token2 = insert(:oauth_token, scopes: ["follow"])
  58. token3 = insert(:oauth_token, scopes: ["something"])
  59. another_user = insert(:user)
  60. for token <- [token1, token2, token3] do
  61. conn =
  62. build_conn()
  63. |> put_req_header("authorization", "Bearer #{token.token}")
  64. |> put_req_header("content-type", "application/json")
  65. |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
  66. if token == token3 do
  67. assert %{"error" => "Insufficient permissions: follow | write:follows."} ==
  68. json_response(conn, 403)
  69. else
  70. assert json_response(conn, 200)
  71. end
  72. end
  73. end
  74. test "it imports follows with different nickname variations", %{conn: conn} do
  75. users = [user2, user3, user4, user5, user6] = insert_list(5, :user)
  76. identifiers =
  77. [
  78. user2.ap_id,
  79. user3.nickname,
  80. " ",
  81. "@" <> user4.nickname,
  82. user5.nickname <> "@localhost",
  83. "@" <> user6.nickname <> "@localhost"
  84. ]
  85. |> Enum.join("\n")
  86. assert "job started" ==
  87. conn
  88. |> put_req_header("content-type", "application/json")
  89. |> post("/api/pleroma/follow_import", %{"list" => identifiers})
  90. |> json_response_and_validate_schema(200)
  91. assert [{:ok, job_result}] = ObanHelpers.perform_all()
  92. assert job_result == users
  93. end
  94. end
  95. describe "POST /api/pleroma/blocks_import" do
  96. # Note: "follow" or "write:blocks" permission is required
  97. setup do: oauth_access(["write:blocks"])
  98. test "it returns HTTP 200", %{conn: conn} do
  99. user2 = insert(:user)
  100. assert "job started" ==
  101. conn
  102. |> put_req_header("content-type", "application/json")
  103. |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
  104. |> json_response_and_validate_schema(200)
  105. end
  106. test "it imports blocks users from file", %{conn: conn} do
  107. users = [user2, user3] = insert_list(2, :user)
  108. with_mocks([
  109. {File, [], read!: fn "blocks_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
  110. ]) do
  111. assert "job started" ==
  112. conn
  113. |> put_req_header("content-type", "application/json")
  114. |> post("/api/pleroma/blocks_import", %{
  115. "list" => %Plug.Upload{path: "blocks_list.txt"}
  116. })
  117. |> json_response_and_validate_schema(200)
  118. assert [{:ok, job_result}] = ObanHelpers.perform_all()
  119. assert job_result == users
  120. end
  121. end
  122. test "it imports blocks with different nickname variations", %{conn: conn} do
  123. users = [user2, user3, user4, user5, user6] = insert_list(5, :user)
  124. identifiers =
  125. [
  126. user2.ap_id,
  127. user3.nickname,
  128. "@" <> user4.nickname,
  129. user5.nickname <> "@localhost",
  130. "@" <> user6.nickname <> "@localhost"
  131. ]
  132. |> Enum.join(" ")
  133. assert "job started" ==
  134. conn
  135. |> put_req_header("content-type", "application/json")
  136. |> post("/api/pleroma/blocks_import", %{"list" => identifiers})
  137. |> json_response_and_validate_schema(200)
  138. assert [{:ok, job_result}] = ObanHelpers.perform_all()
  139. assert job_result == users
  140. end
  141. end
  142. describe "POST /api/pleroma/mutes_import" do
  143. # Note: "follow" or "write:mutes" permission is required
  144. setup do: oauth_access(["write:mutes"])
  145. test "it returns HTTP 200", %{user: user, conn: conn} do
  146. user2 = insert(:user)
  147. assert "job started" ==
  148. conn
  149. |> put_req_header("content-type", "application/json")
  150. |> post("/api/pleroma/mutes_import", %{"list" => "#{user2.ap_id}"})
  151. |> json_response_and_validate_schema(200)
  152. assert [{:ok, job_result}] = ObanHelpers.perform_all()
  153. assert job_result == [user2]
  154. assert Pleroma.User.mutes?(user, user2)
  155. end
  156. test "it imports mutes users from file", %{user: user, conn: conn} do
  157. users = [user2, user3] = insert_list(2, :user)
  158. with_mocks([
  159. {File, [], read!: fn "mutes_list.txt" -> "#{user2.ap_id} #{user3.ap_id}" end}
  160. ]) do
  161. assert "job started" ==
  162. conn
  163. |> put_req_header("content-type", "application/json")
  164. |> post("/api/pleroma/mutes_import", %{
  165. "list" => %Plug.Upload{path: "mutes_list.txt"}
  166. })
  167. |> json_response_and_validate_schema(200)
  168. assert [{:ok, job_result}] = ObanHelpers.perform_all()
  169. assert job_result == users
  170. assert Enum.all?(users, &Pleroma.User.mutes?(user, &1))
  171. end
  172. end
  173. test "it imports mutes with different nickname variations", %{user: user, conn: conn} do
  174. users = [user2, user3, user4, user5, user6] = insert_list(5, :user)
  175. identifiers =
  176. [
  177. user2.ap_id,
  178. user3.nickname,
  179. "@" <> user4.nickname,
  180. user5.nickname <> "@localhost",
  181. "@" <> user6.nickname <> "@localhost"
  182. ]
  183. |> Enum.join(" ")
  184. assert "job started" ==
  185. conn
  186. |> put_req_header("content-type", "application/json")
  187. |> post("/api/pleroma/mutes_import", %{"list" => identifiers})
  188. |> json_response_and_validate_schema(200)
  189. assert [{:ok, job_result}] = ObanHelpers.perform_all()
  190. assert job_result == users
  191. assert Enum.all?(users, &Pleroma.User.mutes?(user, &1))
  192. end
  193. end
  194. end