diff --git a/backend/tasks/tasks/views.py b/backend/tasks/tasks/views.py index b9f4481..0f75ced 100644 --- a/backend/tasks/tasks/views.py +++ b/backend/tasks/tasks/views.py @@ -23,6 +23,7 @@ class TaskCreateView(CreateAPIView): class TaskRetrieveView(RetrieveAPIView): queryset = Task.objects.all() serializer_class = TaskGeneralSerializer + permission_classes = [IsAuthenticated] class TaskUpdateView(RetrieveUpdateAPIView): diff --git a/backend/tasks/tests/__init__.py b/backend/tasks/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/backend/tasks/tests/test_task_creation.py b/backend/tasks/tests/test_task_creation.py new file mode 100644 index 0000000..ff3c1ff --- /dev/null +++ b/backend/tasks/tests/test_task_creation.py @@ -0,0 +1,73 @@ +from django.urls import reverse +from rest_framework import status +from rest_framework.test import APITestCase + +from .utils import create_test_user, login_user +from ..models import Task + +class TaskCreateViewTests(APITestCase): + def setUp(self): + + self.user = create_test_user() + self.client = login_user(self.user) + self.url = reverse("add-task") + + def test_create_valid_task(self): + """ + Test creating a valid task using the API. + """ + data = { + 'title': 'Test Task', + 'type': 'habit', + 'exp': 10, + 'attribute': 'str', + 'priority': 1.5, + 'difficulty': 1, + 'user': self.user.id, + } + response = self.client.post(self.url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_201_CREATED) + self.assertEqual(Task.objects.count(), 1) + self.assertEqual(Task.objects.get().title, 'Test Task') + + def test_create_invalid_task(self): + """ + Test creating an invalid task using the API. + """ + data = { + 'type': 'invalid', # Invalid task type + } + + response = self.client.post(self.url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(Task.objects.count(), 0) # No task should be created + + def test_missing_required_fields(self): + """ + Test creating a task with missing required fields using the API. + """ + data = { + 'title': 'Incomplete Task', + 'type': 'habit', + } + + response = self.client.post(self.url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(Task.objects.count(), 0) # No task should be created + + def test_invalid_user_id(self): + """ + Test creating a task with an invalid user ID using the API. + """ + data = { + 'title': 'Test Task', + 'type': 'habit', + 'exp': 10, + 'priority': 1.5, + 'difficulty': 1, + 'user': 999, # Invalid user ID + } + + response = self.client.post(self.url, data, format='json') + self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) + self.assertEqual(Task.objects.count(), 0) # No task should be created diff --git a/backend/tasks/tests/utils.py b/backend/tasks/tests/utils.py new file mode 100644 index 0000000..80767fc --- /dev/null +++ b/backend/tasks/tests/utils.py @@ -0,0 +1,66 @@ +from rest_framework.test import APIClient + +from users.models import CustomUser +from ..models import Task + + +def create_test_user(email="testusertestuser@example.com", username="testusertestuser", + first_name="Test", password="testpassword",): + """create predifined user for testing""" + return CustomUser.objects.create_user( + email=email, + username=username, + first_name=first_name, + password=password, + ) + + +def login_user(user): + """Login a user to API client.""" + + client = APIClient() + client.force_authenticate(user=user) + return client + + +def create_task_json(user, **kwargs): + """Create task JSON data to use with the API.""" + defaults = { + "title": "Test Task", + "type": "habit", + "notes": "This is a test task created via the API.", + "exp": 10, + "priority": 1.5, + "difficulty": 1, + "attribute": "str", + "challenge": False, + "reminders": False, + "fromSystem": False, + "creation_date": None, + "last_update": None, + } + + task_attributes = {**defaults, **kwargs} + task_attributes["user"] = user + + return task_attributes + + +def create_test_task(user, **kwargs): + """Create a test task and associate it with the given user.""" + defaults = { + 'title': "Test Task", + 'task_type': 'habit', + 'notes': "This is a test task created via the API.", + 'exp': 10, + 'priority': 1.5, + 'difficulty': 1, + 'attribute': 'str', + 'challenge': False, + 'reminders': False, + 'fromSystem': False, + } + + task_attributes = {**defaults, **kwargs} + + return Task.objects.create(user=user, **task_attributes) \ No newline at end of file