[#114] Added UserInviteToken creation, adjusted invitation email link to include it.
authorIvan Tashkinov <ivantashkinov@gmail.com>
Thu, 13 Dec 2018 14:58:40 +0000 (17:58 +0300)
committerIvan Tashkinov <ivantashkinov@gmail.com>
Fri, 14 Dec 2018 09:28:22 +0000 (12:28 +0300)
lib/pleroma/emails/user_email.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_controller_test.exs

index 47dcd42e0ca9decad6ab4c57cd174f67cc1a6597..ee41ce50ae74ce26c3ad88fabb3c445f3e8da90b 100644 (file)
@@ -38,12 +38,12 @@ defmodule Pleroma.UserEmail do
     |> html_body(html_body)
   end
 
-  def user_invitation_email(user, to_email, to_name \\ nil) do
+  def user_invitation_email(user, user_invite_token, to_email, to_name \\ nil) do
     registration_url =
       Router.Helpers.redirect_url(
         Endpoint,
         :registration_page,
-        ""
+        user_invite_token.token
       )
 
     html_body = """
index d51d712991fadb57aaf014bbcc57418cbaf596b8..0607a1a6a833fa6e12ed03a43ab360589d8c2896 100644 (file)
@@ -336,8 +336,12 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
   def confirm_email(_conn, _params), do: :noop
 
   def email_invite(%{assigns: %{user: user}} = conn, %{"email" => email} = params) do
-    with true <- Pleroma.Config.get([:instance, :invites_enabled]),
-         email <- Pleroma.UserEmail.user_invitation_email(user, email, params["name"]),
+    with true <-
+           Pleroma.Config.get([:instance, :invites_enabled]) &&
+             !Pleroma.Config.get([:instance, :registrations_open]),
+         {:ok, invite_token} <- Pleroma.UserInviteToken.create_token(),
+         email <-
+           Pleroma.UserEmail.user_invitation_email(user, invite_token, email, params["name"]),
          {:ok, _} <- Pleroma.Mailer.deliver(email) do
       json_response(conn, :no_content, "")
     end
index cbb5f77962b0d0a9cc7d1087d0da5ffa2700ae38..e5c6f848d08d66a4271c53cf409475728882117d 100644 (file)
@@ -877,10 +877,13 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     setup [:valid_user]
 
     setup do
+      registrations_open = Pleroma.Config.get([:instance, :registrations_open])
       invites_enabled = Pleroma.Config.get([:instance, :invites_enabled])
+      Pleroma.Config.put([:instance, :registrations_open], false)
       Pleroma.Config.put([:instance, :invites_enabled], true)
 
       on_exit(fn ->
+        Pleroma.Config.put([:instance, :registrations_open], registrations_open)
         Pleroma.Config.put([:instance, :invites_enabled], invites_enabled)
         :ok
       end)
@@ -888,7 +891,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
       :ok
     end
 
-    test "it returns 204", %{conn: conn, user: user} do
+    test "sends invitation and returns 204", %{conn: conn, user: user} do
       recipient_email = "foo@bar.com"
       recipient_name = "J. D."
 
@@ -899,9 +902,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
 
       assert json_response(conn, :no_content)
 
-      Swoosh.TestAssertions.assert_email_sent(
-        Pleroma.UserEmail.user_invitation_email(user, recipient_email, recipient_name)
-      )
+      Swoosh.TestAssertions.assert_email_sent()
     end
   end