Skip to content

Commit 1fbfbf5

Browse files
authored
Merge pull request #5092 from mateusmsf94/5065-validation-negative-numbers
Add validation for money_raised in Donation model to ensure it is non…
2 parents e747392 + 084a7bb commit 1fbfbf5

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

app/models/donation.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class Donation < ApplicationRecord
6161
validates :manufacturer, presence:
6262
{ message: "must be specified since you chose '#{SOURCES[:manufacturer]}'" }, if: :from_manufacturer?
6363
validates :source, presence: true, inclusion: { in: SOURCES.values, message: "Must be a valid source." }
64+
validates :money_raised, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
6465
validate :line_items_quantity_is_positive
6566

6667
# TODO: move this to Organization.donations as an extension

spec/models/donation_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@
6363
d.line_items << build(:line_item, quantity: -1)
6464
expect(d).not_to be_valid
6565
end
66+
it "ensures that money_raised cannot be negative" do
67+
expect(build(:donation, money_raised: -1)).not_to be_valid
68+
expect(build(:donation, money_raised: 0)).to be_valid
69+
expect(build(:donation, money_raised: 100)).to be_valid
70+
expect(build(:donation, money_raised: nil)).to be_valid
71+
end
6672
end
6773

6874
context "Callbacks >" do

spec/requests/donations_requests_spec.rb

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@
108108
let(:manufacturer) { create(:manufacturer, organization:) }
109109
let(:source) { Donation::SOURCES[:manufacturer] }
110110
let(:issued_at) { Date.yesterday }
111+
let(:money_raised) { 5 }
111112

112113
let(:params) do
113114
{
@@ -116,7 +117,7 @@
116117
manufacturer_id: manufacturer.id,
117118
product_drive_id: product_drive.id,
118119
storage_location_id: storage_location.id,
119-
money_raised_in_dollars: 5,
120+
money_raised_in_dollars: money_raised,
120121
product_drive_participant_id: nil,
121122
comment: "",
122123
issued_at: issued_at,
@@ -146,6 +147,15 @@
146147
expect(flash[:error]).to include("Issue date can't be blank")
147148
end
148149
end
150+
151+
context "with negative money raised" do
152+
let(:money_raised) { -5 }
153+
154+
it "flashes the correct validation error" do
155+
post donations_path(params)
156+
expect(flash[:error]).to include("Money raised must be greater than or equal to 0")
157+
end
158+
end
149159
end
150160

151161
describe "PATCH #update" do
@@ -168,6 +178,15 @@
168178
expect(flash[:alert]).to include("Issue date can't be blank")
169179
end
170180
end
181+
182+
context "with negative money raised" do
183+
it "flashes the correct validation error" do
184+
donation_params[:money_raised_in_dollars] = -5
185+
put donation_path(params)
186+
187+
expect(flash[:alert]).to include("Money raised must be greater than or equal to 0")
188+
end
189+
end
171190
end
172191

173192
describe "GET #print" do

0 commit comments

Comments
 (0)