logo

pleroma

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

account_controller_test.exs (8592B)


  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.AccountControllerTest do
  5. use Pleroma.Web.ConnCase
  6. alias Pleroma.Config
  7. alias Pleroma.Tests.ObanHelpers
  8. alias Pleroma.User
  9. alias Pleroma.Web.CommonAPI
  10. import Pleroma.Factory
  11. import Swoosh.TestAssertions
  12. describe "POST /api/v1/pleroma/accounts/confirmation_resend" do
  13. setup do
  14. {:ok, user} =
  15. insert(:user)
  16. |> User.confirmation_changeset(need_confirmation: true)
  17. |> User.update_and_set_cache()
  18. assert user.confirmation_pending
  19. [user: user]
  20. end
  21. setup do: clear_config([:instance, :account_activation_required], true)
  22. test "resend account confirmation email", %{conn: conn, user: user} do
  23. conn
  24. |> put_req_header("content-type", "application/json")
  25. |> post("/api/v1/pleroma/accounts/confirmation_resend?email=#{user.email}")
  26. |> json_response_and_validate_schema(:no_content)
  27. ObanHelpers.perform_all()
  28. email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
  29. notify_email = Config.get([:instance, :notify_email])
  30. instance_name = Config.get([:instance, :name])
  31. assert_email_sent(
  32. from: {instance_name, notify_email},
  33. to: {user.name, user.email},
  34. html_body: email.html_body
  35. )
  36. end
  37. test "resend account confirmation email (with nickname)", %{conn: conn, user: user} do
  38. conn
  39. |> put_req_header("content-type", "application/json")
  40. |> post("/api/v1/pleroma/accounts/confirmation_resend?nickname=#{user.nickname}")
  41. |> json_response_and_validate_schema(:no_content)
  42. ObanHelpers.perform_all()
  43. email = Pleroma.Emails.UserEmail.account_confirmation_email(user)
  44. notify_email = Config.get([:instance, :notify_email])
  45. instance_name = Config.get([:instance, :name])
  46. assert_email_sent(
  47. from: {instance_name, notify_email},
  48. to: {user.name, user.email},
  49. html_body: email.html_body
  50. )
  51. end
  52. end
  53. describe "getting favorites timeline of specified user" do
  54. setup do
  55. [current_user, user] = insert_pair(:user, hide_favorites: false)
  56. %{user: current_user, conn: conn} = oauth_access(["read:favourites"], user: current_user)
  57. [current_user: current_user, user: user, conn: conn]
  58. end
  59. test "returns list of statuses favorited by specified user", %{
  60. conn: conn,
  61. user: user
  62. } do
  63. [activity | _] = insert_pair(:note_activity)
  64. CommonAPI.favorite(user, activity.id)
  65. response =
  66. conn
  67. |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
  68. |> json_response_and_validate_schema(:ok)
  69. [like] = response
  70. assert length(response) == 1
  71. assert like["id"] == activity.id
  72. end
  73. test "returns favorites for specified user_id when requester is not logged in", %{
  74. user: user
  75. } do
  76. activity = insert(:note_activity)
  77. CommonAPI.favorite(user, activity.id)
  78. response =
  79. build_conn()
  80. |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
  81. |> json_response_and_validate_schema(200)
  82. assert length(response) == 1
  83. end
  84. test "returns favorited DM only when user is logged in and he is one of recipients", %{
  85. current_user: current_user,
  86. user: user
  87. } do
  88. {:ok, direct} =
  89. CommonAPI.post(current_user, %{
  90. status: "Hi @#{user.nickname}!",
  91. visibility: "direct"
  92. })
  93. CommonAPI.favorite(user, direct.id)
  94. for u <- [user, current_user] do
  95. response =
  96. build_conn()
  97. |> assign(:user, u)
  98. |> assign(:token, insert(:oauth_token, user: u, scopes: ["read:favourites"]))
  99. |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
  100. |> json_response_and_validate_schema(:ok)
  101. assert length(response) == 1
  102. end
  103. response =
  104. build_conn()
  105. |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
  106. |> json_response_and_validate_schema(200)
  107. assert length(response) == 0
  108. end
  109. test "does not return others' favorited DM when user is not one of recipients", %{
  110. conn: conn,
  111. user: user
  112. } do
  113. user_two = insert(:user)
  114. {:ok, direct} =
  115. CommonAPI.post(user_two, %{
  116. status: "Hi @#{user.nickname}!",
  117. visibility: "direct"
  118. })
  119. CommonAPI.favorite(user, direct.id)
  120. response =
  121. conn
  122. |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
  123. |> json_response_and_validate_schema(:ok)
  124. assert Enum.empty?(response)
  125. end
  126. test "paginates favorites using since_id and max_id", %{
  127. conn: conn,
  128. user: user
  129. } do
  130. activities = insert_list(10, :note_activity)
  131. Enum.each(activities, fn activity ->
  132. CommonAPI.favorite(user, activity.id)
  133. end)
  134. third_activity = Enum.at(activities, 2)
  135. seventh_activity = Enum.at(activities, 6)
  136. response =
  137. conn
  138. |> get(
  139. "/api/v1/pleroma/accounts/#{user.id}/favourites?since_id=#{third_activity.id}&max_id=#{
  140. seventh_activity.id
  141. }"
  142. )
  143. |> json_response_and_validate_schema(:ok)
  144. assert length(response) == 3
  145. refute third_activity in response
  146. refute seventh_activity in response
  147. end
  148. test "limits favorites using limit parameter", %{
  149. conn: conn,
  150. user: user
  151. } do
  152. 7
  153. |> insert_list(:note_activity)
  154. |> Enum.each(fn activity ->
  155. CommonAPI.favorite(user, activity.id)
  156. end)
  157. response =
  158. conn
  159. |> get("/api/v1/pleroma/accounts/#{user.id}/favourites?limit=3")
  160. |> json_response_and_validate_schema(:ok)
  161. assert length(response) == 3
  162. end
  163. test "returns empty response when user does not have any favorited statuses", %{
  164. conn: conn,
  165. user: user
  166. } do
  167. response =
  168. conn
  169. |> get("/api/v1/pleroma/accounts/#{user.id}/favourites")
  170. |> json_response_and_validate_schema(:ok)
  171. assert Enum.empty?(response)
  172. end
  173. test "returns 404 error when specified user is not exist", %{conn: conn} do
  174. conn = get(conn, "/api/v1/pleroma/accounts/test/favourites")
  175. assert json_response_and_validate_schema(conn, 404) == %{"error" => "Record not found"}
  176. end
  177. test "returns 403 error when user has hidden own favorites", %{conn: conn} do
  178. user = insert(:user, hide_favorites: true)
  179. activity = insert(:note_activity)
  180. CommonAPI.favorite(user, activity.id)
  181. conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
  182. assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
  183. end
  184. test "hides favorites for new users by default", %{conn: conn} do
  185. user = insert(:user)
  186. activity = insert(:note_activity)
  187. CommonAPI.favorite(user, activity.id)
  188. assert user.hide_favorites
  189. conn = get(conn, "/api/v1/pleroma/accounts/#{user.id}/favourites")
  190. assert json_response_and_validate_schema(conn, 403) == %{"error" => "Can't get favorites"}
  191. end
  192. end
  193. describe "subscribing / unsubscribing" do
  194. test "subscribing / unsubscribing to a user" do
  195. %{user: user, conn: conn} = oauth_access(["follow"])
  196. subscription_target = insert(:user)
  197. ret_conn =
  198. conn
  199. |> assign(:user, user)
  200. |> post("/api/v1/pleroma/accounts/#{subscription_target.id}/subscribe")
  201. assert %{"id" => _id, "subscribing" => true} =
  202. json_response_and_validate_schema(ret_conn, 200)
  203. conn = post(conn, "/api/v1/pleroma/accounts/#{subscription_target.id}/unsubscribe")
  204. assert %{"id" => _id, "subscribing" => false} = json_response_and_validate_schema(conn, 200)
  205. end
  206. end
  207. describe "subscribing" do
  208. test "returns 404 when subscription_target not found" do
  209. %{conn: conn} = oauth_access(["write:follows"])
  210. conn = post(conn, "/api/v1/pleroma/accounts/target_id/subscribe")
  211. assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
  212. end
  213. end
  214. describe "unsubscribing" do
  215. test "returns 404 when subscription_target not found" do
  216. %{conn: conn} = oauth_access(["follow"])
  217. conn = post(conn, "/api/v1/pleroma/accounts/target_id/unsubscribe")
  218. assert %{"error" => "Record not found"} = json_response_and_validate_schema(conn, 404)
  219. end
  220. end
  221. end