-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitems_spec.rb
1074 lines (822 loc) · 41.6 KB
/
items_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'rails_helper'
RSpec.shared_examples 'suppressed items are suppressed' do
it 'excludes suppressed items' do
expected_data = all_items.where(suppressed: false)
expect(expected_data.count).to be > 0
expect(all_items.count).to be > expected_data.count # just to be sure
expected_mms_ids = expected_data.reorder(nil).pluck('DISTINCT(mms_id)')
expected_meta = {
availability: AvailabilityService.availability_for(expected_mms_ids),
pagination: { current: 1, records: expected_mms_ids.count, offset: 0, limit: 30 }
}
get items_url, params: params
expect(response).to be_successful
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
_links = parsed_response.delete('links')
expect(parsed_response).to contain_jsonapi_for(expected_data, { meta: expected_meta })
end
end
RSpec.describe 'Items', type: :request do
before do
create_all(Item)
create_all(Term)
end
describe 'reading' do
before do
allow(AvailabilityService).to receive(:availability_for) do |mms_ids|
mms_ids.compact.to_h do |mms_id|
available = mms_id[mms_id.length / 2].to_i.odd?
[mms_id, available]
end
end
end
describe :index do
it 'returns the items' do
expect(Item).to exist # just to be sure
get items_url
expect(response).to be_successful
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
links = parsed_response.delete('links')
# TODO: test pagination properly
expect(links['self']).to eq(items_url)
expect(links['current']).to eq("#{items_url}?page[number]=1")
expected_data = Item.where(suppressed: false).includes(:terms)
expected_mms_ids = expected_data.reorder(nil).pluck('DISTINCT(mms_id)')
expected_meta = {
availability: AvailabilityService.availability_for(expected_mms_ids),
pagination: { current: 1, records: expected_mms_ids.count, offset: 0, limit: 30 }
}
expect(parsed_response).to contain_jsonapi_for(expected_data, { meta: expected_meta })
end
it 'accepts JSONAPI include parameters' do
get items_url, params: { include: 'image,terms,terms.facet' }
expect(response).to be_successful
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
links = parsed_response.delete('links')
expect(links['self']).to eq("#{items_url}?include=image%2Cterms%2Cterms.facet")
expect(links['current']).to eq("#{items_url}?include=image,terms,terms.facet&page[number]=1")
expected_data = Item.where(suppressed: false).includes(:terms)
expected_mms_ids = expected_data.reorder(nil).pluck('DISTINCT(mms_id)')
expected_meta = {
availability: AvailabilityService.availability_for(expected_mms_ids),
pagination: { current: 1, records: expected_mms_ids.count, offset: 0, limit: 30 }
}
expect(parsed_response).to contain_jsonapi_for(expected_data, { include: %i[image terms terms.facet], meta: expected_meta })
end
it 'supports searches by facet' do
# NOTE: The query string format here follows JSONAPI recommendations, not Rails nested
# parameter conventions. See https://jsonapi.org/recommendations/#filtering
facet_values = { 'Medium' => %w[Etching Lithograph], 'Genre' => %w[Landscape Figurative] }
get items_url, params: facet_values.map { |f, vv| "filter[#{f}]=#{vv.join(',')}" }.join('&')
expect(response).to be_successful
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
_links = parsed_response.delete('links')
# expect(links['self']).to eq("#{items_url}?include=terms%2Cterms.facet")
# expect(links['current']).to eq("#{links['self']}&page[number]=1")
expected_data = Item.with_facet_values(facet_values).where(suppressed: false)
expected_mms_ids = expected_data.reorder(nil).pluck('DISTINCT(mms_id)')
expected_meta = {
availability: AvailabilityService.availability_for(expected_mms_ids),
pagination: { current: 1, records: expected_mms_ids.count, offset: 0, limit: 30 }
}
expect(parsed_response).to contain_jsonapi_for(expected_data, { meta: expected_meta })
end
it 'supports keyword searches' do
keywords = 'color medium numbered'
get items_url, params: { 'filter[keywords]' => keywords }
expect(response).to be_successful
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
_links = parsed_response.delete('links')
expected_data = Item.where(suppressed: false).with_all_keywords(keywords)
expected_mms_ids = expected_data.reorder(nil).pluck('DISTINCT(mms_id)')
expected_meta = {
availability: AvailabilityService.availability_for(expected_mms_ids),
pagination: { current: 1, records: expected_mms_ids.count, offset: 0, limit: 30 }
}
expect(parsed_response).to contain_jsonapi_for(expected_data, { meta: expected_meta })
end
context 'suppressed items' do
let(:keywords) { 'color medium' }
attr_reader :all_items
attr_reader :params
before do
@all_items = Item.with_all_keywords(keywords)
expect(all_items.count).to be > 2 # just to be sure
all_items.take.tap { |it| it.update(suppressed: true) }
@params = { 'filter[keywords]' => keywords }
end
context 'without filter[suppressed]' do
context 'without login' do
it_behaves_like 'suppressed items are suppressed'
end
context 'as patron' do
include_context 'patron request'
it_behaves_like 'suppressed items are suppressed'
end
context 'as admin' do
include_context 'admin request'
it_behaves_like 'suppressed items are suppressed'
end
end
context 'with filter[suppressed]=false' do
before do
params.merge!({ 'filter[suppressed]' => 'false' })
end
context 'without login' do
it_behaves_like 'suppressed items are suppressed'
end
context 'as patron' do
include_context 'patron request'
it_behaves_like 'suppressed items are suppressed'
end
context 'as admin' do
include_context 'admin request'
it_behaves_like 'suppressed items are suppressed'
end
end
context 'with filter[suppressed]=true,false' do
before do
params.merge!({ 'filter[suppressed]' => 'true,false' })
end
context 'without login' do
it_behaves_like 'suppressed items are suppressed'
end
context 'as patron' do
include_context 'patron request'
it_behaves_like 'suppressed items are suppressed'
end
context 'as admin' do
include_context 'admin request'
it 'includes both suppressed and unsuppressed items' do
expected_mms_ids = all_items.reorder(nil).pluck('DISTINCT(mms_id)')
expected_meta = {
availability: AvailabilityService.availability_for(expected_mms_ids),
pagination: { current: 1, records: expected_mms_ids.count, offset: 0, limit: 30 }
}
get items_url, params: params
expect(response).to be_successful
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
_links = parsed_response.delete('links')
expect(parsed_response).to contain_jsonapi_for(all_items, { meta: expected_meta })
end
end
end
context 'with filter[suppressed]=true' do
before do
params.merge!({ 'filter[suppressed]' => 'true' })
end
context 'without login' do
it_behaves_like 'suppressed items are suppressed'
end
context 'as patron' do
include_context 'patron request'
it_behaves_like 'suppressed items are suppressed'
end
context 'as admin' do
include_context 'admin request'
it 'includes only suppressed items' do
expected_data = all_items.where(suppressed: true)
expect(expected_data.count).to be > 0
expect(all_items.count).to be > expected_data.count # just to be sure
expected_mms_ids = expected_data.reorder(nil).pluck('DISTINCT(mms_id)')
expected_meta = {
availability: AvailabilityService.availability_for(expected_mms_ids),
pagination: { current: 1, records: expected_mms_ids.count, offset: 0, limit: 30 }
}
get items_url, params: params
expect(response).to be_successful
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
_links = parsed_response.delete('links')
expect(parsed_response).to contain_jsonapi_for(expected_data, { meta: expected_meta })
end
end
end
end
end
describe :show do
it 'returns an item' do
item = Item.take
get item_url(item)
expect(response).to be_successful
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
links = parsed_response.delete('links')
expect(links['self']).to eq(item_url(item))
expect(parsed_response).to contain_jsonapi_for(item)
end
it 'includes the terms' do
item = Item.take
get item_url(item)
parsed_response = JSON.parse(response.body)
data = parsed_response['data']
terms_data = data['relationships']['terms']['data']
expected_count = item.terms.count
expect(expected_count).to be > 0 # just to be sure
expect(terms_data).to be_a(Array)
expected_terms_data = item.terms.map { |t| { 'type' => 'term', 'id' => t.id.to_s } }
expect(terms_data).to contain_exactly(*expected_terms_data)
end
it 'can include both image and terms' do
item = Item.take
get item_url(item), params: { include: 'image,terms' }
parsed_response = JSON.parse(response.body)
data = parsed_response['data']
item_data = data['relationships']['image']['data']
expect(item_data).to eq({ 'id' => item.image.id.to_s, 'type' => 'image' })
included = parsed_response['included']
expect(included).to be_a(Array)
expected_data = ImageSerializer.new(item.image).serializable_hash[:data]
expect(included[0]).to deep_eq_hash(expected_data, indifferent: true)
item.terms.each do |t|
expected_term_data = TermSerializer.new(t).serializable_hash[:data]
actual_term_data = included.find { |h| h['type'] == 'term' && h['id'] == t.id.to_s }
expect(actual_term_data).not_to(be_nil)
expect(actual_term_data).to deep_eq_hash(expected_term_data, indifferent: true)
end
end
end
end
describe 'writing' do
def to_relationships(terms: nil, image: nil)
{}.tap do |rels|
rels[:terms] = { data: terms.map { |term| { type: 'term', id: term.id.to_s } } } if terms
rels[:image] = { data: { type: 'image', id: image.id.to_s } } if image
end
end
let(:valid_image) do
basename = 'viera da silva (composition).jpg'
thumbnail = 'viera da silva (composition)_360px.jpg'
create(:image, basename: basename, thumbnail: thumbnail)
end
let(:valid_attributes) do
{
# image: 'viera da silva (composition).jpg',
title: 'Composition (Le Gardin)',
artist: 'Vieira da Silva, Maria Elena',
artist_url: 'https://en.wikipedia.org/wiki/Maria_Helena_Vieira_da_Silva',
date: '1959',
# decade: '1950-1959',
description: 'Annotated "Epreuve d\'artiste."',
# medium: ['Stencil', 'Serigraph'],
# colors: 'Color',
# genre: 'Still Life',
dimensions: '19 x 15"',
# size: 'Small',
series: 'Artist\'s Proof',
mms_id: '991051333089706532',
barcode: 'c093329284',
circulation: 'NON-CIRC',
location: 'Stored in box',
value: '800',
appraisal_date: '2006',
notes: '17741786',
reserve_date: Date.new(2019, 9, 16), # '2019-09-16'
suppressed: false
}
end
let(:valid_terms) do
factory_names = %i[term_1950_1959 term_serigraph term_stencil term_color term_still_life term_small]
factory_names.map { |factory_name| create(factory_name) }
end
let(:valid_relationships) do
to_relationships(terms: valid_terms, image: valid_image)
end
context 'as admin' do
include_context 'admin request'
# TODO: share code with closures_spec
def expected_errors_for(attributes, terms: valid_terms, image: valid_image, item_to_update: nil)
(item_to_update ? Item.find(item_to_update.id) : Item.new).tap do |item|
Item.transaction do
item.assign_attributes(**attributes)
item.terms = terms
item.image = image
item.validate
expect(item).not_to be_valid # just to be sure
raise ActiveRecord::Rollback
end
end.errors
end
describe :create do
describe 'success' do
it 'creates an item' do
payload = {
data: {
type: 'item',
attributes: valid_attributes,
relationships: valid_relationships
}
}
# TODO: figure out how to test w/suppressed as JSON boolean rather than string
expect { post items_url, params: payload, as: :jsonapi }.to change(Item, :count).by(1)
expect(response).to have_http_status(:created)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
item_id = parsed_response['data']['id'].to_i
item = Item.find(item_id)
expect(item).not_to be_nil
valid_attributes.each { |attr, val| expect(item.send(attr)).to eq(val) }
expect(item.terms).to contain_exactly(*valid_terms)
links = parsed_response.delete('links')
expect(links['self']).to eq(items_url)
expect(parsed_response).to contain_jsonapi_for(item)
expect(response.headers['Location']).to eq(item_url(item))
end
it 'accepts a suppressed item with a nil MMS ID' do
attributes = valid_attributes.except(:mms_id).merge(suppressed: true)
payload = { data: { type: 'item', attributes: attributes, relationships: to_relationships(image: valid_image) } }
expect { post items_url, params: payload, as: :jsonapi }.to change(Item, :count).by(1)
expect(response).to have_http_status(:created)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
item_id = parsed_response['data']['id'].to_i
item = Item.find(item_id)
expect(item).not_to be_nil
attributes.each { |attr, val| expect(item.send(attr)).to eq(val) }
links = parsed_response.delete('links')
expect(links['self']).to eq(items_url)
expect(parsed_response).to contain_jsonapi_for(item)
expect(response.headers['Location']).to eq(item_url(item))
end
it 'treats a blank MMS ID as nil' do
post_attributes = valid_attributes.except(:mms_id).merge(suppressed: true, mms_id: ' ')
payload = { data: { type: 'item', attributes: post_attributes, relationships: to_relationships(image: valid_image) } }
expect { post items_url, params: payload, as: :jsonapi }.to change(Item, :count).by(1)
expect(response).to have_http_status(:created)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
item_id = parsed_response['data']['id'].to_i
item = Item.find(item_id)
expect(item).not_to be_nil
expected_attributes = post_attributes.merge(mms_id: nil)
expected_attributes.each { |attr, val| expect(item.send(attr)).to eq(val) }
links = parsed_response.delete('links')
expect(links['self']).to eq(items_url)
expect(parsed_response).to contain_jsonapi_for(item)
expect(response.headers['Location']).to eq(item_url(item))
end
it 'accepts a suppressed item without an image' do
expected_attributes = valid_attributes.merge(suppressed: true)
payload = {
data: {
type: 'item',
attributes: expected_attributes,
relationships: to_relationships(terms: valid_terms)
}
}
expect { post items_url, params: payload, as: :jsonapi }.to change(Item, :count).by(1)
expect(response).to have_http_status(:created)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
parsed_response = JSON.parse(response.body)
item_id = parsed_response['data']['id'].to_i
item = Item.find(item_id)
expect(item).not_to be_nil
expected_attributes.each { |attr, val| expect(item.send(attr)).to eq(val) }
expect(item.terms).to contain_exactly(*valid_terms)
expect(item.image).to be_nil
expect(item).to be_suppressed
links = parsed_response.delete('links')
expect(links['self']).to eq(items_url)
expect(parsed_response).to contain_jsonapi_for(item)
expect(response.headers['Location']).to eq(item_url(item))
end
end
describe 'failure' do
it 'fails with 422 Unprocessable Entity for a missing title' do
invalid_attributes = valid_attributes.except(:title)
payload = { data: { type: 'item', attributes: invalid_attributes, relationships: valid_relationships } }
allow(Rails.logger).to receive(:error)
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordInvalid))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expected_errors = expected_errors_for(invalid_attributes)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
it 'treats a blank title as missing' do
post_attributes = valid_attributes.merge(title: ' ')
payload = { data: { type: 'item', attributes: post_attributes, relationships: valid_relationships } }
allow(Rails.logger).to receive(:error)
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordInvalid))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
invalid_attributes = post_attributes.merge(title: nil)
expected_errors = expected_errors_for(invalid_attributes)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
it 'fails with 422 Unprocessable Entity for a missing image' do
payload = {
data: {
type: 'item',
attributes: valid_attributes,
relationships: to_relationships(terms: valid_terms)
}
}
allow(Rails.logger).to receive(:error)
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActionController::ParameterMissing))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expect(actual_errors.size).to eq(1)
end
it 'fails with 422 Unprocessable Entity for a duplicate MMS ID' do
existing_mms_id = Item.pluck(:mms_id).compact.first
invalid_attributes = valid_attributes.merge({ mms_id: existing_mms_id })
payload = { data: { type: 'item', attributes: invalid_attributes, relationships: valid_relationships } }
allow(Rails.logger).to receive(:error)
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordInvalid))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expected_errors = expected_errors_for(invalid_attributes)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
it 'fails with 422 Unprocessable Entity for a missing MMS ID when not suppressed' do
invalid_attributes = valid_attributes.merge(mms_id: nil)
payload = { data: { type: 'item', attributes: invalid_attributes, relationships: valid_relationships } }
allow(Rails.logger).to receive(:error)
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordInvalid))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expected_errors = expected_errors_for(invalid_attributes)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
it 'treats a blank MMS ID as missing' do
post_attributes = valid_attributes.merge(mms_id: ' ')
payload = { data: { type: 'item', attributes: post_attributes, relationships: valid_relationships } }
allow(Rails.logger).to receive(:error)
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordInvalid))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
invalid_attributes = post_attributes.merge(mms_id: nil)
expected_errors = expected_errors_for(invalid_attributes)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
it 'fails with 404 Not Found for an invalid term' do
invalid_id = Term.maximum(:id) + 999
payload = {
data: {
type: 'item',
attributes: valid_attributes,
relationships: {
terms: {
data: [{ 'type' => 'term', 'id' => invalid_id.to_s }]
},
image: {
data: { 'type' => 'image', 'id' => valid_image.id }
}
}
}
}
allow(Rails.logger).to receive(:error)
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordNotFound))
expect(response).to have_http_status(:not_found)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expect(actual_errors.size).to eq(1)
end
it 'fails with 404 Not Found for a nonexistent image' do
invalid_id = Image.maximum(:id) + 999
payload = {
data: {
type: 'item',
attributes: valid_attributes,
relationships: {
image: {
data: { 'type' => 'image', 'id' => invalid_id.to_s }
}
}
}
}
allow(Rails.logger).to receive(:error)
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordNotFound))
expect(response).to have_http_status(:not_found)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expect(actual_errors.size).to eq(1)
end
xit 'returns 403 forbidden for a client-generated ID'
context 'multiple term validation' do
attr_reader :singular_facets
before do
@singular_facets = Facet
.where(allow_multiple: false)
.where.not(name: 'Concept') # exclude mock facet
.to_a
expect(singular_facets.size).to be > 1 # just to be sure
end
it 'fails with 422 Unprocessable Entity for multiple term values on singular facets' do
singular_facets.each do |facet|
invalid_terms = facet.terms.limit(2)
term_ids = invalid_terms.pluck(:id)
expect(term_ids.size).to eq(2) # just to be sure
payload = {
data: {
type: 'item',
attributes: valid_attributes,
relationships: {
terms: {
data: term_ids.map { |id| { 'type' => 'term', 'id' => id.to_s } }
},
image: {
data: { 'type' => 'image', 'id' => valid_image.id }
}
}
}
}
expect { post items_url, params: payload, as: :jsonapi }.not_to change(Item, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expected_errors = expected_errors_for(valid_attributes, terms: invalid_terms, image: valid_image)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
end
end
end
end
# TODO: separate PUT and PATCH tests
describe :update do
describe 'success' do
it 'updates an item' do
item = Item.take
old_attributes = item.attributes.slice(*Item::EDIT_ATTRS)
new_attributes = old_attributes.transform_values.with_index { |v, i| (i % 3 == 0) && v.is_a?(String) ? "#{v} (new)" : v }
expected_attributes = old_attributes.merge(new_attributes)
expected_terms = item.terms.to_a + [create(:term_abstract), create(:term_pop_art)] - [create(:term_stencil)]
payload = { data: { type: 'item', id: item.id.to_s, attributes: new_attributes,
relationships: to_relationships(terms: expected_terms, image: item.image) } }
patch item_url(item), params: payload, as: :jsonapi
expect(response).to have_http_status(:ok)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
item.reload
expected_attributes.each { |attr, val| expect(item.send(attr)).to eq(val), "Wrong value for #{attr}" }
expect(item.terms).to contain_exactly(*expected_terms)
parsed_response = JSON.parse(response.body)
links = parsed_response.delete('links')
expect(links['self']).to eq(item_url(item))
expect(parsed_response).to contain_jsonapi_for(item)
end
it 'accepts a suppressed item without an image' do
item = Item.take
old_attributes = item.attributes.slice(*Item::EDIT_ATTRS)
new_attributes = old_attributes.merge(suppressed: true)
expected_attributes = old_attributes.merge(new_attributes)
expected_terms = item.terms.to_a
relationships = to_relationships(terms: expected_terms).merge(image: { data: nil })
payload = { data: { type: 'item', id: item.id.to_s, attributes: new_attributes,
relationships: relationships } }
patch item_url(item), params: payload, as: :jsonapi
expect(response).to have_http_status(:ok)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
item.reload
expected_attributes.each do |attr, val|
actual = item.send(attr)
expect(actual).to eq(val), "Wrong value for #{attr}; expected #{val.inspect}, was #{actual.inspect}"
end
expect(item.terms).to contain_exactly(*expected_terms)
expect(item.image).to be_nil
expect(item).to be_suppressed
parsed_response = JSON.parse(response.body)
links = parsed_response.delete('links')
expect(links['self']).to eq(item_url(item))
expect(parsed_response).to contain_jsonapi_for(item)
end
it 'accepts a suppressed item with a nil MMS ID' do
item = Item.take
old_attributes = item.attributes.slice(*Item::EDIT_ATTRS)
expected_attributes = old_attributes.merge(suppressed: true, mms_id: nil)
expected_terms = item.terms.to_a
payload = { data: { type: 'item', id: item.id.to_s, attributes: expected_attributes,
relationships: to_relationships(terms: expected_terms, image: item.image) } }
patch item_url(item), params: payload, as: :jsonapi
expect(response).to have_http_status(:ok)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
item.reload
expected_attributes.each do |attr, val|
actual = item.send(attr)
expect(actual).to eq(val), "Wrong value for #{attr}; expected #{val.inspect}, was #{actual.inspect}"
end
expect(item.terms).to contain_exactly(*expected_terms)
parsed_response = JSON.parse(response.body)
links = parsed_response.delete('links')
expect(links['self']).to eq(item_url(item))
expect(parsed_response).to contain_jsonapi_for(item)
end
it 'treats a blank MMS ID as nil' do
item = Item.take
old_attributes = item.attributes.slice(*Item::EDIT_ATTRS)
patch_attributes = old_attributes.merge(suppressed: true, mms_id: '')
expected_terms = item.terms.to_a
payload = { data: { type: 'item', id: item.id.to_s, attributes: patch_attributes,
relationships: to_relationships(terms: expected_terms, image: item.image) } }
patch item_url(item), params: payload, as: :jsonapi
expect(response).to have_http_status(:ok)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
item.reload
expected_attributes = patch_attributes.merge(mms_id: nil)
expected_attributes.each do |attr, val|
actual = item.send(attr)
expect(actual).to eq(val), "Wrong value for #{attr}; expected #{val.inspect}, was #{actual.inspect}"
end
expect(item.terms).to contain_exactly(*expected_terms)
parsed_response = JSON.parse(response.body)
links = parsed_response.delete('links')
expect(links['self']).to eq(item_url(item))
expect(parsed_response).to contain_jsonapi_for(item)
end
end
describe 'failure' do
it 'fails with 422 Unprocessable Entity for a missing title' do
item = Item.take
invalid_attributes = { title: nil }
payload = { data: { type: 'item', id: item.id.to_s, attributes: invalid_attributes, relationships: valid_relationships } }
allow(Rails.logger).to receive(:error)
patch item_url(item), params: payload, as: :jsonapi
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordInvalid))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expected_errors = expected_errors_for(invalid_attributes, item_to_update: item)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
it 'fails with 422 Unprocessable Entity for a duplicate MMS ID' do
item = Item.take
mms_id = Item.pluck(:mms_id).find { |id| id && id != item.mms_id }
invalid_attributes = { mms_id: mms_id }
payload = { data: { type: 'item', id: item.id.to_s, attributes: invalid_attributes, relationships: valid_relationships } }
allow(Rails.logger).to receive(:error)
patch item_url(item), params: payload, as: :jsonapi
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordInvalid))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expect(actual_errors).to be_a(Array)
expected_errors = expected_errors_for(invalid_attributes, item_to_update: item)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
it 'fails with 422 Unprocessable Entity for a missing MMS ID when not suppressed' do
item = Item.take
expect(item.suppressed).to eq(false) # just to be sure
invalid_attributes = { mms_id: nil }
payload = { data: { type: 'item', id: item.id.to_s, attributes: invalid_attributes, relationships: valid_relationships } }
allow(Rails.logger).to receive(:error)
patch item_url(item), params: payload, as: :jsonapi
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordInvalid))
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expect(actual_errors).to be_a(Array)
expected_errors = expected_errors_for(invalid_attributes, item_to_update: item)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
end
xit 'returns 409 conflict for an invalid type'
xit 'returns 409 conflict for an ID that does not match the URL'
xit 'returns 404 not found for a nonexistent object'
context 'multiple term validation' do
attr_reader :singular_facets
before do
# creating the terms creates the facets by side effect
create_all(Term)
@singular_facets = Facet
.where(allow_multiple: false)
.where.not(name: 'Concept') # exclude mock facet
.to_a
expect(singular_facets.size).to be > 1 # just to be sure
end
it 'fails with 422 Unprocessable Entity for multiple term values on singular facets' do
item = Item.take
original_updated_at = item.updated_at
original_terms = item.terms.to_a
singular_facets.each do |facet|
terms_added = facet.terms.limit(2)
expect(terms_added.size).to eq(2) # just to be sure
invalid_terms = (original_terms + terms_added).uniq
payload = { data: { type: 'item', attributes: valid_attributes,
relationships: to_relationships(terms: invalid_terms, image: item.image) } }
expect { patch item_url(item), params: payload, as: :jsonapi }.not_to change(ItemsTerm, :count)
expect(response).to have_http_status(:unprocessable_entity)
expect(response.content_type).to start_with(JSONAPI::MEDIA_TYPE)
actual_errors = JSON.parse(response.body)['errors']
expected_errors = expected_errors_for(valid_attributes, terms: invalid_terms, image: valid_image, item_to_update: item)
expected_json = expected_errors.map { |err| jsonapi_for(err) }
expect(actual_errors).to contain_exactly(*expected_json)
item.reload
expect(item.updated_at).to eq(original_updated_at)
expect(item.terms.pluck(:value)).to contain_exactly(*original_terms.map(&:value))
end
end
end
end
end
describe :destroy do
describe 'success' do
it 'deletes an item' do
item = Item.take
expect { delete item_url(item), as: :jsonapi }.to change(Item, :count).by(-1)
expect(response).to have_http_status(:no_content)
expect(response.body).to be_empty
expect(Item.where(id: item.id)).not_to exist
end
end
describe 'failure' do
it 'fails with 404 Not Found if the item already does not exist' do
item = Item.take
item.destroy!
allow(Rails.logger).to receive(:error)
expect { delete item_url(item), as: :jsonapi }.not_to change(Item, :count)
expect(Rails.logger).to have_received(:error).with(kind_of(ActiveRecord::RecordNotFound))
expect(response).to have_http_status(:not_found)
end
end
end
end
context 'as patron' do
include_context 'patron request'
describe :create do
it 'returns 403 Forbidden' do
count_before = Item.count