From fc38057172bf2259227e5534e699ce6b4f4648dd Mon Sep 17 00:00:00 2001 From: Lou Date: Mon, 10 May 2021 22:06:12 +0200 Subject: [PATCH] Add destroy def and unit test --- app/controllers/api/v1/tasks_controller.rb | 5 +++++ .../api/v1/activities_controller_test.rb | 3 ++- .../api/v1/tasks_controller_test.rb | 18 ++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/tasks_controller.rb b/app/controllers/api/v1/tasks_controller.rb index 5b68704..8a0c14b 100644 --- a/app/controllers/api/v1/tasks_controller.rb +++ b/app/controllers/api/v1/tasks_controller.rb @@ -29,6 +29,11 @@ class Api::V1::TasksController < ApplicationController end end + def destroy + @task.destroy + head 204 + end + private def task_params diff --git a/test/controllers/api/v1/activities_controller_test.rb b/test/controllers/api/v1/activities_controller_test.rb index d92555c..67cb8f5 100644 --- a/test/controllers/api/v1/activities_controller_test.rb +++ b/test/controllers/api/v1/activities_controller_test.rb @@ -95,7 +95,8 @@ class Api::V1::ActivitiesControllerTest < ActionDispatch::IntegrationTest test "should forbid destroy activity - not logged in" do assert_no_difference('Activity.count') do - delete api_v1_activity_url(@activity), as: :json + delete api_v1_activity_url(@activity), + as: :json end assert_response :forbidden end diff --git a/test/controllers/api/v1/tasks_controller_test.rb b/test/controllers/api/v1/tasks_controller_test.rb index fb4b5a8..7602112 100644 --- a/test/controllers/api/v1/tasks_controller_test.rb +++ b/test/controllers/api/v1/tasks_controller_test.rb @@ -75,4 +75,22 @@ class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest as: :json assert_response :forbidden end + + # DESTROY + test "should destroy task" do + assert_difference "Task.count", -1 do + delete api_v1_activity_task_url(@activity, @task), + headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, + as: :json + end + assert_response :no_content + end + + test "should not destroy task" do + assert_no_difference('Task.count') do + delete api_v1_activity_task_url(@activity, @task), + as: :json + end + assert_response :forbidden + end end