Skip to content

Commit 8e30920

Browse files
committed
Merge pull request #2012 from bf4/cleanup_isolated_jsonapi_renderer_tests_a_bit
Cleanup assertions in isolated jsonapi renderer tests a bit
1 parent 3a88c39 commit 8e30920

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

test/active_model_serializers/register_jsonapi_renderer_test_isolated.rb

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ class << self
1212
end
1313

1414
def render_with_jsonapi_renderer
15-
unlocked_params = Rails::VERSION::MAJOR >= 5 ? params.to_unsafe_h : params
16-
attributes = unlocked_params[:data].present? ? unlocked_params[:data][:attributes] : {}
15+
permitted_params = params.permit(data: [:id, :type, attributes: [:name]])
16+
permitted_params = permitted_params.to_h.with_indifferent_access
17+
attributes =
18+
if permitted_params[:data]
19+
permitted_params[:data][:attributes].merge(id: permitted_params[:data][:id])
20+
else
21+
# Rails returns empty params when no mime type can be negotiated.
22+
# (Until https://github.com/rails/rails/pull/26632 is reviewed.)
23+
permitted_params
24+
end
1725
author = Author.new(attributes)
1826
render jsonapi: author
1927
end
@@ -34,6 +42,17 @@ def assert_parses(expected, actual, headers = {})
3442
assert_equal(expected, TestController.last_request_parameters)
3543
end
3644

45+
def define_author_model_and_serializer
46+
TestController.const_set(:Author, Class.new(ActiveModelSerializers::Model) do
47+
attributes :name
48+
end)
49+
TestController.const_set(:AuthorSerializer, Class.new(ActiveModel::Serializer) do
50+
type 'users'
51+
attribute :id
52+
attribute :name
53+
end)
54+
end
55+
3756
class WithoutRenderer < JsonApiRendererTest
3857
setup do
3958
require 'rails'
@@ -49,6 +68,7 @@ class WithoutRenderer < JsonApiRendererTest
4968
match ':action', to: TestController, via: [:get, :post]
5069
end
5170
end
71+
define_author_model_and_serializer
5272
end
5373

5474
def test_jsonapi_parser_not_registered
@@ -61,12 +81,12 @@ def test_jsonapi_parser_not_registered
6181
end
6282

6383
def test_jsonapi_renderer_not_registered
64-
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
84+
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
6585
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
6686
post '/render_with_jsonapi_renderer', params: payload, headers: headers
67-
assert_equal 500, response.status
6887
assert_equal '', response.body
69-
assert response.request.env['action_dispatch.exception'].is_a?(ActionView::MissingTemplate) if response.request.present?
88+
assert_equal 500, response.status
89+
assert_equal ActionView::MissingTemplate, request.env['action_dispatch.exception'].class
7090
end
7191

7292
def test_jsonapi_parser
@@ -94,6 +114,7 @@ class WithRenderer < JsonApiRendererTest
94114
match ':action', to: TestController, via: [:get, :post]
95115
end
96116
end
117+
define_author_model_and_serializer
97118
end
98119

99120
def test_jsonapi_parser_registered
@@ -109,18 +130,13 @@ def test_jsonapi_parser_registered
109130
def test_jsonapi_renderer_registered
110131
expected = {
111132
'data' => {
112-
'id' => 'author',
113-
'type' => 'authors',
114-
'attributes' => { 'name' => 'Johnny Rico' },
115-
'relationships' => {
116-
'posts' => { 'data' => nil },
117-
'roles' => { 'data' => nil },
118-
'bio' => { 'data' => nil }
119-
}
133+
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765',
134+
'type' => 'users',
135+
'attributes' => { 'name' => 'Johnny Rico' }
120136
}
121137
}
122138

123-
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "authors"}}'
139+
payload = '{"data": {"attributes": {"name": "Johnny Rico"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}'
124140
headers = { 'CONTENT_TYPE' => 'application/vnd.api+json' }
125141
post '/render_with_jsonapi_renderer', params: payload, headers: headers
126142
assert_equal expected.to_json, response.body
@@ -133,10 +149,11 @@ def test_jsonapi_parser
133149
'attributes' => {
134150
'name' => 'John Doe'
135151
},
136-
'type' => 'users'
152+
'type' => 'users',
153+
'id' => '36c9c04e-86b1-4636-a5b0-8616672d1765'
137154
}
138155
},
139-
'{"data": {"attributes": {"name": "John Doe"}, "type": "users"}}',
156+
'{"data": {"attributes": {"name": "John Doe"}, "type": "users", "id": "36c9c04e-86b1-4636-a5b0-8616672d1765"}}',
140157
'CONTENT_TYPE' => 'application/vnd.api+json'
141158
)
142159
end

test/active_model_serializers/test/schema_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def test_with_a_non_existent_file
116116

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

121121
get :invalid_json_body
122122

0 commit comments

Comments
 (0)