Merge branch 'fix/1658-invite-send' into 'develop'
authorlain <lain@soykaf.club>
Thu, 9 Apr 2020 10:42:43 +0000 (10:42 +0000)
committerlain <lain@soykaf.club>
Thu, 9 Apr 2020 10:42:43 +0000 (10:42 +0000)
Error improvement for email_invite endpoint

See merge request pleroma/pleroma!2344

1  2 
docs/API/admin_api.md
test/web/admin_api/admin_api_controller_test.exs

diff --combined docs/API/admin_api.md
index 58d7023472d0202aa023b7e40790423346d48761,b3cf89818cfaa056954b8d8627372ac75c14c101..57fb6bc6a9c7a273ab2f741dad0ddfe66ccc20ed
@@@ -392,6 -392,19 +392,19 @@@ Note: Available `:permission_group` is 
    - `email`
    - `name`, optional
  
+ - Response:
+   - On success: `204`, empty response
+   - On failure:
+     - 400 Bad Request, JSON:
+     ```json
+       [
+         {
+           "error": "Appropriate error message here"
+         }
+       ]
+     ```
  ## `GET /api/pleroma/admin/users/:nickname/password_reset`
  
  ### Get a password reset token for a given nickname
@@@ -841,8 -854,6 +854,8 @@@ Some modifications are necessary to sav
  Most of the settings will be applied in `runtime`, this means that you don't need to restart the instance. But some settings are applied in `compile time` and require a reboot of the instance, such as:
  - all settings inside these keys:
    - `:hackney_pools`
 +  - `:connections_pool`
 +  - `:pools`
    - `:chat`
  - partially settings inside these keys:
    - `:seconds_valid` in `Pleroma.Captcha`
index fe8a086d8c8449a20eda629d257210bd618b3f35,e8d11b88c5f7408e03a320559f1145e2ce0a10b9..f02f6ae7afedeeb5244d02b3708f0191a591913e
@@@ -625,6 -625,39 +625,39 @@@ defmodule Pleroma.Web.AdminAPI.AdminAPI
  
        assert json_response(conn, :forbidden)
      end
+     test "email with +", %{conn: conn, admin: admin} do
+       recipient_email = "foo+bar@baz.com"
+       conn
+       |> put_req_header("content-type", "application/json;charset=utf-8")
+       |> post("/api/pleroma/admin/users/email_invite", %{email: recipient_email})
+       |> json_response(:no_content)
+       token_record =
+         Pleroma.UserInviteToken
+         |> Repo.all()
+         |> List.last()
+       assert token_record
+       refute token_record.used
+       notify_email = Config.get([:instance, :notify_email])
+       instance_name = Config.get([:instance, :name])
+       email =
+         Pleroma.Emails.UserEmail.user_invitation_email(
+           admin,
+           token_record,
+           recipient_email
+         )
+       Swoosh.TestAssertions.assert_email_sent(
+         from: {instance_name, notify_email},
+         to: recipient_email,
+         html_body: email.html_body
+       )
+     end
    end
  
    describe "POST /api/pleroma/admin/users/email_invite, with invalid config" do
  
        conn = post(conn, "/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD")
  
-       assert json_response(conn, :internal_server_error)
+       assert json_response(conn, :bad_request) ==
+                "To send invites you need to set the `invites_enabled` option to true."
      end
  
      test "it returns 500 if `registrations_open` is enabled", %{conn: conn} do
  
        conn = post(conn, "/api/pleroma/admin/users/email_invite?email=foo@bar.com&name=JD")
  
-       assert json_response(conn, :internal_server_error)
+       assert json_response(conn, :bad_request) ==
+                "To send invites you need to set the `registrations_open` option to false."
      end
    end
  
      end
  
      test "common config example", %{conn: conn} do
 -      adapter = Application.get_env(:tesla, :adapter)
 -      on_exit(fn -> Application.put_env(:tesla, :adapter, adapter) end)
 -
        conn =
          post(conn, "/api/pleroma/admin/config", %{
            configs: [
                  %{"tuple" => [":seconds_valid", 60]},
                  %{"tuple" => [":path", ""]},
                  %{"tuple" => [":key1", nil]},
 -                %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]},
                  %{"tuple" => [":regex1", "~r/https:\/\/example.com/"]},
                  %{"tuple" => [":regex2", "~r/https:\/\/example.com/u"]},
                  %{"tuple" => [":regex3", "~r/https:\/\/example.com/i"]},
                  %{"tuple" => [":regex4", "~r/https:\/\/example.com/s"]},
                  %{"tuple" => [":name", "Pleroma"]}
                ]
 -            },
 -            %{
 -              "group" => ":tesla",
 -              "key" => ":adapter",
 -              "value" => "Tesla.Adapter.Httpc"
              }
            ]
          })
  
 -      assert Application.get_env(:tesla, :adapter) == Tesla.Adapter.Httpc
        assert Config.get([Pleroma.Captcha.NotReal, :name]) == "Pleroma"
  
        assert json_response(conn, 200) == %{
                       %{"tuple" => [":seconds_valid", 60]},
                       %{"tuple" => [":path", ""]},
                       %{"tuple" => [":key1", nil]},
 -                     %{"tuple" => [":partial_chain", "&:hackney_connect.partial_chain/1"]},
                       %{"tuple" => [":regex1", "~r/https:\\/\\/example.com/"]},
                       %{"tuple" => [":regex2", "~r/https:\\/\\/example.com/u"]},
                       %{"tuple" => [":regex3", "~r/https:\\/\\/example.com/i"]},
                       ":seconds_valid",
                       ":path",
                       ":key1",
 -                     ":partial_chain",
                       ":regex1",
                       ":regex2",
                       ":regex3",
                       ":regex4",
                       ":name"
                     ]
 -                 },
 -                 %{
 -                   "group" => ":tesla",
 -                   "key" => ":adapter",
 -                   "value" => "Tesla.Adapter.Httpc",
 -                   "db" => [":adapter"]
                   }
                 ]
               }