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.

468 lines
14KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 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. use Oban.Testing, repo: Pleroma.Repo
  7. alias Pleroma.Tests.ObanHelpers
  8. alias Pleroma.User
  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. setup do: clear_config([:instance])
  16. setup do: clear_config([:frontend_configurations, :pleroma_fe])
  17. describe "PUT /api/pleroma/notification_settings" do
  18. setup do: oauth_access(["write:accounts"])
  19. test "it updates notification settings", %{user: user, conn: conn} do
  20. conn
  21. |> put(
  22. "/api/pleroma/notification_settings?#{
  23. URI.encode_query(%{
  24. block_from_strangers: true
  25. })
  26. }"
  27. )
  28. |> json_response_and_validate_schema(:ok)
  29. user = refresh_record(user)
  30. assert %Pleroma.User.NotificationSetting{
  31. block_from_strangers: true,
  32. hide_notification_contents: false
  33. } == user.notification_settings
  34. end
  35. test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do
  36. conn
  37. |> put(
  38. "/api/pleroma/notification_settings?#{
  39. URI.encode_query(%{
  40. hide_notification_contents: 1
  41. })
  42. }"
  43. )
  44. |> json_response_and_validate_schema(:ok)
  45. user = refresh_record(user)
  46. assert %Pleroma.User.NotificationSetting{
  47. block_from_strangers: false,
  48. hide_notification_contents: true
  49. } == user.notification_settings
  50. end
  51. end
  52. describe "GET /api/pleroma/frontend_configurations" do
  53. test "returns everything in :pleroma, :frontend_configurations", %{conn: conn} do
  54. config = [
  55. frontend_a: %{
  56. x: 1,
  57. y: 2
  58. },
  59. frontend_b: %{
  60. z: 3
  61. }
  62. ]
  63. clear_config(:frontend_configurations, config)
  64. response =
  65. conn
  66. |> get("/api/pleroma/frontend_configurations")
  67. |> json_response_and_validate_schema(:ok)
  68. assert response == Jason.encode!(config |> Enum.into(%{})) |> Jason.decode!()
  69. end
  70. end
  71. describe "/api/pleroma/emoji" do
  72. test "returns json with custom emoji with tags", %{conn: conn} do
  73. emoji =
  74. conn
  75. |> get("/api/pleroma/emoji")
  76. |> json_response_and_validate_schema(200)
  77. assert Enum.all?(emoji, fn
  78. {_key,
  79. %{
  80. "image_url" => url,
  81. "tags" => tags
  82. }} ->
  83. is_binary(url) and is_list(tags)
  84. end)
  85. end
  86. end
  87. describe "GET /api/pleroma/healthcheck" do
  88. setup do: clear_config([:instance, :healthcheck])
  89. test "returns 503 when healthcheck disabled", %{conn: conn} do
  90. clear_config([:instance, :healthcheck], false)
  91. response =
  92. conn
  93. |> get("/api/pleroma/healthcheck")
  94. |> json_response_and_validate_schema(503)
  95. assert response == %{}
  96. end
  97. test "returns 200 when healthcheck enabled and all ok", %{conn: conn} do
  98. clear_config([:instance, :healthcheck], true)
  99. with_mock Pleroma.Healthcheck,
  100. system_info: fn -> %Pleroma.Healthcheck{healthy: true} end do
  101. response =
  102. conn
  103. |> get("/api/pleroma/healthcheck")
  104. |> json_response_and_validate_schema(200)
  105. assert %{
  106. "active" => _,
  107. "healthy" => true,
  108. "idle" => _,
  109. "memory_used" => _,
  110. "pool_size" => _
  111. } = response
  112. end
  113. end
  114. test "returns 503 when healthcheck enabled and health is false", %{conn: conn} do
  115. clear_config([:instance, :healthcheck], true)
  116. with_mock Pleroma.Healthcheck,
  117. system_info: fn -> %Pleroma.Healthcheck{healthy: false} end do
  118. response =
  119. conn
  120. |> get("/api/pleroma/healthcheck")
  121. |> json_response_and_validate_schema(503)
  122. assert %{
  123. "active" => _,
  124. "healthy" => false,
  125. "idle" => _,
  126. "memory_used" => _,
  127. "pool_size" => _
  128. } = response
  129. end
  130. end
  131. end
  132. describe "POST /api/pleroma/disable_account" do
  133. setup do: oauth_access(["write:accounts"])
  134. test "with valid permissions and password, it disables the account", %{conn: conn, user: user} do
  135. response =
  136. conn
  137. |> post("/api/pleroma/disable_account?password=test")
  138. |> json_response_and_validate_schema(:ok)
  139. assert response == %{"status" => "success"}
  140. ObanHelpers.perform_all()
  141. user = User.get_cached_by_id(user.id)
  142. refute user.is_active
  143. end
  144. test "with valid permissions and invalid password, it returns an error", %{conn: conn} do
  145. user = insert(:user)
  146. response =
  147. conn
  148. |> post("/api/pleroma/disable_account?password=test1")
  149. |> json_response_and_validate_schema(:ok)
  150. assert response == %{"error" => "Invalid password."}
  151. user = User.get_cached_by_id(user.id)
  152. assert user.is_active
  153. end
  154. end
  155. describe "POST /main/ostatus - remote_subscribe/2" do
  156. setup do: clear_config([:instance, :federating], true)
  157. test "renders subscribe form", %{conn: conn} do
  158. user = insert(:user)
  159. response =
  160. conn
  161. |> post("/main/ostatus", %{"nickname" => user.nickname, "profile" => ""})
  162. |> response(:ok)
  163. refute response =~ "Could not find user"
  164. assert response =~ "Remotely follow #{user.nickname}"
  165. end
  166. test "renders subscribe form with error when user not found", %{conn: conn} do
  167. response =
  168. conn
  169. |> post("/main/ostatus", %{"nickname" => "nickname", "profile" => ""})
  170. |> response(:ok)
  171. assert response =~ "Could not find user"
  172. refute response =~ "Remotely follow"
  173. end
  174. test "it redirect to webfinger url", %{conn: conn} do
  175. user = insert(:user)
  176. user2 = insert(:user, ap_id: "shp@social.heldscal.la")
  177. conn =
  178. conn
  179. |> post("/main/ostatus", %{
  180. "user" => %{"nickname" => user.nickname, "profile" => user2.ap_id}
  181. })
  182. assert redirected_to(conn) ==
  183. "https://social.heldscal.la/main/ostatussub?profile=#{user.ap_id}"
  184. end
  185. test "it renders form with error when user not found", %{conn: conn} do
  186. user2 = insert(:user, ap_id: "shp@social.heldscal.la")
  187. response =
  188. conn
  189. |> post("/main/ostatus", %{"user" => %{"nickname" => "jimm", "profile" => user2.ap_id}})
  190. |> response(:ok)
  191. assert response =~ "Something went wrong."
  192. end
  193. end
  194. test "it returns new captcha", %{conn: conn} do
  195. with_mock Pleroma.Captcha,
  196. new: fn -> "test_captcha" end do
  197. resp =
  198. conn
  199. |> get("/api/pleroma/captcha")
  200. |> response(200)
  201. assert resp == "\"test_captcha\""
  202. assert called(Pleroma.Captcha.new())
  203. end
  204. end
  205. describe "POST /api/pleroma/change_email" do
  206. setup do: oauth_access(["write:accounts"])
  207. test "without permissions", %{conn: conn} do
  208. conn =
  209. conn
  210. |> assign(:token, nil)
  211. |> put_req_header("content-type", "multipart/form-data")
  212. |> post("/api/pleroma/change_email", %{password: "hi", email: "test@test.com"})
  213. assert json_response_and_validate_schema(conn, 403) == %{
  214. "error" => "Insufficient permissions: write:accounts."
  215. }
  216. end
  217. test "with proper permissions and invalid password", %{conn: conn} do
  218. conn =
  219. conn
  220. |> put_req_header("content-type", "multipart/form-data")
  221. |> post("/api/pleroma/change_email", %{password: "hi", email: "test@test.com"})
  222. assert json_response_and_validate_schema(conn, 200) == %{"error" => "Invalid password."}
  223. end
  224. test "with proper permissions, valid password and invalid email", %{
  225. conn: conn
  226. } do
  227. conn =
  228. conn
  229. |> put_req_header("content-type", "multipart/form-data")
  230. |> post("/api/pleroma/change_email", %{password: "test", email: "foobar"})
  231. assert json_response_and_validate_schema(conn, 200) == %{
  232. "error" => "Email has invalid format."
  233. }
  234. end
  235. test "with proper permissions, valid password and no email", %{
  236. conn: conn
  237. } do
  238. conn =
  239. conn
  240. |> put_req_header("content-type", "multipart/form-data")
  241. |> post("/api/pleroma/change_email", %{password: "test"})
  242. assert %{"error" => "Missing field: email."} = json_response_and_validate_schema(conn, 400)
  243. end
  244. test "with proper permissions, valid password and blank email", %{
  245. conn: conn
  246. } do
  247. conn =
  248. conn
  249. |> put_req_header("content-type", "multipart/form-data")
  250. |> post("/api/pleroma/change_email", %{password: "test", email: ""})
  251. assert json_response_and_validate_schema(conn, 200) == %{"error" => "Email can't be blank."}
  252. end
  253. test "with proper permissions, valid password and non unique email", %{
  254. conn: conn
  255. } do
  256. user = insert(:user)
  257. conn =
  258. conn
  259. |> put_req_header("content-type", "multipart/form-data")
  260. |> post("/api/pleroma/change_email", %{password: "test", email: user.email})
  261. assert json_response_and_validate_schema(conn, 200) == %{
  262. "error" => "Email has already been taken."
  263. }
  264. end
  265. test "with proper permissions, valid password and valid email", %{
  266. conn: conn
  267. } do
  268. conn =
  269. conn
  270. |> put_req_header("content-type", "multipart/form-data")
  271. |> post("/api/pleroma/change_email", %{password: "test", email: "cofe@foobar.com"})
  272. assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
  273. end
  274. end
  275. describe "POST /api/pleroma/change_password" do
  276. setup do: oauth_access(["write:accounts"])
  277. test "without permissions", %{conn: conn} do
  278. conn =
  279. conn
  280. |> assign(:token, nil)
  281. |> put_req_header("content-type", "multipart/form-data")
  282. |> post("/api/pleroma/change_password", %{
  283. "password" => "hi",
  284. "new_password" => "newpass",
  285. "new_password_confirmation" => "newpass"
  286. })
  287. assert json_response_and_validate_schema(conn, 403) == %{
  288. "error" => "Insufficient permissions: write:accounts."
  289. }
  290. end
  291. test "with proper permissions and invalid password", %{conn: conn} do
  292. conn =
  293. conn
  294. |> put_req_header("content-type", "multipart/form-data")
  295. |> post("/api/pleroma/change_password", %{
  296. "password" => "hi",
  297. "new_password" => "newpass",
  298. "new_password_confirmation" => "newpass"
  299. })
  300. assert json_response_and_validate_schema(conn, 200) == %{"error" => "Invalid password."}
  301. end
  302. test "with proper permissions, valid password and new password and confirmation not matching",
  303. %{
  304. conn: conn
  305. } do
  306. conn =
  307. conn
  308. |> put_req_header("content-type", "multipart/form-data")
  309. |> post("/api/pleroma/change_password", %{
  310. "password" => "test",
  311. "new_password" => "newpass",
  312. "new_password_confirmation" => "notnewpass"
  313. })
  314. assert json_response_and_validate_schema(conn, 200) == %{
  315. "error" => "New password does not match confirmation."
  316. }
  317. end
  318. test "with proper permissions, valid password and invalid new password", %{
  319. conn: conn
  320. } do
  321. conn =
  322. conn
  323. |> put_req_header("content-type", "multipart/form-data")
  324. |> post("/api/pleroma/change_password", %{
  325. password: "test",
  326. new_password: "",
  327. new_password_confirmation: ""
  328. })
  329. assert json_response_and_validate_schema(conn, 200) == %{
  330. "error" => "New password can't be blank."
  331. }
  332. end
  333. test "with proper permissions, valid password and matching new password and confirmation", %{
  334. conn: conn,
  335. user: user
  336. } do
  337. conn =
  338. conn
  339. |> put_req_header("content-type", "multipart/form-data")
  340. |> post(
  341. "/api/pleroma/change_password",
  342. %{
  343. password: "test",
  344. new_password: "newpass",
  345. new_password_confirmation: "newpass"
  346. }
  347. )
  348. assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
  349. fetched_user = User.get_cached_by_id(user.id)
  350. assert Pleroma.Password.Pbkdf2.verify_pass("newpass", fetched_user.password_hash) == true
  351. end
  352. end
  353. describe "POST /api/pleroma/delete_account" do
  354. setup do: oauth_access(["write:accounts"])
  355. test "without permissions", %{conn: conn} do
  356. conn =
  357. conn
  358. |> assign(:token, nil)
  359. |> post("/api/pleroma/delete_account")
  360. assert json_response_and_validate_schema(conn, 403) ==
  361. %{"error" => "Insufficient permissions: write:accounts."}
  362. end
  363. test "with proper permissions and wrong or missing password", %{conn: conn} do
  364. for params <- [%{"password" => "hi"}, %{}] do
  365. ret_conn = post(conn, "/api/pleroma/delete_account", params)
  366. assert json_response_and_validate_schema(ret_conn, 200) == %{
  367. "error" => "Invalid password."
  368. }
  369. end
  370. end
  371. test "with proper permissions and valid password", %{conn: conn, user: user} do
  372. conn = post(conn, "/api/pleroma/delete_account?password=test")
  373. ObanHelpers.perform_all()
  374. assert json_response_and_validate_schema(conn, 200) == %{"status" => "success"}
  375. user = User.get_by_id(user.id)
  376. refute user.is_active
  377. assert user.name == nil
  378. assert user.bio == ""
  379. assert user.password_hash == nil
  380. end
  381. end
  382. end