mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Cleanup assertions in isolated jsonapi renderer tests a bit
This commit is contained in:
parent
f246741cc5
commit
4394f76b86
@ -12,8 +12,16 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
end
|
end
|
||||||
|
|
||||||
def render_with_jsonapi_renderer
|
def render_with_jsonapi_renderer
|
||||||
unlocked_params = Rails::VERSION::MAJOR >= 5 ? params.to_unsafe_h : params
|
permitted_params = params.permit(data: [:id, :type, attributes: [:name]])
|
||||||
attributes = unlocked_params[:data].present? ? unlocked_params[:data][:attributes] : {}
|
permitted_params = permitted_params.to_h.with_indifferent_access
|
||||||
|
attributes =
|
||||||
|
if permitted_params[:data]
|
||||||
|
permitted_params[:data][:attributes].merge(id: permitted_params[:data][:id])
|
||||||
|
else
|
||||||
|
# Rails returns empty params when no mime type can be negotiated.
|
||||||
|
# (Until https://github.com/rails/rails/pull/26632 is reviewed.)
|
||||||
|
permitted_params
|
||||||
|
end
|
||||||
author = Author.new(attributes)
|
author = Author.new(attributes)
|
||||||
render jsonapi: author
|
render jsonapi: author
|
||||||
end
|
end
|
||||||
@ -34,6 +42,17 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
assert_equal(expected, TestController.last_request_parameters)
|
assert_equal(expected, TestController.last_request_parameters)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def define_author_model_and_serializer
|
||||||
|
TestController.const_set(:Author, Class.new(ActiveModelSerializers::Model) do
|
||||||
|
attributes :name
|
||||||
|
end)
|
||||||
|
TestController.const_set(:AuthorSerializer, Class.new(ActiveModel::Serializer) do
|
||||||
|
type 'users'
|
||||||
|
attribute :id
|
||||||
|
attribute :name
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
class WithoutRenderer < JsonApiRendererTest
|
class WithoutRenderer < JsonApiRendererTest
|
||||||
setup do
|
setup do
|
||||||
require 'rails'
|
require 'rails'
|
||||||
@ -49,6 +68,7 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
match ':action', to: TestController, via: [:get, :post]
|
match ':action', to: TestController, via: [:get, :post]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
define_author_model_and_serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_jsonapi_parser_not_registered
|
def test_jsonapi_parser_not_registered
|
||||||
@ -61,12 +81,12 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
end
|
end
|
||||||
|
|
||||||
def test_jsonapi_renderer_not_registered
|
def test_jsonapi_renderer_not_registered
|
||||||
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
|
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
|
||||||
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
||||||
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
||||||
assert_equal 500, response.status
|
|
||||||
assert_equal '', response.body
|
assert_equal '', response.body
|
||||||
assert response.request.env['action_dispatch.exception'].is_a?(ActionView::MissingTemplate) if response.request.present?
|
assert_equal 500, response.status
|
||||||
|
assert_equal ActionView::MissingTemplate, request.env['action_dispatch.exception'].class
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_jsonapi_parser
|
def test_jsonapi_parser
|
||||||
@ -94,6 +114,7 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
match ':action', to: TestController, via: [:get, :post]
|
match ':action', to: TestController, via: [:get, :post]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
define_author_model_and_serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_jsonapi_parser_registered
|
def test_jsonapi_parser_registered
|
||||||
@ -109,18 +130,13 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
def test_jsonapi_renderer_registered
|
def test_jsonapi_renderer_registered
|
||||||
expected = {
|
expected = {
|
||||||
'data' => {
|
'data' => {
|
||||||
'id' => 'author',
|
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765',
|
||||||
'type' => 'authors',
|
'type' => 'users',
|
||||||
'attributes' => { 'name' => 'Johnny Rico' },
|
'attributes' => { 'name' => 'Johnny Rico' }
|
||||||
'relationships' => {
|
|
||||||
'posts' => { 'data' => nil },
|
|
||||||
'roles' => { 'data' => nil },
|
|
||||||
'bio' => { 'data' => nil }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
|
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
|
||||||
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
|
||||||
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
post '/render_with_jsonapi_renderer', params: payload, headers: headers
|
||||||
assert_equal expected.to_json, response.body
|
assert_equal expected.to_json, response.body
|
||||||
@ -133,10 +149,11 @@ class JsonApiRendererTest < ActionDispatch::IntegrationTest
|
|||||||
'attributes' => {
|
'attributes' => {
|
||||||
'name' => 'John Doe'
|
'name' => 'John Doe'
|
||||||
},
|
},
|
||||||
'type' => 'users'
|
'type' => 'users',
|
||||||
|
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765'
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}',
|
'{"data": {"attributes": {"name": "John Doe"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}',
|
||||||
'CONTENT_TYPE' => 'application/vnd.api+json'
|
'CONTENT_TYPE' => 'application/vnd.api+json'
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user