From 5dac3727f10d65eea284da56fe4b0db5cab53f1f Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 16 Apr 2017 11:01:24 +0200 Subject: [PATCH] Add registration to the TwAPI controller. --- lib/pleroma/web/router.ex | 1 + .../web/twitter_api/twitter_api_controller.ex | 11 ++++++ .../twitter_api_controller_test.exs | 38 +++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 7bb3c9763..2749be5e9 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -26,6 +26,7 @@ def user_fetcher(username) 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 diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index a4d8a1d14..fa26bb3e9 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -131,6 +131,17 @@ def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) 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") diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 0eb1b60c5..3bc4eb700 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -216,6 +216,44 @@ test "with credentials", %{conn: conn, user: current_user} 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]