From 4b003411c5dcbf071d0137fd8f7e201cfc7a1128 Mon Sep 17 00:00:00 2001 From: Denis Talakevich Date: Fri, 26 Feb 2016 12:42:32 +0200 Subject: [PATCH] add tests about link check for nested resource routes --- test/fixtures/active_record.rb | 29 ++++++++++ test/integration/requests/request_test.rb | 70 +++++++++++++++++++++++ test/test_helper.rb | 6 ++ 3 files changed, 105 insertions(+) diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index c0dad8b0b..f778afad8 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -643,6 +643,18 @@ class AuthorsController < JSONAPI::ResourceController end ### CONTROLLERS +module NestedApi + class PostsController < ActionController::Base + include JSONAPI::ActsAsResourceController + def context + {writer_id: params[:writer_id]} + end + end + class WritersController < ActionController::Base + include JSONAPI::ActsAsResourceController + end +end + module Api module V1 class AuthorsController < JSONAPI::ResourceController @@ -1208,6 +1220,23 @@ def custom_links(options) end end +module NestedApi + class WriterResource < JSONAPI::Resource + attributes :name + model_name 'Person' + has_many :posts + end + + class PostResource < JSONAPI::Resource + attributes :title, :body + has_one :writer, foreign_key: 'author_id', class_name: 'Writer' + + def self.records(options = {}) + context = options.fetch(:context, {}) + super.where(author_id: context[:writer_id]) + end + end +end module Api module V1 diff --git a/test/integration/requests/request_test.rb b/test/integration/requests/request_test.rb index 2e603ed7d..39d21c144 100644 --- a/test/integration/requests/request_test.rb +++ b/test/integration/requests/request_test.rb @@ -493,6 +493,54 @@ def test_pagination_empty_results # assert_equal 'This is comment 18 on book 1.', json_response['data'][9]['attributes']['body'] # end + def test_nested_route_relation_links + original_config = JSONAPI.configuration.dup + JSONAPI.configuration.json_key_format = :underscored_key + JSONAPI.configuration.route_format = :underscored_route + + get "/nested_api/writers/#{$test_user.id}/posts" + assert_jsonapi_response 200 + post_1 = json_response['data'][0] + assert_hash_equals({ + 'self' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts/#{post_1['id']}/relationships/writer", + 'related' => "http://www.example.com/nested_api/writers/#{$test_user.id}" + }, post_1['relationships']['writer']['links']) + ensure + JSONAPI.configuration = original_config + end + + def test_nested_route_pagination_links + original_config = JSONAPI.configuration.dup + JSONAPI.configuration.json_key_format = :underscored_key + JSONAPI.configuration.route_format = :underscored_route + NestedApi::PostResource.paginator :paged + + get "/nested_api/writers/#{$test_user.id}/posts?page[size]=1" + assert_jsonapi_response 200 + assert_hash_equals({ + 'first' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts?page%5Bnumber%5D=1&page%5Bsize%5D=1", + 'next' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts?page%5Bnumber%5D=2&page%5Bsize%5D=1", + 'last' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts?page%5Bnumber%5D=3&page%5Bsize%5D=1" + }, json_response['links']) + ensure + JSONAPI.configuration = original_config + end + + def test_nested_route_self_links + original_config = JSONAPI.configuration.dup + JSONAPI.configuration.json_key_format = :underscored_key + JSONAPI.configuration.route_format = :underscored_route + + get "/nested_api/writers/#{$test_user.id}/posts" + assert_jsonapi_response 200 + post_1 = json_response['data'][0] + assert_hash_equals({ + 'self' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts/#{post_1['id']}" + }, post_1['links']) + ensure + JSONAPI.configuration = original_config + end + def test_flow_self get '/posts' @@ -504,6 +552,28 @@ def test_flow_self assert_hash_equals post_1, json_response['data'] end + def test_flow_link_to_one_self_link_for_nested_resources + original_config = JSONAPI.configuration.dup + JSONAPI.configuration.json_key_format = :underscored_key + JSONAPI.configuration.route_format = :underscored_route + + get "/nested_api/writers/#{$test_user.id}/posts" + assert_jsonapi_response 200 + post_1 = json_response['data'][0] + + get post_1['relationships']['writer']['links']['self'] + assert_jsonapi_response 200 + assert_hash_equals({ + 'links' => { + 'self' => "http://www.example.com/nested_api/writers/#{$test_user.id}/posts/#{post_1['id']}/relationships/writer", + 'related' => "http://www.example.com/nested_api/writers/#{$test_user.id}" + }, + 'data' => {type: 'people', id: $test_user.id.to_s} + }, json_response) + ensure + JSONAPI.configuration = original_config + end + def test_flow_link_to_one_self_link get '/posts' assert_jsonapi_response 200 diff --git a/test/test_helper.rb b/test/test_helper.rb index e2b96ecc0..74577a98e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -149,6 +149,12 @@ class CatResource < JSONAPI::Resource jsonapi_resources :books jsonapi_resources :authors + namespace :nested_api do + jsonapi_resources :writers do + jsonapi_resources :posts + end + end + namespace :api do namespace :v1 do jsonapi_resources :people