-
-
Notifications
You must be signed in to change notification settings - Fork 573
Expand file tree
/
Copy pathdistributions_requests_spec.rb
More file actions
970 lines (792 loc) · 40.6 KB
/
distributions_requests_spec.rb
File metadata and controls
970 lines (792 loc) · 40.6 KB
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
RSpec.describe "Distributions", type: :request do
let(:organization) { create(:organization) }
let(:user) { create(:user, organization: organization) }
let(:organization_admin) { create(:organization_admin, organization: organization) }
let(:secret_key) { "HI MOM THIS IS ME AND I'M CODING" }
let(:crypt) { ActiveSupport::MessageEncryptor.new(secret_key) }
let(:hashed_id) { CGI.escape(crypt.encrypt_and_sign(organization.id)) }
before(:each) do
allow(Rails.application).to receive(:secret_key_base).and_return(secret_key)
end
context "While signed in" do
before do
sign_in(user)
end
describe "GET #itemized_breakdown" do
let(:fake_csv) { "FAKE OUTPUT" }
before do
allow_any_instance_of(DistributionItemizedBreakdownService).to receive(:fetch_csv).and_return(fake_csv)
end
it "returns http success" do
get itemized_breakdown_distributions_path(format: :csv)
expect(response).to be_successful
expect(response.body).to eq(fake_csv)
end
end
describe "GET #print" do
subject { get print_distribution_path(id: create(:distribution).id) }
it "returns http success" do
get print_distribution_path(id: create(:distribution).id)
expect(response).to be_successful
end
context "with signature lines enabled" do
it "returns http success" do
organization.update!(signature_for_distribution_pdf: true)
get print_distribution_path(id: create(:distribution).id)
expect(response).to be_successful
end
end
context "with non-UTF8 characters" do
let(:non_utf8_partner) { create(:partner, name: "KOKA Keiki O Ka ‘Āina") }
it "returns http success" do
get print_distribution_path(id: create(:distribution, partner: non_utf8_partner).id)
expect(response).to be_successful
end
end
include_examples "restricts access to organization users/admins"
end
describe "GET #reclaim" do
it "returns http success" do
get distributions_path(id: create(:distribution).id)
expect(response).to be_successful
end
end
describe "GET #index" do
subject { get distributions_path }
let(:item) { create(:item, value_in_cents: 100, organization: organization) }
let!(:distribution) { create(:distribution, :with_items, :past, item: item, item_quantity: 10, organization: organization) }
it "returns http success" do
get distributions_path
expect(response).to be_successful
end
context "with export_csv param" do
it "redirects then renders an iframe to trigger CSV download" do
get distributions_path(export_csv: true, foo: "bar")
expect(response).to redirect_to(distributions_path(foo: "bar"))
follow_redirect!
expect(response.body).to include("iframe")
expect(response.body).to include("distributions.csv?foo=bar")
end
end
it "sums distribution totals accurately" do
create(:distribution, :with_items, item_quantity: 5, organization: organization)
create(:line_item, :distribution, itemizable_id: distribution.id, quantity: 7)
get distributions_path
expect(assigns(:total_items_all_distributions)).to eq(22)
expect(assigns(:total_items_paginated_distributions)).to eq(22)
end
it "shows an enabled edit and reclaim button" do
get distributions_path
page = Nokogiri::HTML(response.body)
edit = page.at_css("a[href='#{edit_distribution_path(id: distribution.id)}']")
reclaim = page.at_css("form[action='#{distribution_path(id: distribution.id)}'] .btn-danger")
expect(edit.attr("class")).not_to match(/disabled/)
expect(reclaim.attr("class")).not_to match(/disabled/)
expect(response.body).not_to match(/Has Inactive Items/)
end
context "with a disabled item" do
before do
item.update(active: false)
end
it "shows a disabled edit and reclaim button" do
get distributions_path
page = Nokogiri::HTML(response.body)
edit = page.at_css("a[href='#{edit_distribution_path(id: distribution.id)}']")
reclaim = page.at_css("form[action='#{distribution_path(id: distribution.id)}'] .btn-danger")
expect(edit.attr("class")).to match(/disabled/)
expect(reclaim.attr("class")).to match(/disabled/)
expect(response.body).to match(/Has Inactive Items/)
end
end
context "with filters" do
it "shows all active partners in dropdown filter unrestricted by current filter" do
inactive_partner_name = create(:partner, :deactivated, organization:).name
active_partner_name = distribution.partner.name
# Filter by date with no distributions
params = { filters: { date_range: "January 1,9999 - January 1,9999"} }
get distributions_path, params: params
page = Nokogiri::HTML(response.body)
partner_select = page.at_css("select[name='filters[by_partner]']")
expect(partner_select).to be_present
expect(partner_select.text).to include(active_partner_name)
expect(partner_select.text).not_to include(inactive_partner_name)
end
end
context "when filtering by item id" do
let!(:item_2) { create(:item, value_in_cents: 100, organization: organization) }
let(:params) { { filters: { by_item_id: item.id } } }
before do
distribution.line_items << create(:line_item, item: item_2, quantity: 10)
end
it "shows value and quantity for that item in distributions" do
get distributions_path, params: params
page = Nokogiri::HTML(response.body)
item_quantity, item_value = page.css("table tbody tr td.numeric")
# total value/quantity of distribution
expect(distribution.total_quantity).to eq(20)
expect(distribution.value_per_itemizable).to eq(2000)
# displays quantity and value of filtered item in distribution
expect(item_quantity.text).to eq("10")
expect(item_value.text).to eq("$10.00")
end
it "changes the total quantity and value headers" do
get distributions_path, params: params
page = Nokogiri::HTML(response.body)
item_total_header, item_value_header = page.css("table thead tr th.numeric")
expect(item_total_header.text).to eq("Total #{item.name}")
expect(item_value_header.text).to eq("Value of #{item.name}")
end
end
context "when filtering by item category id" do
let!(:item_category) { create(:item_category, organization:) }
let!(:item_category_2) { create(:item_category, organization:) }
let!(:item_2) { create(:item, item_category: item_category_2, value_in_cents: 100, organization: organization) }
let(:params) { { filters: { by_item_category_id: item.item_category_id } } }
before do
item.update(item_category: item_category)
distribution.line_items << create(:line_item, item: item_2, quantity: 10)
end
it "shows value and quantity for that item category in distributions" do
get distributions_path, params: params
page = Nokogiri::HTML(response.body)
item_quantity, item_value = page.css("table tbody tr td.numeric")
# total value/quantity of distribution
expect(distribution.total_quantity).to eq(20)
expect(distribution.value_per_itemizable).to eq(2000)
# displays quantity and value of filtered item in distribution
expect(item_quantity.text).to eq("10")
expect(item_value.text).to eq("$10.00")
end
it "changes the total quantity and value headers" do
get distributions_path, params: params
page = Nokogiri::HTML(response.body)
item_total_header, item_value_header = page.css("table thead tr th.numeric")
expect(item_total_header.text).to eq("Total in #{item_category.name}")
expect(item_value_header.text).to eq("Value of #{item_category.name}")
end
it "doesn't show duplicate distributions" do
# Add another item in given category so that a JOIN clauses would produce duplicates
item.update(item_category: item_category_2, value_in_cents: 50)
get distributions_path, params: params
page = Nokogiri::HTML(response.body)
distribution_rows = page.css("table tbody tr")
expect(distribution_rows.count).to eq(1)
end
end
include_examples "restricts access to organization users/admins"
end
describe "POST #create" do
subject { post distributions_path(distribution:) }
let!(:storage_location) { create(:storage_location, organization: organization) }
let!(:partner) { create(:partner, organization: organization) }
let(:issued_at) { Time.current }
let(:distribution) do
{ storage_location_id: storage_location.id, partner_id: partner.id, issued_at:, delivery_method: :delivery }
end
it "redirects to #show on success" do
expect(storage_location).to be_valid
expect(partner).to be_valid
expect(PartnerMailerJob).to receive(:perform_later).once
post distributions_path(distribution:, format: :turbo_stream)
expect(response).to have_http_status(:redirect)
last_distribution = Distribution.last
expect(response).to redirect_to(distribution_path(last_distribution))
end
it "renders #new again on failure, with notice" do
post distributions_path(distribution: { comment: nil, partner_id: nil, storage_location_id: nil }, format: :turbo_stream)
expect(response).to have_http_status(400)
expect(response).to have_error
end
it "renders #new with item quantities in dropdowns listed" do
create(:item, :with_unit, organization: organization, name: 'Item 1', unit: 'pack')
post distributions_path(distribution: distribution.except(:partner_id), format: :turbo_stream)
expect(response).to have_http_status(400)
expect(flash[:error]).to include("Sorry, we weren't able to save the distribution.")
expect(response.body).to include("Item 1 (0)")
end
it "renders #new on failure with only active items in dropdown" do
create(:item, organization: organization, name: 'Active Item')
create(:item, :inactive, organization: organization, name: 'Inactive Item')
post distributions_path(distribution: { comment: nil, partner_id: nil, storage_location_id: nil }, format: :turbo_stream)
expect(response).to have_http_status(400)
page = Nokogiri::HTML(response.body)
selectable_items = page.at_css("select.line_item_name").text.split("\n")
expect(selectable_items).to include("Active Item")
expect(selectable_items).not_to include("Inactive Item")
end
context "Deactivated partners should not be displayed in partner dropdown" do
before do
create(:partner, name: 'Active Partner', organization: organization, status: "approved")
create(:partner, name: 'Deactivated Partner', organization: organization, status: "deactivated")
end
it "should not display deactivated partners after error and re-render of form" do
post distributions_path(distribution: { comment: nil, partner_id: nil, storage_location_id: nil }, format: :turbo_stream)
expect(response).to have_http_status(400)
expect(response).to have_error
expect(response.body).not_to include("Deactivated Partner")
expect(response.body).to include("Active Partner")
end
end
context "with missing issued_at field" do
let(:issued_at) { "" }
it "fails and returns validation error message" do
post distributions_path(distribution:, format: :turbo_stream)
expect(response).to have_http_status(400)
expect(flash[:error]).to include("Distribution date and time can't be blank")
end
end
include_examples "restricts access to organization users/admins"
end
describe "GET #new" do
subject { get new_distribution_path(default_params) }
let!(:partner) { create(:partner, organization: organization) }
let(:request) { create(:request, partner: partner, organization: organization, item_requests: item_requests) }
let(:items) {
[
create(:item, :with_unit, organization: organization, name: 'Item 1', unit: 'pack'),
create(:item, organization: organization, name: 'Item 2')
]
}
let(:item_requests) {
[
create(:item_request, item: items[0], quantity: 50, request_unit: 'pack'),
create(:item_request, item: items[1], quantity: 25)
]
}
let(:storage_location) { create(:storage_location, :with_items, organization: organization) }
let(:default_params) { { request_id: request.id } }
it "returns http success" do
get new_distribution_path(default_params)
expect(response).to be_successful
# default should be nothing selected
page = Nokogiri::HTML(response.body)
expect(page.css('#distribution_storage_location_id option[selected]')).to be_empty
end
it "should only show active items in item dropdown" do
create(:item, :inactive, organization: organization, name: 'Inactive Item')
get new_distribution_path(default_params)
page = Nokogiri::HTML(response.body)
selectable_items = page.at_css("select#barcode_item_barcodeable_id").text.split("\n")
expect(selectable_items).to include("Item 1", "Item 2")
expect(selectable_items).not_to include("Inactive Item")
end
it "disables the partner field when distribution is created from a request" do
get new_distribution_path(default_params)
page = Nokogiri::HTML(response.body)
expect(page.at_css("select#distribution_partner_id").classes).to include("disabled")
end
context "with org default but no partner default" do
it "selects org default" do
organization.update!(default_storage_location: storage_location.id)
get new_distribution_path(default_params)
expect(response).to be_successful
page = Nokogiri::HTML(response.body)
expect(page.css(%(#distribution_storage_location_id option[selected][value="#{storage_location.id}"]))).not_to be_empty
end
end
context "with partner default" do
it "selects partner default" do
location2 = create(:storage_location, :with_items)
organization.update!(default_storage_location: location2.id)
partner.update!(default_storage_location_id: storage_location.id)
get new_distribution_path(default_params)
expect(response).to be_successful
page = Nokogiri::HTML(response.body)
expect(page.css(%(#distribution_storage_location_id option[selected][value="#{storage_location.id}"]))).not_to be_empty
end
end
context "Deactivated partners should not be displayed in partner dropdown" do
before do
create(:partner, name: 'Active Partner', organization: organization, status: "approved")
create(:partner, name: 'Deactivated Partner', organization: organization, status: "deactivated")
end
it "should not display deactivated partners on new distribution" do
get new_distribution_path(default_params)
expect(response.body).not_to include("Deactivated Partner")
expect(response.body).to include("Active Partner")
end
end
context 'with units' do
before(:each) do
Flipper.enable(:enable_packs)
end
it 'should behave correctly' do
get new_distribution_path(default_params)
expect(response).to be_successful
page = Nokogiri::HTML(response.body)
# should have a disabled select and a hidden input
expect(page.css('select[disabled][name="distribution[line_items_attributes][0][item_id]"]')).not_to be_empty
expect(page.css('input[name="distribution[line_items_attributes][0][item_id]"]')).not_to be_empty
expect(page.css('select[disabled][name="distribution[line_items_attributes][1][item_id]"]')).not_to be_empty
expect(page.css('input[name="distribution[line_items_attributes][1][item_id]"]')).not_to be_empty
# input with packs should be blank
expect(page.css('#distribution_line_items_attributes_0_quantity').attr('value')).to eq(nil)
# input with no packs should show quantity
expect(page.css('#distribution_line_items_attributes_1_quantity').attr('value').value).to eq('25')
end
context 'with no request' do
it 'should have no inputs' do
get new_distribution_path({})
expect(response).to be_successful
page = Nokogiri::HTML(response.body)
# blank input shown
expect(page.css('select[name="distribution[line_items_attributes][0][item_id]"]')).not_to be_empty
expect(page.css('#distribution_line_items_attributes_0_quantity').attr('value')).to eq(nil)
# in the template
expect(page.css('select[name="distribution[line_items_attributes][1][item_id]"]')).not_to be_empty
end
it "should have partner select field enabled" do
get new_distribution_path({})
page = Nokogiri::HTML(response.body)
expect(page.at_css("select#distribution_partner_id").classes).not_to include("disabled")
end
end
end
include_examples "restricts access to organization users/admins"
end
describe "GET #show" do
subject { get distribution_path(id: distribution.id) }
let(:item) { create(:item, organization: organization) }
let!(:distribution) { create(:distribution, :with_items, item: item, item_quantity: 1, organization: organization) }
it "sums distribution totals accurately" do
distribution = create(:distribution, :with_items, item_quantity: 1, organization: organization)
item_quantity = 6
package_size = 2
item = create(:item, package_size: package_size)
create(
:line_item,
:distribution,
itemizable_id: distribution.id,
item_id: item.id,
quantity: item_quantity
)
get distribution_path(id: distribution.id)
expect(response).to be_successful
expect(assigns(:total_quantity)).to eq(item_quantity + 1)
expect(assigns(:total_package_count)).to eq(item_quantity / package_size)
end
it "shows an enabled edit button" do
get distribution_path(id: distribution.id)
page = Nokogiri::HTML(response.body)
edit = page.at_css("a[href='#{edit_distribution_path(id: distribution.id)}']")
expect(edit.attr("class")).not_to match(/disabled/)
expect(response.body).not_to match(/please make the following items active:/)
end
context "with an inactive item" do
before do
item.update(active: false)
end
it "shows a disabled edit button" do
get distribution_path(id: distribution.id)
page = Nokogiri::HTML(response.body)
edit = page.at_css("a[href='#{edit_distribution_path(id: distribution.id)}']")
expect(edit.attr("class")).to match(/disabled/)
expect(response.body).to match(/please make the following items active: #{item.name}/)
end
end
include_examples "restricts access to organization users/admins"
end
describe "GET #schedule" do
subject { get schedule_distributions_path }
it "returns http success" do
get schedule_distributions_path
expect(response).to be_successful
page = Nokogiri::HTML(response.body)
url = page.at_css('#copy-calendar-button').attributes['data-url'].value
hash = url.match(/\?hash=(.*)/)[1]
expect(crypt.decrypt_and_verify(CGI.unescape(hash))).to eq(organization.id)
end
include_examples "restricts access to organization users/admins"
end
describe 'PATCH #picked_up' do
subject { patch picked_up_distribution_path(id: distribution.id) }
context 'when the distribution is successfully updated' do
let(:distribution) { create(:distribution, state: :scheduled, organization: organization) }
it "updates the state to 'complete'" do
subject
expect(distribution.reload.state).to eq 'complete'
end
it 'redirects the user back to the distributions page' do
expect(subject).to redirect_to distribution_path
end
include_examples "restricts access to organization users/admins"
end
end
describe "GET #pickup_day" do
subject { get pickup_day_distributions_path }
it "returns http success" do
get pickup_day_distributions_path
expect(response).to be_successful
end
it "correctly sums the item counts from distributions" do
first_item = create(:item, organization: organization)
second_item = create(:item, organization: organization)
first_distribution = create(:distribution, organization: organization)
second_distribution = create(:distribution, organization: organization)
create(:line_item, :distribution, item_id: first_item.id, itemizable_id: first_distribution.id, quantity: 7)
create(:line_item, :distribution, item_id: first_item.id, itemizable_id: second_distribution.id, quantity: 4)
create(:line_item, :distribution, item_id: second_item.id, itemizable_id: second_distribution.id, quantity: 5)
get pickup_day_distributions_path
expect(assigns(:daily_items).detect { |item| item[:name] == first_item.name }[:quantity]).to eq(11)
expect(assigns(:daily_items).detect { |item| item[:name] == second_item.name }[:quantity]).to eq(5)
expect(assigns(:daily_items).sum { |item| item[:quantity] }).to eq(16)
end
it "correctly sums the item package counts from distributions" do
first_item = create(:item, package_size: 2, organization: organization)
second_item = create(:item, package_size: 3, organization: organization)
first_distribution = create(:distribution, organization: organization)
second_distribution = create(:distribution, organization: organization)
create(:line_item, :distribution, item_id: first_item.id, itemizable_id: first_distribution.id, quantity: 7)
create(:line_item, :distribution, item_id: first_item.id, itemizable_id: second_distribution.id, quantity: 4)
create(:line_item, :distribution, item_id: second_item.id, itemizable_id: second_distribution.id, quantity: 6)
get pickup_day_distributions_path
expect(assigns(:daily_items).detect { |item| item[:name] == first_item.name }[:package_count]).to eq(5)
expect(assigns(:daily_items).detect { |item| item[:name] == second_item.name }[:package_count]).to eq(2)
expect(assigns(:daily_items).sum { |item| item[:package_count] }).to eq(7)
end
include_examples "restricts access to organization users/admins"
end
describe "PATCH #update" do
subject { patch distribution_path(distribution_params) }
let(:partner_name) { "Patrick" }
let(:location) { create(:storage_location, organization: organization) }
let(:partner) { create(:partner, name: partner_name, organization: organization) }
let(:distribution) { create(:distribution, partner: partner, organization: organization) }
let(:issued_at) { distribution.issued_at }
let(:distribution_params) do
{ id: distribution.id,
distribution: {
partner_id: partner.id,
storage_location_id: location.id,
'issued_at(1i)' => issued_at.to_date.year,
'issued_at(2i)' => issued_at.to_date.month,
'issued_at(3i)' => issued_at.to_date.day
}}
end
it "returns a 200" do
patch distribution_path(distribution_params)
expect(response.status).to redirect_to(distribution_path(distribution.to_param))
end
context "with invalid issued_at field" do
let(:distribution_params) do
{ id: distribution.id,
distribution: {
partner_id: partner.id,
storage_location_id: location.id,
'issued_at(1i)' => issued_at.to_date.year,
'issued_at(2i)' => issued_at.to_date.month,
'issued_at(3i)' => nil # day part of date missing
}}
end
it "fails and returns validation error message" do
patch distribution_path(distribution_params)
expect(flash[:error]).to include("Distribution date and time can't be blank")
expect(response).not_to redirect_to(anything)
end
it "renders storage location dropdowns" do
patch distribution_path(distribution_params)
page = Nokogiri::HTML(response.body)
selectable_partners = page.at_css("select#distribution_partner_id").text.split("\n")
expect(selectable_partners).to include("Patrick")
end
end
context 'without line items - with intervening snapshot' do
it 'should save the other parameters' do
distribution = FactoryBot.create(:distribution,
:with_items,
organization: organization,
created_at: 1.week.ago)
SnapshotEvent.create!(organization_id: organization.id,
created_at: 1.day.ago,
event_time: 1.day.ago,
eventable: organization,
data: EventTypes::Inventory.new(
organization_id: organization.id, storage_locations: {}
))
put distribution_path({ id: distribution.id, distribution: {comment: "A new comment"}})
expect(distribution.reload.comment).to eq("A new comment")
end
end
describe "when changing storage location" do
let(:item) { create(:item, organization: organization) }
it "updates storage quantity correctly" do
new_storage_location = create(:storage_location, organization: organization)
create(:donation, :with_items, item: item, item_quantity: 30, storage_location: new_storage_location, organization: organization)
distribution = create(:distribution, :with_items, item: item, item_quantity: 10, organization: organization)
original_storage_location = distribution.storage_location
line_item = distribution.line_items.first
line_item_params = {
"0" => {
"_destroy" => "false",
item_id: line_item.item_id,
quantity: "5",
id: line_item.id
}
}
distribution_params = { storage_location_id: new_storage_location.id, line_items_attributes: line_item_params }
expect do
put distribution_path(id: distribution.id, distribution: distribution_params)
end.to change { original_storage_location.size }.by(10) # removes the whole distribution of 10 - increasing inventory
expect(new_storage_location.size).to eq 25
end
end
context "mail follow up" do
subject { patch distribution_path(distribution_params) }
it "does not send an e-mail" do
expect { subject }.not_to change { ActionMailer::Base.deliveries.count }
end
context "sending" do
let(:issued_at) { distribution.issued_at + 1.day }
it "does send an e-mail" do
expect { subject }.to change { ActionMailer::Base.deliveries.count }.by(1)
end
end
context "partner reminder sending switched off" do
let(:issued_at) { distribution.issued_at + 1.day }
before { partner.update!(send_reminders: false) }
it "does not send the e-mail" do
expect { subject }.not_to change { ActionMailer::Base.deliveries.count }
end
end
end
include_examples "restricts access to organization users/admins"
end
describe "GET #edit" do
subject { get edit_distribution_path(id: distribution.id) }
let(:location) { create(:storage_location, organization: organization) }
let(:partner) { create(:partner, organization: organization) }
let(:distribution) { create(:distribution, partner: partner, created_at: 1.week.ago) }
it 'should not allow edits if there is an intervening snapshot' do
distribution.line_items << build(:line_item,
quantity: 5,
item: organization.items.last,
itemizable: distribution)
SnapshotEvent.create!(organization_id: organization.id,
created_at: 1.day.ago,
event_time: 1.day.ago,
eventable: organization,
data: EventTypes::Inventory.new(
organization_id: organization.id, storage_locations: {}
))
get edit_distribution_path(id: distribution.id)
expect(response.body)
.to include('This distribution is too old to edit inventory. You can only change non-inventory fields.')
expect(response.body).not_to include('Add Another Item')
expect(response.body).not_to include('Remove Item')
parsed_body = Nokogiri::HTML(response.body)
expect(parsed_body.css('select.line_item_name[disabled]')).not_to be_empty
end
it "should show the distribution" do
get edit_distribution_path(id: distribution.id)
expect(response).to be_successful
expect(response.body).not_to include("You’ve had an audit since this distribution was started.")
end
describe 'audit warnings' do
let!(:item) { create(:item, organization: organization, name: "Brightbloom Seed") }
let!(:storage_location) { create(:storage_location, :with_items, item: item, organization: organization) }
let!(:distribution) { create(:distribution, :with_items, item: item, storage_location: storage_location, created_at: 1.week.ago) }
context "when an audit has been performed on the purchased items" do
before(:each) do
create(:audit, :with_items, item: item, storage_location: storage_location, status: "finalized")
end
it "shows a warning" do
get edit_distribution_path(distribution)
expect(response.body).to include("You’ve had an audit since this distribution was started.")
end
context 'with an intervening snapshot' do
it 'does not show a warning' do
SnapshotEvent.create!(organization_id: organization.id,
created_at: 1.day.ago,
event_time: 1.day.ago,
eventable: organization,
data: EventTypes::Inventory.new(
organization_id: organization.id, storage_locations: {}
))
get edit_distribution_path(distribution)
expect(response.body).not_to include("You’ve had an audit since this distribution was started.")
end
end
end
context "when no audit has been performed" do
it "does not show a warning" do
get edit_distribution_path(distribution)
expect(response.body).to_not include("You’ve had an audit since this distribution was started.")
end
end
end
it "should show a warning if there is an inteverning audit" do
distribution.update!(created_at: 1.week.ago)
create(:audit, storage_location: distribution.storage_location, organization: organization)
get edit_distribution_path(id: distribution.id)
expect(response.body).to include("You’ve had an audit since this distribution was started.")
end
it "should not show a warning if the audit is for another location" do
distribution.update!(created_at: 1.week.ago)
create(:audit, storage_location: create(:storage_location))
get edit_distribution_path(id: distribution.id)
expect(response.body).not_to include("You’ve had an audit since this distribution was started.")
end
it "should display deactivated partners in partner dropdown" do
create(:partner, name: 'Active Partner', organization: organization, status: "approved")
create(:partner, name: 'Deactivated Partner', organization: organization, status: "deactivated")
get edit_distribution_path(id: distribution.id)
expect(response.body).to include("Deactivated Partner")
expect(response.body).to include("Active Partner")
end
it "should only show active items in item dropdown" do
create(:item, organization: organization, name: 'Active Item')
create(:item, :inactive, organization: organization, name: 'Inactive Item')
get edit_distribution_path(id: distribution.id)
page = Nokogiri::HTML(response.body)
selectable_items = page.at_css("select#barcode_item_barcodeable_id").text.split("\n")
expect(selectable_items).to include("Active Item")
expect(selectable_items).not_to include("Inactive Item")
end
it "should have partner select field enabled" do
get edit_distribution_path(id: distribution.id)
page = Nokogiri::HTML(response.body)
expect(page.at_css("select#distribution_partner_id").classes).not_to include("disabled")
end
context 'with units' do
let!(:request) {
create(:request,
partner: partner,
organization: organization,
distribution_id: distribution.id,
item_requests: item_requests)
}
let(:items) {
[
create(:item, :with_unit, organization: organization, name: 'Item 1', unit: 'pack'),
create(:item, organization: organization, name: 'Item 2'),
create(:item, organization: organization, name: 'Item 3')
]
}
let!(:item_requests) {
[
create(:item_request, item: items[0], quantity: 50, request_unit: 'pack'),
create(:item_request, item: items[1], quantity: 25)
]
}
before(:each) do
Flipper.enable(:enable_packs)
create(:line_item, itemizable: distribution, item_id: items[0].id, quantity: 25)
create(:line_item, itemizable: distribution, item_id: items[2].id, quantity: 10)
end
it 'should behave correctly' do
get edit_distribution_path(id: distribution.id)
expect(response).to be_successful
page = Nokogiri::HTML(response.body)
# should have a regular select and no hidden input
expect(page.css('select[disabled][name="distribution[line_items_attributes][0][item_id]"]')).not_to be_empty
expect(page.css('input[name="distribution[line_items_attributes][0][item_id]"]')).not_to be_empty
# should have a regular select and no hidden input
expect(page.css('select[name="distribution[line_items_attributes][1][item_id]"]')).not_to be_empty
expect(page.css('select[disabled][name="distribution[line_items_attributes][1][item_id]"]')).to be_empty
expect(page.css('input[name="distribution[line_items_attributes][1][item_id]"]')).to be_empty
# should have a disabled select and a hidden input
expect(page.css('select[disabled][name="distribution[line_items_attributes][2][item_id]"]')).not_to be_empty
expect(page.css('input[name="distribution[line_items_attributes][2][item_id]"]')).not_to be_empty
# existing inputs should show numbers
expect(page.css('#distribution_line_items_attributes_0_quantity').attr('value').value).to eq('25')
expect(page.css('#distribution_line_items_attributes_1_quantity').attr('value').value).to eq('10')
# input from request should show 0
expect(page.css('#distribution_line_items_attributes_2_quantity').attr('value').value).to eq('0')
end
it "disables the partner field when distribution is created from a request" do
get edit_distribution_path(id: distribution.id)
page = Nokogiri::HTML(response.body)
expect(page.at_css("select#distribution_partner_id").classes).to include("disabled")
end
context 'with no request' do
it 'should have everything enabled' do
request.destroy
get edit_distribution_path(id: distribution.id)
expect(response).to be_successful
page = Nokogiri::HTML(response.body)
expect(page.css('select[name="distribution[line_items_attributes][0][item_id]"]')).not_to be_empty
expect(page.css('select[disabled][name="distribution[line_items_attributes][0][item_id]"]')).to be_empty
expect(page.css('input[name="distribution[line_items_attributes][0][item_id]"]')).to be_empty
expect(page.css('select[name="distribution[line_items_attributes][1][item_id]"]')).not_to be_empty
expect(page.css('select[disabled][name="distribution[line_items_attributes][1][item_id]"]')).to be_empty
expect(page.css('input[name="distribution[line_items_attributes][1][item_id]"]')).to be_empty
end
end
end
# Bug fix #4537
context "when distribution sets storage location total inventory to zero" do
let(:item1) { create(:item, name: "Item 1", organization: organization) }
let(:test_storage_name) { "Test Storage" }
let(:storage_location) { create(:storage_location, name: test_storage_name, organization: organization) }
before(:each) do
quantity = 20
TestInventory.create_inventory(organization, {
storage_location.id => {
item1.id => quantity
}
})
@distribution_all = create(:distribution, :with_items, item: item1, item_quantity: quantity, storage_location: storage_location, organization: organization)
DistributionCreateService.new(@distribution_all).call
end
it "allows you to select the original storage location for the distribution" do
get edit_distribution_path(id: @distribution_all.id)
expect(response.body).to include("<option selected=\"selected\" value=\"#{storage_location.id}\">#{test_storage_name}</option>")
end
end
include_examples "restricts access to organization users/admins"
end
describe 'POST #validate' do
it 'should handle missing CSRF gracefully' do
ActionController::Base.allow_forgery_protection = true
post validate_partners_individuals_requests_path
ActionController::Base.allow_forgery_protection = false
expect(JSON.parse(response.body)).to eq({'valid' => false})
expect(response.status).to eq(200)
end
end
describe 'DELETE #destroy' do
subject { delete distribution_path(id: distribution.id) }
let!(:distribution) { create(:distribution, organization: organization, created_at: 1.week.ago) }
it "deletes the distribution" do
expect { subject }.to change { Distribution.count }.by(-1)
expect(response).to redirect_to(distributions_path)
expect(flash[:notice]).to include("Distribution #{distribution.id} has been reclaimed!")
end
it "does not delete the distribution if there is an intervening snapshot" do
data = EventTypes::Inventory.new(storage_locations: {}, organization_id: organization.id)
travel(-1.day) do
SnapshotEvent.create!(organization_id: organization.id,
eventable: organization,
data: data,
event_time: Time.zone.now)
end
expect { subject }.not_to change { Distribution.count }
expect(flash[:error]).to eq("We can't delete distributions entered before #{1.day.ago.to_date}.")
end
include_examples "restricts access to organization users/admins"
end
end
context "While not signed in" do
let(:object) { create(:distribution) }
# calendar does not need signin
describe 'GET #calendar' do
before(:each) do
allow(CalendarService).to receive(:calendar).and_return("SOME ICS STRING")
end
context 'with a correct hash id' do
it 'should render the calendar' do
get calendar_distributions_path(hash: hashed_id)
expect(CalendarService).to have_received(:calendar).with(organization.id)
expect(response.media_type).to include('text/calendar')
expect(response.body).to eq('SOME ICS STRING')
end
end
context 'without a correct hash id' do
it 'should error unauthorized' do
get calendar_distributions_path(hash: 'some-wrong-id')
expect(response.status).to eq(401)
end
end
end
end
end