From 959c44b8f8db1e3b58c432645704fb15031030ef Mon Sep 17 00:00:00 2001 From: Mitchell Gerber Date: Sat, 6 Jan 2018 12:25:50 -0600 Subject: [PATCH] end point tests figured out --- config/config.secret.exs.template | 1 + config/test.exs | 5 ++ lib/myapp/auth/pipeline.ex | 6 +-- .../controllers/category_controller.ex | 2 - test/myapp/data/user_test.exs | 13 +++-- .../controllers/thread_controller_test.exs | 51 +++++++++++++++++++ test/support/data/test_helpers.ex | 2 + 7 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 test/myapp_web/controllers/thread_controller_test.exs diff --git a/config/config.secret.exs.template b/config/config.secret.exs.template index 89f20ee..28b56d3 100644 --- a/config/config.secret.exs.template +++ b/config/config.secret.exs.template @@ -1,4 +1,5 @@ # secret dev configurations - api keys go here +use Mix.Config config :myapp, bnet_client_id: "", diff --git a/config/test.exs b/config/test.exs index 51e26f8..1d0e1c5 100644 --- a/config/test.exs +++ b/config/test.exs @@ -16,4 +16,9 @@ config :myapp, MyApp.Repo, password: "postgres", database: "myapp_test", hostname: "localhost", + template: "template0", pool: Ecto.Adapters.SQL.Sandbox + +config :myapp, MyApp.Guardian, + issuer: "myapp", + secret_key: "secret" diff --git a/lib/myapp/auth/pipeline.ex b/lib/myapp/auth/pipeline.ex index 9320699..1ea9c89 100644 --- a/lib/myapp/auth/pipeline.ex +++ b/lib/myapp/auth/pipeline.ex @@ -1,5 +1,5 @@ defmodule MyApp.Guardian.Auth.Pipeline.User do - use Guardian.Plug.Pipeline, otp_app: :MyApp, + use Guardian.Plug.Pipeline, otp_app: :myapp, module: MyApp.Guardian, error_handler: MyApp.Auth.ErrorHandler @@ -14,7 +14,7 @@ defmodule MyApp.Guardian.Auth.Pipeline.User do end defmodule MyApp.Guardian.Auth.Pipeline.Mod do - use Guardian.Plug.Pipeline, otp_app: :MyApp, + use Guardian.Plug.Pipeline, otp_app: :myapp, module: MyApp.Guardian, error_handler: MyApp.Auth.ErrorHandler @@ -28,7 +28,7 @@ defmodule MyApp.Guardian.Auth.Pipeline.Mod do end defmodule MyApp.Guardian.Auth.Pipeline.Admin do - use Guardian.Plug.Pipeline, otp_app: :MyApp, + use Guardian.Plug.Pipeline, otp_app: :myapp, module: MyApp.Guardian, error_handler: MyApp.Auth.ErrorHandler diff --git a/lib/myapp_web/controllers/category_controller.ex b/lib/myapp_web/controllers/category_controller.ex index e681965..373e4e3 100644 --- a/lib/myapp_web/controllers/category_controller.ex +++ b/lib/myapp_web/controllers/category_controller.ex @@ -5,9 +5,7 @@ defmodule MyAppWeb.CategoryController do @spec get_collection(map, map) :: any def get_collection(conn, _params) do - output = Data.Category.get_categories() - conn |> put_status(200) |> Response.json(output) diff --git a/test/myapp/data/user_test.exs b/test/myapp/data/user_test.exs index 78222d9..2d88639 100644 --- a/test/myapp/data/user_test.exs +++ b/test/myapp/data/user_test.exs @@ -5,13 +5,12 @@ defmodule MyApp.Data.UserTest do test "user is inserted into database" do {:ok, user} = new_user() - assert user == %{ - access_token: "test_token", - battle_net_id: 1, - battletag: "mgerb42", - id: user.id, - permissions: "user", - } + assert user.access_token == "test_token" + assert user.battle_net_id == 1 + assert user.battletag == "mgerb42" + assert user.id == user.id + assert user.permissions == "user" + assert user.token =~ ~r/(?).(?).(?)/ end test "user's battletag is updated" do diff --git a/test/myapp_web/controllers/thread_controller_test.exs b/test/myapp_web/controllers/thread_controller_test.exs new file mode 100644 index 0000000..eb01afe --- /dev/null +++ b/test/myapp_web/controllers/thread_controller_test.exs @@ -0,0 +1,51 @@ +defmodule MyAppWeb.ThreadControllerTest do + use MyAppWeb.ConnCase, async: true + import MyAppWeb.ThreadController + import MyApp.Data.TestHelpers + + test "insert new thread should fail" do + {:ok, user} = new_user() + new_conn = build_conn() + |> put_req_header("authorization", "Bearer " <> user.token) + + conn = post(new_conn, "/api/thread") + body = conn |> response(400) |> Poison.decode! + + assert body["error"]["message"] == [ + %{"title" => "can't be blank"}, + %{"category_id" => "can't be blank"}, + %{"content" => "can't be blank"}, + ] + + conn = post(new_conn, "/api/thread", %{"title" => "t"}) + body = conn |> response(400) |> Poison.decode! + assert body["error"]["message"] == [ + %{"category_id" => "can't be blank"}, + %{"content" => "can't be blank"}, + ] + + conn = post(new_conn, "/api/thread", %{"title" => "t", "category_id" => 1}) + body = conn |> response(400) |> Poison.decode! + assert body["error"]["message"] == [%{"content" => "can't be blank"}] + + conn = post(new_conn, "/api/thread", %{"title" => "t", "category_id" => 100000, "content" => "t"}) + body = conn |> response(400) |> Poison.decode! + assert body["error"]["message"] == [%{"category_id" => "does not exist"}] + end + + test "insert new thread should succeed" do + {:ok, user} = new_user() + new_conn = build_conn() + |> put_req_header("authorization", "Bearer " <> user.token) + conn = post(new_conn, "/api/thread", %{"title" => "t", "category_id" => 1, "content" => "t"}) + body = conn |> response(200) |> Poison.decode! + + data = body["data"] + assert data["user_id"] == user.id + assert data["category_id"] == 1 + assert data["title"] == "t" + assert data["content"] == "t" + end + + # TODO: update thread / delete thread +end diff --git a/test/support/data/test_helpers.ex b/test/support/data/test_helpers.ex index 7f78b3d..9c8faf9 100644 --- a/test/support/data/test_helpers.ex +++ b/test/support/data/test_helpers.ex @@ -1,9 +1,11 @@ defmodule MyApp.Data.TestHelpers do import MyApp.Data.User + alias MyApp.Guardian.Auth @spec new_user() :: {:ok, map} def new_user() do upsert_user(%{"battle_net_id" => 1, "battletag" => "mgerb42", "access_token" => "test_token"}) + |> Auth.Token.add_token_and_map_claims end @spec atom_key_to_string(map) :: map