Skip to content

Commit

Permalink
Added tests for the area limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Rusakov authored and lanseg committed Dec 2, 2024
1 parent 61948dd commit 712a51b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
4 changes: 3 additions & 1 deletion api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,11 @@ class OrderSerializer(serializers.ModelSerializer):
def validate(self, attrs):
super().validate(attrs)
self._errors = {}
if 'geom' not in attrs:
return attrs
geom = attrs['geom']
area = geom.area
if area > settings.MAX_ORDER_AREA:
if settings.MAX_ORDER_AREA > 0 and area > settings.MAX_ORDER_AREA:
raise ValidationError({
'message': _(f'Order area is too large'),
'expected': settings.MAX_ORDER_AREA,
Expand Down
41 changes: 40 additions & 1 deletion api/tests/test_order.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import json

from django.urls import reverse
from django.core import mail
from django.test import override_settings

from djmoney.money import Money
from rest_framework import status
Expand Down Expand Up @@ -427,4 +430,40 @@ def test_group_with_validation(self):
"""
Tests email is sent when a product inside a group needs validation
"""
self.order_item_validation(True)
self.order_item_validation(True)

@override_settings(MAX_ORDER_AREA = 1000)
def test_order_geom_is_too_big(self):
url = reverse('order-list')
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.config.client_token)
self.order_data['geom'] = {
'type': 'Polygon',
'coordinates': [
[[2545488, 1203070],
[2545605, 1211390],
[2557441, 1202601],
[2557089, 1210921],
[2545488, 1203070]]
]}
response = self.client.post(url, self.order_data, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, response.content)
errorDetails = json.loads(response.content)
self.assertEqual(errorDetails['message'], ['Order area is too large'])
self.assertEqual(errorDetails['expected'], ['1000'])
self.assertTrue(errorDetails['actual'][0].startswith('109980.5'))

@override_settings(MAX_ORDER_AREA = 1000000)
def test_order_geom_is_fine(self):
url = reverse('order-list')
self.client.credentials(HTTP_AUTHORIZATION='Bearer ' + self.config.client_token)
self.order_data['geom'] = {
'type': 'Polygon',
'coordinates': [
[[2545488, 1203070],
[2545605, 1211390],
[2557441, 1202601],
[2557089, 1210921],
[2545488, 1203070]]
]}
response = self.client.post(url, self.order_data, format='json')
self.assertEqual(response.status_code, status.HTTP_201_CREATED, response.content)
4 changes: 2 additions & 2 deletions default_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,5 +376,5 @@ def check_oidc() -> bool:
}
}

# Maximum allowed area of an order, in square meters
MAX_ORDER_AREA=60000 # 6 hectares
# Limit maximum allowed area of an order, in square meters. 0 for unlimited
MAX_ORDER_AREA=0

0 comments on commit 712a51b

Please sign in to comment.