diff --git a/coveralls.json b/coveralls.json index 8785677..9778e26 100644 --- a/coveralls.json +++ b/coveralls.json @@ -1,9 +1,9 @@ { - "coverage_options": { - "minimum_coverage": 100 - }, - "skip_files": [ - "test/", - "lib/app_api.ex" - ] - } \ No newline at end of file + "coverage_options": { + "minimum_coverage": 80 + }, + "skip_files": [ + "test/", + "lib/app_api.ex" + ] +} \ No newline at end of file diff --git a/test/app_api_web/controllers/login_controller_test.exs b/test/app_api_web/controllers/login_controller_test.exs new file mode 100644 index 0000000..9f63dd5 --- /dev/null +++ b/test/app_api_web/controllers/login_controller_test.exs @@ -0,0 +1,10 @@ +defmodule AppApi.LoginControllerTest do + use AppApiWeb.ConnCase + + describe "test login controller" do + test "index page redirects to auth service", %{conn: conn} do + conn = get(conn, Routes.login_path(conn, :index)) + assert conn.status == 302 + end + end +end diff --git a/test/app_api_web/controllers/person_controller_test.exs b/test/app_api_web/controllers/person_controller_test.exs new file mode 100644 index 0000000..47d2411 --- /dev/null +++ b/test/app_api_web/controllers/person_controller_test.exs @@ -0,0 +1,23 @@ +defmodule AppApi.PersonControllerTest do + use AppApiWeb.ConnCase + alias AppApi.Tags + + describe "test person endoints" do + test "attempt to get person info without a jwt returns an unauthorized error", %{conn: conn} do + conn = get(conn, Routes.person_path(conn, :index)) + assert conn.status == 401 + end + + test "attempt to get person without a valid jwt", %{conn: conn} do + conn + + conn = + conn + |> put_req_header("content-type", "application/json") + |> put_req_header("authorization", "Bearer 123") + |> get(Routes.person_path(conn, :index)) + + assert conn.status == 401 + 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 index 41b5096..ee41cea 100644 --- a/test/app_api_web/controllers/tag_controller_test.exs +++ b/test/app_api_web/controllers/tag_controller_test.exs @@ -14,7 +14,7 @@ defmodule AppApi.TagControllerTest do test "index endpoint", %{conn: conn} do tag_fixture() conn = get(conn, Routes.tag_path(conn, :index)) - response = get_response = json_response(conn, 200) + response = json_response(conn, 200) assert Enum.count(response["data"]) == 1 end end diff --git a/test/app_api_web/controllers/timer_controller_test.exs b/test/app_api_web/controllers/timer_controller_test.exs index e69de29..5323f59 100644 --- a/test/app_api_web/controllers/timer_controller_test.exs +++ b/test/app_api_web/controllers/timer_controller_test.exs @@ -0,0 +1,100 @@ +defmodule AppApi.TimerControllerTest do + use AppApiWeb.ConnCase + alias AppApi.Timers + alias AppApi.Captures + + @valid_attrs_capture %{completed: true, id_person: 42, text: "some text", tags: "tag1, tag2"} + @valid_attrs %{started_at: DateTime.utc_now(), stopped_at: nil} + + def capture_fixture(attrs \\ %{}) do + attrs + |> Enum.into(@valid_attrs_capture) + |> Captures.create_capture() + end + + def timer_fixture(attrs \\ %{}) do + capture = capture_fixture() + + timer_attrs = + attrs + |> Enum.into(@valid_attrs) + + {:ok, timer} = Timers.create_timer(capture, timer_attrs) + timer + end + + describe "test timer endoints" do + test "create endpoint", %{conn: conn} do + capture = capture_fixture() + conn = post(conn, Routes.capture_timer_path(conn, :create, capture.id)) + json_response(conn, 200) + + conn = get(conn, Routes.capture_timer_path(conn, :index, capture.id)) + response_timers = json_response(conn, 200) + assert Enum.count(response_timers["data"]) == 1 + end + + test "create endpoint with different id person returns 401", %{conn: conn} do + capture = capture_fixture(%{id_person: 1}) + conn = post(conn, Routes.capture_timer_path(conn, :create, capture.id)) + assert conn.status == 401 + end + + test "update endpoint", %{conn: conn} do + timer = timer_fixture() + + conn = + put( + conn, + Routes.capture_timer_path( + conn, + :update, + timer.capture.id, + timer.id + ), + %{"capture_id" => timer.capture.id, "timer_id" => timer.id, "action" => "stop"} + ) + + response_update = json_response(conn, 200) + refute is_nil(response_update["data"]["stopped_at"]) + end + + test "update timer with wrong action", %{conn: conn} do + timer = timer_fixture() + + conn = + put( + conn, + Routes.capture_timer_path( + conn, + :update, + timer.capture.id, + timer.id + ), + %{"capture_id" => timer.capture.id, "timer_id" => timer.id, "action" => "wrong action"} + ) + + # action not found + assert conn.status == 404 + end + + test "update with wrong catpure returns 401", %{conn: conn} do + timer = timer_fixture() + capture = capture_fixture(%{id_person: 1}) + + conn = + put( + conn, + Routes.capture_timer_path( + conn, + :update, + capture.id, + timer.id + ), + %{"capture_id" => timer.capture.id, "timer_id" => timer.id, "action" => "stop"} + ) + + assert conn.status == 401 + end + end +end diff --git a/test/app_api_web/views/person_view_test.exs b/test/app_api_web/views/person_view_test.exs new file mode 100644 index 0000000..fae6419 --- /dev/null +++ b/test/app_api_web/views/person_view_test.exs @@ -0,0 +1,11 @@ +defmodule AppApiWeb.PersonViewTest do + use AppApiWeb.ConnCase, async: true + + # Bring render/3 and render_to_string/3 for testing custom views + import Phoenix.View + + test "renders index.json" do + person = %{email: "email", name: "name"} + assert render(AppApiWeb.PersonView, "index.json", %{person: person}) == %{data: person} + end +end