Skip to content

Commit

Permalink
Fix dialyzer and test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
doughsay committed Feb 16, 2019
1 parent 2814ae3 commit 102355e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
3 changes: 1 addition & 2 deletions lib/tipalti/ipn/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,7 @@ defmodule Tipalti.IPN.Router do
end

defp handle_event(_, event) do
Logger.warn("[Tipalti IPN] Invalid event received: #{inspect(event)}")

:ok = Logger.warn("[Tipalti IPN] Invalid event received: #{inspect(event)}")
:ok
end

Expand Down
35 changes: 26 additions & 9 deletions test/tipalti/ipn/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Tipalti.IPN.RouterTest do

defmodule SuccessfulHandler do
def call(event) do
assert event == %{"foo" => "bar", "bar" => "baz"}
assert event == %{"bill_refcode" => "pi_12345", "bill_status" => "Paid", "type" => "bill_updated"}
:ok
end
end
Expand All @@ -29,11 +29,14 @@ defmodule Tipalti.IPN.RouterTest do
on "bill_updated", FailingHandler
end

@good_event "type=bill_updated&bill_refcode=pi_12345&bill_status=Paid"
@bad_event "type=foo"

test "handles successful calls" do
expect(BodyReaderMock, :read_body, fn conn, opts -> Plug.Conn.read_body(conn, opts) end)
expect(IPNClientMock, :ack, fn _ -> :ok end)

conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", "foo=bar&bar=baz")
conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", @good_event)
conn = SuccessfulRouter.call(conn, [])

assert conn.resp_body == "OK"
Expand All @@ -44,7 +47,7 @@ defmodule Tipalti.IPN.RouterTest do
expect(BodyReaderMock, :read_body, fn conn, opts -> Plug.Conn.read_body(conn, opts) end)
expect(IPNClientMock, :ack, fn _ -> :ok end)

conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", "foo=bar&bar=baz")
conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", @good_event)

assert_raise RuntimeError, "Unable to process IPN: {:error, :something_bad}", fn ->
FailingRouter.call(conn, [])
Expand All @@ -55,7 +58,7 @@ defmodule Tipalti.IPN.RouterTest do
expect(BodyReaderMock, :read_body, fn conn, opts -> Plug.Conn.read_body(conn, opts) end)
expect(IPNClientMock, :ack, fn _ -> {:error, :unknown} end)

conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", "foo=bar&bar=baz")
conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", @good_event)

assert_raise RuntimeError, "Unable to ack IPN: {:error, :unknown}", fn ->
SuccessfulRouter.call(conn, [])
Expand All @@ -66,21 +69,35 @@ defmodule Tipalti.IPN.RouterTest do
expect(BodyReaderMock, :read_body, fn conn, opts -> Plug.Conn.read_body(conn, opts) end)
expect(IPNClientMock, :ack, fn _ -> {:error, :bad_ipn} end)

conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", "foo=bar&bar=baz")
conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", @good_event)

assert capture_log(fn ->
conn = SuccessfulRouter.call(conn, [])

assert conn.resp_body == "OK"
assert conn.status == 200
end) =~ "Invalid IPN received"
end

test "logs a warning if an unknown event type is received" do
expect(BodyReaderMock, :read_body, fn conn, opts -> Plug.Conn.read_body(conn, opts) end)
expect(IPNClientMock, :ack, fn _ -> :ok end)

conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", @bad_event)

assert capture_log(fn ->
conn = SuccessfulRouter.call(conn, [])

assert conn.resp_body == "OK"
assert conn.status == 200
end) =~ "[warn] Invalid IPN received"
end) =~ "Invalid event received"
end

test "raises BadRequestError on bad body" do
expect(BodyReaderMock, :read_body, fn _conn, _opts -> {:error, :invalid_body} end)
expect(IPNClientMock, :ack, fn _ -> :ok end)

conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", "foo=bar&bar=baz")
conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", @good_event)

assert_raise Plug.BadRequestError, fn ->
SuccessfulRouter.call(conn, [])
Expand All @@ -91,7 +108,7 @@ defmodule Tipalti.IPN.RouterTest do
expect(BodyReaderMock, :read_body, fn conn, _opts -> {:more, "", conn} end)
expect(IPNClientMock, :ack, fn _ -> :ok end)

conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", "foo=bar&bar=baz")
conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", @good_event)

assert_raise Plug.BadRequestError, fn ->
SuccessfulRouter.call(conn, [])
Expand All @@ -102,7 +119,7 @@ defmodule Tipalti.IPN.RouterTest do
expect(BodyReaderMock, :read_body, fn _conn, _opts -> {:error, :timeout} end)
expect(IPNClientMock, :ack, fn _ -> :ok end)

conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", "foo=bar&bar=baz")
conn = Plug.Adapters.Test.Conn.conn(%Plug.Conn{}, :post, "/events/bill_updated", @good_event)

assert_raise Plug.TimeoutError, fn ->
SuccessfulRouter.call(conn, [])
Expand Down

0 comments on commit 102355e

Please sign in to comment.