adding __future__ division import; addressing pr comments

This commit is contained in:
Ullauri 2019-01-01 17:38:22 -05:00
parent a5819e9b48
commit b64b8118e9
3 changed files with 59 additions and 36 deletions

View file

@ -53,11 +53,16 @@ class CalculatorStringField(StringField):
def process_formdata(self, valuelist):
if valuelist:
error_msg = "Not a valid amount or expression"
message = _(
"Not a valid amount or expression."
"Only numbers and + - * / operators"
"are accepted."
)
value = str(valuelist[0]).replace(",", ".")
if not match(r'^[ 0-9\.\+\-\*/\(\)]{0,50}$', value) or "**" in value:
raise ValueError(error_msg)
# avoid exponents to prevent expensive calculations i.e 2**9999999999**9999999
if not match(r'^[ 0-9\.\+\-\*/\(\)]{0,200}$', value) or "**" in value:
raise ValueError(Markup(message))
valuelist[0] = str(eval_arithmetic_expression(value))

View file

@ -1357,22 +1357,37 @@ class APITestCase(IhatemoneyTestCase):
self.api_add_member("raclette", "alexis")
self.api_add_member("raclette", "fred")
# add a bill
req = self.client.post("/api/projects/raclette/bills", data={
# valid amounts
input_expected = [
("((100 + 200.25) * 2 - 100) / 2", 250.25),
("3/2", 1.5),
]
for i, pair in enumerate(input_expected):
input_amount, expected_amount = pair
id = i + 1
req = self.client.post(
"/api/projects/raclette/bills",
data={
'date': '2011-08-10',
'what': 'fromage',
'payer': "1",
'payed_for': ["1", "2"],
'amount': '((100 + 200.25) * 2 - 100) / 2',
}, headers=self.get_auth("raclette"))
'amount': input_amount,
},
headers=self.get_auth("raclette")
)
# should return the id
self.assertStatus(201, req)
self.assertEqual(req.data.decode('utf-8'), "1\n")
self.assertEqual(req.data.decode('utf-8'), "{}\n".format(id))
# get this bill details
req = self.client.get("/api/projects/raclette/bills/1",
headers=self.get_auth("raclette"))
# get this bill's details
req = self.client.get(
"/api/projects/raclette/bills/{}".format(id),
headers=self.get_auth("raclette")
)
# compare with the added info
self.assertStatus(200, req)
@ -1382,9 +1397,10 @@ class APITestCase(IhatemoneyTestCase):
"owers": [
{"activated": True, "id": 1, "name": "alexis", "weight": 1},
{"activated": True, "id": 2, "name": "fred", "weight": 1}],
"amount": 250.25,
"amount": expected_amount,
"date": "2011-08-10",
"id": 1}
"id": id,
}
got = json.loads(req.data.decode('utf-8'))
self.assertEqual(
@ -1394,12 +1410,13 @@ class APITestCase(IhatemoneyTestCase):
del got["creation_date"]
self.assertDictEqual(expected, got)
# should raise errors
erroneous_amounts = [
"lambda ", # letters
"(20 + 2", # invalid expression
"20/0", # invalid calc
"9999**99999999999999999", # exponents
"2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2*2", # greater than 50 chars,
"2" * 201, # greater than 200 chars,
]
for amount in erroneous_amounts:

View file

@ -1,3 +1,4 @@
from __future__ import division
import base64
import re
import ast