test/controllers/controller_test.rb in jsonapi-resources-0.4.2 vs test/controllers/controller_test.rb in jsonapi-resources-0.4.3

- old
+ new

@@ -52,49 +52,49 @@ assert_equal 2, json_response['data'].size assert_equal 1, json_response['included'].size end def test_index_include_one_level_query_count - query_count = count_queries do + count_queries do get :index, {include: 'author'} end assert_response :success - assert_equal 2, query_count + assert_query_count(2) end def test_index_include_two_levels_query_count - query_count = count_queries do + count_queries do get :index, {include: 'author,author.comments'} end assert_response :success - assert_equal 3, query_count + assert_query_count(3) end def test_index_filter_by_ids_and_fields get :index, {filter: {id: '1,2'}, fields: {posts: 'id,title,author'}} assert_response :success assert_equal 2, json_response['data'].size # type, id, links, attributes, relationships assert_equal 5, json_response['data'][0].size - assert json_response['data'][0].has_key?('type') - assert json_response['data'][0].has_key?('id') - assert json_response['data'][0]['attributes'].has_key?('title') - assert json_response['data'][0].has_key?('links') + assert json_response['data'][0].key?('type') + assert json_response['data'][0].key?('id') + assert json_response['data'][0]['attributes'].key?('title') + assert json_response['data'][0].key?('links') end def test_index_filter_by_ids_and_fields_specify_type get :index, {filter: {id: '1,2'}, 'fields' => {'posts' => 'id,title,author'}} assert_response :success assert_equal 2, json_response['data'].size # type, id, links, attributes, relationships assert_equal 5, json_response['data'][0].size - assert json_response['data'][0].has_key?('type') - assert json_response['data'][0].has_key?('id') - assert json_response['data'][0]['attributes'].has_key?('title') - assert json_response['data'][0].has_key?('links') + assert json_response['data'][0].key?('type') + assert json_response['data'][0].key?('id') + assert json_response['data'][0]['attributes'].key?('title') + assert json_response['data'][0].key?('links') end def test_index_filter_by_ids_and_fields_specify_unrelated_type get :index, {filter: {id: '1,2'}, 'fields' => {'currencies' => 'code'}} assert_response :bad_request @@ -106,26 +106,32 @@ assert_response :success assert_equal 2, json_response['data'].size # type, id, links, relationships assert_equal 4, json_response['data'][0].size - assert json_response['data'][0].has_key?('type') - assert json_response['data'][0].has_key?('id') - assert json_response['data'][0]['relationships'].has_key?('author') + assert json_response['data'][0].key?('type') + assert json_response['data'][0].key?('id') + assert json_response['data'][0]['relationships'].key?('author') end def test_filter_association_single - get :index, {filter: {tags: '5,1'}} + count_queries do + get :index, {filter: {tags: '5,1'}} + end + assert_query_count(1) assert_response :success assert_equal 3, json_response['data'].size assert_match /New post/, response.body assert_match /JR Solves your serialization woes!/, response.body assert_match /JR How To/, response.body end def test_filter_associations_multiple - get :index, {filter: {tags: '5,1', comments: '3'}} + count_queries do + get :index, {filter: {tags: '5,1', comments: '3'}} + end + assert_query_count(1) assert_response :success assert_equal 1, json_response['data'].size assert_match /JR Solves your serialization woes!/, response.body end @@ -225,10 +231,19 @@ assert_equal 'New post', json_response['data']['attributes']['title'] assert_equal 'A body!!!', json_response['data']['attributes']['body'] assert_nil json_response['included'] end + def test_show_does_not_include_records_count_in_meta + JSONAPI.configuration.top_level_meta_include_record_count = true + get :show, { id: Post.first.id } + assert_response :success + assert_equal json_response['meta'], nil + ensure + JSONAPI.configuration.top_level_meta_include_record_count = false + end + def test_show_single_with_includes get :show, {id: '1', include: 'comments'} assert_response :success assert json_response['data'].is_a?(Hash) assert_equal 'New post', json_response['data']['attributes']['title'] @@ -315,11 +330,11 @@ } } } assert_response :unprocessable_entity - # Todo: check if this validation is working + # TODO: check if this validation is working assert_match /author - can't be blank/, response.body end def test_create_extra_param set_content_type_header! @@ -619,10 +634,32 @@ assert_equal 'AAAA', json_response['data']['attributes']['body'] assert matches_array?([{'type' => 'tags', 'id' => '3'}, {'type' => 'tags', 'id' => '4'}], json_response['data']['relationships']['tags']['data']) end + def test_update_with_internal_server_error + set_content_type_header! + post_object = Post.find(3) + title = post_object.title + + put :update, + { + id: 3, + data: { + id: '3', + type: 'posts', + attributes: { + title: 'BOOM' + } + } + } + + assert_response 500 + post_object = Post.find(3) + assert_equal title, post_object.title + end + def test_update_remove_links set_content_type_header! put :update, { id: 3, @@ -1516,20 +1553,20 @@ def test_expense_entries_show_fields get :show, {id: 1, include: 'isoCurrency,employee', 'fields' => {'expenseEntries' => 'transactionDate'}} assert_response :success assert json_response['data'].is_a?(Hash) - assert json_response['data']['attributes'].has_key?('transactionDate') + assert json_response['data']['attributes'].key?('transactionDate') assert_equal 2, json_response['included'].size end def test_expense_entries_show_fields_type_many get :show, {id: 1, include: 'isoCurrency,employee', 'fields' => {'expenseEntries' => 'transactionDate', 'isoCurrencies' => 'id,name'}} assert_response :success assert json_response['data'].is_a?(Hash) - assert json_response['data']['attributes'].has_key?('transactionDate') + assert json_response['data']['attributes'].key?('transactionDate') assert_equal 2, json_response['included'].size end def test_create_expense_entries_underscored set_content_type_header! @@ -1861,11 +1898,11 @@ end def test_get_related_resource JSONAPI.configuration.json_key_format = :dasherized_key JSONAPI.configuration.route_format = :underscored_key - get :get_related_resource, {post_id: '2', association: 'author', :source=>'posts'} + get :get_related_resource, {post_id: '2', association: 'author', source:'posts'} assert_response :success assert_hash_equals( { data: { id: '1', @@ -1905,20 +1942,26 @@ "links" => { "self" => "http://test.host/people/1/relationships/hair_cut", "related" => "http://test.host/people/1/hair_cut" }, "data" => nil - } + }, + vehicles: { + links: { + self: "http://test.host/people/1/relationships/vehicles", + related: "http://test.host/people/1/vehicles" + } + } } } }, json_response ) end def test_get_related_resource_nil - get :get_related_resource, {post_id: '17', association: 'author', :source=>'posts'} + get :get_related_resource, {post_id: '17', association: 'author', source:'posts'} assert_response :success assert_hash_equals json_response, { data: nil } @@ -2131,10 +2174,11 @@ end class Api::V2::BooksControllerTest < ActionController::TestCase def setup JSONAPI.configuration.json_key_format = :dasherized_key + $test_user = Person.find(1) end def after_teardown Api::V2::BookResource.paginator :offset end @@ -2153,11 +2197,11 @@ JSONAPI.configuration.top_level_meta_include_record_count = true get :index, {include: 'book-comments'} JSONAPI.configuration.top_level_meta_include_record_count = false assert_response :success - assert_equal 1000, json_response['meta']['record-count'] + assert_equal 901, json_response['meta']['record-count'] assert_equal 10, json_response['data'].size assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] end def test_books_record_count_in_meta_custom_name @@ -2168,37 +2212,37 @@ get :index, {include: 'book-comments'} JSONAPI.configuration.top_level_meta_include_record_count = false JSONAPI.configuration.top_level_meta_record_count_key = :record_count assert_response :success - assert_equal 1000, json_response['meta']['total-records'] + assert_equal 901, json_response['meta']['total-records'] assert_equal 10, json_response['data'].size assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] end def test_books_offset_pagination_no_params_includes_query_count_one_level Api::V2::BookResource.paginator :offset - query_count = count_queries do + count_queries do get :index, {include: 'book-comments'} end assert_response :success assert_equal 10, json_response['data'].size assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] - assert_equal 3, query_count + assert_query_count(3) end def test_books_offset_pagination_no_params_includes_query_count_two_levels Api::V2::BookResource.paginator :offset - query_count = count_queries do + count_queries do get :index, {include: 'book-comments,book-comments.author'} end assert_response :success assert_equal 10, json_response['data'].size assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] - assert_equal 4, query_count + assert_query_count(4) end def test_books_offset_pagination Api::V2::BookResource.paginator :offset @@ -2313,12 +2357,198 @@ get :index, {page: 3} assert_response :success assert_equal 10, json_response['data'].size assert_equal 'Book 20', json_response['data'][0]['attributes']['title'] end + + def test_books_included_paged + Api::V2::BookResource.paginator :offset + + count_queries do + get :index, {filter: {id: '0'}, include: 'book-comments'} + end + assert_response :success + assert_equal 1, json_response['data'].size + assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] + assert_query_count(3) + end + + def test_books_banned_non_book_admin + $test_user = Person.find(1) + Api::V2::BookResource.paginator :offset + count_queries do + JSONAPI.configuration.top_level_meta_include_record_count = true + get :index, {page: {offset: 50, limit: 12}} + JSONAPI.configuration.top_level_meta_include_record_count = false + end + assert_response :success + assert_equal 12, json_response['data'].size + assert_equal 'Book 50', json_response['data'][0]['attributes']['title'] + assert_equal 901, json_response['meta']['record-count'] + assert_query_count(2) + end + + def test_books_banned_non_book_admin_includes_switched + $test_user = Person.find(1) + Api::V2::BookResource.paginator :offset + count_queries do + JSONAPI.configuration.top_level_meta_include_record_count = true + get :index, {page: {offset: 0, limit: 12}, include: 'book-comments'} + JSONAPI.configuration.top_level_meta_include_record_count = false + end + + assert_response :success + assert_equal 12, json_response['data'].size + assert_equal 130, json_response['included'].size + assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] + assert_equal 26, json_response['data'][0]['relationships']['book-comments']['data'].size + assert_equal 'book-comments', json_response['included'][0]['type'] + assert_equal 901, json_response['meta']['record-count'] + assert_query_count(3) + end + + def test_books_banned_non_book_admin_includes_nested_includes + $test_user = Person.find(1) + Api::V2::BookResource.paginator :offset + count_queries do + JSONAPI.configuration.top_level_meta_include_record_count = true + get :index, {page: {offset: 0, limit: 12}, include: 'book-comments.author'} + JSONAPI.configuration.top_level_meta_include_record_count = false + end + assert_response :success + assert_equal 12, json_response['data'].size + assert_equal 131, json_response['included'].size + assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] + assert_equal 901, json_response['meta']['record-count'] + assert_query_count(4) + end + + def test_books_banned_admin + $test_user = Person.find(5) + Api::V2::BookResource.paginator :offset + query_count = count_queries do + JSONAPI.configuration.top_level_meta_include_record_count = true + get :index, {page: {offset: 50, limit: 12}, filter: {banned: 'true'}} + JSONAPI.configuration.top_level_meta_include_record_count = false + end + assert_response :success + assert_equal 12, json_response['data'].size + assert_equal 'Book 651', json_response['data'][0]['attributes']['title'] + assert_equal 99, json_response['meta']['record-count'] + assert_query_count(2) + end + + def test_books_not_banned_admin + $test_user = Person.find(5) + Api::V2::BookResource.paginator :offset + count_queries do + JSONAPI.configuration.top_level_meta_include_record_count = true + get :index, {page: {offset: 50, limit: 12}, filter: {banned: 'false'}} + JSONAPI.configuration.top_level_meta_include_record_count = false + end + assert_response :success + assert_equal 12, json_response['data'].size + assert_equal 'Book 50', json_response['data'][0]['attributes']['title'] + assert_equal 901, json_response['meta']['record-count'] + assert_query_count(2) + end + + def test_books_banned_non_book_admin_overlapped + $test_user = Person.find(1) + Api::V2::BookResource.paginator :offset + count_queries do + JSONAPI.configuration.top_level_meta_include_record_count = true + get :index, {page: {offset: 590, limit: 20}} + JSONAPI.configuration.top_level_meta_include_record_count = false + end + assert_response :success + assert_equal 20, json_response['data'].size + assert_equal 'Book 590', json_response['data'][0]['attributes']['title'] + assert_equal 901, json_response['meta']['record-count'] + assert_query_count(2) + end + + def test_books_included_exclude_unapproved + $test_user = Person.find(1) + Api::V2::BookResource.paginator :none + + count_queries do + get :index, {filter: {id: '0,1,2,3,4'}, include: 'book-comments'} + end + assert_response :success + assert_equal 5, json_response['data'].size + assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] + assert_equal 130, json_response['included'].size + assert_equal 26, json_response['data'][0]['relationships']['book-comments']['data'].size + assert_query_count(2) + end + + def test_books_included_all_comments_for_admin + $test_user = Person.find(5) + Api::V2::BookResource.paginator :none + + get :index, {filter: {id: '0,1,2,3,4'}, include: 'book-comments'} + assert_response :success + assert_equal 5, json_response['data'].size + assert_equal 'Book 0', json_response['data'][0]['attributes']['title'] + assert_equal 255, json_response['included'].size + assert_equal 51, json_response['data'][0]['relationships']['book-comments']['data'].size + end + + def test_books_filter_by_book_comment_id_limited_user + $test_user = Person.find(1) + get :index, {filter: {book_comments: '0,52' }} + assert_response :success + assert_equal 1, json_response['data'].size + end + + def test_books_filter_by_book_comment_id_admin_user + $test_user = Person.find(5) + get :index, {filter: {book_comments: '0,52' }} + assert_response :success + assert_equal 2, json_response['data'].size + end end +class Api::V2::BookCommentsControllerTest < ActionController::TestCase + def setup + JSONAPI.configuration.json_key_format = :dasherized_key + Api::V2::BookCommentResource.paginator :none + $test_user = Person.find(1) + end + + def test_book_comments_all_for_admin + $test_user = Person.find(5) + count_queries do + get :index + end + assert_response :success + assert_equal 255, json_response['data'].size + assert_query_count(1) + end + + def test_book_comments_unapproved_context_based + $test_user = Person.find(5) + count_queries do + get :index, {filter: {approved: 'false'}} + end + assert_response :success + assert_equal 125, json_response['data'].size + assert_query_count(1) + end + + def test_book_comments_exclude_unapproved_context_based + $test_user = Person.find(1) + count_queries do + get :index + end + assert_response :success + assert_equal 130, json_response['data'].size + assert_query_count(1) + end +end + class Api::V4::BooksControllerTest < ActionController::TestCase def setup JSONAPI.configuration.json_key_format = :camelized_key end @@ -2327,11 +2557,11 @@ Api::V4::BookResource.paginator :offset get :index, {page: {offset: 50, limit: 12}} assert_response :success assert_equal 12, json_response['data'].size assert_equal 'Book 50', json_response['data'][0]['attributes']['title'] - assert_equal 1000, json_response['meta']['totalRecords'] + assert_equal 901, json_response['meta']['totalRecords'] JSONAPI.configuration.operations_processor = :active_record end def test_books_operation_links JSONAPI.configuration.operations_processor = :counting_active_record @@ -2395,6 +2625,6 @@ } assert_response :unprocessable_entity assert_match /Save failed or was cancelled/, json_response['errors'][0]['detail'] end -end \ No newline at end of file +end