diff --git a/app/api/helpers.rb b/app/api/helpers.rb index 6fa9468..606b2e9 100644 --- a/app/api/helpers.rb +++ b/app/api/helpers.rb @@ -144,7 +144,8 @@ module Api end def find_task(asset, id) - task = asset.tasks.find(id) + task = asset.tasks.find_by(id: id) + not_found! if task.blank? current_person.has_permission?(task.permission, asset) ? task : forbidden! end diff --git a/app/api/v1/tasks.rb b/app/api/v1/tasks.rb index 912e7bb..c099493 100644 --- a/app/api/v1/tasks.rb +++ b/app/api/v1/tasks.rb @@ -23,6 +23,16 @@ module Api task = find_task(environment, params[:id]) present_partial task, :with => Entities::Task end + + %w[finish cancel].each do |action| + desc "#{action.capitalize} a task" + put ":id/#{action}" do + authenticate! + task = find_task(current_person, params[:id]) + task.send(action, current_person) if (task.status == Task::Status::ACTIVE) + present_partial task, :with => Entities::Task + end + end end kinds = %w[community person enterprise] diff --git a/test/api/task_test.rb b/test/api/task_test.rb index 2c28542..3b0c11b 100644 --- a/test/api/task_test.rb +++ b/test/api/task_test.rb @@ -183,6 +183,29 @@ class TasksTest < ActiveSupport::TestCase assert_equal person, Task.last.target end + task_actions=%w[finish cancel] + task_actions_state={"finish"=>"FINISHED","cancel"=>"CANCELLED"} + task_actions.each do |action| + should "person be able to #{action} his own task" do + login_api + person1 = fast_create(Person) + task = create(Task, :requestor => person1, :target => person) + put "/api/v1/tasks/#{task.id}/#{action}?#{params.to_query}" + assert_equal person.reload.id, task.reload.closed_by_id + assert_equal "Task::Status::#{task_actions_state[action]}".constantize, task.reload.status + end + + should "person not be able to #{action} other person's task" do + login_api + user = fast_create(User) + person1 = fast_create(Person, :user_id => user) + task = create(Task, :requestor => person, :target => person1) + put "/api/v1/tasks/#{task.id}/#{action}?#{params.to_query}" + assert_nil task.reload.closed_by_id + assert_equal Task::Status::ACTIVE, task.status + end + end + ############################# # Enterprise Tasks # ############################# -- libgit2 0.21.2