Skip to content

Commit

Permalink
enforce nonzero price in ratechecker API
Browse files Browse the repository at this point in the history
Passing a 'price' parameter of 0 causes a DivisionByZero exception.
This change modifies the API to change price=0 to price=1. This solely
prevents this case in the API and has no effect on the application
frontend which already enforces a minimum price.
  • Loading branch information
chosak committed Mar 13, 2017
1 parent 68dbdb7 commit 5a59431
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file.
We follow the [Semantic Versioning 2.0.0](http://semver.org/) format.

## Unreleased
- Enforce non-zero price in ratechecker API.

## 0.9.94 - 2017-02-02
- Add a monitor to watch for changes in census county values
Expand Down
6 changes: 5 additions & 1 deletion ratechecker/ratechecker_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,14 @@ def validate(self, attrs):

def validate_price(self, value):
"""
Validate price and convert to positive if negative
Validate price, convert to positive if negative, and enforce a
minimum value of 0.
"""
if value < 0:
value = abs(value)
elif value == 0:
value = Decimal('1')

return value

def validate_lock(self, value):
Expand Down
12 changes: 12 additions & 0 deletions ratechecker/tests/test_views_ratecheckerparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,24 @@ def test_is_valid__loan_amount_negative(self):
self.assertTrue(serializer.is_valid())
self.assertEqual(serializer.validated_data.get('loan_amount'), Decimal('10000'))

def test_is_valid__price(self):
self.data['price'] = 1000
serializer = ParamsSerializer(data=self.data)
self.assertTrue(serializer.is_valid())
self.assertEqual(serializer.validated_data['price'], Decimal('1000'))

def test_is_valid__price_negative(self):
self.data['price'] = -10000
serializer = ParamsSerializer(data=self.data)
self.assertTrue(serializer.is_valid())
self.assertEqual(serializer.validated_data.get('price'), Decimal('10000'))

def test_is_valid__price_zero(self):
self.data['price'] = 0
serializer = ParamsSerializer(data=self.data)
self.assertTrue(serializer.is_valid())
self.assertEqual(serializer.validated_data['price'], Decimal('1'))

def test_is_valid__state_invalid(self):
self.data['state'] = 123
serializer = ParamsSerializer(data=self.data)
Expand Down

0 comments on commit 5a59431

Please sign in to comment.