mirror of
https://github.com/TurTaskProject/TurTaskWeb.git
synced 2025-12-18 21:44:07 +01:00
Add Task CRUD API tests
This commit is contained in:
parent
afe95d8075
commit
0d7dc3d644
@ -23,6 +23,7 @@ class TaskCreateView(CreateAPIView):
|
||||
class TaskRetrieveView(RetrieveAPIView):
|
||||
queryset = Task.objects.all()
|
||||
serializer_class = TaskGeneralSerializer
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
|
||||
class TaskUpdateView(RetrieveUpdateAPIView):
|
||||
|
||||
0
backend/tasks/tests/__init__.py
Normal file
0
backend/tasks/tests/__init__.py
Normal file
73
backend/tasks/tests/test_task_creation.py
Normal file
73
backend/tasks/tests/test_task_creation.py
Normal file
@ -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
|
||||
66
backend/tasks/tests/utils.py
Normal file
66
backend/tasks/tests/utils.py
Normal file
@ -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)
|
||||
Loading…
Reference in New Issue
Block a user