Skip to content

Commit 230e244

Browse files
committed
Tests validation behavior on :base
1 parent 0c31405 commit 230e244

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

test/controllers/controller_test.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,11 +1869,21 @@ def test_update_bad_attributes
18691869
assert_response :bad_request
18701870
end
18711871

1872-
def test_delete_with_validation_error
1872+
def test_delete_with_validation_error_base
18731873
post = Post.create!(title: "can't destroy me", author: Person.first)
18741874
delete :destroy, params: { id: post.id }
18751875

18761876
assert_equal "can't destroy me", json_response['errors'][0]['title']
1877+
assert_equal "/data", json_response['errors'][0]['source']['pointer']
1878+
assert_response :unprocessable_entity
1879+
end
1880+
1881+
def test_delete_with_validation_error_attr
1882+
post = Post.create!(title: "locked title", author: Person.first)
1883+
delete :destroy, params: { id: post.id }
1884+
1885+
assert_equal "is locked", json_response['errors'][0]['title']
1886+
assert_equal "/data/attributes/title", json_response['errors'][0]['source']['pointer']
18771887
assert_response :unprocessable_entity
18781888
end
18791889

@@ -3755,6 +3765,15 @@ def test_caching_with_join_from_resource_with_sql_fragment
37553765
assert_cacheable_get :index, params: {include: 'section'}
37563766
assert_response :success
37573767
end
3768+
3769+
def test_delete_with_validation_error_base_on_resource
3770+
post = Post.create!(title: "can't destroy me either", author: Person.first)
3771+
delete :destroy, params: { id: post.id }
3772+
3773+
assert_equal "can't destroy me", json_response['errors'][0]['title']
3774+
assert_equal "/data/attributes/base", json_response['errors'][0]['source']['pointer']
3775+
assert_response :unprocessable_entity
3776+
end
37583777
end
37593778

37603779
class Api::V6::SectionsControllerTest < ActionController::TestCase

test/fixtures/active_record.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,19 @@ class Post < ActiveRecord::Base
376376
before_destroy :destroy_callback
377377

378378
def destroy_callback
379-
if title == "can't destroy me"
380-
errors.add(:title, "can't destroy me")
379+
case title
380+
when "can't destroy me", "can't destroy me either"
381+
errors.add(:base, "can't destroy me")
382+
383+
# :nocov:
384+
if Rails::VERSION::MAJOR >= 5
385+
throw(:abort)
386+
else
387+
return false
388+
end
389+
# :nocov:
390+
when "locked title"
391+
errors.add(:title, "is locked")
381392

382393
# :nocov:
383394
if Rails::VERSION::MAJOR >= 5
@@ -1775,6 +1786,12 @@ class PostResource < PostResource
17751786
def self.records(options = {})
17761787
_model_class.all.joins('INNER JOIN people on people.id = author_id')
17771788
end
1789+
1790+
attribute :base
1791+
1792+
def base
1793+
_model.title
1794+
end
17781795
end
17791796

17801797
class CustomerResource < JSONAPI::Resource

0 commit comments

Comments
 (0)