-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathdeserialization_spec.rb
139 lines (114 loc) · 3.57 KB
/
deserialization_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
require 'rails_helper'
describe ActionController::Base, '.deserializable_resource',
type: :controller do
let(:payload) do
{
_jsonapi: {
'data' => {
'type' => 'users',
'attributes' => { 'name' => 'Lucas' }
}
}
}
end
context 'when using default deserializer' do
controller do
deserializable_resource :user
def create
render plain: 'ok'
end
end
it 'makes the deserialized resource available in params' do
post :create, params: payload
expected = { 'type' => 'users', 'name' => 'Lucas' }
expect(controller.params[:user]).to eq(expected)
end
it 'makes the deserialization mapping available via #jsonapi_pointers' do
post :create, params: payload
expected = { name: '/data/attributes/name',
type: '/data/type' }
expect(controller.jsonapi_pointers).to eq(expected)
end
end
context 'when using a customized deserializer' do
controller do
deserializable_resource :user do
attribute(:name) do |val|
{ 'first_name'.to_sym => val }
end
end
def create
render plain: 'ok'
end
end
it 'makes the deserialized resource available in params' do
post :create, params: payload
expected = { 'type' => 'users', 'first_name' => 'Lucas' }
expect(controller.params[:user]).to eq(expected)
end
it 'makes the deserialization mapping available via #jsonapi_pointers' do
post :create, params: payload
expected = { first_name: '/data/attributes/name',
type: '/data/type' }
expect(controller.jsonapi_pointers).to eq(expected)
end
end
context 'when using a customized deserializer with key_format' do
controller do
deserializable_resource :user do
key_format(&:capitalize)
end
def create
render plain: 'ok'
end
end
it 'makes the deserialized resource available in params' do
post :create, params: payload
expected = { 'type' => 'users', 'Name' => 'Lucas' }
expect(controller.params[:user]).to eq(expected)
end
it 'makes the deserialization mapping available via #jsonapi_pointers' do
post :create, params: payload
expected = { Name: '/data/attributes/name',
type: '/data/type' }
expect(controller.jsonapi_pointers).to eq(expected)
end
end
context 'when unable to deserialize resource from params' do
controller do
deserializable_resource :user
def create
render plain: :ok
end
end
context 'with the default config' do
it 'makes the deserialized resource available in params' do
post :create
expect(response.body).to eq(
{
:errors => [
{
:links => {
:about => "http://jsonapi.org/format/"
},
:title => "Non-compliant Request Body",
:detail => "The request was not formatted in compliance with the application/vnd.api+json spec"
}
],
:jsonapi => {
:version=>"1.0"
}
}.to_json
)
expect(response).to be_bad_request
end
end
context 'when the config[:jsonapi_payload_malformed] == nil' do
it 'makes the deserialization mapping available via #jsonapi_pointers' do
with_config(jsonapi_payload_malformed: nil) { post :create }
expect(response.body).to eq('ok')
expect(response).to be_success
end
end
end
end