Skip to content

Commit

Permalink
Merge pull request #2012 from bf4/cleanup_isolated_jsonapi_renderer_t…
Browse files Browse the repository at this point in the history
…ests_a_bit

Cleanup assertions in isolated jsonapi renderer tests a bit
  • Loading branch information
bf4 committed Jan 7, 2017
1 parent 59aed4d commit 85dfef9
Showing 1 changed file with 33 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ class << self
end

def render_with_jsonapi_renderer
unlocked_params = Rails::VERSION::MAJOR >= 5 ? params.to_unsafe_h : params
attributes = unlocked_params[:data].present? ? unlocked_params[:data][:attributes] : {}
permitted_params = params.permit(data: [:id, :type, attributes: [:name]])
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)
render jsonapi: author
end
Expand All @@ -34,6 +42,17 @@ def assert_parses(expected, actual, headers = {})
assert_equal(expected, TestController.last_request_parameters)
end

def define_author_model_and_serializer
TestController.const_set(:Author, Class.new(ActiveModelSerializers::Model) do
attr_accessor :id, :name
end)
TestController.const_set(:AuthorSerializer, Class.new(ActiveModel::Serializer) do
type 'users'
attribute :id
attribute :name
end)
end

class WithoutRenderer < JsonApiRendererTest
setup do
require 'rails'
Expand All @@ -49,6 +68,7 @@ class WithoutRenderer < JsonApiRendererTest
match ':action', to: TestController, via: [:get, :post]
end
end
define_author_model_and_serializer
end

def test_jsonapi_parser_not_registered
Expand All @@ -61,12 +81,12 @@ def test_jsonapi_parser_not_registered
end

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' }
post '/render_with_jsonapi_renderer', params: payload, headers: headers
assert_equal 500, response.status
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

def test_jsonapi_parser
Expand Down Expand Up @@ -94,6 +114,7 @@ class WithRenderer < JsonApiRendererTest
match ':action', to: TestController, via: [:get, :post]
end
end
define_author_model_and_serializer
end

def test_jsonapi_parser_registered
Expand All @@ -109,18 +130,13 @@ def test_jsonapi_parser_registered
def test_jsonapi_renderer_registered
expected = {
'data' => {
'id' => 'author',
'type' => 'authors',
'attributes' => { 'name' => 'Johnny Rico' },
'relationships' => {
'posts' => { 'data' => nil },
'roles' => { 'data' => nil },
'bio' => { 'data' => nil }
}
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765',
'type' => 'users',
'attributes' => { 'name' => 'Johnny Rico' }
}
}

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' }
post '/render_with_jsonapi_renderer', params: payload, headers: headers
assert_equal expected.to_json, response.body
Expand All @@ -133,10 +149,11 @@ def test_jsonapi_parser
'attributes' => {
'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'
)
end
Expand Down

0 comments on commit 85dfef9

Please sign in to comment.