Add user registration to TwAPI.
authorRoger Braun <roger@rogerbraun.net>
Sun, 16 Apr 2017 08:25:27 +0000 (10:25 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sun, 16 Apr 2017 08:25:27 +0000 (10:25 +0200)
lib/pleroma/web/twitter_api/twitter_api.ex
test/web/twitter_api/twitter_api_test.exs

index 58f8bc1d3b7a14a715c9d67fc16535f85d7f350c..557faf4543683d663e5fe4ba9c922d9f349f8c71 100644 (file)
@@ -1,7 +1,7 @@
 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   alias Pleroma.{User, Activity, Repo, Object}
   alias Pleroma.Web.ActivityPub.ActivityPub
-  alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
+  alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
 
   import Ecto.Query
 
@@ -227,6 +227,28 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     end
   end
 
+  def register_user(params) do
+    params = %{
+      nickname: params["nickname"],
+      name: params["fullname"],
+      bio: params["bio"],
+      email: params["email"],
+      password: params["password"],
+      password_confirmation: params["confirm"]
+    }
+
+    changeset = User.register_changeset(%User{}, params)
+
+    with {:ok, user} <- Repo.insert(changeset) do
+      {:ok, UserRepresenter.to_map(user)}
+    else
+      {:error, changeset} ->
+        errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} -> msg end)
+        |> Poison.encode!
+        {:error, %{error: errors}}
+    end
+  end
+
   defp activities_to_statuses(activities, opts) do
     Enum.map(activities, fn(activity) ->
       activity_to_status(activity, opts)
index 71b0d8b129a8f0c4c815f7f2e186dc1742420125..eb5f8ec26ece74e3167369d4462989b7658da180 100644 (file)
@@ -3,7 +3,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
   alias Pleroma.Builders.{UserBuilder, ActivityBuilder}
   alias Pleroma.Web.TwitterAPI.TwitterAPI
   alias Pleroma.{Activity, User, Object, Repo}
-  alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
+  alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
   alias Pleroma.Web.ActivityPub.ActivityPub
 
   import Pleroma.Factory
@@ -220,6 +220,37 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
   end
 
+  test "it registers a new user and returns the user." do
+    data = %{
+      "nickname" => "lain",
+      "email" => "lain@wired.jp",
+      "fullname" => "lain iwakura",
+      "bio" => "close the world.",
+      "password" => "bear",
+      "confirm" => "bear"
+    }
+
+    {:ok, user} = TwitterAPI.register_user(data)
+
+    fetched_user = Repo.get_by(User, nickname: "lain")
+    assert user == UserRepresenter.to_map(fetched_user)
+  end
+
+  test "it returns the error on registration problems" do
+    data = %{
+      "nickname" => "lain",
+      "email" => "lain@wired.jp",
+      "fullname" => "lain iwakura",
+      "bio" => "close the world.",
+      "password" => "bear"
+    }
+
+    {:error, error_object} = TwitterAPI.register_user(data)
+
+    assert is_binary(error_object[:error])
+    refute Repo.get_by(User, nickname: "lain")
+  end
+
   setup do
     Supervisor.terminate_child(Pleroma.Supervisor, ConCache)
     Supervisor.restart_child(Pleroma.Supervisor, ConCache)