Skip to content

Commit e623364

Browse files
committed
Refactored Category Controller tests with helpers
1 parent ef84323 commit e623364

File tree

1 file changed

+37
-58
lines changed

1 file changed

+37
-58
lines changed
Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,102 +1,81 @@
11
defmodule CodeCorps.CategoryControllerTest do
2-
use CodeCorps.ApiCase
2+
use CodeCorps.ApiCase, resource_name: :category
33

4-
alias CodeCorps.Category
5-
6-
@valid_attrs %{description: "You want to improve software tools and infrastructure.", name: "Technology"}
4+
@valid_attrs %{name: "Technology"}
75
@invalid_attrs %{name: nil}
86

9-
def request_create(conn, attrs) do
10-
path = conn |> category_path(:create)
11-
payload = json_payload(:category, attrs)
12-
conn |> post(path, payload)
13-
end
14-
15-
def request_update(conn, attrs) do
16-
category = insert(:category)
17-
payload = json_payload(:category, attrs)
18-
path = conn |> category_path(:update, category)
19-
20-
conn |> put(path, payload)
21-
end
7+
describe "index" do
8+
test "lists all entries on index", %{conn: conn} do
9+
[category_1, category_2] = insert_pair(:category)
2210

23-
test "lists all entries on index", %{conn: conn} do
24-
conn = get conn, category_path(conn, :index)
25-
assert json_response(conn, 200)["data"] == []
11+
conn
12+
|> request_index
13+
|> json_response(200)
14+
|> assert_ids_from_response([category_1.id, category_2.id])
15+
end
2616
end
2717

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

32-
path = conn |> category_path(:show, category)
33-
data = conn |> get(path) |> json_response(200) |> Map.get("data")
34-
35-
assert data["id"] == category.id |> Integer.to_string
36-
assert data["type"] == "category"
37-
38-
assert data["attributes"]["name"] == category.name
39-
assert data["attributes"]["slug"] == category.slug
40-
assert data["attributes"]["description"] == category.description
22+
conn
23+
|> request_show(category)
24+
|> json_response(200)
25+
|> Map.get("data")
26+
|> assert_result_id(category.id)
4127
end
4228

43-
test "renders page not found when id is nonexistent", %{conn: conn} do
44-
path = conn |> category_path(:show, -1)
45-
assert conn |> get(path) |> json_response(404)
29+
test "renders 404 when id is nonexistent", %{conn: conn} do
30+
assert conn |> request_show(:not_found) |> json_response(404)
4631
end
4732
end
4833

4934
describe "create" do
5035
@tag authenticated: :admin
5136
test "creates and renders resource when data is valid", %{conn: conn} do
52-
response = conn |> request_create(@valid_attrs) |> json_response(201)
53-
54-
assert response["data"]["id"]
55-
assert response["data"]["attributes"]["slug"] == "technology"
56-
assert Repo.get_by(Category, @valid_attrs)
37+
assert conn |> request_create(@valid_attrs) |> json_response(201)
5738
end
5839

5940
@tag authenticated: :admin
60-
test "does not create resource and renders errors when data is invalid", %{conn: conn} do
61-
response = conn |> request_create(@invalid_attrs) |> json_response(422)
62-
63-
assert response["errors"] != %{}
41+
test "renders 422 when data is invalid", %{conn: conn} do
42+
assert conn |> request_create(@invalid_attrs) |> json_response(422)
6443
end
6544

66-
test "does not create resource and renders 401 when not authenticated", %{conn: conn} do
67-
assert conn |> request_create(@valid_attrs) |> json_response(401)
45+
test "renders 401 when not authenticated", %{conn: conn} do
46+
assert conn |> request_create |> json_response(401)
6847
end
6948

7049
@tag :authenticated
71-
test "does not create resource and renders 403 when not authorized", %{conn: conn} do
72-
assert conn |> request_create(@valid_attrs) |> json_response(403)
50+
test "renders 403 when not authorized", %{conn: conn} do
51+
assert conn |> request_create |> json_response(403)
7352
end
7453
end
7554

7655
describe "update" do
7756
@tag authenticated: :admin
7857
test "updates and renders chosen resource when data is valid", %{conn: conn} do
79-
response = conn |> request_update(@valid_attrs) |> json_response(200)
80-
81-
category = Repo.get_by(Category, @valid_attrs)
82-
assert category
83-
assert response["data"]["id"] == "#{category.id}"
58+
assert conn |> request_update(@valid_attrs) |> json_response(200)
8459
end
8560

8661
@tag authenticated: :admin
87-
test "does not update chosen resource and renders errors when data is invalid", %{conn: conn} do
88-
response = conn |> request_update(@invalid_attrs) |> json_response(422)
89-
90-
assert response["errors"] != %{}
62+
test "renders 422 when data is invalid", %{conn: conn} do
63+
assert conn |> request_update(@invalid_attrs) |> json_response(422)
9164
end
9265

93-
test "does not update resource and renders 401 when not authenticated", %{conn: conn} do
94-
assert conn |> request_update(@valid_attrs) |> json_response(401)
66+
test "renders 401 when not authenticated", %{conn: conn} do
67+
assert conn |> request_update |> json_response(401)
9568
end
9669

9770
@tag :authenticated
98-
test "does not update resource and renders 403 when not authorized", %{conn: conn} do
99-
assert conn |> request_update(@valid_attrs) |> json_response(403)
71+
test "renders 403 when not authorized", %{conn: conn} do
72+
assert conn |> request_update |> json_response(403)
10073
end
74+
75+
@tag authenticated: :admin
76+
test "renders 404 when id is nonexistent", %{conn: conn} do
77+
assert conn |> request_update(:not_found) |> json_response(404)
78+
end
79+
10180
end
10281
end

0 commit comments

Comments
 (0)