 defmodule CodeCorps.SkillControllerTest do
-  use CodeCorps.ApiCase
-
-  alias CodeCorps.Skill
-  alias CodeCorps.Repo
+  use CodeCorps.ApiCase, resource_name: :skill
 
   @valid_attrs %{
     description: "Elixir is a functional, concurrent, general-purpose programming language that runs on the Erlang virtual machine (BEAM).",
     original_row: 1,
     title: "Elixir"
   }
-  @invalid_attrs %{}
-
-  defp build_payload, do: %{ "data" => %{"type" => "skill"}}
+  @invalid_attrs %{title: nil}
 
   describe "index" do
     test "lists all entries on index", %{conn: conn} do
-      path = conn |> skill_path(:index)
-      json = conn |> get(path) |> json_response(200)
+      [skill_1, skill_2] = insert_pair(:skill)
 
-      assert json["data"] == []
+      conn
+      |> request_index
+      |> json_response(200)
+      |> assert_ids_from_response([skill_1.id, skill_2.id])
     end
 
     test "filters resources on index", %{conn: conn} do
-      elixir = insert(:skill, title: "Elixir")
-      phoenix = insert(:skill, title: "Phoenix")
-      insert(:skill, title: "Rails")
+      [skill_1, skill_2 | _] = insert_list(3, :skill)
 
-      params = %{"filter" => %{"id" => "#{elixir.id},#{phoenix.id}"}}
-      path = conn |> skill_path(:index, params)
+      path = "skills/?filter[id]=#{skill_1.id},#{skill_2.id}"
 
-      json = conn |> get(path) |> json_response(200)
-
-      data = json["data"]
-      assert data |> length == 2
-
-      [first_result, second_result | _] = data
-      assert first_result["id"] == "#{elixir.id}"
-      assert second_result["id"] == "#{phoenix.id}"
+      conn
+      |> get(path)
+      |> json_response(200)
+      |> assert_ids_from_response([skill_1.id, skill_2.id])
     end
 
     test "returns search results on index", %{conn: conn} do

      params = %{"query" => "r"}
      path = conn |> skill_path(:index, params)

      json = conn |> get(path) |> json_response(200)
      data = json["data"]

      [first_result, second_result | _] = data
      assert length(data) == 2
      assert first_result["id"] == "#{ruby.id}"
      assert second_result["id"] == "#{rails.id}"
    end

    test "limit filter limits results on index", %{conn: conn} do

  describe "show" do
    test "shows chosen resource", %{conn: conn} do
      skill = insert(:skill)
      conn
      |> request_show(skill)
      |> json_response(200)
      |> Map.get("data")
      |> assert_result_id(skill.id)
    end

    test "renders 404 when id is nonexistent", %{conn: conn} do
      assert conn |> request_show(:not_found) |> json_response(404)
    end
  end

  describe "create" do
    @tag authenticated: :admin
    test "creates and renders resource when data is valid", %{conn: conn} do
      assert conn |> request_create(@valid_attrs) |> json_response(201)
    end

    @tag authenticated: :admin
    test "does not create resource and renders 422 when data is invalid", %{conn: conn} do
      assert conn |> request_create(@invalid_attrs) |> json_response(422)
    end

    test "does not create resource and 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
      assert conn |> request_create |> json_response(403)
    end
  end
end
