Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
amyschools committed Nov 3, 2016
1 parent dd136c2 commit 9686440
Showing 1 changed file with 31 additions and 67 deletions.
98 changes: 31 additions & 67 deletions test/controllers/user_skill_controller_test.exs
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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

0 comments on commit 9686440

Please sign in to comment.