user creation admin api will create multiple users
authorSachin Joshi <satchin.joshi@gmail.com>
Fri, 17 May 2019 06:35:31 +0000 (12:20 +0545)
committerSachin Joshi <satchin.joshi@gmail.com>
Fri, 17 May 2019 06:36:58 +0000 (12:21 +0545)
CHANGELOG.md
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/admin_api/views/account_view.ex
lib/pleroma/web/router.ex
test/web/admin_api/admin_api_controller_test.exs

index b0e8492857fae3d1d5e92eed816db9cd557bb8a7..5ee853c7698a782b9a9e328faf2801cada205c5c 100644 (file)
@@ -46,6 +46,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Configuration: Added `extra_cookie_attrs` for setting non-standard cookie attributes. Defaults to ["SameSite=Lax"] so that remote follows work.
 - Timelines: Messages involving people you have blocked will be excluded from the timeline in all cases instead of just repeats.
 - Admin API: Move the user related API to `api/pleroma/admin/users`
+- Admin API: `POST /api/pleroma/admin/users` will take list of users
 - Pleroma API: Support for emoji tags in `/api/pleroma/emoji` resulting in a breaking API change
 - Mastodon API: Support for `exclude_types`, `limit` and `min_id` in `/api/v1/notifications`
 - Mastodon API: Add `languages` and `registrations` to `/api/v1/instance`
index e00b33aba698585f43adc1254096231ac6180afe..6048ed35b70cfbefbfae88319c375eea633b51c5 100644 (file)
@@ -46,24 +46,31 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
     |> json("ok")
   end
 
-  def user_create(
-        conn,
-        %{"nickname" => nickname, "email" => email, "password" => password}
-      ) do
-    user_data = %{
-      nickname: nickname,
-      name: nickname,
-      email: email,
-      password: password,
-      password_confirmation: password,
-      bio: "."
-    }
+  def users_create(conn, %{"users" => users}) do
+    result =
+      Enum.map(users, fn %{"nickname" => nickname, "email" => email, "password" => password} ->
+        user_data = %{
+          nickname: nickname,
+          name: nickname,
+          email: email,
+          password: password,
+          password_confirmation: password,
+          bio: "."
+        }
+
+        changeset = User.register_changeset(%User{}, user_data, need_confirmation: false)
+
+        case User.register(changeset) do
+          {:ok, user} ->
+            AccountView.render("created.json", %{user: user})
 
-    changeset = User.register_changeset(%User{}, user_data, need_confirmation: false)
-    {:ok, user} = User.register(changeset)
+          {:error, changeset} ->
+            AccountView.render("create-error.json", %{changeset: changeset})
+        end
+      end)
 
     conn
-    |> json(user.nickname)
+    |> json(result)
   end
 
   def user_show(conn, %{"nickname" => nickname}) do
index 28bb667d84d3a4ad39ecdc479a160dd5fe34e681..e1825c5f1401111aa58e24682dc1f1dbc57ac79e 100644 (file)
@@ -44,4 +44,50 @@ defmodule Pleroma.Web.AdminAPI.AccountView do
       invites: render_many(invites, AccountView, "invite.json", as: :invite)
     }
   end
+
+  def render("created.json", %{user: user}) do
+    %{
+      type: "success",
+      code: 201,
+      data: %{
+        nickname: user.nickname,
+        email: user.email
+      }
+    }
+  end
+
+  def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
+    %{
+      type: "error",
+      code: 409,
+      error: parse_error(errors),
+      data: %{
+        nickname: Map.get(changes, :nickname),
+        email: Map.get(changes, :email)
+      }
+    }
+  end
+
+  defp parse_error([]), do: ""
+
+  defp parse_error(errors) do
+    ## when nickname is duplicate ap_id constraint error is raised
+    nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
+    email_error = Keyword.get(errors, :email)
+    password_error = Keyword.get(errors, :password)
+
+    cond do
+      nickname_error ->
+        "nickname #{elem(nickname_error, 0)}"
+
+      email_error ->
+        "email #{elem(email_error, 0)}"
+
+      password_error ->
+        "password #{elem(password_error, 0)}"
+
+      true ->
+        ""
+    end
+  end
 end
index 7fef82f82a3eb613d37e6b63d321155b91fe5079..bbc2fda9bea670f557aa7ff0597c65518cc91af6 100644 (file)
@@ -156,7 +156,7 @@ defmodule Pleroma.Web.Router do
     post("/user", AdminAPIController, :user_create)
 
     delete("/users", AdminAPIController, :user_delete)
-    post("/users", AdminAPIController, :user_create)
+    post("/users", AdminAPIController, :users_create)
     patch("/users/:nickname/toggle_activation", AdminAPIController, :user_toggle_activation)
     put("/users/tag", AdminAPIController, :tag_users)
     delete("/users/tag", AdminAPIController, :untag_users)
index 6c1897b5a9354336d00fe2df857abf0a68ff0331..a0c9fd56fcceb53b20a3ab1c8182380d261a84cb 100644 (file)
@@ -31,12 +31,87 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
         |> assign(:user, admin)
         |> put_req_header("accept", "application/json")
         |> post("/api/pleroma/admin/users", %{
-          "nickname" => "lain",
-          "email" => "lain@example.org",
-          "password" => "test"
+          "users" => [
+            %{
+              "nickname" => "lain",
+              "email" => "lain@example.org",
+              "password" => "test"
+            }
+          ]
         })
 
-      assert json_response(conn, 200) == "lain"
+      assert json_response(conn, 200) == [
+               %{
+                 "code" => 201,
+                 "data" => %{
+                   "email" => "lain@example.org",
+                   "nickname" => "lain"
+                 },
+                 "type" => "success"
+               }
+             ]
+    end
+
+    test "Cannot create user with exisiting email" do
+      admin = insert(:user, info: %{is_admin: true})
+      user = insert(:user)
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> put_req_header("accept", "application/json")
+        |> post("/api/pleroma/admin/users", %{
+          "users" => [
+            %{
+              "nickname" => "lain",
+              "email" => user.email,
+              "password" => "test"
+            }
+          ]
+        })
+
+      assert json_response(conn, 200) == [
+               %{
+                 "code" => 409,
+                 "data" => %{
+                   "email" => user.email,
+                   "nickname" => "lain"
+                 },
+                 "error" => "email has already been taken",
+                 "type" => "error"
+               }
+             ]
+    end
+
+    test "Cannot create user with exisiting nickname" do
+      admin = insert(:user, info: %{is_admin: true})
+      user = insert(:user)
+
+      conn =
+        build_conn()
+        |> assign(:user, admin)
+        |> put_req_header("accept", "application/json")
+        |> post("/api/pleroma/admin/users", %{
+          "users" => [
+            %{
+              "nickname" => user.nickname,
+              "email" => "someuser@plerama.social",
+              "password" => "test"
+            }
+          ]
+        })
+
+      assert json_response(conn, 200) == [
+               %{
+                 "code" => 409,
+                 "data" => %{
+                   "email" => "someuser@plerama.social",
+                   "nickname" => user.nickname
+                 },
+                 "error" => "nickname has already been taken",
+                 "type" => "error"
+               }
+             ]
     end
   end