diff --git a/tests/tests.py b/tests/tests.py index 2110c82..6b2049e 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -288,6 +288,27 @@ def test_update_task_issue(self): self.assertEqual(resp.status_code, 400) self.assertEqual(resp.json(), {'issue': ['Subtask needs a parent']}) + def test_epic_to_subtask(self): + """ + Block moving epic to subtask, if epic has subtasks + """ + parent = TaskFactory(project=self.project, created_by=self.creator, code='NP-3') + epic_task = TaskFactory(project=self.project, created_by=self.creator) + TaskFactory(project=self.project, issue=Issue.SUBTASK, parent=epic_task, code='NP-2', created_by=self.creator) + + data = { + 'project_id': epic_task.project.id, + 'assigned_to_id': epic_task.assigned_to.id, + 'title': epic_task.title, + 'description': epic_task.description, + 'issue': Issue.SUBTASK.name, + 'parent_id': parent.id + } + url = reverse('task-detail', kwargs={'code': epic_task.code}) + resp = self.client.put(url, data, format='json') + self.assertEqual(resp.status_code, 400) + self.assertEqual(resp.json(), {'issue': ['Subtask cannot have subtasks']}) + class SubTasksTestCase(APITestCase): url = reverse('task-list') @@ -311,7 +332,7 @@ def test_epic_parent(self): } resp = self.client.post(self.url, data, format='json') self.assertEqual(resp.status_code, 400) - self.assertEqual(resp.json(), {'non_field_errors': ['Epic task cannot have parent']}) + self.assertEqual(resp.json(), {'issue': ['Epic task cannot have parent']}) def test_subtask_subtask(self): subtask = TaskFactory( @@ -438,10 +459,6 @@ def test_subtask_to_epic(self): self.assertEqual(subtask.parent, None) - - - - class AdminFormTestCase(APITestCase): def setUp(self) -> None: self.user = User.objects.create(username='creator', password='password') diff --git a/umsebenzi/serializers.py b/umsebenzi/serializers.py index 840ffb0..6fa922f 100644 --- a/umsebenzi/serializers.py +++ b/umsebenzi/serializers.py @@ -92,9 +92,8 @@ def get_subtasks(self, obj: Task): def validate(self, attrs): issue = attrs.get('issue') parent = attrs.get('parent_id') - # breakpoint() if issue == Issue.EPIC and parent: - raise serializers.ValidationError('Epic task cannot have parent') + raise serializers.ValidationError({'issue': 'Epic task cannot have parent'}) if parent and parent.issue is Issue.SUBTASK and issue == Issue.SUBTASK: raise serializers.ValidationError('Subtask cannot have subtask parent') if issue == Issue.SUBTASK and not parent: @@ -116,9 +115,16 @@ def create(self, validated_data): ) def update(self, instance: Task, validated_data): + if Task.objects.filter(parent=instance).exists(): + raise serializers.ValidationError({'issue': ['Subtask cannot have subtasks']}) + + issue = validated_data.get('issue') + if issue == Issue.EPIC: + instance.parent = None + else: + instance.parent = validated_data.get('parent_id', instance.parent) instance.project = validated_data.get('project_id', instance.project) instance.assigned_to = validated_data.get('assigned_to_id', instance.assigned_to) - instance.parent = validated_data.get('parent_id', instance.parent) instance.title = validated_data.get('title', instance.title) instance.description = validated_data.get('description', instance.description) instance.status = validated_data.get('status', instance.status)