active_model_serializers/test/action_controller/namespace_lookup_test.rb
Benjamin Fleischer 93ca27fe44 Merge 0-10-stable into master (to fix breaking change). (#2023)
* Merge pull request #1990 from mxie/mx-result-typo

Fix typos and capitalization in Relationship Links docs [ci skip]

* Merge pull request #1992 from ojiry/bump_ruby_versions

Run tests by Ruby 2.2.6 and 2.3.3

* Merge pull request #1994 from bf4/promote_architecture

Promote important architecture description that answers a lot of questions we get
Conflicts:
	docs/ARCHITECTURE.md

* Merge pull request #1999 from bf4/typos

Fix typos [ci skip]

* Merge pull request #2000 from berfarah/patch-1

Link to 0.10.3 tag instead of `master` branch

* Merge pull request #2007 from bf4/check_ci

Test was failing due to change in JSON exception message when parsing empty string

* Swap out KeyTransform for CaseTransform (#1993)

* delete KeyTransform, use CaseTransform

* added changelog

Conflicts:
	CHANGELOG.md

* Merge pull request #2005 from kofronpi/support-ruby-2.4

Update jsonapi runtime dependency to 0.1.1.beta6

* Bump to v0.10.4

* Merge pull request #2018 from rails-api/bump_version

Bump to v0.10.4 [ci skip]
Conflicts:
	CHANGELOG.md

* Merge pull request #2019 from bf4/fix_method_redefined_warning

Fix AMS warnings

* Merge pull request #2020 from bf4/silence_grape_warnings

Silence Grape warnings

* Merge pull request #2017 from bf4/remove_warnings

Fix mt6 assert_nil warnings

* Updated isolated tests to assert correct behavior. (#2010)

* Updated isolated tests to assert correct behavior.
* Added check to get unsafe params if rails version is great than 5

* Merge pull request #2012 from bf4/cleanup_isolated_jsonapi_renderer_tests_a_bit

Cleanup assertions in isolated jsonapi renderer tests a bit

* Add Model#attributes helper; make test attributes explicit

* Fix model attributes accessors

* Fix typos

* Randomize testing of compatibility layer against regressions

* Test bugfix

* Add CHANGELOG

* Merge pull request #1981 from groyoh/link_doc

Fix relationship links doc
Conflicts:
	CHANGELOG.md
2017-01-10 02:28:50 -06:00

233 lines
6.7 KiB
Ruby

require 'test_helper'
module ActionController
module Serialization
class NamespaceLookupTest < ActionController::TestCase
class Book < ::Model
attributes :id, :title, :body
associations :writer, :chapters
end
class Chapter < ::Model
attributes :title
end
class Writer < ::Model
attributes :name
end
module Api
module V2
class BookSerializer < ActiveModel::Serializer
attributes :title
end
end
module VHeader
class BookSerializer < ActiveModel::Serializer
attributes :title, :body
def body
'header'
end
end
end
module V3
class BookSerializer < ActiveModel::Serializer
attributes :title, :body
belongs_to :writer
has_many :chapters
end
class ChapterSerializer < ActiveModel::Serializer
attribute :title do
"Chapter - #{object.title}"
end
end
class WriterSerializer < ActiveModel::Serializer
attributes :name
end
class LookupTestController < ActionController::Base
before_action only: [:namespace_set_in_before_filter] do
self.namespace_for_serializer = Api::V2
end
def implicit_namespaced_serializer
writer = Writer.new(name: 'Bob')
book = Book.new(title: 'New Post', body: 'Body', writer: writer, chapters: [])
render json: book
end
def implicit_namespaced_collection_serializer
chapter1 = Chapter.new(title: 'Oh')
chapter2 = Chapter.new(title: 'Oh my')
render json: [chapter1, chapter2]
end
def implicit_has_many_namespaced_serializer
chapter1 = Chapter.new(title: 'Odd World')
chapter2 = Chapter.new(title: 'New World')
book = Book.new(title: 'New Post', body: 'Body', chapters: [chapter1, chapter2])
render json: book
end
def explicit_namespace_as_module
book = Book.new(title: 'New Post', body: 'Body')
render json: book, namespace: Api::V2
end
def explicit_namespace_as_string
book = Book.new(title: 'New Post', body: 'Body')
# because this is a string, ruby can't auto-lookup the constant, so otherwise
# the lookup thinks we mean ::Api::V2
render json: book, namespace: 'ActionController::Serialization::NamespaceLookupTest::Api::V2'
end
def explicit_namespace_as_symbol
book = Book.new(title: 'New Post', body: 'Body')
# because this is a string, ruby can't auto-lookup the constant, so otherwise
# the lookup thinks we mean ::Api::V2
render json: book, namespace: :'ActionController::Serialization::NamespaceLookupTest::Api::V2'
end
def invalid_namespace
book = Book.new(id: 'invalid_namespace_book_id', title: 'New Post', body: 'Body')
render json: book, namespace: :api_v2
end
def namespace_set_in_before_filter
book = Book.new(title: 'New Post', body: 'Body')
render json: book
end
def namespace_set_by_request_headers
book = Book.new(title: 'New Post', body: 'Body')
version_from_header = request.headers['X-API_VERSION']
namespace = "ActionController::Serialization::NamespaceLookupTest::#{version_from_header}"
render json: book, namespace: namespace
end
end
end
end
tests Api::V3::LookupTestController
setup do
@test_namespace = self.class.parent
end
test 'uses request headers to determine the namespace' do
request.env['X-API_VERSION'] = 'Api::VHeader'
get :namespace_set_by_request_headers
assert_serializer Api::VHeader::BookSerializer
end
test 'implicitly uses namespaced serializer' do
get :implicit_namespaced_serializer
assert_serializer Api::V3::BookSerializer
expected = { 'title' => 'New Post', 'body' => 'Body', 'writer' => { 'name' => 'Bob' }, 'chapters' => [] }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'implicitly uses namespaced serializer for collection' do
get :implicit_namespaced_collection_serializer
assert_serializer 'ActiveModel::Serializer::CollectionSerializer'
expected = [{ 'title' => 'Chapter - Oh' }, { 'title' => 'Chapter - Oh my' }]
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'implicitly uses namespaced serializer for has_many' do
get :implicit_has_many_namespaced_serializer
assert_serializer Api::V3::BookSerializer
expected = {
'title' => 'New Post',
'body' => 'Body', 'writer' => nil,
'chapters' => [
{ 'title' => 'Chapter - Odd World' },
{ 'title' => 'Chapter - New World' }
]
}
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'explicit namespace as module' do
get :explicit_namespace_as_module
assert_serializer Api::V2::BookSerializer
expected = { 'title' => 'New Post' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'explicit namespace as string' do
get :explicit_namespace_as_string
assert_serializer Api::V2::BookSerializer
expected = { 'title' => 'New Post' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'explicit namespace as symbol' do
get :explicit_namespace_as_symbol
assert_serializer Api::V2::BookSerializer
expected = { 'title' => 'New Post' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'invalid namespace' do
get :invalid_namespace
assert_serializer ActiveModel::Serializer::Null
expected = { 'id' => 'invalid_namespace_book_id', 'title' => 'New Post', 'body' => 'Body' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
test 'namespace set in before filter' do
get :namespace_set_in_before_filter
assert_serializer Api::V2::BookSerializer
expected = { 'title' => 'New Post' }
actual = JSON.parse(@response.body)
assert_equal expected, actual
end
end
end
end