logo

pleroma

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

invite_controller_test.exs (8841B)


  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.AdminAPI.InviteControllerTest do
  5. use Pleroma.Web.ConnCase, async: true
  6. import Pleroma.Factory
  7. alias Pleroma.Config
  8. alias Pleroma.Repo
  9. alias Pleroma.UserInviteToken
  10. setup do
  11. admin = insert(:user, is_admin: true)
  12. token = insert(:oauth_admin_token, user: admin)
  13. conn =
  14. build_conn()
  15. |> assign(:user, admin)
  16. |> assign(:token, token)
  17. {:ok, %{admin: admin, token: token, conn: conn}}
  18. end
  19. describe "POST /api/pleroma/admin/users/email_invite, with valid config" do
  20. setup do: clear_config([:instance, :registrations_open], false)
  21. setup do: clear_config([:instance, :invites_enabled], true)
  22. test "sends invitation and returns 204", %{admin: admin, conn: conn} do
  23. recipient_email = "foo@bar.com"
  24. recipient_name = "J. D."
  25. conn =
  26. conn
  27. |> put_req_header("content-type", "application/json;charset=utf-8")
  28. |> post("/api/pleroma/admin/users/email_invite", %{
  29. email: recipient_email,
  30. name: recipient_name
  31. })
  32. assert json_response_and_validate_schema(conn, :no_content)
  33. token_record = List.last(Repo.all(Pleroma.UserInviteToken))
  34. assert token_record
  35. refute token_record.used
  36. notify_email = Config.get([:instance, :notify_email])
  37. instance_name = Config.get([:instance, :name])
  38. email =
  39. Pleroma.Emails.UserEmail.user_invitation_email(
  40. admin,
  41. token_record,
  42. recipient_email,
  43. recipient_name
  44. )
  45. Swoosh.TestAssertions.assert_email_sent(
  46. from: {instance_name, notify_email},
  47. to: {recipient_name, recipient_email},
  48. html_body: email.html_body
  49. )
  50. end
  51. test "it returns 403 if requested by a non-admin" do
  52. non_admin_user = insert(:user)
  53. token = insert(:oauth_token, user: non_admin_user)
  54. conn =
  55. build_conn()
  56. |> assign(:user, non_admin_user)
  57. |> assign(:token, token)
  58. |> put_req_header("content-type", "application/json;charset=utf-8")
  59. |> post("/api/pleroma/admin/users/email_invite", %{
  60. email: "foo@bar.com",
  61. name: "JD"
  62. })
  63. assert json_response(conn, :forbidden)
  64. end
  65. test "email with +", %{conn: conn, admin: admin} do
  66. recipient_email = "foo+bar@baz.com"
  67. conn
  68. |> put_req_header("content-type", "application/json;charset=utf-8")
  69. |> post("/api/pleroma/admin/users/email_invite", %{email: recipient_email})
  70. |> json_response_and_validate_schema(:no_content)
  71. token_record =
  72. Pleroma.UserInviteToken
  73. |> Repo.all()
  74. |> List.last()
  75. assert token_record
  76. refute token_record.used
  77. notify_email = Config.get([:instance, :notify_email])
  78. instance_name = Config.get([:instance, :name])
  79. email =
  80. Pleroma.Emails.UserEmail.user_invitation_email(
  81. admin,
  82. token_record,
  83. recipient_email
  84. )
  85. Swoosh.TestAssertions.assert_email_sent(
  86. from: {instance_name, notify_email},
  87. to: recipient_email,
  88. html_body: email.html_body
  89. )
  90. end
  91. end
  92. describe "POST /api/pleroma/admin/users/email_invite, with invalid config" do
  93. setup do: clear_config([:instance, :registrations_open])
  94. setup do: clear_config([:instance, :invites_enabled])
  95. test "it returns 500 if `invites_enabled` is not enabled", %{conn: conn} do
  96. Config.put([:instance, :registrations_open], false)
  97. Config.put([:instance, :invites_enabled], false)
  98. conn =
  99. conn
  100. |> put_req_header("content-type", "application/json")
  101. |> post("/api/pleroma/admin/users/email_invite", %{
  102. email: "foo@bar.com",
  103. name: "JD"
  104. })
  105. assert json_response_and_validate_schema(conn, :bad_request) ==
  106. %{
  107. "error" =>
  108. "To send invites you need to set the `invites_enabled` option to true."
  109. }
  110. end
  111. test "it returns 500 if `registrations_open` is enabled", %{conn: conn} do
  112. Config.put([:instance, :registrations_open], true)
  113. Config.put([:instance, :invites_enabled], true)
  114. conn =
  115. conn
  116. |> put_req_header("content-type", "application/json")
  117. |> post("/api/pleroma/admin/users/email_invite", %{
  118. email: "foo@bar.com",
  119. name: "JD"
  120. })
  121. assert json_response_and_validate_schema(conn, :bad_request) ==
  122. %{
  123. "error" =>
  124. "To send invites you need to set the `registrations_open` option to false."
  125. }
  126. end
  127. end
  128. describe "POST /api/pleroma/admin/users/invite_token" do
  129. test "without options", %{conn: conn} do
  130. conn =
  131. conn
  132. |> put_req_header("content-type", "application/json")
  133. |> post("/api/pleroma/admin/users/invite_token")
  134. invite_json = json_response_and_validate_schema(conn, 200)
  135. invite = UserInviteToken.find_by_token!(invite_json["token"])
  136. refute invite.used
  137. refute invite.expires_at
  138. refute invite.max_use
  139. assert invite.invite_type == "one_time"
  140. end
  141. test "with expires_at", %{conn: conn} do
  142. conn =
  143. conn
  144. |> put_req_header("content-type", "application/json")
  145. |> post("/api/pleroma/admin/users/invite_token", %{
  146. "expires_at" => Date.to_string(Date.utc_today())
  147. })
  148. invite_json = json_response_and_validate_schema(conn, 200)
  149. invite = UserInviteToken.find_by_token!(invite_json["token"])
  150. refute invite.used
  151. assert invite.expires_at == Date.utc_today()
  152. refute invite.max_use
  153. assert invite.invite_type == "date_limited"
  154. end
  155. test "with max_use", %{conn: conn} do
  156. conn =
  157. conn
  158. |> put_req_header("content-type", "application/json")
  159. |> post("/api/pleroma/admin/users/invite_token", %{"max_use" => 150})
  160. invite_json = json_response_and_validate_schema(conn, 200)
  161. invite = UserInviteToken.find_by_token!(invite_json["token"])
  162. refute invite.used
  163. refute invite.expires_at
  164. assert invite.max_use == 150
  165. assert invite.invite_type == "reusable"
  166. end
  167. test "with max use and expires_at", %{conn: conn} do
  168. conn =
  169. conn
  170. |> put_req_header("content-type", "application/json")
  171. |> post("/api/pleroma/admin/users/invite_token", %{
  172. "max_use" => 150,
  173. "expires_at" => Date.to_string(Date.utc_today())
  174. })
  175. invite_json = json_response_and_validate_schema(conn, 200)
  176. invite = UserInviteToken.find_by_token!(invite_json["token"])
  177. refute invite.used
  178. assert invite.expires_at == Date.utc_today()
  179. assert invite.max_use == 150
  180. assert invite.invite_type == "reusable_date_limited"
  181. end
  182. end
  183. describe "GET /api/pleroma/admin/users/invites" do
  184. test "no invites", %{conn: conn} do
  185. conn = get(conn, "/api/pleroma/admin/users/invites")
  186. assert json_response_and_validate_schema(conn, 200) == %{"invites" => []}
  187. end
  188. test "with invite", %{conn: conn} do
  189. {:ok, invite} = UserInviteToken.create_invite()
  190. conn = get(conn, "/api/pleroma/admin/users/invites")
  191. assert json_response_and_validate_schema(conn, 200) == %{
  192. "invites" => [
  193. %{
  194. "expires_at" => nil,
  195. "id" => invite.id,
  196. "invite_type" => "one_time",
  197. "max_use" => nil,
  198. "token" => invite.token,
  199. "used" => false,
  200. "uses" => 0
  201. }
  202. ]
  203. }
  204. end
  205. end
  206. describe "POST /api/pleroma/admin/users/revoke_invite" do
  207. test "with token", %{conn: conn} do
  208. {:ok, invite} = UserInviteToken.create_invite()
  209. conn =
  210. conn
  211. |> put_req_header("content-type", "application/json")
  212. |> post("/api/pleroma/admin/users/revoke_invite", %{"token" => invite.token})
  213. assert json_response_and_validate_schema(conn, 200) == %{
  214. "expires_at" => nil,
  215. "id" => invite.id,
  216. "invite_type" => "one_time",
  217. "max_use" => nil,
  218. "token" => invite.token,
  219. "used" => true,
  220. "uses" => 0
  221. }
  222. end
  223. test "with invalid token", %{conn: conn} do
  224. conn =
  225. conn
  226. |> put_req_header("content-type", "application/json")
  227. |> post("/api/pleroma/admin/users/revoke_invite", %{"token" => "foo"})
  228. assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}
  229. end
  230. end
  231. end