Skip to content

Commit

Permalink
refactored project_category controller tests to add helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
amyschools committed Nov 3, 2016
1 parent f9eef9d commit 3358049
Showing 1 changed file with 35 additions and 101 deletions.
136 changes: 35 additions & 101 deletions test/controllers/project_category_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,47 +1,25 @@
defmodule CodeCorps.ProjectCategoryControllerTest do
use CodeCorps.ApiCase

alias CodeCorps.Category
alias CodeCorps.Project
alias CodeCorps.ProjectCategory
alias CodeCorps.Repo

@attrs %{}

defp build_payload, do: %{ "data" => %{"type" => "project-category", "attributes" => %{}}}
use CodeCorps.ApiCase, resource_name: :project_category

describe "index" do
test "lists all entries on index", %{conn: conn} do
conn = get conn, project_category_path(conn, :index)
assert json_response(conn, 200)["data"] == []
[project_category_1, project_category_2] = insert_pair(:project_category)

conn
|> request_index
|> json_response(200)
|> assert_ids_from_response([project_category_1.id, project_category_2.id])
end

test "filters resources on index", %{conn: conn} do
arts = insert(:category, name: "Arts")
society = insert(:category, name: "Society")
technology = insert(:category, name: "Technology")
[project_category_1, project_category_2 | _] = insert_list(3, :project_category)

project = insert(:project)
project_category_1 = insert(:project_category, project: project, category: arts)
project_category_2 = insert(:project_category, project: project, category: society)
insert(:project_category, project: project, category: technology)

response =
conn
|> get("project-categories/?filter[id]=#{project_category_1.id},#{project_category_2.id}")
|> json_response(200)

[first_result, second_result] = response |> Map.get("data")

first_result
|> assert_result_id(project_category_1.id)
|> assert_jsonapi_relationship("project", project.id)
|> assert_jsonapi_relationship("category", arts.id)

second_result
|> assert_result_id(project_category_2.id)
|> assert_jsonapi_relationship("project", project.id)
|> assert_jsonapi_relationship("category", society.id)
path = "project-categories/?filter[id]=#{project_category_1.id},#{project_category_2.id}"

conn
|> get(path)
|> json_response(200)
|> assert_ids_from_response([project_category_1.id, project_category_2.id])
end
end

Expand All @@ -52,106 +30,62 @@ defmodule CodeCorps.ProjectCategoryControllerTest do
project_category = insert(:project_category, project: project, category: category)

conn
|> get(project_category_path(conn, :show, project_category))
|> request_show(project_category)
|> json_response(200)
|> Map.get("data")
|> assert_result_id(project_category.id)
|> assert_jsonapi_relationship("project", project.id)
|> assert_jsonapi_relationship("category", category.id)
end

test "does not show resource and instead throw error when id is nonexistent", %{conn: conn} do
path = conn |> project_category_path(:show, -1)
assert conn |> get(path) |> json_response(:not_found)
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
test "creates and renders resource when data is valid", %{conn: conn, current_user: current_user} do
@tag authenticated: :admin
test "creates and renders resource when data is valid", %{conn: conn} do
organization = insert(:organization)
category = insert(:category)
project = insert(:project, organization: organization)
insert(:organization_membership, role: "admin", member: current_user, organization: organization)


payload = build_payload |> put_relationships(project, category)

path = conn |> project_category_path(:create)
response = conn |> post(path, payload) |> json_response(201)
data = response |> Map.get("data")

data
|> assert_jsonapi_relationship("project", project.id)
|> assert_jsonapi_relationship("category", category.id)

project_category = ProjectCategory |> Repo.get(data["id"])
assert project_category
assert project_category.project_id == project.id
assert project_category.category_id == category.id
attrs = %{category: category, project: project}
assert conn |> request_create(attrs) |> json_response(201)
end

@tag authenticated: :admin
test "does not create resource and renders 422 when data is invalid", %{conn: conn} do
payload = build_payload()

path = conn |> project_category_path(:create)
data = conn |> post(path, payload) |> json_response(422)
assert data["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
payload = build_payload()

path = conn |> project_category_path(:create)
assert conn |> post(path, payload) |> 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, current_user: current_user} do
organization = insert(:organization)
project = insert(:project, organization: organization)
category = insert(:category)
insert(:organization_membership, role: "contributor", member: current_user, organization: organization)

payload = build_payload |> put_relationships(project, category)

path = conn |> project_category_path(:create)
assert conn |> post(path, payload) |> json_response(403)
test "renders 403 when not authorized", %{conn: conn} do
assert conn |> request_create |> json_response(403)
end
end

describe "delete" do
@tag authenticated: :admin
test "deletes resource", %{conn: conn} do
project_category = insert(:project_category)

path = conn |> project_category_path(:delete, project_category)

assert conn |> delete(path) |> response(204)

refute Repo.get(ProjectCategory, project_category.id)
assert Repo.get(Project, project_category.project_id)
assert Repo.get(Category, project_category.category_id)
assert conn |> request_delete |> response(204)
end

test "does not delete resource and renders 401 when unauthenticated", %{conn: conn} do
project_category = insert(:project_category)
path = conn |> project_category_path(:delete, project_category)
assert conn |> delete(path) |> json_response(401)
test "renders 401 when unauthenticated", %{conn: conn} do
assert conn |> request_delete |> json_response(401)
end

@tag :authenticated
test "does not create resource and renders 403 when not authorized", %{conn: conn} do
project_category = insert(:project_category)
path = conn |> project_category_path(:delete, project_category)
assert conn |> delete(path) |> json_response(403)
test "renders 403 when not authorized", %{conn: conn} do
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 |> project_category_path(:delete, -1)
assert conn |> delete(path) |> json_response(404)
test "renders 404 when id is nonexistent", %{conn: conn} do
assert conn |> request_delete(:not_found) |> json_response(404)
end
end
end

0 comments on commit 3358049

Please sign in to comment.