Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 28 additions & 59 deletions test/controllers/skill_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,42 +1,32 @@
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")

params = %{"filter" => %{"id" => "#{elixir.id},#{phoenix.id}"}}
path = conn |> skill_path(:index, params)

json = conn |> get(path) |> json_response(200)
[skill_1, skill_2 | _] = insert_list(3, :skill)

data = json["data"]
assert data |> length == 2
path = "skills/?filter[id]=#{skill_1.id},#{skill_2.id}"

[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
Expand All @@ -47,13 +37,10 @@ defmodule CodeCorps.SkillControllerTest 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}"
conn
|> get(path)
|> json_response(200)
|> assert_ids_from_response([ruby.id, rails.id])
end

test "limit filter limits results on index", %{conn: conn} do
Expand All @@ -71,54 +58,36 @@ defmodule CodeCorps.SkillControllerTest do
describe "show" do
test "shows chosen resource", %{conn: conn} do
skill = insert(:skill)

path = conn |> skill_path(:show, skill)
json = conn |> get(path) |> json_response(200)

data = json["data"]
assert data["id"] == "#{skill.id}"
assert data["type"] == "skill"
assert data["attributes"]["title"] == skill.title
assert data["attributes"]["description"] == skill.description
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
path = conn |> comment_path(:show, -1)
assert conn |> get(path) |> json_response(404)
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
path = conn |> skill_path(:create)
payload = build_payload |> put_attributes(@valid_attrs)
json = conn |> post(path, payload) |> json_response(201)

assert json["data"]["id"]
assert Repo.get_by(Skill, @valid_attrs)
assert conn |> request_create(@valid_attrs) |> json_response(201)
end

@tag authenticated: :admin
test "does not create resource and renders errors when data is invalid", %{conn: conn} do
path = conn |> skill_path(:create)
payload = build_payload |> put_attributes(@invalid_attrs)
json = conn |> post(path, payload) |> json_response(422)

assert json["errors"] != %{}
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
path = conn |> skill_path(:create)
payload = build_payload |> put_attributes(@valid_attrs)
assert conn |> post(path, payload) |> json_response(401)
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 |> skill_path(:create)
payload = build_payload |> put_attributes(@valid_attrs)
assert conn |> post(path, payload) |> json_response(403)
assert conn |> request_create |> json_response(403)
end
end
end