From 712a51b3e35e33abbcc687a5ba6652592d20849c Mon Sep 17 00:00:00 2001 From: Andrey Rusakov Date: Mon, 25 Nov 2024 14:20:05 +0100 Subject: [PATCH] Added tests for the area limit --- api/serializers.py | 4 +++- api/tests/test_order.py | 41 ++++++++++++++++++++++++++++++++++++++++- default_settings.py | 4 ++-- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/api/serializers.py b/api/serializers.py index 0ffda205..c63d8501 100644 --- a/api/serializers.py +++ b/api/serializers.py @@ -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, diff --git a/api/tests/test_order.py b/api/tests/test_order.py index 1dd4fdcb..0f9da7ba 100644 --- a/api/tests/test_order.py +++ b/api/tests/test_order.py @@ -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 @@ -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) \ No newline at end of file + 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) \ No newline at end of file diff --git a/default_settings.py b/default_settings.py index 184573c4..9d0c0dbb 100644 --- a/default_settings.py +++ b/default_settings.py @@ -376,5 +376,5 @@ def check_oidc() -> bool: } } -# Maximum allowed area of an order, in square meters -MAX_ORDER_AREA=60000 # 6 hectares \ No newline at end of file +# Limit maximum allowed area of an order, in square meters. 0 for unlimited +MAX_ORDER_AREA=0 \ No newline at end of file