diff --git a/config/api.yml b/config/api.yml index 7df49bfc72..98ede6f2f0 100644 --- a/config/api.yml +++ b/config/api.yml @@ -2621,7 +2621,7 @@ :identifier: tasks :options: - :collection - :verbs: *gp + :verbs: *gpd :klass: MiqTask :collection_actions: :get: @@ -2630,10 +2630,18 @@ :post: - :name: query :identifier: tasks_view + - :name: delete + :identifier: miq_task_delete :resource_actions: :get: - :name: read :identifier: tasks_view + :post: + - :name: delete + :identifier: miq_task_delete + :delete: + - :name: delete + :identifier: miq_task_delete :templates: :description: Templates :identifier: miq_template diff --git a/spec/requests/tasks_spec.rb b/spec/requests/tasks_spec.rb new file mode 100644 index 0000000000..0786ae008e --- /dev/null +++ b/spec/requests/tasks_spec.rb @@ -0,0 +1,61 @@ +describe 'TasksController' do + let(:task) { FactoryGirl.create(:miq_task, :state => MiqTask::STATE_FINISHED) } + let(:task2) { FactoryGirl.create(:miq_task, :state => MiqTask::STATE_FINISHED) } + + def expect_deleted(*args) + args.each do |arg| + expect(MiqTask.find_by(:id => arg.id)).to be_nil + end + end + + it 'deletes on DELETE' do + api_basic_authorize resource_action_identifier(:tasks, :delete, :delete) + + delete(api_task_url(nil, task)) + + expect(response).to have_http_status(:no_content) # 204 + expect_deleted(task) + end + + it 'deletes on POST' do + api_basic_authorize resource_action_identifier(:tasks, :delete) + + data = { + :action => 'delete' + } + post(api_task_url(nil, task), :params => data) + + expect(response).to have_http_status(:ok) # 200 + expect_deleted(task) + + expected = { + 'success' => true, + 'message' => "tasks id: #{task.id} deleting" + } + expect(response.parsed_body).to include(expected) + end + + it 'bulk deletes' do + api_basic_authorize collection_action_identifier(:tasks, :delete) + + data = { + :action => 'delete', + :resources => [ + {:href => api_task_url(nil, task)}, + {:href => api_task_url(nil, task2)} + ] + } + post(api_tasks_url, :params => data) + + expect(response).to have_http_status(:ok) # 200 + expect_deleted(task, task2) + + expected = { + 'results' => a_collection_including( + a_hash_including('success' => true, 'message' => "tasks id: #{task.id} deleting"), + a_hash_including('success' => true, 'message' => "tasks id: #{task2.id} deleting") + ) + } + expect(response.parsed_body).to include(expected) + end +end diff --git a/spec/support/api/helpers.rb b/spec/support/api/helpers.rb index 09897dc2f4..735df293af 100644 --- a/spec/support/api/helpers.rb +++ b/spec/support/api/helpers.rb @@ -53,6 +53,10 @@ def collection_action_identifier(type, action, method = :post) action_identifier(type, action, :collection_actions, method) end + def resource_action_identifier(type, action, method = :post) + action_identifier(type, action, :resource_actions, method) + end + def subcollection_action_identifier(type, subtype, action, method = :post) subtype_actions = "#{subtype}_subcollection_actions".to_sym if ::Api::ApiConfig.collections[type][subtype_actions]