test/controllers/controller_test.rb in jsonapi-resources-0.5.9 vs test/controllers/controller_test.rb in jsonapi-resources-0.6.0

- old
+ new

@@ -1206,25 +1206,43 @@ put :update_relationship, {post_id: 14, relationship: 'tags', data: [{type: 'tags', id: 2}, {type: 'tags', id: 3}]} assert_response :no_content p = Post.find(14) assert_equal [2, 3], p.tag_ids - delete :destroy_relationship, {post_id: 14, relationship: 'tags', keys: '3'} + delete :destroy_relationship, {post_id: 14, relationship: 'tags', data: [{type: 'tags', id: 3}]} p.reload assert_response :no_content assert_equal [2], p.tag_ids end + def test_delete_relationship_to_many_with_relationship_url_not_matching_type + set_content_type_header! + PostResource.has_many :special_tags, relation_name: :special_tags, class_name: "Tag" + post :create_relationship, {post_id: 14, relationship: 'special_tags', data: [{type: 'tags', id: 2}]} + + #check the relationship was created successfully + assert_equal 1, Post.find(14).special_tags.count + before_tags = Post.find(14).tags.count + + delete :destroy_relationship, {post_id: 14, relationship: 'special_tags', data: [{type: 'tags', id: 2}]} + assert_equal 0, Post.find(14).special_tags.count, "Relationship that matches URL relationship not destroyed" + + #check that the tag association is not affected + assert_equal Post.find(14).tags.count, before_tags + ensure + PostResource.instance_variable_get(:@_relationships).delete(:special_tags) + end + def test_delete_relationship_to_many_does_not_exist set_content_type_header! put :update_relationship, {post_id: 14, relationship: 'tags', data: [{type: 'tags', id: 2}, {type: 'tags', id: 3}]} assert_response :no_content p = Post.find(14) assert_equal [2, 3], p.tag_ids - delete :destroy_relationship, {post_id: 14, relationship: 'tags', keys: '4'} + delete :destroy_relationship, {post_id: 14, relationship: 'tags', data: [{type: 'tags', id: 4}]} p.reload assert_response :not_found assert_equal [2, 3], p.tag_ids end @@ -2825,10 +2843,58 @@ $test_user = Person.find(5) get :index, {filter: {book_comments: '0,52' }} assert_response :success assert_equal 2, json_response['data'].size end + + def test_books_create_unapproved_comment_limited_user_using_relation_name + set_content_type_header! + $test_user = Person.find(1) + + book_comment = BookComment.create(body: 'Not Approved dummy comment', approved: false) + post :create_relationship, {book_id: 1, relationship: 'book_comments', data: [{type: 'book_comments', id: book_comment.id}]} + + # Note the not_found response is coming from the BookComment's overridden records method, not the relation + assert_response :not_found + + ensure + book_comment.delete + end + + def test_books_create_approved_comment_limited_user_using_relation_name + set_content_type_header! + $test_user = Person.find(1) + + book_comment = BookComment.create(body: 'Approved dummy comment', approved: true) + post :create_relationship, {book_id: 1, relationship: 'book_comments', data: [{type: 'book_comments', id: book_comment.id}]} + assert_response :success + + ensure + book_comment.delete + end + + def test_books_delete_unapproved_comment_limited_user_using_relation_name + $test_user = Person.find(1) + + book_comment = BookComment.create(book_id: 1, body: 'Not Approved dummy comment', approved: false) + delete :destroy_relationship, {book_id: 1, relationship: 'book_comments', data: [{type: 'book_comments', id: book_comment.id}]} + assert_response :not_found + + ensure + book_comment.delete + end + + def test_books_delete_approved_comment_limited_user_using_relation_name + $test_user = Person.find(1) + + book_comment = BookComment.create(book_id: 1, body: 'Approved dummy comment', approved: true) + delete :destroy_relationship, {book_id: 1, relationship: 'book_comments', data: [{type: 'book_comments', id: book_comment.id}]} + assert_response :no_content + + ensure + book_comment.delete + end end class Api::V2::BookCommentsControllerTest < ActionController::TestCase def setup JSONAPI.configuration.json_key_format = :dasherized_key @@ -2968,9 +3034,21 @@ planet: {links: {self: "http://test.host/moons/1/relationships/planet", related: "http://test.host/moons/1/planet"}}, craters: {links: {self: "http://test.host/moons/1/relationships/craters", related: "http://test.host/moons/1/craters"}}} } } + end + + def test_get_related_resources_with_select_some_db_columns + PlanetResource.paginator :paged + original_config = JSONAPI.configuration.dup + JSONAPI.configuration.top_level_meta_include_record_count = true + JSONAPI.configuration.json_key_format = :dasherized_key + get :get_related_resources, {planet_id: '1', relationship: 'moons', source: 'api/v1/planets'} + assert_response :success + assert_equal 1, json_response['meta']['record-count'] + ensure + JSONAPI.configuration = original_config end end class Api::V1::CratersControllerTest < ActionController::TestCase def test_show_single