From e07d85319250c14690e2a62bfe9a202f0143f112 Mon Sep 17 00:00:00 2001 From: SimonLab Date: Wed, 22 Apr 2020 16:09:14 +0100 Subject: [PATCH] test controllers, #14 --- lib/plugs/validate_token.ex | 11 ++++++---- .../controllers/capture_controller_test.exs | 20 ++++++++++++++++++ .../controllers/tag_controller_test.exs | 21 +++++++++++++++++++ .../controllers/timer_controller_test.exs | 0 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 test/app_api_web/controllers/capture_controller_test.exs create mode 100644 test/app_api_web/controllers/tag_controller_test.exs create mode 100644 test/app_api_web/controllers/timer_controller_test.exs diff --git a/lib/plugs/validate_token.ex b/lib/plugs/validate_token.ex index 7e525fe..e802553 100644 --- a/lib/plugs/validate_token.ex +++ b/lib/plugs/validate_token.ex @@ -9,9 +9,13 @@ defmodule AppApi.Plugs.ValidateToken do def init(opts), do: opts def call(conn, _) do - case AppApiWeb.AuthServiceApi.get_person_information(conn) do - {:error, _} -> unauthorized(conn) - {:ok, person} -> assign(conn, :person, person) + if Mix.env() == :test do + assign(conn, :person, %{email: "email", name: "name", id_person: 42}) + else + case AppApiWeb.AuthServiceApi.get_person_information(conn) do + {:error, _} -> unauthorized(conn) + {:ok, person} -> assign(conn, :person, person) + end end end @@ -21,5 +25,4 @@ defmodule AppApi.Plugs.ValidateToken do |> send_resp(401, "unauthorized") |> halt() end - end diff --git a/test/app_api_web/controllers/capture_controller_test.exs b/test/app_api_web/controllers/capture_controller_test.exs new file mode 100644 index 0000000..7f4732a --- /dev/null +++ b/test/app_api_web/controllers/capture_controller_test.exs @@ -0,0 +1,20 @@ +defmodule AppApi.CaptureControllerTest do + use AppApiWeb.ConnCase + alias AppApiWeb.CaptureController + + describe "test captures endpoint" do + test "index and show and create endpoints", %{conn: conn} do + conn = post(conn, Routes.capture_path(conn, :create, text: "text capture")) + create_response = json_response(conn, 200) + assert create_response["data"]["text"] == "text capture" + + conn = get(conn, Routes.capture_path(conn, :index)) + list_captures = json_response(conn, 200) + assert Enum.count(list_captures) == 1 + + conn = get(conn, Routes.capture_path(conn, :show, create_response["data"]["capture_id"])) + get_capture_response = json_response(conn, 200) + assert get_capture_response["data"]["text"] == "text capture" + end + end +end diff --git a/test/app_api_web/controllers/tag_controller_test.exs b/test/app_api_web/controllers/tag_controller_test.exs new file mode 100644 index 0000000..41b5096 --- /dev/null +++ b/test/app_api_web/controllers/tag_controller_test.exs @@ -0,0 +1,21 @@ +defmodule AppApi.TagControllerTest do + use AppApiWeb.ConnCase + alias AppApi.Tags + + @valid_attrs %{id_person: 42, text: "tag1"} + + def tag_fixture(attrs \\ %{}) do + attrs + |> Enum.into(@valid_attrs) + |> Tags.create_tag() + end + + describe "test tag endoints" do + test "index endpoint", %{conn: conn} do + tag_fixture() + conn = get(conn, Routes.tag_path(conn, :index)) + response = get_response = json_response(conn, 200) + assert Enum.count(response["data"]) == 1 + end + end +end diff --git a/test/app_api_web/controllers/timer_controller_test.exs b/test/app_api_web/controllers/timer_controller_test.exs new file mode 100644 index 0000000..e69de29