fun_with_json_api 0.0.10.4 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ require 'spec_helper'
2
+
3
+ describe FunWithJsonApi::Configuration do
4
+ describe '#force_render_parse_errors_as_json_api?' do
5
+ it 'has a writer method' do
6
+ instance = described_class.new
7
+
8
+ instance.force_render_parse_errors_as_json_api = true
9
+ expect(instance.force_render_parse_errors_as_json_api?).to be true
10
+
11
+ instance.force_render_parse_errors_as_json_api = false
12
+ expect(instance.force_render_parse_errors_as_json_api?).to be false
13
+ end
14
+
15
+ it 'defaults to false by default' do
16
+ expect(described_class.new.force_render_parse_errors_as_json_api?).to be false
17
+ end
18
+ end
19
+ end
@@ -23,8 +23,8 @@ describe FunWithJsonApi::Railtie do
23
23
  controller do
24
24
  def index
25
25
  respond_to do |format|
26
- format.json_api { render text: 'passed' }
27
- format.all { render text: 'failed' }
26
+ format.json_api { render plain: 'passed' }
27
+ format.all { render plain: 'failed' }
28
28
  end
29
29
  end
30
30
  end
@@ -32,6 +32,27 @@ describe FunWithJsonApi::SchemaValidators::CheckDocumentIdMatchesResource do
32
32
  end
33
33
  end
34
34
  end
35
+
36
+ context 'when /data/id is not a string' do
37
+ before do
38
+ allow(schema_validator).to receive(:document_id).and_return(42)
39
+ end
40
+
41
+ it 'raises a InvalidDocument error' do
42
+ expect do
43
+ subject
44
+ end.to raise_error(FunWithJsonApi::Exceptions::InvalidDocumentIdentifier) do |e|
45
+ expect(e.payload.size).to eq 1
46
+
47
+ payload = e.payload.first
48
+ expect(payload.code).to eq 'invalid_document_identifier'
49
+ expect(payload.pointer).to eq '/data/id'
50
+ expect(payload.title).to eq 'Request json_api data id is invalid'
51
+ expect(payload.detail).to eq 'data id value must be a JSON String (i.e. "1234")'
52
+ expect(payload.status).to eq '409'
53
+ end
54
+ end
55
+ end
35
56
  end
36
57
 
37
58
  context 'when the resource is not persisted' do
@@ -78,6 +99,27 @@ describe FunWithJsonApi::SchemaValidators::CheckDocumentIdMatchesResource do
78
99
  )
79
100
  end
80
101
 
102
+ context 'when /data/id is not a string' do
103
+ before do
104
+ allow(schema_validator).to receive(:document_id).and_return(42)
105
+ end
106
+
107
+ it 'raises a InvalidDocument error' do
108
+ expect do
109
+ subject
110
+ end.to raise_error(FunWithJsonApi::Exceptions::InvalidDocumentIdentifier) do |e|
111
+ expect(e.payload.size).to eq 1
112
+
113
+ payload = e.payload.first
114
+ expect(payload.code).to eq 'invalid_document_identifier'
115
+ expect(payload.pointer).to eq '/data/id'
116
+ expect(payload.title).to eq 'Request json_api data id is invalid'
117
+ expect(payload.detail).to eq 'data id value must be a JSON String (i.e. "1234")'
118
+ expect(payload.status).to eq '409'
119
+ end
120
+ end
121
+ end
122
+
81
123
  context 'when a resource matching id exists' do
82
124
  before do
83
125
  allow(deserializer).to receive(:load_resource_from_id_value)
