|
32 | 32 | it do |
33 | 33 | expect(response).to have_http_status(:unprocessable_entity) |
34 | 34 | expect(response_json['errors'].size).to eq(1) |
35 | | - |
36 | | - expect(response_json['errors'].first.keys) |
37 | | - .to contain_exactly('status', 'source', 'title', 'detail', 'code') |
38 | | - |
39 | | - expect(response_json['errors'][0]['status']).to eq('422') |
40 | | - expect(response_json['errors'][0]['title']) |
41 | | - .to eq(Rack::Utils::HTTP_STATUS_CODES[422]) |
42 | | - expect(response_json['errors'][0]['source']).to eq('pointer' => '') |
43 | | - expect(response_json['errors'][0]['detail']).to be_nil |
44 | | - expect(response_json['errors'][0]['code']).to be_nil |
| 35 | + expect(response_json['errors']).to contain_exactly( |
| 36 | + { |
| 37 | + 'status' => '422', |
| 38 | + 'source' => { 'pointer' => '' }, |
| 39 | + 'title' => 'Unprocessable Entity', |
| 40 | + 'detail' => nil, |
| 41 | + 'code' => nil |
| 42 | + } |
| 43 | + ) |
45 | 44 | end |
46 | 45 | end |
47 | 46 |
|
|
55 | 54 | it do |
56 | 55 | expect(response).to have_http_status(:unprocessable_entity) |
57 | 56 | expect(response_json['errors'].size).to eq(1) |
58 | | - expect(response_json['errors'][0]['status']).to eq('422') |
59 | | - expect(response_json['errors'][0]['code']).to include('blank') |
60 | | - expect(response_json['errors'][0]['title']) |
61 | | - .to eq(Rack::Utils::HTTP_STATUS_CODES[422]) |
62 | | - expect(response_json['errors'][0]['source']) |
63 | | - .to eq('pointer' => '/data/relationships/user') |
64 | | - if Rails.gem_version >= Gem::Version.new('6.1') |
65 | | - expect(response_json['errors'][0]['detail']) |
66 | | - .to eq('User must exist') |
| 57 | + expected_detail = if Rails.gem_version >= Gem::Version.new('6.1') |
| 58 | + 'User must exist' |
67 | 59 | else |
68 | | - expect(response_json['errors'][0]['detail']) |
69 | | - .to eq('User can\'t be blank') |
| 60 | + 'User can\'t be blank' |
70 | 61 | end |
| 62 | + expect(response_json['errors']).to contain_exactly( |
| 63 | + { |
| 64 | + 'status' => '422', |
| 65 | + 'source' => { 'pointer' => '/data/relationships/user' }, |
| 66 | + 'title' => 'Unprocessable Entity', |
| 67 | + 'detail' => expected_detail, |
| 68 | + 'code' => 'blank' |
| 69 | + } |
| 70 | + ) |
71 | 71 | end |
72 | 72 |
|
73 | 73 | context 'required by validations' do |
|
81 | 81 | it do |
82 | 82 | expect(response).to have_http_status(:unprocessable_entity) |
83 | 83 | expect(response_json['errors'].size).to eq(3) |
84 | | - expect(response_json['errors'][0]['status']).to eq('422') |
85 | | - expect(response_json['errors'][0]['code']).to include('invalid') |
86 | | - expect(response_json['errors'][0]['title']) |
87 | | - .to eq(Rack::Utils::HTTP_STATUS_CODES[422]) |
88 | | - expect(response_json['errors'][0]['source']) |
89 | | - .to eq('pointer' => '/data/attributes/title') |
90 | | - expect(response_json['errors'][0]['detail']) |
91 | | - .to eq('Title is invalid') |
92 | | - |
93 | | - expect(response_json['errors'][1]['status']).to eq('422') |
94 | | - |
95 | | - if Rails::VERSION::MAJOR >= 5 |
96 | | - expect(response_json['errors'][1]['code']).to eq('invalid') |
97 | | - else |
98 | | - expect(response_json['errors'][1]['code']).to eq('has_typos') |
99 | | - end |
100 | | - |
101 | | - expect(response_json['errors'][1]['title']) |
102 | | - .to eq(Rack::Utils::HTTP_STATUS_CODES[422]) |
103 | | - expect(response_json['errors'][1]['source']) |
104 | | - .to eq('pointer' => '/data/attributes/title') |
105 | | - expect(response_json['errors'][1]['detail']) |
106 | | - .to eq('Title has typos') |
107 | | - |
108 | | - expect(response_json['errors'][2]['status']).to eq('422') |
109 | | - |
110 | | - if Rails::VERSION::MAJOR >= 5 |
111 | | - expect(response_json['errors'][2]['code']).to eq('less_than') |
112 | | - else |
113 | | - expect(response_json['errors'][2]['code']) |
114 | | - .to eq('must_be_less_than_100') |
115 | | - end |
116 | | - |
117 | | - expect(response_json['errors'][2]['title']) |
118 | | - .to eq(Rack::Utils::HTTP_STATUS_CODES[422]) |
119 | | - expect(response_json['errors'][2]['source']) |
120 | | - .to eq('pointer' => '/data/attributes/quantity') |
121 | | - expect(response_json['errors'][2]['detail']) |
122 | | - .to eq('Quantity must be less than 100') |
| 84 | + expect(response_json['errors']).to contain_exactly( |
| 85 | + { |
| 86 | + 'status' => '422', |
| 87 | + 'source' => { 'pointer' => '/data/attributes/title' }, |
| 88 | + 'title' => 'Unprocessable Entity', |
| 89 | + 'detail' => 'Title is invalid', |
| 90 | + 'code' => 'invalid' |
| 91 | + }, |
| 92 | + { |
| 93 | + 'status' => '422', |
| 94 | + 'source' => { 'pointer' => '/data/attributes/title' }, |
| 95 | + 'title' => 'Unprocessable Entity', |
| 96 | + 'detail' => 'Title has typos', |
| 97 | + 'code' => 'invalid' |
| 98 | + }, |
| 99 | + { |
| 100 | + 'status' => '422', |
| 101 | + 'source' => { 'pointer' => '/data/attributes/quantity' }, |
| 102 | + 'title' => 'Unprocessable Entity', |
| 103 | + 'detail' => 'Quantity must be less than 100', |
| 104 | + 'code' => 'less_than' |
| 105 | + } |
| 106 | + ) |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + context 'validations with non-interpolated messages' do |
| 111 | + let(:params) do |
| 112 | + payload = note_params.dup |
| 113 | + payload[:data][:attributes][:title] = 'SLURS ARE GREAT' |
| 114 | + payload |
| 115 | + end |
| 116 | + |
| 117 | + it do |
| 118 | + expect(response).to have_http_status(:unprocessable_entity) |
| 119 | + expect(response_json['errors'].size).to eq(1) |
| 120 | + expect(response_json['errors']).to contain_exactly( |
| 121 | + { |
| 122 | + 'status' => '422', |
| 123 | + 'source' => { 'pointer' => '' }, |
| 124 | + 'title' => 'Unprocessable Entity', |
| 125 | + 'detail' => 'Title has slurs', |
| 126 | + 'code' => 'title_has_slurs' |
| 127 | + } |
| 128 | + ) |
123 | 129 | end |
124 | 130 | end |
125 | 131 |
|
|
134 | 140 |
|
135 | 141 | it do |
136 | 142 | expect(response).to have_http_status(:unprocessable_entity) |
137 | | - expect(response_json['errors'][0]['source']) |
138 | | - .to eq('pointer' => '/data/attributes/title') |
| 143 | + expect(response_json['errors']).to contain_exactly( |
| 144 | + { |
| 145 | + 'status' => '422', |
| 146 | + 'source' => { 'pointer' => '/data/attributes/title' }, |
| 147 | + 'title' => 'Unprocessable Entity', |
| 148 | + 'detail' => nil, |
| 149 | + 'code' => nil |
| 150 | + } |
| 151 | + ) |
139 | 152 | end |
140 | 153 | end |
141 | 154 | end |
|
147 | 160 | it do |
148 | 161 | expect(response).to have_http_status(:not_found) |
149 | 162 | expect(response_json['errors'].size).to eq(1) |
150 | | - expect(response_json['errors'][0]['status']).to eq('404') |
151 | | - expect(response_json['errors'][0]['title']) |
152 | | - .to eq(Rack::Utils::HTTP_STATUS_CODES[404]) |
153 | | - expect(response_json['errors'][0]['source']).to be_nil |
154 | | - expect(response_json['errors'][0]['detail']).to be_nil |
| 163 | + expect(response_json['errors']).to contain_exactly( |
| 164 | + { |
| 165 | + 'status' => '404', |
| 166 | + 'source' => nil, |
| 167 | + 'title' => 'Not Found', |
| 168 | + 'detail' => nil, |
| 169 | + 'code' => nil |
| 170 | + } |
| 171 | + ) |
155 | 172 | end |
156 | 173 | end |
157 | 174 |
|
|
162 | 179 | it do |
163 | 180 | expect(response).to have_http_status(:internal_server_error) |
164 | 181 | expect(response_json['errors'].size).to eq(1) |
165 | | - expect(response_json['errors'][0]['status']).to eq('500') |
166 | | - expect(response_json['errors'][0]['title']) |
167 | | - .to eq(Rack::Utils::HTTP_STATUS_CODES[500]) |
168 | | - expect(response_json['errors'][0]['source']).to be_nil |
169 | | - expect(response_json['errors'][0]['detail']).to be_nil |
| 182 | + expect(response_json['errors']).to contain_exactly( |
| 183 | + { |
| 184 | + 'status' => '500', |
| 185 | + 'source' => nil, |
| 186 | + 'title' => 'Internal Server Error', |
| 187 | + 'detail' => nil, |
| 188 | + 'code' => nil |
| 189 | + } |
| 190 | + ) |
170 | 191 | end |
171 | 192 | end |
172 | 193 | end |
|
0 commit comments