From 819db653c18db0ea191421ffb7d21e17d0b262ca Mon Sep 17 00:00:00 2001 From: nelsonic Date: Sun, 15 Jan 2023 07:16:25 +0000 Subject: [PATCH] remove redundant status code assertions in API tests #256 --- api.md | 52 +++++++++---------- .../controllers/api/item_controller_test.exs | 5 -- 2 files changed, 25 insertions(+), 32 deletions(-) diff --git a/api.md b/api.md index fdf7753d..ac5dff45 100644 --- a/api.md +++ b/api.md @@ -1,19 +1,19 @@
-# `REST`ful API integration +# `REST`ful `API` Integration
This guide demonstrates -how to *extend* our MVP `Phoenix` application -so it also acts as an **API** +how to *extend* our MVP `Phoenix` App +so it also acts as an **`API`** returning `JSON` data. -We want our users to securely query -and manipulate their data -and want to ensure all actions -that are performed in the Web API -can also be done through our `RESTful` API +`people` want to securely query +and update their data. +We want to ensure all actions +that are performed in the Web UI +can also be done through our `REST API` *and* `WebSocket API` (for all real-time updates). @@ -21,14 +21,14 @@ Let's get cracking! 🎉 ## 1. Add `/api` scope and pipeline -We want all our API requests -to be made under the `/api` route. -This is easier for us to manage changes to API -that don't create unnecessary regressions to our liveviews. +We want all `API` requests +to be made under the `/api` namespace. +This is easier for us to manage changes to `API` +that don't create unnecessary complexity in the `LiveView` code. Let's start by opening `lib/router.ex` and create a new `:api` pipeline -to be used under `scope "/api"`. +to be used under `scope "/api"`: ```elixir @@ -50,12 +50,12 @@ to be used under `scope "/api"`. ``` We are creating an `:api` pipeline -that will only accepts and returns `json` objects. +that will only accept and return `json` objects. `:fetch_session` is added as a plug because `:authOptional` requires us to do so. Every request that is routed to `/api` -will be piped through both `:api` and `:authOptional` pipelines. +will be piped through both the `:api` and `:authOptional` pipelines. You might have noticed two new controllers: `API.ItemController` and `API.TimerController`. @@ -71,7 +71,7 @@ Before creating our controller, let's define our requirements. We want the API t - edit an `item` We want each endpoint to respond appropriately if any data is invalid, -the response body and status should inform the user what went wrong. +the response body and status should inform the `person` what went wrong. We can leverage changesets to validate the `item` and `timer` and check if it's correctly formatted. @@ -86,13 +86,15 @@ Create two new files: - `test/app_web/api/timer_controller_test.exs` Before implementing, -we recommend giving a look at [`learn-api-design`](https://github.com/dwyl/learn-api-design), -we are going to be using some tips coming from there! +we recommend giving a look at +[`learn-api-design`](https://github.com/dwyl/learn-api-design), +we are going to be using some best practices described there! -We want the API requests to be handled gracefully +We want the `API` requests +to be handled gracefully when an error occurs. -The person using the API -[**should be shown meaningful errors**](https://github.com/dwyl/learn-api-design/blob/revamp/README.md#show-meaningful-errors). +The `person` using the `API` +[**should be shown _meaningful_ errors**](https://github.com/dwyl/learn-api-design/blob/main/README.md#show-meaningful-errors). Therefore, we need to test how our API behaves when invalid attributes are requested and/or an error occurs **and where**. @@ -113,7 +115,6 @@ defmodule AppWeb.API.ItemControllerTest do {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = get(conn, Routes.item_path(conn, :show, item.id)) - assert conn.status == 200 assert json_response(conn, 200)["id"] == item.id assert json_response(conn, 200)["text"] == item.text end @@ -134,7 +135,6 @@ defmodule AppWeb.API.ItemControllerTest do test "a valid item", %{conn: conn} do conn = post(conn, Routes.item_path(conn, :create, @create_attrs)) - assert conn.status == 200 assert json_response(conn, 200)["text"] == Map.get(@create_attrs, "text") assert json_response(conn, 200)["status"] == @@ -147,7 +147,6 @@ defmodule AppWeb.API.ItemControllerTest do test "an invalid item", %{conn: conn} do conn = post(conn, Routes.item_path(conn, :create, @invalid_attrs)) - assert conn.status == 400 assert length(json_response(conn, 400)["errors"]["text"]) > 0 end end @@ -157,7 +156,6 @@ defmodule AppWeb.API.ItemControllerTest do {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = put(conn, Routes.item_path(conn, :update, item.id, @update_attrs)) - assert conn.status == 200 assert json_response(conn, 200)["text"] == Map.get(@update_attrs, :text) end @@ -165,14 +163,14 @@ defmodule AppWeb.API.ItemControllerTest do {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = put(conn, Routes.item_path(conn, :update, item.id, @invalid_attrs)) - assert conn.status == 400 assert length(json_response(conn, 400)["errors"]["text"]) > 0 end end end ``` -In `/item`, users will be able to +In `/item`, +a `person` will be able to **create**, **update** or **query a single item**. In each test we are testing successful scenarios (the [Happy Path](https://en.wikipedia.org/wiki/Happy_path)), diff --git a/test/app_web/controllers/api/item_controller_test.exs b/test/app_web/controllers/api/item_controller_test.exs index 5dcf0eb5..9d6df513 100644 --- a/test/app_web/controllers/api/item_controller_test.exs +++ b/test/app_web/controllers/api/item_controller_test.exs @@ -11,7 +11,6 @@ defmodule AppWeb.API.ItemControllerTest do {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = get(conn, Routes.item_path(conn, :show, item.id)) - assert conn.status == 200 assert json_response(conn, 200)["id"] == item.id assert json_response(conn, 200)["text"] == item.text end @@ -32,7 +31,6 @@ defmodule AppWeb.API.ItemControllerTest do test "a valid item", %{conn: conn} do conn = post(conn, Routes.item_path(conn, :create, @create_attrs)) - assert conn.status == 200 assert json_response(conn, 200)["text"] == Map.get(@create_attrs, "text") assert json_response(conn, 200)["status"] == @@ -45,7 +43,6 @@ defmodule AppWeb.API.ItemControllerTest do test "an invalid item", %{conn: conn} do conn = post(conn, Routes.item_path(conn, :create, @invalid_attrs)) - assert conn.status == 400 assert length(json_response(conn, 400)["errors"]["text"]) > 0 end end @@ -55,7 +52,6 @@ defmodule AppWeb.API.ItemControllerTest do {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = put(conn, Routes.item_path(conn, :update, item.id, @update_attrs)) - assert conn.status == 200 assert json_response(conn, 200)["text"] == Map.get(@update_attrs, :text) end @@ -63,7 +59,6 @@ defmodule AppWeb.API.ItemControllerTest do {:ok, %{model: item, version: _version}} = Item.create_item(@create_attrs) conn = put(conn, Routes.item_path(conn, :update, item.id, @invalid_attrs)) - assert conn.status == 400 assert length(json_response(conn, 400)["errors"]["text"]) > 0 end end