forked from code-corps/code-corps-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_controller_test.exs
More file actions
238 lines (192 loc) · 7.21 KB
/
task_controller_test.exs
File metadata and controls
238 lines (192 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
defmodule CodeCorps.TaskControllerTest do
use CodeCorps.ApiCase, resource_name: :task
@valid_attrs %{
title: "Test task",
task_type: "issue",
markdown: "A test task",
status: "open"
}
@invalid_attrs %{
title: nil,
task_type: "issue",
status: "nonexistent"
}
describe "index" do
test "lists all entries", %{conn: conn} do
[task_1, task_2] = insert_pair(:task)
conn
|> request_index
|> json_response(200)
|> assert_ids_from_response([task_1.id, task_2.id])
end
test "lists all entries newest first", %{conn: conn} do
# Has to be done manually. Inserting as a list is too quick.
# Field lacks the resolution to differentiate.
task_1 = insert(:task, inserted_at: Ecto.DateTime.cast!("2000-01-15T00:00:10"))
task_2 = insert(:task, inserted_at: Ecto.DateTime.cast!("2000-01-15T00:00:20"))
task_3 = insert(:task, inserted_at: Ecto.DateTime.cast!("2000-01-15T00:00:30"))
path = conn |> task_path(:index)
json = conn |> get(path) |> json_response(200)
ids =
json["data"]
|> Enum.map(&Map.get(&1, "id"))
|> Enum.map(&Integer.parse/1)
|> Enum.map(fn({id, _rem}) -> id end)
assert ids == [task_3.id, task_2.id, task_1.id]
end
test "lists all tasks for a project", %{conn: conn} do
project_1 = insert(:project)
project_2 = insert(:project)
user = insert(:user)
insert(:task, project: project_1, user: user)
insert(:task, project: project_1, user: user)
insert(:task, project: project_2, user: user)
json =
conn
|> get("projects/#{project_1.id}/tasks")
|> json_response(200)
assert json["data"] |> Enum.count == 2
end
test "lists all tasks filtered by task_type", %{conn: conn} do
project_1 = insert(:project)
user = insert(:user)
insert(:task, task_type: "idea", project: project_1, user: user)
insert(:task, task_type: "issue", project: project_1, user: user)
insert(:task, task_type: "task", project: project_1, user: user)
json =
conn
|> get("projects/#{project_1.id}/tasks?task_type=idea,issue")
|> json_response(200)
assert json["data"] |> Enum.count == 2
task_types =
json["data"]
|> Enum.map(fn(task_json) -> task_json["attributes"] end)
|> Enum.map(fn(task_attributes) -> task_attributes["task-type"] end)
assert task_types |> Enum.member?("issue")
assert task_types |> Enum.member?("idea")
refute task_types |> Enum.member?("task")
end
test "lists all tasks filtered by status", %{conn: conn} do
project = insert(:project)
task_1 = insert(:task, status: "open", project: project)
task_2 = insert(:task, status: "closed", project: project)
json =
conn
|> get("projects/#{project.id}/tasks?status=open")
|> json_response(200)
assert json["data"] |> Enum.count == 1
[task] = json["data"]
assert task["id"] == task_1.id |> Integer.to_string
json =
conn
|> get("projects/#{project.id}/tasks?status=closed")
|> json_response(200)
assert json["data"] |> Enum.count == 1
[task] = json["data"]
assert task["id"] == task_2.id |> Integer.to_string
end
end
describe "show" do
test "shows chosen resource", %{conn: conn} do
task = insert(:task)
conn
|> request_show(task)
|> json_response(200)
|> Map.get("data")
|> assert_result_id(task.id)
end
test "shows task by number for project", %{conn: conn} do
task = insert(:task)
path = conn |> project_task_path(:show, task.project_id, task.number)
data = conn |> get(path) |> json_response(200) |> Map.get("data")
assert data["id"] == "#{task.id}"
assert data["type"] == "task"
end
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
test "creates and renders resource when data is valid", %{conn: conn, current_user: current_user} do
project = insert(:project)
attrs = @valid_attrs |> Map.merge(%{project: project, user: current_user})
json = conn |> request_create(attrs) |> json_response(201)
# ensure record is reloaded from database before serialized, since number is added
# on database level upon insert
assert json["data"]["attributes"]["number"] == 1
end
@tag :authenticated
test "renders 422 when data is invalid", %{conn: conn, current_user: current_user} do
project = insert(:project)
attrs = @invalid_attrs |> Map.merge(%{project: project, user: current_user})
assert conn |> request_create(attrs) |> json_response(422)
end
test "renders 401 when unauthenticated", %{conn: conn} do
assert conn |> request_create |> json_response(401)
end
end
describe "update" do
@tag :authenticated
test "updates and renders chosen resource when data is valid", %{conn: conn, current_user: current_user} do
task = insert(:task, user: current_user)
assert conn |> request_update(task, @valid_attrs) |> json_response(200)
end
@tag :authenticated
test "renders 422 when data is invalid", %{conn: conn, current_user: current_user} do
task = insert(:task, user: current_user)
assert conn |> request_update(task, @invalid_attrs) |> json_response(422)
end
test "renders 401 when unauthenticated", %{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 |> json_response(403)
end
end
describe "pagination" do
test "specifying a page size works", %{conn: conn} do
project_1 = insert(:project)
user = insert(:user)
insert_list(3, :task, project: project_1, user: user)
path = conn |> task_path(:index)
json =
conn
|> get(path, page: %{page_size: 2})
|> json_response(200)
assert json["data"] |> Enum.count == 2
end
test "specifying a page number works", %{conn: conn} do
project_1 = insert(:project)
user = insert(:user)
insert_list(2, :task, project: project_1, user: user)
task_to_test = insert(:task, project: project_1, user: user)
insert(:task, project: project_1, user: user)
path = conn |> task_path(:index)
json =
conn
|> get(path, page: %{ page: 2, page_size: 2 })
|> json_response(200)
[ %{"id" => id} | _ ] = json["data"]
assert String.to_integer(id) == task_to_test.id
end
test "paginated results include a valid meta key", %{conn: conn} do
project_1 = insert(:project)
user = insert(:user)
insert_list(6, :task, project: project_1, user: user)
meta = %{
"total_records" => 6,
"total_pages" => 3,
"page_size" => 2,
"current_page" => 1,
}
path = conn |> task_path(:index)
json =
conn
|> get(path, page: %{ page_size: 2 })
|> json_response(200)
assert json["meta"] == meta
end
end
end