Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup assertions in isolated jsonapi renderer tests a bit #2012

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strong params sure can be annoying

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
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh!

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
attributes :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
2 changes: 1 addition & 1 deletion test/active_model_serializers/test/schema_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def test_with_a_non_existent_file

def test_that_raises_with_a_invalid_json_body
# message changes from JSON gem 2.0.2 to 2.2.0
message = /A JSON text must at least contain two octets!|an unexpected token at ''/
message = /A JSON text must at least contain two octets!|unexpected token at ''/

get :invalid_json_body

Expand Down