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
95 changes: 37 additions & 58 deletions test/controllers/category_controller_test.exs
Original file line number Diff line number Diff line change
@@ -1,102 +1,81 @@
defmodule CodeCorps.CategoryControllerTest do
use CodeCorps.ApiCase
use CodeCorps.ApiCase, resource_name: :category

alias CodeCorps.Category

@valid_attrs %{description: "You want to improve software tools and infrastructure.", name: "Technology"}
@valid_attrs %{name: "Technology"}
@invalid_attrs %{name: nil}

def request_create(conn, attrs) do
path = conn |> category_path(:create)
payload = json_payload(:category, attrs)
conn |> post(path, payload)
end

def request_update(conn, attrs) do
category = insert(:category)
payload = json_payload(:category, attrs)
path = conn |> category_path(:update, category)

conn |> put(path, payload)
end
describe "index" do
test "lists all entries on index", %{conn: conn} do
[category_1, category_2] = insert_pair(:category)

test "lists all entries on index", %{conn: conn} do
conn = get conn, category_path(conn, :index)
assert json_response(conn, 200)["data"] == []
conn
|> request_index
|> json_response(200)
|> assert_ids_from_response([category_1.id, category_2.id])
end
end

describe "show" do
test "shows chosen resource", %{conn: conn} do
category = insert(:category)

path = conn |> category_path(:show, category)
data = conn |> get(path) |> json_response(200) |> Map.get("data")

assert data["id"] == category.id |> Integer.to_string
assert data["type"] == "category"

assert data["attributes"]["name"] == category.name
assert data["attributes"]["slug"] == category.slug
assert data["attributes"]["description"] == category.description
conn
|> request_show(category)
|> json_response(200)
|> Map.get("data")
|> assert_result_id(category.id)
end

test "renders page not found when id is nonexistent", %{conn: conn} do
path = conn |> category_path(:show, -1)
assert conn |> get(path) |> json_response(404)
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
response = conn |> request_create(@valid_attrs) |> json_response(201)

assert response["data"]["id"]
assert response["data"]["attributes"]["slug"] == "technology"
assert Repo.get_by(Category, @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
response = conn |> request_create(@invalid_attrs) |> json_response(422)

assert response["errors"] != %{}
test "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 not authenticated", %{conn: conn} do
assert conn |> request_create(@valid_attrs) |> json_response(401)
test "renders 401 when not authenticated", %{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(@valid_attrs) |> json_response(403)
test "renders 403 when not authorized", %{conn: conn} do
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
response = conn |> request_update(@valid_attrs) |> json_response(200)

category = Repo.get_by(Category, @valid_attrs)
assert category
assert response["data"]["id"] == "#{category.id}"
assert conn |> request_update(@valid_attrs) |> json_response(200)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the organization membership update test:

organization = insert(:organization)
membership = insert(:organization_membership, organization: organization, role: "pending")
insert(:organization_membership, organization: organization, member: current_user, role: "owner")

  `assert conn |> request_update(membership, @valid_attrs) |> json_response(200)`

Should I change it to look more like this one? Looks like the request_update helper has a default_record.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, because you do need someone who's an organization admin to approve, right?

end

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

assert response["errors"] != %{}
test "renders 422 when data is invalid", %{conn: conn} do
assert conn |> request_update(@invalid_attrs) |> json_response(422)
end

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

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

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

end
end