Skip to content

Commit

Permalink
404 on customer not found
Browse files Browse the repository at this point in the history
  • Loading branch information
nkezhaya committed Sep 9, 2019
1 parent e73f8aa commit 57bbc1f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
7 changes: 5 additions & 2 deletions lib/stripe_mock/repo.ex
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@ defmodule StripeMock.Repo do

def get!(schema, id) do
case GenServer.call(pid(), {:get, schema, id}) do
nil -> raise "No #{schema} found with id #{id}."
record -> record
nil ->
raise Ecto.NoResultsError, message: "No #{schema} found with id #{id}.", queryable: schema

record ->
record
end
end

Expand Down
5 changes: 3 additions & 2 deletions lib/stripe_mock_web/controllers/customer_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ defmodule StripeMockWeb.CustomerController do
end

def show(conn, %{"id" => id}) do
customer = API.get_customer!(id)
render(conn, "show.json", customer: customer)
with {:ok, customer} <- API.get_customer(id) do
render(conn, "show.json", customer: customer)
end
end

def update(conn, %{"id" => id} = customer_params) do
Expand Down
14 changes: 14 additions & 0 deletions test/stripe_mock_web/controllers/customer_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ defmodule StripeMockWeb.CustomerControllerTest do
end
end

describe "show" do
setup :create_customer

test "renders customer data", %{conn: conn, customer: customer} do
conn = get(conn, Routes.customer_path(conn, :show, customer.id))
assert %{"id" => id} = json_response(conn, 200)
end

test "renders 404 on not found", %{conn: conn} do
conn = get(conn, Routes.customer_path(conn, :show, "invalid_id"))
assert %{} = json_response(conn, 404)
end
end

describe "create customer" do
test "renders customer when data is valid", %{conn: conn} do
conn = post(conn, Routes.customer_path(conn, :create), @create_attrs)
Expand Down

0 comments on commit 57bbc1f

Please sign in to comment.