Skip to content

Commit

Permalink
test controllers, #14
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonLab committed Apr 22, 2020
1 parent 90bd161 commit e07d853
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/plugs/validate_token.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -21,5 +25,4 @@ defmodule AppApi.Plugs.ValidateToken do
|> send_resp(401, "unauthorized")
|> halt()
end

end
20 changes: 20 additions & 0 deletions test/app_api_web/controllers/capture_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions test/app_api_web/controllers/tag_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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
Empty file.

0 comments on commit e07d853

Please sign in to comment.