|
1 | 1 | defmodule CodeCorps.CategoryControllerTest do |
2 | | - use CodeCorps.ApiCase |
| 2 | + use CodeCorps.ApiCase, resource_name: :category |
3 | 3 |
|
4 | | - alias CodeCorps.Category |
5 | | - |
6 | | - @valid_attrs %{description: "You want to improve software tools and infrastructure.", name: "Technology"} |
| 4 | + @valid_attrs %{name: "Technology"} |
7 | 5 | @invalid_attrs %{name: nil} |
8 | 6 |
|
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) |
22 | 10 |
|
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 |
26 | 16 | end |
27 | 17 |
|
28 | 18 | describe "show" do |
29 | 19 | test "shows chosen resource", %{conn: conn} do |
30 | 20 | category = insert(:category) |
31 | 21 |
|
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) |
41 | 27 | end |
42 | 28 |
|
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) |
46 | 31 | end |
47 | 32 | end |
48 | 33 |
|
49 | 34 | describe "create" do |
50 | 35 | @tag authenticated: :admin |
51 | 36 | 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) |
57 | 38 | end |
58 | 39 |
|
59 | 40 | @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) |
64 | 43 | end |
65 | 44 |
|
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) |
68 | 47 | end |
69 | 48 |
|
70 | 49 | @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) |
73 | 52 | end |
74 | 53 | end |
75 | 54 |
|
76 | 55 | describe "update" do |
77 | 56 | @tag authenticated: :admin |
78 | 57 | 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) |
84 | 59 | end |
85 | 60 |
|
86 | 61 | @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) |
91 | 64 | end |
92 | 65 |
|
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) |
95 | 68 | end |
96 | 69 |
|
97 | 70 | @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) |
100 | 73 | 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 | + |
101 | 80 | end |
102 | 81 | end |
0 commit comments