logo

pleroma

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

util_controller_test.exs (12544B)


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