From 968644043d1cb13d071bfd1d29a38093471c93c7 Mon Sep 17 00:00:00 2001 From: Amy Schools Date: Thu, 3 Nov 2016 12:07:34 -0700 Subject: [PATCH] first commit --- .../user_skill_controller_test.exs | 98 ++++++------------- 1 file changed, 31 insertions(+), 67 deletions(-) diff --git a/test/controllers/user_skill_controller_test.exs b/test/controllers/user_skill_controller_test.exs index 935193a8d..caf36d598 100644 --- a/test/controllers/user_skill_controller_test.exs +++ b/test/controllers/user_skill_controller_test.exs @@ -1,57 +1,41 @@ defmodule CodeCorps.UserSkillControllerTest do - use CodeCorps.ApiCase - - alias CodeCorps.UserSkill - alias CodeCorps.Repo - - defp build_payload, do: %{ "data" => %{"type" => "user-skill", "attributes" => %{}}} + use CodeCorps.ApiCase, resource_name: :user_skill describe "index" do test "lists all entries on index", %{conn: conn} do - path = conn |> user_skill_path(:index) - json = conn |> get(path) |> json_response(200) + [user_skill_1, user_skill_2] = insert_pair(:user_skill) - assert json["data"] == [] + conn + |> request_index + |> json_response(200) + |> assert_ids_from_response([user_skill_1.id, user_skill_2.id]) end test "filters resources on index", %{conn: conn} do - elixir = insert(:skill, title: "Elixir") - phoenix = insert(:skill, title: "Phoenix") - rails = insert(:skill, title: "Rails") - - user = insert(:user) - user_skill_1 = insert(:user_skill, user: user, skill: elixir) - user_skill_2 = insert(:user_skill, user: user, skill: phoenix) - insert(:user_skill, user: user, skill: rails) + [user_skill_1, user_skill_2 | _] = insert_list(3, :user_skill) path = "user-skills/?filter[id]=#{user_skill_1.id},#{user_skill_2.id}" - json = conn |> get(path) |> json_response(200) - - data = json["data"] - assert length(data) == 2 - [first_result, second_result | _] = data - assert first_result["id"] == "#{user_skill_1.id}" - assert second_result["id"] == "#{user_skill_2.id}" + conn + |> get(path) + |> json_response(200) + |> assert_ids_from_response([user_skill_1.id, user_skill_2.id]) end end describe "show" do test "shows chosen resource", %{conn: conn} do user_skill = insert(:user_skill) - path = conn |> user_skill_path(:show, user_skill) - json = conn |> get(path) |> json_response(200) - - data = json["data"] - assert data["id"] == "#{user_skill.id}" - assert data["type"] == "user-skill" - assert data["relationships"]["user"]["data"]["id"] == "#{user_skill.user_id}" - assert data["relationships"]["skill"]["data"]["id"] == "#{user_skill.skill_id}" + + conn + |> request_show(user_skill) + |> json_response(200) + |> Map.get("data") + |> assert_result_id(user_skill.id) end - test "does not show resource and instead throw error when id is nonexistent", %{conn: conn} do - path = conn |> user_skill_path(:show, -1) - assert conn |> get(path) |> json_response(:not_found) + test "renders 404 error when id is nonexistent", %{conn: conn} do + assert conn |> request_show(:not_found) |> json_response(404) end end @@ -61,64 +45,44 @@ defmodule CodeCorps.UserSkillControllerTest do user = insert(:user) skill = insert(:skill, title: "test-skill") - payload = build_payload |> put_relationships(user, skill) - path = conn |> user_skill_path(:create) - json = conn |> post(path, payload) |> json_response(201) - - id = json["data"]["id"] |> String.to_integer - user_skill = UserSkill |> Repo.get(id) - - assert json["data"]["id"] == "#{user_skill.id}" - assert json["data"]["type"] == "user-skill" - assert json["data"]["relationships"]["user"]["data"]["id"] == "#{user.id}" - assert json["data"]["relationships"]["skill"]["data"]["id"] == "#{skill.id}" + attrs = %{user: user, skill: skill} + assert conn |> request_create(attrs) |> json_response(201) end @tag authenticated: :admin - test "does not create resource and renders errors when data is invalid", %{conn: conn} do - payload = build_payload - path = conn |> user_skill_path(:create) - json = conn |> post(path, payload) |> json_response(422) - - assert json["errors"] != %{} + test "renders 422 when data is invalid", %{conn: conn} do + invalid_attrs = %{} + assert conn |> request_create(invalid_attrs) |> json_response(422) end - test "does not create resource and renders 401 when unauthenticated", %{conn: conn} do - path = conn |> user_skill_path(:create) - assert conn |> post(path) |> json_response(401) + test "renders 401 when unauthenticated", %{conn: conn} do + assert conn |> request_create |> json_response(401) end @tag :authenticated test "does not create resource and renders 403 when not authorized", %{conn: conn} do - path = conn |> user_skill_path(:create) - assert conn |> post(path, build_payload) |> json_response(403) + assert conn |> request_create |> json_response(403) end end describe "delete" do @tag authenticated: :admin test "deletes resource", %{conn: conn} do - user_skill = insert(:user_skill) - path = conn |> user_skill_path(:delete, user_skill) - assert conn |> delete(path) |> response(204) + assert conn |> request_delete |> response(204) end test "does not delete resource and renders 401 when unauthenticated", %{conn: conn} do - path = conn |> user_skill_path(:delete, "id not important") - assert conn |> delete(path) |> json_response(401) + assert conn |> request_delete |> json_response(401) end @tag :authenticated test "does not create resource and renders 403 when not authorized", %{conn: conn} do - user_skill = insert(:user_skill) - path = conn |> user_skill_path(:delete, user_skill) - assert conn |> delete(path) |> json_response(403) + assert conn |> request_delete |> json_response(403) end @tag :authenticated test "renders page not found when id is nonexistent on delete", %{conn: conn} do - path = conn |> user_skill_path(:delete, -1) - assert conn |> delete(path) |> json_response(404) + assert conn |> request_delete(:not_found) |> json_response(404) end end end