Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for too high values #989

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ihatemoney/forms.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import decimal
from re import match
from types import SimpleNamespace

Expand Down Expand Up @@ -26,6 +27,7 @@
from wtforms.fields.html5 import URLField
except ModuleNotFoundError:
from wtforms.fields import URLField

from wtforms.validators import (
URL,
DataRequired,
Expand Down Expand Up @@ -384,6 +386,9 @@ def set_default(self):
def validate_amount(self, field):
if field.data == 0:
raise ValidationError(_("Bills can't be null"))
elif decimal.Decimal(field.data) > decimal.MAX_EMAX:
# See https://github.com/python-babel/babel/issues/821
raise ValidationError(f"Result is too high: {field.data}")


class MemberForm(FlaskForm):
Expand Down
20 changes: 20 additions & 0 deletions ihatemoney/tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,26 @@ def test_project_creation_with_mixed_case(self):
)
self.assertStatus(200, resp)

def test_amount_too_high(self):
self.api_create("raclette")
# add participants
self.api_add_member("raclette", "zorglub")

# add a bill with too high amount
# See https://github.com/python-babel/babel/issues/821
req = self.client.post(
"/api/projects/raclette/bills",
data={
"date": "2011-08-10",
"what": "fromage",
"payer": "1",
"payed_for": ["1"],
"amount": "9347242149381274732472348728748723473278472843.12",
},
headers=self.get_auth("raclette"),
)
self.assertStatus(400, req)


if __name__ == "__main__":
unittest.main()
26 changes: 26 additions & 0 deletions ihatemoney/tests/budget_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,32 @@ def test_decimals_on_weighted_members_list(self):
'fred<span class="light">(x1.15)</span>', resp.data.decode("utf-8")
)

def test_amount_too_high(self):
self.post_project("raclette")

# add participants
self.client.post("/raclette/members/add", data={"name": "zorglub"})

# High amount should be rejected.
# See https://github.com/python-babel/babel/issues/821
resp = self.client.post(
"/raclette/add",
data={
"date": "2016-12-31",
"what": "fromage à raclette",
"payer": 1,
"payed_for": [1],
"amount": "9347242149381274732472348728748723473278472843.12",
"original_currency": "EUR",
},
)
assert '<p class="alert alert-danger">' in resp.data.decode("utf-8")

# Without any check, the following request will fail.
resp = self.client.get("/raclette/")
# No bills, the previous one was not added
assert "No bills" in resp.data.decode("utf-8")


if __name__ == "__main__":
unittest.main()