make bulk user creation from admin works as a transaction
[akkoma] / test / web / admin_api / admin_api_controller_test.exs
index a0c9fd56fcceb53b20a3ab1c8182380d261a84cb..0199051374cab85b0cb1a7c06f5d384b7b89a5b3 100644 (file)
@@ -36,18 +36,31 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
               "nickname" => "lain",
               "email" => "lain@example.org",
               "password" => "test"
+            },
+            %{
+              "nickname" => "lain2",
+              "email" => "lain2@example.org",
+              "password" => "test"
             }
           ]
         })
 
       assert json_response(conn, 200) == [
                %{
-                 "code" => 201,
+                 "code" => 200,
                  "data" => %{
                    "email" => "lain@example.org",
                    "nickname" => "lain"
                  },
                  "type" => "success"
+               },
+               %{
+                 "code" => 200,
+                 "data" => %{
+                   "email" => "lain2@example.org",
+                   "nickname" => "lain2"
+                 },
+                 "type" => "success"
                }
              ]
     end
@@ -70,7 +83,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
           ]
         })
 
-      assert json_response(conn, 200) == [
+      assert json_response(conn, 409) == [
                %{
                  "code" => 409,
                  "data" => %{
@@ -101,7 +114,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
           ]
         })
 
-      assert json_response(conn, 200) == [
+      assert json_response(conn, 409) == [
                %{
                  "code" => 409,
                  "data" => %{
@@ -113,6 +126,53 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                }
              ]
     end
+
+    test "Multiple user creation works in transaction" 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" => "newuser",
+              "email" => "newuser@pleroma.social",
+              "password" => "test"
+            },
+            %{
+              "nickname" => "lain",
+              "email" => user.email,
+              "password" => "test"
+            }
+          ]
+        })
+
+      assert json_response(conn, 409) == [
+               %{
+                 "code" => 409,
+                 "data" => %{
+                   "email" => user.email,
+                   "nickname" => "lain"
+                 },
+                 "error" => "email has already been taken",
+                 "type" => "error"
+               },
+               %{
+                 "code" => 409,
+                 "data" => %{
+                   "email" => "newuser@pleroma.social",
+                   "nickname" => "newuser"
+                 },
+                 "error" => "",
+                 "type" => "error"
+               }
+             ]
+
+      assert User.get_by_nickname("newuser") === nil
+    end
   end
 
   describe "/api/pleroma/admin/users/:nickname" do