-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
265 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,36 @@ | ||
defmodule RadiatorWeb.Api.OutlineController do | ||
use RadiatorWeb, :controller | ||
|
||
alias Radiator.Accounts | ||
alias Radiator.Outline | ||
|
||
def create(conn, %{"content" => content}) do | ||
{:ok, node} = Outline.create_node(%{"content" => content}) | ||
def create(conn, %{"content" => content, "token" => token}) do | ||
{status_code, body} = | ||
token | ||
|> decode_token() | ||
|> get_user_by_token() | ||
|> create_node(content) | ||
|> get_response() | ||
|
||
conn | ||
|> put_resp_content_type("application/json") | ||
|> send_resp(200, Jason.encode!(%{uuid: node.uuid})) | ||
|> send_resp(status_code, Jason.encode!(body)) | ||
end | ||
|
||
def create(conn, _params) do | ||
conn | ||
|> put_resp_content_type("application/json") | ||
|> send_resp(400, Jason.encode!(%{error: "missing params"})) | ||
end | ||
|
||
defp decode_token(token), do: Base.url_decode64(token, padding: false) | ||
|
||
defp get_user_by_token({:ok, token}), do: Accounts.get_user_by_api_token(token) | ||
defp get_user_by_token(:error), do: {:error, :token} | ||
|
||
defp create_node(nil, _), do: {:error, :user} | ||
defp create_node(user, content), do: Outline.create_node(%{"content" => content}, user) | ||
|
||
defp get_response({:ok, node}), do: {200, %{uuid: node.uuid}} | ||
defp get_response({:error, _}), do: {400, %{error: "params"}} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 24 additions & 3 deletions
27
test/radiator_web/controllers/api/outline_controller_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,43 @@ | ||
defmodule RadiatorWeb.Api.OutlineControllerTest do | ||
use RadiatorWeb.ConnCase, async: true | ||
|
||
import Radiator.AccountsFixtures | ||
|
||
alias Radiator.Accounts | ||
alias Radiator.Outline | ||
|
||
describe "POST /api/v1/outline" do | ||
test "creates a node if content is present", %{conn: conn} do | ||
body = %{"content" => "new node content"} | ||
setup %{conn: conn} do | ||
user = user_fixture() | ||
|
||
token = | ||
user | ||
|> Accounts.generate_user_api_token() | ||
|> Base.url_encode64() | ||
|
||
%{conn: conn, user: user, token: token} | ||
end | ||
|
||
test "creates a node if content is present", %{conn: conn, user: %{id: user_id}, token: token} do | ||
body = %{"content" => "new node content", "token" => token} | ||
conn = post(conn, ~p"/api/v1/outline", body) | ||
|
||
%{"uuid" => uuid} = json_response(conn, 200) | ||
|
||
assert %{content: "new node content"} = Outline.get_node!(uuid) | ||
assert %{content: "new node content", creator_id: ^user_id} = Outline.get_node!(uuid) | ||
end | ||
|
||
test "can't create node when content is missing", %{conn: conn} do | ||
conn = post(conn, ~p"/api/v1/outline", nil) | ||
|
||
assert %{"error" => "missing params"} = json_response(conn, 400) | ||
end | ||
|
||
test "can't create node when token is wrong", %{conn: conn} do | ||
body = %{"content" => "new node content", "token" => "invalid"} | ||
conn = post(conn, ~p"/api/v1/outline", body) | ||
|
||
assert %{"error" => "params"} = json_response(conn, 400) | ||
end | ||
end | ||
end |