Add registration to the TwAPI controller.
authorRoger Braun <roger@rogerbraun.net>
Sun, 16 Apr 2017 09:01:24 +0000 (11:01 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sun, 16 Apr 2017 09:01:24 +0000 (11:01 +0200)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_controller_test.exs

index 7bb3c9763e79ae8c71394ac15cb679fd2f8aef04..2749be5e90bfe0965cdf4a4c158e28417fbc6df8 100644 (file)
@@ -26,6 +26,7 @@ defmodule Pleroma.Web.Router do
     get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
     get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
     get "/statusnet/config", TwitterAPI.Controller, :config
+    post "/account/register", TwitterAPI.Controller, :register
   end
 
   scope "/api", Pleroma.Web do
index a4d8a1d14ee44e2067c8a252313b7a513069bbcc..fa26bb3e9f5c3f8ffa2818ff677e01c953d1c9f8 100644 (file)
@@ -131,6 +131,17 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> json_reply(200, response)
   end
 
+  def register(conn, params) do
+    with {:ok, user} <- TwitterAPI.register_user(params) do
+      conn
+      |> json_reply(200, Poison.encode!(user))
+    else
+      {:error, errors} ->
+      conn
+      |> json_reply(400, Poison.encode!(errors))
+    end
+  end
+
   defp json_reply(conn, status, json) do
     conn
     |> put_resp_content_type("application/json")
index 0eb1b60c528dd3be3d89f8ef76ebf21e85f6cb35..3bc4eb700a33073df31cbff3cf0b18f69eb2b1c8 100644 (file)
@@ -216,6 +216,44 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "POST /api/account/register" do
+    test "it creates a new user", %{conn: conn} do
+      data = %{
+        "nickname" => "lain",
+        "email" => "lain@wired.jp",
+        "fullname" => "lain iwakura",
+        "bio" => "close the world.",
+        "password" => "bear",
+        "confirm" => "bear"
+      }
+
+      conn = conn
+      |> post("/api/account/register", data)
+
+      user = json_response(conn, 200)
+
+      fetched_user = Repo.get_by(User, nickname: "lain")
+      assert user == UserRepresenter.to_map(fetched_user)
+    end
+
+    test "it returns errors on a problem", %{conn: conn} do
+      data = %{
+        "email" => "lain@wired.jp",
+        "fullname" => "lain iwakura",
+        "bio" => "close the world.",
+        "password" => "bear",
+        "confirm" => "bear"
+      }
+
+      conn = conn
+      |> post("/api/account/register", data)
+
+      errors = json_response(conn, 400)
+
+      assert is_binary(errors["error"])
+    end
+  end
+
   defp valid_user(_context) do
     { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"})
     [user: user]