From 57bbc1f88844fe0ff4dda35619850454e39212d0 Mon Sep 17 00:00:00 2001 From: Nick Kezhaya Date: Mon, 9 Sep 2019 12:36:50 -0500 Subject: [PATCH] 404 on customer not found --- lib/stripe_mock/repo.ex | 7 +++++-- .../controllers/customer_controller.ex | 5 +++-- .../controllers/customer_controller_test.exs | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) diff --git a/lib/stripe_mock/repo.ex b/lib/stripe_mock/repo.ex index e9026f1..322e410 100644 --- a/lib/stripe_mock/repo.ex +++ b/lib/stripe_mock/repo.ex @@ -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 diff --git a/lib/stripe_mock_web/controllers/customer_controller.ex b/lib/stripe_mock_web/controllers/customer_controller.ex index a52e073..794506d 100644 --- a/lib/stripe_mock_web/controllers/customer_controller.ex +++ b/lib/stripe_mock_web/controllers/customer_controller.ex @@ -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 diff --git a/test/stripe_mock_web/controllers/customer_controller_test.exs b/test/stripe_mock_web/controllers/customer_controller_test.exs index f0c4f83..e592bb4 100644 --- a/test/stripe_mock_web/controllers/customer_controller_test.exs +++ b/test/stripe_mock_web/controllers/customer_controller_test.exs @@ -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)