Self hosted FLOSS fitness/workout, nutrition and weight tracker
Make the metric type of a measurement category immutable
Roland Geiderfdb3dd3fd38e
diff --git a/wger/measurements/api/serializers.py b/wger/measurements/api/serializers.pyindex 370f5d62d..bb5103455 100644--- a/wger/measurements/api/serializers.py+++ b/wger/measurements/api/serializers.py@@ -56,14 +56,12 @@ class CategorySerializer(serializers.ModelSerializer):def validate_metric_type(self, metric_type):"""- The metric type of an official category is fixed: the legacy weight- endpoint and the health sync rely on it+ The metric type is fixed once the category exists: the key of a typed+ one is derived from it (Category.deterministic_id). Assigning a type to+ an existing category is a move, not a change of this field"""- if self.instance and self.instance.is_official:- if metric_type != self.instance.metric_type:- raise serializers.ValidationError(- 'The metric type of an official category cannot be changed'- )+ if self.instance and metric_type != self.instance.metric_type:+ raise serializers.ValidationError('The metric type of a category cannot be changed')return metric_typedef validate_parent(self, parent):@@ -126,13 +124,6 @@ class CategorySerializer(serializers.ModelSerializer):{'metric_type': f'A {parent.metric_type} group only holds its own components'})- # A group is a container. It never carries measurements, so a category- # that has some cannot become one- if MetricType.is_group(metric_type) and instance and instance.measurement_set.exists():- raise serializers.ValidationError(- {'metric_type': 'A category with measurements cannot become a group'}- )-# Body weight is read through Measurement.value_in(), which converts# between kg and lb and knows no third unitif metric_type == MetricType.BODY_WEIGHT:@@ -200,7 +191,7 @@ class CategorySerializer(serializers.ModelSerializer):def update(self, instance, validated_data):"""- A category that becomes a group gets its components+ Fills in the components a group is missing"""category = super().update(instance, validated_data)category.create_components()diff --git a/wger/measurements/tests/test_categories.py b/wger/measurements/tests/test_categories.pyindex ad0512d07..8f6a416ff 100644--- a/wger/measurements/tests/test_categories.py+++ b/wger/measurements/tests/test_categories.py@@ -125,9 +125,8 @@ class TypedCategoryTestCase(WgerTestCase):Identity and structural rules of the categories with a metric type"""- # Pinned in test-measurement-categories.json / test-measurements.json+ # Pinned in test-measurement-categories.jsoncategory_empty = 'cccccccc-cccc-cccc-cccc-000000000003' # user 'test', no measurements- category_with_measurements = 'cccccccc-cccc-cccc-cccc-000000000001' # user 'test'def setUp(self):super().setUp()@@ -221,32 +220,71 @@ class TypedCategoryTestCase(WgerTestCase):],)- def test_adopting_a_category_as_group_creates_components(self):+ def test_free_form_category_cannot_be_typed(self):"""- Test that a category changed into a group also gets its components+ Test that a metric type cannot be assigned to an existing category"""response = self.client.patch(self.detail_url(self.category_empty),- {'metric_type': MetricType.BLOOD_PRESSURE},+ {'metric_type': MetricType.DISTANCE},content_type='application/json',)- self.assertEqual(response.status_code, 200)- self.assertEqual(Category.objects.filter(parent_id=self.category_empty).count(), 2)+ self.assertEqual(response.status_code, 400)+ self.assertIn('metric_type', response.data)+ self.assertEqual(+ Category.objects.get(pk=self.category_empty).metric_type,+ MetricType.CUSTOM,+ )- def test_category_with_measurements_cannot_become_group(self):+ def test_typed_category_cannot_be_reset(self):"""- Test that a category holding measurements cannot be turned into a group+ Test that a typed category cannot go back to being free-form"""+ category = self.create_category(metric_type=MetricType.STEPS)+response = self.client.patch(- self.detail_url(self.category_with_measurements),- {'metric_type': MetricType.BLOOD_PRESSURE},+ self.detail_url(category.data['id']),+ {'metric_type': MetricType.CUSTOM},content_type='application/json',)self.assertEqual(response.status_code, 400)self.assertIn('metric_type', response.data)+ def test_group_cannot_change_its_type(self):+ """+ Test that a group keeps its type, and with it its components+ """+ group = self.create_category(+ name='Blood pressure',+ unit='mmHg',+ metric_type=MetricType.BLOOD_PRESSURE,+ )++ response = self.client.patch(+ self.detail_url(group.data['id']),+ {'metric_type': MetricType.SLEEP},+ content_type='application/json',+ )++ self.assertEqual(response.status_code, 400)+ self.assertEqual(Category.objects.filter(parent_id=group.data['id']).count(), 2)++ def test_unchanged_metric_type_accepted(self):+ """+ Test that an edit sending the type along is not read as a change+ """+ category = self.create_category(metric_type=MetricType.STEPS)++ response = self.client.patch(+ self.detail_url(category.data['id']),+ {'name': 'Schritte', 'metric_type': MetricType.STEPS},+ content_type='application/json',+ )++ self.assertEqual(response.status_code, 200)+def test_component_without_group_rejected(self):"""Test that a component category cannot be created on its own