1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.InviteControllerTest do
6 use Pleroma.Web.ConnCase, async: true
12 alias Pleroma.UserInviteToken
15 admin = insert(:user, is_admin: true)
16 token = insert(:oauth_admin_token, user: admin)
20 |> assign(:user, admin)
21 |> assign(:token, token)
23 {:ok, %{admin: admin, token: token, conn: conn}}
26 describe "POST /api/pleroma/admin/users/email_invite, with valid config" do
27 setup do: clear_config([:instance, :registrations_open], false)
28 setup do: clear_config([:instance, :invites_enabled], true)
30 test "sends invitation and returns 204", %{admin: admin, conn: conn} do
31 recipient_email = "foo@bar.com"
32 recipient_name = "J. D."
36 |> put_req_header("content-type", "application/json;charset=utf-8")
37 |> post("/api/pleroma/admin/users/email_invite", %{
38 email: recipient_email,
42 assert json_response_and_validate_schema(conn, :no_content)
44 token_record = List.last(Repo.all(Pleroma.UserInviteToken))
46 refute token_record.used
48 notify_email = Config.get([:instance, :notify_email])
49 instance_name = Config.get([:instance, :name])
52 Pleroma.Emails.UserEmail.user_invitation_email(
59 Swoosh.TestAssertions.assert_email_sent(
60 from: {instance_name, notify_email},
61 to: {recipient_name, recipient_email},
62 html_body: email.html_body
66 test "it returns 403 if requested by a non-admin" do
67 non_admin_user = insert(:user)
68 token = insert(:oauth_token, user: non_admin_user)
72 |> assign(:user, non_admin_user)
73 |> assign(:token, token)
74 |> put_req_header("content-type", "application/json;charset=utf-8")
75 |> post("/api/pleroma/admin/users/email_invite", %{
80 assert json_response(conn, :forbidden)
83 test "email with +", %{conn: conn, admin: admin} do
84 recipient_email = "foo+bar@baz.com"
87 |> put_req_header("content-type", "application/json;charset=utf-8")
88 |> post("/api/pleroma/admin/users/email_invite", %{email: recipient_email})
89 |> json_response_and_validate_schema(:no_content)
92 Pleroma.UserInviteToken
97 refute token_record.used
99 notify_email = Config.get([:instance, :notify_email])
100 instance_name = Config.get([:instance, :name])
103 Pleroma.Emails.UserEmail.user_invitation_email(
109 Swoosh.TestAssertions.assert_email_sent(
110 from: {instance_name, notify_email},
112 html_body: email.html_body
117 describe "POST /api/pleroma/admin/users/email_invite, with invalid config" do
118 setup do: clear_config([:instance, :registrations_open])
119 setup do: clear_config([:instance, :invites_enabled])
121 test "it returns 500 if `invites_enabled` is not enabled", %{conn: conn} do
122 Config.put([:instance, :registrations_open], false)
123 Config.put([:instance, :invites_enabled], false)
127 |> put_req_header("content-type", "application/json")
128 |> post("/api/pleroma/admin/users/email_invite", %{
129 email: "foo@bar.com",
133 assert json_response_and_validate_schema(conn, :bad_request) ==
136 "To send invites you need to set the `invites_enabled` option to true."
140 test "it returns 500 if `registrations_open` is enabled", %{conn: conn} do
141 Config.put([:instance, :registrations_open], true)
142 Config.put([:instance, :invites_enabled], true)
146 |> put_req_header("content-type", "application/json")
147 |> post("/api/pleroma/admin/users/email_invite", %{
148 email: "foo@bar.com",
152 assert json_response_and_validate_schema(conn, :bad_request) ==
155 "To send invites you need to set the `registrations_open` option to false."
160 describe "POST /api/pleroma/admin/users/invite_token" do
161 test "without options", %{conn: conn} do
164 |> put_req_header("content-type", "application/json")
165 |> post("/api/pleroma/admin/users/invite_token")
167 invite_json = json_response_and_validate_schema(conn, 200)
168 invite = UserInviteToken.find_by_token!(invite_json["token"])
170 refute invite.expires_at
171 refute invite.max_use
172 assert invite.invite_type == "one_time"
175 test "with expires_at", %{conn: conn} do
178 |> put_req_header("content-type", "application/json")
179 |> post("/api/pleroma/admin/users/invite_token", %{
180 "expires_at" => Date.to_string(Date.utc_today())
183 invite_json = json_response_and_validate_schema(conn, 200)
184 invite = UserInviteToken.find_by_token!(invite_json["token"])
187 assert invite.expires_at == Date.utc_today()
188 refute invite.max_use
189 assert invite.invite_type == "date_limited"
192 test "with max_use", %{conn: conn} do
195 |> put_req_header("content-type", "application/json")
196 |> post("/api/pleroma/admin/users/invite_token", %{"max_use" => 150})
198 invite_json = json_response_and_validate_schema(conn, 200)
199 invite = UserInviteToken.find_by_token!(invite_json["token"])
201 refute invite.expires_at
202 assert invite.max_use == 150
203 assert invite.invite_type == "reusable"
206 test "with max use and expires_at", %{conn: conn} do
209 |> put_req_header("content-type", "application/json")
210 |> post("/api/pleroma/admin/users/invite_token", %{
212 "expires_at" => Date.to_string(Date.utc_today())
215 invite_json = json_response_and_validate_schema(conn, 200)
216 invite = UserInviteToken.find_by_token!(invite_json["token"])
218 assert invite.expires_at == Date.utc_today()
219 assert invite.max_use == 150
220 assert invite.invite_type == "reusable_date_limited"
224 describe "GET /api/pleroma/admin/users/invites" do
225 test "no invites", %{conn: conn} do
226 conn = get(conn, "/api/pleroma/admin/users/invites")
228 assert json_response_and_validate_schema(conn, 200) == %{"invites" => []}
231 test "with invite", %{conn: conn} do
232 {:ok, invite} = UserInviteToken.create_invite()
234 conn = get(conn, "/api/pleroma/admin/users/invites")
236 assert json_response_and_validate_schema(conn, 200) == %{
241 "invite_type" => "one_time",
243 "token" => invite.token,
252 describe "POST /api/pleroma/admin/users/revoke_invite" do
253 test "with token", %{conn: conn} do
254 {:ok, invite} = UserInviteToken.create_invite()
258 |> put_req_header("content-type", "application/json")
259 |> post("/api/pleroma/admin/users/revoke_invite", %{"token" => invite.token})
261 assert json_response_and_validate_schema(conn, 200) == %{
264 "invite_type" => "one_time",
266 "token" => invite.token,
272 test "with invalid token", %{conn: conn} do
275 |> put_req_header("content-type", "application/json")
276 |> post("/api/pleroma/admin/users/revoke_invite", %{"token" => "foo"})
278 assert json_response_and_validate_schema(conn, :not_found) == %{"error" => "Not found"}