Commit ada7cf6688b52d1532ff515426db1df6ba929fb4
Committed by
Vitor Barbosa de Araujo
1 parent
f2b2ff07
Exists in
staging
and in
9 other branches
Add feature to close tasks in api
Signed-off-by: Marcos Ronaldo <marcos.rpj2@gmail.com> Signed-off-by: Victor Navarro <victor.matias.navarro@gmail.com> Signed-off-by: Vitor Barbosa <vitornga15@gmail.com>
Showing
3 changed files
with
35 additions
and
1 deletions
Show diff stats
app/api/helpers.rb
@@ -144,7 +144,8 @@ module Api | @@ -144,7 +144,8 @@ module Api | ||
144 | end | 144 | end |
145 | 145 | ||
146 | def find_task(asset, id) | 146 | def find_task(asset, id) |
147 | - task = asset.tasks.find(id) | 147 | + task = asset.tasks.find_by(id: id) |
148 | + not_found! if task.blank? | ||
148 | current_person.has_permission?(task.permission, asset) ? task : forbidden! | 149 | current_person.has_permission?(task.permission, asset) ? task : forbidden! |
149 | end | 150 | end |
150 | 151 |
app/api/v1/tasks.rb
@@ -23,6 +23,16 @@ module Api | @@ -23,6 +23,16 @@ module Api | ||
23 | task = find_task(environment, params[:id]) | 23 | task = find_task(environment, params[:id]) |
24 | present_partial task, :with => Entities::Task | 24 | present_partial task, :with => Entities::Task |
25 | end | 25 | end |
26 | + | ||
27 | + %w[finish cancel].each do |action| | ||
28 | + desc "#{action.capitalize} a task" | ||
29 | + put ":id/#{action}" do | ||
30 | + authenticate! | ||
31 | + task = find_task(current_person, params[:id]) | ||
32 | + task.send(action, current_person) if (task.status == Task::Status::ACTIVE) | ||
33 | + present_partial task, :with => Entities::Task | ||
34 | + end | ||
35 | + end | ||
26 | end | 36 | end |
27 | 37 | ||
28 | kinds = %w[community person enterprise] | 38 | kinds = %w[community person enterprise] |
test/api/task_test.rb
@@ -183,6 +183,29 @@ class TasksTest < ActiveSupport::TestCase | @@ -183,6 +183,29 @@ class TasksTest < ActiveSupport::TestCase | ||
183 | assert_equal person, Task.last.target | 183 | assert_equal person, Task.last.target |
184 | end | 184 | end |
185 | 185 | ||
186 | + task_actions=%w[finish cancel] | ||
187 | + task_actions_state={"finish"=>"FINISHED","cancel"=>"CANCELLED"} | ||
188 | + task_actions.each do |action| | ||
189 | + should "person be able to #{action} his own task" do | ||
190 | + login_api | ||
191 | + person1 = fast_create(Person) | ||
192 | + task = create(Task, :requestor => person1, :target => person) | ||
193 | + put "/api/v1/tasks/#{task.id}/#{action}?#{params.to_query}" | ||
194 | + assert_equal person.reload.id, task.reload.closed_by_id | ||
195 | + assert_equal "Task::Status::#{task_actions_state[action]}".constantize, task.reload.status | ||
196 | + end | ||
197 | + | ||
198 | + should "person not be able to #{action} other person's task" do | ||
199 | + login_api | ||
200 | + user = fast_create(User) | ||
201 | + person1 = fast_create(Person, :user_id => user) | ||
202 | + task = create(Task, :requestor => person, :target => person1) | ||
203 | + put "/api/v1/tasks/#{task.id}/#{action}?#{params.to_query}" | ||
204 | + assert_nil task.reload.closed_by_id | ||
205 | + assert_equal Task::Status::ACTIVE, task.status | ||
206 | + end | ||
207 | + end | ||
208 | + | ||
186 | ############################# | 209 | ############################# |
187 | # Enterprise Tasks # | 210 | # Enterprise Tasks # |
188 | ############################# | 211 | ############################# |