From 47f84b36d3f205c6e118b57879a5c68fd65d7b0c Mon Sep 17 00:00:00 2001 From: Mikhail Volkov <61934932+greenpandorik@users.noreply.github.com> Date: Sun, 4 Feb 2024 23:29:07 +0300 Subject: [PATCH] Some fix api-tasks and tests (#61) --- api/v1/serializers/api/task_serializer.py | 1 + api/v1/tests/test_tasks_api.py | 31 ++++++++++++++++++++++- api/v1/views/task_views.py | 8 +++++- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/api/v1/serializers/api/task_serializer.py b/api/v1/serializers/api/task_serializer.py index fc33ccd..f54cbde 100644 --- a/api/v1/serializers/api/task_serializer.py +++ b/api/v1/serializers/api/task_serializer.py @@ -13,6 +13,7 @@ class Meta: "name", "description", "creator", + "executor", "creation_date", "start_date", "end_date", diff --git a/api/v1/tests/test_tasks_api.py b/api/v1/tests/test_tasks_api.py index 582755c..d8f823f 100644 --- a/api/v1/tests/test_tasks_api.py +++ b/api/v1/tests/test_tasks_api.py @@ -6,24 +6,33 @@ from ipr.models import IPR from tasks.models import Task +from users.models import Team User = get_user_model() class TaskAPITests(APITestCase): def setUp(self): + self.user0 = User.objects.create_user( + username="user0", + email="user0@mail.com", + password="password", + ) + self.team1 = Team.objects.create(name="Team 1", boss=self.user0) self.user1 = User.objects.create_user( username="user1", email="user1@mail.com", password="password", ) + self.team2 = Team.objects.create(name="Team 1", boss=self.user1) self.user2 = User.objects.create_user( username="user2", email="user2@mail.com", password="password", + team=self.team2, ) self.client = APIClient() - self.client.force_authenticate(user=self.user2) + self.client.force_authenticate(user=self.user1) self.ipr = IPR.objects.create( title="Test IPR", creation_date=timezone.now().date(), @@ -83,6 +92,26 @@ def test_create_task(self): self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(Task.objects.count(), 3) + def test_create_task_for_other_user(self): + """ + Проверка создания новой задачи для пол-ля другой команды (POST) + """ + url = reverse("tasks-list") + data = { + "name": "New Task", + "description": "New Description", + "creator": self.user0.id, + "creation_date": timezone.now().date(), + "start_date": (timezone.now() + timezone.timedelta(days=1)).date(), + "end_date": (timezone.now() + timezone.timedelta(days=2)).date(), + "executor": self.user2.id, + "ipr": self.ipr.id, + } + response = self.client.post(url, data, format="json") + + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(Task.objects.count(), 3) + def test_update_task(self): """ Проверка обновления задачи (PATCH) diff --git a/api/v1/views/task_views.py b/api/v1/views/task_views.py index bc5eda8..6197016 100644 --- a/api/v1/views/task_views.py +++ b/api/v1/views/task_views.py @@ -1,7 +1,8 @@ from django.contrib.auth import get_user_model from drf_spectacular.utils import extend_schema, extend_schema_view -from rest_framework import viewsets +from rest_framework import permissions, viewsets +from api.v1.permissions import TeamBossPermission from api.v1.serializers.api.task_serializer import ( TaskSerializer, TaskSerializerPost, @@ -53,3 +54,8 @@ def get_serializer_class(self): def perform_create(self, serializer): serializer.save(creator=self.request.user) + + def get_permissions(self): + if self.request.method not in permissions.SAFE_METHODS: + self.permission_classes = [TeamBossPermission] + return super(TaskViewSet, self).get_permissions()