Skip to content

Commit

Permalink
Refactor Donation Goals Controller Tests with helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
mackenziehicks committed Nov 7, 2016
1 parent 7f418b2 commit d0cb48a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
54 changes: 29 additions & 25 deletions test/controllers/donation_goal_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
defmodule CodeCorps.DonationGoalControllerTest do
use CodeCorps.ApiCase, resource_name: :donation_goal

alias CodeCorps.DonationGoal

@valid_attrs %{amount: 200, current: false, description: "A description"}
@invalid_attrs %{description: nil}

describe "index" do
test "lists all entries on index", %{conn: conn} do
path = conn |> path_for(:index)
json = conn |> get(path) |> json_response(200)
[donation_goal_1, donation_goal_2] = insert_pair(:donation_goal)

conn
|> request_index
|> json_response(200)
|> assert_ids_from_response([donation_goal_1.id, donation_goal_2.id])
end

assert json["data"] == []
test "filters resources on index", %{conn: conn} do
[donation_goal_1, donation_goal_2 | _] = insert_list(3, :donation_goal)

path = "donation-goals/?filter[id]=#{donation_goal_1.id},#{donation_goal_2.id}"

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

Expand All @@ -35,39 +46,33 @@ defmodule CodeCorps.DonationGoalControllerTest do
test "creates and renders resource when data is valid", %{conn: conn} do
project = insert(:project)
attrs = @valid_attrs |> Map.merge(%{project: project})
json = conn |> request_create(attrs) |> json_response(201)
assert json["data"]["id"]
assert Repo.get_by(DonationGoal, @valid_attrs)
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
json = conn |> request_create(@invalid_attrs) |> json_response(422)
assert json["errors"] != %{}
assert conn |> request_create(@invalid_attrs) |> json_response(422)
end

test "renders 401 when not authenticated", %{conn: conn} do
assert conn |> request_create(@valid_attrs) |> json_response(401)
assert conn |> request_create |> json_response(401)
end

@tag :authenticated
test "renders 403 when not authorized", %{conn: conn} do
assert conn |> request_create(@valid_attrs) |> json_response(403)
assert conn |> request_create |> json_response(403)
end
end

describe "update" do
@tag authenticated: :admin
test "updates and renders chosen resource when data is valid", %{conn: conn} do
json = conn |> request_update(@valid_attrs) |> json_response(200)
assert json["data"]["id"]
assert Repo.get_by(DonationGoal, @valid_attrs)
assert conn |> request_update(@valid_attrs) |> json_response(200)
end

@tag authenticated: :admin
test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
json = conn |> request_update(@invalid_attrs) |> json_response(422)
assert json["errors"] != %{}
assert conn |> request_update(@invalid_attrs) |> json_response(422)
end

@tag authenticated: :admin
Expand All @@ -76,12 +81,12 @@ defmodule CodeCorps.DonationGoalControllerTest do
end

test "renders 401 when not authenticated", %{conn: conn} do
assert conn |> request_update(@valid_attrs) |> json_response(401)
assert conn |> request_update |> json_response(401)
end

@tag :authenticated
test "renders 403 when not authorized", %{conn: conn} do
assert conn |> request_update(@valid_attrs) |> json_response(403)
assert conn |> request_update |> json_response(403)
end
end

Expand All @@ -90,12 +95,6 @@ defmodule CodeCorps.DonationGoalControllerTest do
test "deletes chosen resource", %{conn: conn} do
donation_goal = insert(:donation_goal)
assert conn |> request_delete(donation_goal) |> response(204)
refute Repo.get(DonationGoal, donation_goal.id)
end

@tag authenticated: :admin
test "renders 404 when id is nonexistent", %{conn: conn} do
assert conn |> request_delete(:not_found) |> json_response(404)
end

test "renders 401 when not authenticated", %{conn: conn} do
Expand All @@ -106,5 +105,10 @@ defmodule CodeCorps.DonationGoalControllerTest do
test "renders 403 when not authorized", %{conn: conn} do
assert conn |> request_delete |> json_response(403)
end

@tag authenticated: :admin
test "renders 404 when id is nonexistent", %{conn: conn} do
assert conn |> request_delete(:not_found) |> json_response(404)
end
end
end
4 changes: 2 additions & 2 deletions test/controllers/user_role_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ defmodule CodeCorps.UserRoleControllerTest do
end

test "does not create resource and renders 401 when unauthenticated", %{conn: conn} do
assert conn |> request_create(%{}) |> 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
assert conn |> request_create(%{}) |> json_response(403)
assert conn |> request_create |> json_response(403)
end
end

Expand Down
4 changes: 4 additions & 0 deletions web/controllers/donation_goal_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ defmodule CodeCorps.DonationGoalController do
use CodeCorps.Web, :controller
use JaResource

import CodeCorps.Helpers.Query, only: [id_filter: 2]

alias CodeCorps.DonationGoal

plug :load_and_authorize_changeset, model: DonationGoal, only: [:create]
plug :load_and_authorize_resource, model: DonationGoal, only: [:update, :delete]
plug JaResource

def filter(_conn, query, "id", id_list), do: id_filter(query, id_list)

def handle_create(_conn, attributes) do
%DonationGoal{}
|> DonationGoal.create_changeset(attributes)
Expand Down

0 comments on commit d0cb48a

Please sign in to comment.