diff --git a/app/controllers/api/note_subscriptions_controller.rb b/app/controllers/api/note_subscriptions_controller.rb index b307c3d9b8..c416dd8036 100644 --- a/app/controllers/api/note_subscriptions_controller.rb +++ b/app/controllers/api/note_subscriptions_controller.rb @@ -9,14 +9,19 @@ def create note_id = params[:note_id].to_i note = Note.find(note_id) note.subscribers << current_user + rescue ActiveRecord::RecordNotFound + report_error "Note #{note_id} not found.", :not_found rescue ActiveRecord::RecordNotUnique - head :conflict + report_error "You are already subscribed to note #{note_id}.", :conflict end def destroy note_id = params[:note_id].to_i - count = NoteSubscription.where(:user => current_user, :note => note_id).delete_all - head :not_found if count.zero? + note = Note.find(note_id) + count = note.subscriptions.where(:user => current_user).delete_all + report_error "You are not subscribed to note #{note_id}.", :not_found if count.zero? + rescue ActiveRecord::RecordNotFound + report_error "Note #{note_id} not found.", :not_found end end end diff --git a/test/controllers/api/note_subscriptions_controller_test.rb b/test/controllers/api/note_subscriptions_controller_test.rb index f52de58d62..0e388697cc 100644 --- a/test/controllers/api/note_subscriptions_controller_test.rb +++ b/test/controllers/api/note_subscriptions_controller_test.rb @@ -60,6 +60,7 @@ def test_create_fail_note_not_found post api_note_subscription_path(999111), :headers => auth_header assert_response :not_found end + assert_match "not found", @response.body end def test_create_fail_already_subscribed @@ -74,6 +75,7 @@ def test_create_fail_already_subscribed assert_response :conflict end end + assert_match "already subscribed", @response.body end def test_destroy @@ -124,6 +126,7 @@ def test_destroy_fail_note_not_found delete api_note_subscription_path(999111), :headers => auth_header assert_response :not_found + assert_match "not found", @response.body end def test_destroy_fail_not_subscribed @@ -133,6 +136,7 @@ def test_destroy_fail_not_subscribed delete api_note_subscription_path(note), :headers => auth_header assert_response :not_found + assert_match "not subscribed", @response.body end end end