@@ -0,0 +1,79 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'Request Parsing', type: :request do
4
+ it 'converts a json_api request body into param values' do
5
+ request_data = '{"data":{"id":"42","type":"foobar"}}'
6
+
7
+ post '/echo', request_data, 'Accept': 'application/vnd.api+json',
8
+ 'Content-Type': 'application/vnd.api+json'
9
+ expect(
10
+ JSON.parse(response.body, symbolize_names: true)
11
+ ).to eq(data: { id: '42', type: 'foobar' })
12
+ end
13
+
14
+ context 'with invalid json api request data' do
15
+ let(:invalid_request_data) { '{"data":{"id":"42","type":"foobar",}}' } # extra comma
16
+
17
+ context 'when force_render_parse_errors_as_json_api? is true' do
18
+ before do
19
+ FunWithJsonApi.configure do |config|
20
+ config.force_render_parse_errors_as_json_api = true
21
+ end
22
+ end
23
+
24
+ context 'when the request has a json api accept header' do
25
+ it 'renders a json api invalid document response' do
26
+ post '/echo',
27
+ invalid_request_data,
28
+ 'Accept': 'application/vnd.api+json',
29
+ 'Content-Type': 'application/vnd.api+json'
30
+
31
+ expect(response.status).to eq 400
32
+ expect(JSON.parse(response.body, symbolize_names: true)).to eq(
33
+ errors: [{
34
+ code: 'invalid_request_body',
35
+ title: 'Request json_api body could not be parsed',
36
+ status: '400'
37
+ }])
38
+ end
39
+ end
40
+
41
+ context 'when the request does not have a json api accept header' do
42
+ it 'renders a json api invalid document response' do
43
+ invalid_request_data = '{"data":{"id":"42","type":"foobar",}}' # extra comma
44
+
45
+ post '/echo', invalid_request_data, 'Content-Type': 'application/vnd.api+json'
46
+
47
+ expect(response.status).to eq 400
48
+ end
49
+ end
50
+ end
51
+
52
+ context 'when force_render_parse_errors_as_json_api? is false' do
53
+ before do
54
+ FunWithJsonApi.configure do |config|
55
+ config.force_render_parse_errors_as_json_api = false
56
+ end
57
+ end
58
+
59
+ context 'when the request has a json api accept header' do
60
+ it 'renders a json api invalid document response' do
61
+ post '/echo',
62
+ invalid_request_data,
63
+ 'Accept': 'application/vnd.api+json',
64
+ 'Content-Type': 'application/vnd.api+json'
65
+
66
+ expect(response.status).to eq 400
67
+ end
68
+ end
69
+
70
+ context 'when the request does not have a json api accept header' do
71
+ it 'raises a ActionDispatch::ParamsParser::ParseError' do
72
+ expect do
73
+ post '/echo', invalid_request_data, 'Content-Type': 'application/vnd.api+json'
74
+ end.to raise_error(ActionDispatch::ParamsParser::ParseError)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fun_with_json_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10.4
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Morrall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-19 00:00:00.000000000 Z
11
+ date: 2016-04-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.5
19
+ version: '4.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.5
26
+ version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: active_model_serializers
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -161,6 +161,7 @@ files:
161
161
  - lib/fun_with_json_api/attributes/string_attribute.rb
162
162
  - lib/fun_with_json_api/attributes/uuid_v4_attribute.rb
163
163
  - lib/fun_with_json_api/collection_manager.rb
164
+ - lib/fun_with_json_api/configuration.rb
164
165
  - lib/fun_with_json_api/controller_methods.rb
165
166
  - lib/fun_with_json_api/deserializer.rb
166
167
  - lib/fun_with_json_api/deserializer_class_methods.rb
@@ -188,6 +189,7 @@ files:
188
189
  - lib/fun_with_json_api/exceptions/unknown_relationship.rb
189
190
  - lib/fun_with_json_api/find_collection_from_document.rb
190
191
  - lib/fun_with_json_api/find_resource_from_document.rb
192
+ - lib/fun_with_json_api/middleware/catch_json_api_parse_errors.rb
191
193
  - lib/fun_with_json_api/pre_deserializer.rb
192
194
  - lib/fun_with_json_api/railtie.rb
193
195
  - lib/fun_with_json_api/schema_validator.rb
@@ -238,6 +240,7 @@ files:
238
240
  - spec/dummy/public/favicon.ico
239
241
  - spec/fixtures/active_record.rb
240
242
  - spec/fun_with_json_api/collection_manager_spec.rb
243
+ - spec/fun_with_json_api/configuration_spec.rb
241
244
  - spec/fun_with_json_api/controller_methods_spec.rb
242
245
  - spec/fun_with_json_api/deserializer_class_methods_spec.rb
243
246
  - spec/fun_with_json_api/deserializer_spec.rb
@@ -252,6 +255,7 @@ files:
252
255
  - spec/fun_with_json_api/schema_validators/check_document_type_matches_resource_spec.rb
253
256
  - spec/fun_with_json_api/schema_validators/check_relationships_spec.rb
254
257
  - spec/fun_with_json_api_spec.rb
258
+ - spec/requests/request_parsing_spec.rb
255
259
  - spec/spec_helper.rb
256
260
  homepage: https://github.com/bmorrall/fun_with_json_api
257
261
  licenses:
@@ -315,6 +319,7 @@ test_files:
315
319
  - spec/dummy/README.rdoc
316
320
  - spec/fixtures/active_record.rb
317
321
  - spec/fun_with_json_api/collection_manager_spec.rb
322
+ - spec/fun_with_json_api/configuration_spec.rb
318
323
  - spec/fun_with_json_api/controller_methods_spec.rb
319
324
  - spec/fun_with_json_api/deserializer_class_methods_spec.rb
320
325
  - spec/fun_with_json_api/deserializer_spec.rb
@@ -329,4 +334,5 @@ test_files:
329
334
  - spec/fun_with_json_api/schema_validators/check_document_type_matches_resource_spec.rb
330
335
  - spec/fun_with_json_api/schema_validators/check_relationships_spec.rb
331
336
  - spec/fun_with_json_api_spec.rb
337
+ - spec/requests/request_parsing_spec.rb
332
338
  - spec/spec_helper.rb