|
3 | 3 | describe PostsController, type: :controller do |
4 | 4 | include_context 'JSON API headers' |
5 | 5 |
|
6 | | - before(:all) { FactoryGirl.create_list(:post, 3) } |
| 6 | + before(:all) do |
| 7 | + @post = FactoryGirl.create_list(:post, 3).first |
| 8 | + end |
7 | 9 |
|
8 | 10 | before(:each) do |
9 | 11 | JSONAPI.configuration.json_key_format = :underscored_key |
10 | 12 | end |
11 | 13 |
|
12 | | - let(:fields) { (PostResource.fields - %i(id author category)).map(&:to_s) } |
13 | | - let(:relationships) { %w(author category) } |
14 | | - let(:resource) { Post.first } |
15 | | - let(:parent_id) { resource.user_id } |
16 | | - let(:category_id) { resource.category_id } |
| 14 | + let(:relationships) { PostResource._relationships.keys.map(&:to_s) } |
| 15 | + let(:fields) { PostResource.fields.reject { |e| e == :id }.map(&:to_s) - relationships } |
| 16 | + let(:blog_post) { @post } |
| 17 | + let(:parent_id) { blog_post.user_id } |
| 18 | + let(:category_id) { blog_post.category_id } |
17 | 19 |
|
18 | 20 | let(:attributes) do |
19 | 21 | { title: 'Lorem ipsum', body: 'Lorem ipsum dolor sit amet.', content_type: 'article' } |
|
38 | 40 | end |
39 | 41 |
|
40 | 42 | describe 'GET #index' do |
41 | | - subject { get :index, params } |
| 43 | + subject { get :index, params: params } |
42 | 44 |
|
43 | 45 | let(:params) { { user_id: parent_id } } |
44 | 46 |
|
|
53 | 55 | end |
54 | 56 |
|
55 | 57 | context 'with Hash' do |
56 | | - subject { get :index_with_hash, params } |
| 58 | + subject { get :index_with_hash, params: params } |
57 | 59 |
|
58 | 60 | it 'renders a collection of users' do |
59 | 61 | expect(subject).to have_http_status :ok |
|
148 | 150 |
|
149 | 151 | describe 'GET #show' do |
150 | 152 | context 'with ActiveRecord' do |
151 | | - subject { get :show, id: resource.id } |
| 153 | + subject { get :show, params: { id: blog_post.id } } |
152 | 154 |
|
153 | 155 | it 'renders a single post' do |
154 | 156 | expect(subject).to have_http_status :ok |
155 | 157 | expect(subject).to have_primary_data('posts') |
156 | 158 | expect(subject).to have_data_attributes(fields) |
157 | 159 | expect(subject).to have_relationships(relationships) |
158 | | - expect(data.dig('attributes', 'title')).to eq("Title for Post #{resource.id}") |
| 160 | + expect(data.dig('attributes', 'title')).to eq("Title for Post #{blog_post.id}") |
159 | 161 | end |
160 | 162 | end |
161 | 163 |
|
162 | 164 | context 'with Hash' do |
163 | | - subject { get :show_with_hash, id: resource.id } |
| 165 | + subject { get :show_with_hash, params: { id: blog_post.id } } |
164 | 166 |
|
165 | 167 | it 'renders a single post' do |
166 | 168 | expect(subject).to have_http_status :ok |
|
173 | 175 |
|
174 | 176 | context 'when resource was not found' do |
175 | 177 | context 'with conventional id' do |
176 | | - subject { get :show, id: 999 } |
| 178 | + subject { get :show, params: { id: 999 } } |
177 | 179 |
|
178 | 180 | it 'renders a 404 response' do |
179 | 181 | expect(subject).to have_http_status :not_found |
|
184 | 186 | end |
185 | 187 |
|
186 | 188 | context 'with uuid' do |
187 | | - subject { get :show, id: uuid } |
| 189 | + subject { get :show, params: { id: uuid } } |
188 | 190 |
|
189 | 191 | let(:uuid) { SecureRandom.uuid } |
190 | 192 |
|
|
197 | 199 | end |
198 | 200 |
|
199 | 201 | context 'with slug' do |
200 | | - subject { get :show, id: slug } |
| 202 | + subject { get :show, params: { id: slug } } |
201 | 203 |
|
202 | 204 | let(:slug) { 'some-awesome-slug' } |
203 | 205 |
|
|
212 | 214 | end |
213 | 215 |
|
214 | 216 | describe 'POST #create' do |
215 | | - subject { post :create, params.merge(body) } |
| 217 | + subject { post :create, params: params.merge(body) } |
216 | 218 |
|
217 | 219 | let (:params) { { user_id: parent_id } } |
218 | 220 |
|
|
225 | 227 | end |
226 | 228 |
|
227 | 229 | context 'when validation fails on an attribute' do |
228 | | - subject { post :create, params.merge(invalid_body) } |
| 230 | + subject { post :create, params: params.merge(invalid_body) } |
229 | 231 |
|
230 | 232 | let(:invalid_body) do |
231 | 233 | body.tap { |b| b[:data][:attributes][:title] = nil } |
|
234 | 236 | it 'renders a 422 response' do |
235 | 237 | expect { subject }.to change(Post, :count).by(0) |
236 | 238 | expect(response).to have_http_status :unprocessable_entity |
237 | | - expect(errors[0]['id']).to eq('title') |
238 | | - expect(errors[0]['title']).to eq('Title can\'t be blank') |
239 | | - expect(errors[0]['code']).to eq('100') |
240 | | - expect(errors[0]['source']['pointer']).to eq('/data/attributes/title') |
| 239 | + expect(errors.dig(0, 'id')).to eq('title') |
| 240 | + expect(errors.dig(0, 'title')).to eq('Title can\'t be blank') |
| 241 | + expect(errors.dig(0, 'code')).to eq('100') |
| 242 | + expect(errors.dig(0, 'source', 'pointer')).to eq('/data/attributes/title') |
241 | 243 | end |
242 | 244 | end |
243 | 245 |
|
244 | 246 | context 'when validation fails on a relationship' do |
245 | | - subject { post :create, params.merge(invalid_body) } |
| 247 | + subject { post :create, params: params.merge(invalid_body) } |
246 | 248 |
|
247 | 249 | let(:invalid_body) do |
248 | 250 | body.tap { |b| b[:data][:relationships][:author] = nil } |
|
252 | 254 | expect { subject }.to change(Post, :count).by(0) |
253 | 255 | expect(subject).to have_http_status :unprocessable_entity |
254 | 256 |
|
255 | | - expect(errors[0]['id']).to eq('author') |
256 | | - expect(errors[0]['title']).to eq('Author can\'t be blank') |
257 | | - expect(errors[0]['code']).to eq('100') |
258 | | - expect(errors[0]['source']['pointer']).to eq('/data/relationships/author') |
| 257 | + expect(errors.dig(0, 'id')).to eq('author') |
| 258 | + expect(errors.dig(0, 'title')).to eq('Author can\'t be blank') |
| 259 | + expect(errors.dig(0, 'code')).to eq('100') |
| 260 | + expect(errors.dig(0, 'source', 'pointer')).to eq('/data/relationships/author') |
259 | 261 | end |
260 | 262 | end |
261 | 263 |
|
262 | 264 | context 'when validation fails on a foreign key' do |
263 | | - subject { post :create, params.merge(invalid_body) } |
| 265 | + subject { post :create, params: params.merge(invalid_body) } |
264 | 266 |
|
265 | 267 | let(:invalid_body) do |
266 | 268 | body.tap { |b| b[:data][:relationships][:category] = nil } |
|
270 | 272 | expect { subject }.to change(Post, :count).by(0) |
271 | 273 | expect(subject).to have_http_status :unprocessable_entity |
272 | 274 |
|
273 | | - expect(errors[0]['id']).to eq('category') |
274 | | - expect(errors[0]['title']).to eq('Category can\'t be blank') |
275 | | - expect(errors[0]['code']).to eq('100') |
276 | | - expect(errors[0]['source']['pointer']).to eq('/data/relationships/category') |
| 275 | + expect(errors.dig(0, 'id')).to eq('category') |
| 276 | + expect(errors.dig(0, 'title')).to eq('Category can\'t be blank') |
| 277 | + expect(errors.dig(0, 'code')).to eq('100') |
| 278 | + expect(errors.dig(0, 'source', 'pointer')).to eq('/data/relationships/category') |
277 | 279 | end |
278 | 280 | end |
279 | 281 |
|
280 | 282 | context 'when validation fails on a private attribute' do |
281 | | - subject { post :create, params.merge(invalid_body) } |
| 283 | + subject { post :create, params: params.merge(invalid_body) } |
282 | 284 |
|
283 | 285 | let(:invalid_body) do |
284 | 286 | body.tap { |b| b[:data][:attributes][:title] = 'Fail Hidden' } |
|
288 | 290 | expect { subject }.to change(Post, :count).by(0) |
289 | 291 | expect(subject).to have_http_status :unprocessable_entity |
290 | 292 |
|
291 | | - expect(errors[0]['id']).to eq('hidden_field') |
292 | | - expect(errors[0]['title']).to eq('Hidden field error was tripped') |
293 | | - expect(errors[0]['code']).to eq('100') |
294 | | - expect(errors[0]['source']).to be_nil |
| 293 | + expect(errors.dig(0, 'id')).to eq('hidden_field') |
| 294 | + expect(errors.dig(0, 'title')).to eq('Hidden field error was tripped') |
| 295 | + expect(errors.dig(0, 'code')).to eq('100') |
| 296 | + expect(errors.dig(0, 'source', 'pointer')).to be_nil |
295 | 297 | end |
296 | 298 | end |
297 | 299 |
|
298 | 300 | context 'when validation fails with a formatted attribute key' do |
299 | | - subject { post :create, params.merge(invalid_body) } |
| 301 | + subject { post :create, params: params.merge(invalid_body) } |
300 | 302 |
|
301 | 303 | let(:invalid_body) do |
302 | 304 | body.tap { |b| b[:data][:attributes][:title] = 'Fail Hidden' } |
|
315 | 317 | expect { subject }.to change(Post, :count).by(0) |
316 | 318 | expect(subject).to have_http_status :unprocessable_entity |
317 | 319 |
|
318 | | - expect(errors[0]['id']).to eq('content-type') |
319 | | - expect(errors[0]['title']).to eq('Content type can\'t be blank') |
320 | | - expect(errors[0]['code']).to eq('100') |
321 | | - expect(errors[0]['source']['pointer']).to eq('/data/attributes/content-type') |
| 320 | + expect(errors.dig(0, 'id')).to eq('content-type') |
| 321 | + expect(errors.dig(0, 'title')).to eq('Content type can\'t be blank') |
| 322 | + expect(errors.dig(0, 'code')).to eq('100') |
| 323 | + expect(errors.dig(0, 'source', 'pointer')).to eq('/data/attributes/content-type') |
322 | 324 | end |
323 | 325 | end |
324 | 326 | end |
325 | 327 |
|
326 | 328 | describe 'PATCH #update' do |
327 | 329 | shared_context 'update request' do |action:| |
328 | | - subject { patch action, params.merge(body) } |
| 330 | + subject { patch action, params: params.merge(body) } |
329 | 331 |
|
330 | 332 | let(:params) { { id: 1 } } |
331 | 333 | let(:body) { { data: { id: 1, type: 'posts', attributes: { title: 'Foo' } } } } |
|
343 | 345 | expect { subject }.to change(Post, :count).by(0) |
344 | 346 | expect(response).to have_http_status :unprocessable_entity |
345 | 347 |
|
346 | | - expect(errors[0]['id']).to eq('base') |
347 | | - expect(errors[0]['title']).to eq('This is an error on the base') |
348 | | - expect(errors[0]['code']).to eq('100') |
349 | | - expect(errors[0]['source']['pointer']).to eq('/data') |
| 348 | + expect(errors.dig(0, 'id')).to eq('base') |
| 349 | + expect(errors.dig(0, 'title')).to eq('This is an error on the base') |
| 350 | + expect(errors.dig(0, 'code')).to eq('100') |
| 351 | + expect(errors.dig(0, 'source', 'pointer')).to eq('/data') |
350 | 352 | end |
351 | 353 | end |
352 | 354 | end |
|
0 commit comments