Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

316 lines
8.6KB

  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. defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Notification
  7. alias Pleroma.Repo
  8. alias Pleroma.User
  9. alias Pleroma.Web.CommonAPI
  10. import Pleroma.Factory
  11. import Mock
  12. setup do
  13. Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
  14. :ok
  15. end
  16. describe "POST /api/pleroma/follow_import" do
  17. test "it returns HTTP 200", %{conn: conn} do
  18. user1 = insert(:user)
  19. user2 = insert(:user)
  20. response =
  21. conn
  22. |> assign(:user, user1)
  23. |> post("/api/pleroma/follow_import", %{"list" => "#{user2.ap_id}"})
  24. |> json_response(:ok)
  25. assert response == "job started"
  26. end
  27. test "it imports new-style mastodon follow lists", %{conn: conn} do
  28. user1 = insert(:user)
  29. user2 = insert(:user)
  30. response =
  31. conn
  32. |> assign(:user, user1)
  33. |> post("/api/pleroma/follow_import", %{
  34. "list" => "Account address,Show boosts\n#{user2.ap_id},true"
  35. })
  36. |> json_response(:ok)
  37. assert response == "job started"
  38. end
  39. test "requires 'follow' permission", %{conn: conn} do
  40. token1 = insert(:oauth_token, scopes: ["read", "write"])
  41. token2 = insert(:oauth_token, scopes: ["follow"])
  42. another_user = insert(:user)
  43. for token <- [token1, token2] do
  44. conn =
  45. conn
  46. |> put_req_header("authorization", "Bearer #{token.token}")
  47. |> post("/api/pleroma/follow_import", %{"list" => "#{another_user.ap_id}"})
  48. if token == token1 do
  49. assert %{"error" => "Insufficient permissions: follow."} == json_response(conn, 403)
  50. else
  51. assert json_response(conn, 200)
  52. end
  53. end
  54. end
  55. end
  56. describe "POST /api/pleroma/blocks_import" do
  57. test "it returns HTTP 200", %{conn: conn} do
  58. user1 = insert(:user)
  59. user2 = insert(:user)
  60. response =
  61. conn
  62. |> assign(:user, user1)
  63. |> post("/api/pleroma/blocks_import", %{"list" => "#{user2.ap_id}"})
  64. |> json_response(:ok)
  65. assert response == "job started"
  66. end
  67. end
  68. describe "POST /api/pleroma/notifications/read" do
  69. test "it marks a single notification as read", %{conn: conn} do
  70. user1 = insert(:user)
  71. user2 = insert(:user)
  72. {:ok, activity1} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
  73. {:ok, activity2} = CommonAPI.post(user2, %{"status" => "hi @#{user1.nickname}"})
  74. {:ok, [notification1]} = Notification.create_notifications(activity1)
  75. {:ok, [notification2]} = Notification.create_notifications(activity2)
  76. conn
  77. |> assign(:user, user1)
  78. |> post("/api/pleroma/notifications/read", %{"id" => "#{notification1.id}"})
  79. |> json_response(:ok)
  80. assert Repo.get(Notification, notification1.id).seen
  81. refute Repo.get(Notification, notification2.id).seen
  82. end
  83. end
  84. describe "PUT /api/pleroma/notification_settings" do
  85. test "it updates notification settings", %{conn: conn} do
  86. user = insert(:user)
  87. conn
  88. |> assign(:user, user)
  89. |> put("/api/pleroma/notification_settings", %{
  90. "followers" => false,
  91. "bar" => 1
  92. })
  93. |> json_response(:ok)
  94. user = Repo.get(User, user.id)
  95. assert %{
  96. "followers" => false,
  97. "follows" => true,
  98. "non_follows" => true,
  99. "non_followers" => true
  100. } == user.info.notification_settings
  101. end
  102. end
  103. describe "GET /api/statusnet/config.json" do
  104. test "returns the state of safe_dm_mentions flag", %{conn: conn} do
  105. option = Pleroma.Config.get([:instance, :safe_dm_mentions])
  106. Pleroma.Config.put([:instance, :safe_dm_mentions], true)
  107. response =
  108. conn
  109. |> get("/api/statusnet/config.json")
  110. |> json_response(:ok)
  111. assert response["site"]["safeDMMentionsEnabled"] == "1"
  112. Pleroma.Config.put([:instance, :safe_dm_mentions], false)
  113. response =
  114. conn
  115. |> get("/api/statusnet/config.json")
  116. |> json_response(:ok)
  117. assert response["site"]["safeDMMentionsEnabled"] == "0"
  118. Pleroma.Config.put([:instance, :safe_dm_mentions], option)
  119. end
  120. test "it returns the managed config", %{conn: conn} do
  121. Pleroma.Config.put([:instance, :managed_config], false)
  122. Pleroma.Config.put([:frontend_configurations, :pleroma_fe], %{theme: "asuka-hospital"})
  123. response =
  124. conn
  125. |> get("/api/statusnet/config.json")
  126. |> json_response(:ok)
  127. refute response["site"]["pleromafe"]
  128. Pleroma.Config.put([:instance, :managed_config], true)
  129. response =
  130. conn
  131. |> get("/api/statusnet/config.json")
  132. |> json_response(:ok)
  133. assert response["site"]["pleromafe"] == %{"theme" => "asuka-hospital"}
  134. end
  135. end
  136. describe "GET /api/pleroma/frontend_configurations" do
  137. test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
  138. config = [
  139. frontend_a: %{
  140. x: 1,
  141. y: 2
  142. },
  143. frontend_b: %{
  144. z: 3
  145. }
  146. ]
  147. Pleroma.Config.put(:frontend_configurations, config)
  148. response =
  149. conn
  150. |> get("/api/pleroma/frontend_configurations")
  151. |> json_response(:ok)
  152. assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
  153. end
  154. end
  155. describe "/api/pleroma/emoji" do
  156. test "returns json with custom emoji with tags", %{conn: conn} do
  157. emoji =
  158. conn
  159. |> get("/api/pleroma/emoji")
  160. |> json_response(200)
  161. assert Enum.all?(emoji, fn
  162. {_key,
  163. %{
  164. "image_url" => url,
  165. "tags" => tags
  166. }} ->
  167. is_binary(url) and is_list(tags)
  168. end)
  169. end
  170. end
  171. describe "GET /ostatus_subscribe?acct=...." do
  172. test "adds status to pleroma instance if the `acct` is a status", %{conn: conn} do
  173. conn =
  174. get(
  175. conn,
  176. "/ostatus_subscribe?acct=https://mastodon.social/users/emelie/statuses/101849165031453009"
  177. )
  178. assert redirected_to(conn) =~ "/notice/"
  179. end
  180. test "show follow account page if the `acct` is a account link", %{conn: conn} do
  181. response =
  182. get(
  183. conn,
  184. "/ostatus_subscribe?acct=https://mastodon.social/users/emelie"
  185. )
  186. assert html_response(response, 200) =~ "Log in to follow"
  187. end
  188. end
  189. describe "GET /api/pleroma/healthcheck" do
  190. setup do
  191. config_healthcheck = Pleroma.Config.get([:instance, :healthcheck])
  192. on_exit(fn ->
  193. Pleroma.Config.put([:instance, :healthcheck], config_healthcheck)
  194. end)
  195. :ok
  196. end
  197. test "returns 503 when healthcheck disabled", %{conn: conn} do
  198. Pleroma.Config.put([:instance, :healthcheck], false)
  199. response =
  200. conn
  201. |> get("/api/pleroma/healthcheck")
  202. |> json_response(503)
  203. assert response == %{}
  204. end
  205. test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
  206. Pleroma.Config.put([:instance, :healthcheck], true)
  207. with_mock Pleroma.Healthcheck,
  208. system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
  209. response =
  210. conn
  211. |> get("/api/pleroma/healthcheck")
  212. |> json_response(200)
  213. assert %{
  214. "active" => _,
  215. "healthy" => true,
  216. "idle" => _,
  217. "memory_used" => _,
  218. "pool_size" => _
  219. } = response
  220. end
  221. end
  222. test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
  223. Pleroma.Config.put([:instance, :healthcheck], true)
  224. with_mock Pleroma.Healthcheck,
  225. system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
  226. response =
  227. conn
  228. |> get("/api/pleroma/healthcheck")
  229. |> json_response(503)
  230. assert %{
  231. "active" => _,
  232. "healthy" => false,
  233. "idle" => _,
  234. "memory_used" => _,
  235. "pool_size" => _
  236. } = response
  237. end
  238. end
  239. end
  240. describe "POST /api/pleroma/disable_account" do
  241. test "it returns HTTP 200", %{conn: conn} do
  242. user = insert(:user)
  243. response =
  244. conn
  245. |> assign(:user, user)
  246. |> post("/api/pleroma/disable_account", %{"password" => "test"})
  247. |> json_response(:ok)
  248. assert response == %{"status" => "success"}
  249. user = User.get_cached_by_id(user.id)
  250. assert user.info.deactivated == true
  251. end
  252. end
  253. end