Skip to content

Commit 7de8d40

Browse files
authored
Validate issued_at field to prevent future dates further than 1 year (#4749)
1 parent fddd626 commit 7de8d40

File tree

5 files changed

+23
-3
lines changed

5 files changed

+23
-3
lines changed

app/models/concerns/issued_at.rb

+7
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module IssuedAt
1010
scope :by_issued_at, ->(issued_at) { where(issued_at: issued_at.beginning_of_month..issued_at.end_of_month) }
1111
scope :for_year, ->(year) { where("extract(year from issued_at) = ?", year) }
1212
validate :issued_at_cannot_be_before_2000
13+
validate :issued_at_cannot_be_further_than_1_year
1314
end
1415

1516
private
@@ -23,4 +24,10 @@ def issued_at_cannot_be_before_2000
2324
errors.add(:issued_at, "Cannot be before 2000")
2425
end
2526
end
27+
28+
def issued_at_cannot_be_further_than_1_year
29+
if issued_at.present? && issued_at > DateTime.now.next_year
30+
errors.add(:issued_at, "cannot be more than 1 year in the future")
31+
end
32+
end
2633
end

spec/models/distribution_spec.rb

+5
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
expect(d).not_to be_valid
6666
end
6767

68+
it "ensures that the issued at is no later than 1 year" do
69+
d = build(:distribution, issued_at: DateTime.now.next_year(2).to_s)
70+
expect(d).not_to be_valid
71+
end
72+
6873
context "when delivery method is shipped" do
6974
context "shipping cost is negative" do
7075
let(:distribution) { build(:distribution, delivery_method: "shipped", shipping_cost: -13) }

spec/models/donation_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@
5454
d = build(:donation, issued_at: '1999-12-31')
5555
expect(d).not_to be_valid
5656
end
57+
it "ensures that the issued at is no later than 1 year" do
58+
d = build(:donation, issued_at: DateTime.now.next_year(2).to_s)
59+
expect(d).not_to be_valid
60+
end
5761
end
5862

5963
context "Callbacks >" do

spec/models/purchase_spec.rb

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@
8080
p = build(:purchase, issued_at: "1999-12-31")
8181
expect(p).not_to be_valid
8282
end
83+
it "ensures that the issued at is no later than 1 year" do
84+
p = build(:purchase, issued_at: DateTime.now.next_year(2).to_s)
85+
expect(p).not_to be_valid
86+
end
8387
end
8488

8589
context "Callbacks >" do

spec/services/calendar_service_spec.rb

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
partner: partner1, storage_location: storage_location)
1616
create(:distribution, issued_at: time_zone.local(2019, 1, 1),
1717
partner: partner2, storage_location: storage_location)
18-
create(:distribution, issued_at: time_zone.local(2023, 3, 17),
18+
create(:distribution, issued_at: time_zone.local(2022, 3, 17),
1919
partner: partner2, storage_location: storage_location)
2020
result = described_class.calendar(organization.id)
2121
expected = <<~ICAL
@@ -58,8 +58,8 @@
5858
e
5959
END:VEVENT
6060
BEGIN:VEVENT
61-
DTSTART;TZID=America/New_York:20230316T210000
62-
DTEND;TZID=America/New_York:20230316T211500
61+
DTSTART;TZID=America/New_York:20220316T210000
62+
DTEND;TZID=America/New_York:20220316T211500
6363
LOCATION:1500 Remount Road\\, Front Royal\\, VA 22630
6464
SUMMARY:Pickup from Partner 2
6565
URL;VALUE=URI:https://humanessentials.app/diaper_bank/distributions/schedul

0 commit comments

Comments
 (0)