11defmodule CodeCorps.SkillControllerTest do
2- use CodeCorps.ApiCase
3-
4- alias CodeCorps.Skill
5- alias CodeCorps.Repo
2+ use CodeCorps.ApiCase , resource_name: :skill
63
74 @ valid_attrs % {
85 description: "Elixir is a functional, concurrent, general-purpose programming language that runs on the Erlang virtual machine (BEAM)." ,
96 original_row: 1 ,
107 title: "Elixir"
118 }
12- @ invalid_attrs % { }
13-
14- defp build_payload , do: % { "data" => % { "type" => "skill" } }
9+ @ invalid_attrs % { title: nil }
1510
1611 describe "index" do
1712 test "lists all entries on index" , % { conn: conn } do
18- path = conn |> skill_path ( :index )
19- json = conn |> get ( path ) |> json_response ( 200 )
13+ [ skill_1 , skill_2 ] = insert_pair ( :skill )
2014
21- assert json [ "data" ] == [ ]
15+ conn
16+ |> request_index
17+ |> json_response ( 200 )
18+ |> assert_ids_from_response ( [ skill_1 . id , skill_2 . id ] )
2219 end
2320
2421 test "filters resources on index" , % { conn: conn } do
25- elixir = insert ( :skill , title: "Elixir" )
26- phoenix = insert ( :skill , title: "Phoenix" )
27- insert ( :skill , title: "Rails" )
28-
29- params = % { "filter" => % { "id" => "#{ elixir . id } ,#{ phoenix . id } " } }
30- path = conn |> skill_path ( :index , params )
31-
32- json = conn |> get ( path ) |> json_response ( 200 )
22+ [ skill_1 , skill_2 | _ ] = insert_list ( 3 , :skill )
3323
34- data = json [ "data" ]
35- assert data |> length == 2
24+ path = "skills/?filter[id]=#{ skill_1 . id } ,#{ skill_2 . id } "
3625
37- [ first_result , second_result | _ ] = data
38- assert first_result [ "id" ] == "#{ elixir . id } "
39- assert second_result [ "id" ] == "#{ phoenix . id } "
26+ conn
27+ |> get ( path )
28+ |> json_response ( 200 )
29+ |> assert_ids_from_response ( [ skill_1 . id , skill_2 . id ] )
4030 end
4131
4232 test "returns search results on index" , % { conn: conn } do
@@ -47,13 +37,10 @@ defmodule CodeCorps.SkillControllerTest do
4737 params = % { "query" => "r" }
4838 path = conn |> skill_path ( :index , params )
4939
50- json = conn |> get ( path ) |> json_response ( 200 )
51- data = json [ "data" ]
52-
53- [ first_result , second_result | _ ] = data
54- assert length ( data ) == 2
55- assert first_result [ "id" ] == "#{ ruby . id } "
56- assert second_result [ "id" ] == "#{ rails . id } "
40+ conn
41+ |> get ( path )
42+ |> json_response ( 200 )
43+ |> assert_ids_from_response ( [ ruby . id , rails . id ] )
5744 end
5845
5946 test "limit filter limits results on index" , % { conn: conn } do
@@ -71,54 +58,36 @@ defmodule CodeCorps.SkillControllerTest do
7158 describe "show" do
7259 test "shows chosen resource" , % { conn: conn } do
7360 skill = insert ( :skill )
74-
75- path = conn |> skill_path ( :show , skill )
76- json = conn |> get ( path ) |> json_response ( 200 )
77-
78- data = json [ "data" ]
79- assert data [ "id" ] == "#{ skill . id } "
80- assert data [ "type" ] == "skill"
81- assert data [ "attributes" ] [ "title" ] == skill . title
82- assert data [ "attributes" ] [ "description" ] == skill . description
61+ conn
62+ |> request_show ( skill )
63+ |> json_response ( 200 )
64+ |> Map . get ( "data" )
65+ |> assert_result_id ( skill . id )
8366 end
8467
8568 test "renders 404 when id is nonexistent" , % { conn: conn } do
86- path = conn |> comment_path ( :show , - 1 )
87- assert conn |> get ( path ) |> json_response ( 404 )
69+ assert conn |> request_show ( :not_found ) |> json_response ( 404 )
8870 end
8971 end
9072
9173 describe "create" do
9274 @ tag authenticated: :admin
9375 test "creates and renders resource when data is valid" , % { conn: conn } do
94- path = conn |> skill_path ( :create )
95- payload = build_payload |> put_attributes ( @ valid_attrs )
96- json = conn |> post ( path , payload ) |> json_response ( 201 )
97-
98- assert json [ "data" ] [ "id" ]
99- assert Repo . get_by ( Skill , @ valid_attrs )
76+ assert conn |> request_create ( @ valid_attrs ) |> json_response ( 201 )
10077 end
10178
10279 @ tag authenticated: :admin
103- test "does not create resource and renders errors when data is invalid" , % { conn: conn } do
104- path = conn |> skill_path ( :create )
105- payload = build_payload |> put_attributes ( @ invalid_attrs )
106- json = conn |> post ( path , payload ) |> json_response ( 422 )
107-
108- assert json [ "errors" ] != % { }
80+ test "does not create resource and renders 422 when data is invalid" , % { conn: conn } do
81+ assert conn |> request_create ( @ invalid_attrs ) |> json_response ( 422 )
10982 end
11083
11184 test "does not create resource and renders 401 when unauthenticated" , % { conn: conn } do
112- path = conn |> skill_path ( :create )
113- payload = build_payload |> put_attributes ( @ valid_attrs )
114- assert conn |> post ( path , payload ) |> json_response ( 401 )
85+ assert conn |> request_create |> json_response ( 401 )
11586 end
11687
11788 @ tag :authenticated
11889 test "does not create resource and renders 403 when not authorized" , % { conn: conn } do
119- path = conn |> skill_path ( :create )
120- payload = build_payload |> put_attributes ( @ valid_attrs )
121- assert conn |> post ( path , payload ) |> json_response ( 403 )
90+ assert conn |> request_create |> json_response ( 403 )
12291 end
12392 end
12493end
0 commit comments