diff --git a/tasks/models.py b/tasks/models.py index 738e453..15bd2fe 100644 --- a/tasks/models.py +++ b/tasks/models.py @@ -1,6 +1,6 @@ -from django.db import models +from django.db import models, transaction from enumchoicefield import ChoiceEnum, EnumChoiceField -from users.models import User +from users.models import Role, User class Status(ChoiceEnum): @@ -63,6 +63,26 @@ def change_assignee(self, new_assignee): raise ValueError("The new assignee must be of the same team") self.assignee = new_assignee + @classmethod + @transaction.atomic + def create_task(cls, title, assignee, created_by, priority, status, description): + if title == "": + raise ValueError("Title must contain at lease one character") + assigner_role = created_by.role + assigner_team = created_by.team + assigne_team = assignee.team + if assigne_team != assigner_team: + raise ValueError("Manager can assign tasks only for his own employees") + if assigner_role != Role.MANAGER: + raise ValueError("User must be a manager to assign tasks") + task = Task.objects.create(title=title, + assignee=assignee, + created_by=created_by, + priority=priority, + status=status, + description=description) + return task + def update_status(self, status): self.status = status self.save() diff --git a/tasks/tests/test_create_task.py b/tasks/tests/test_create_task.py new file mode 100644 index 0000000..278e57d --- /dev/null +++ b/tasks/tests/test_create_task.py @@ -0,0 +1,103 @@ +import pytest +from tasks.models import Priority, Status, Task +from users.models import User, Team, Role + + +@pytest.mark.django_db +class TestCreateTask: + """ + Add test team to DB + """ + @pytest.fixture + def team(self): + team = Team.objects.create(name="TestTeam", + description="This is a test team") + return team + + """ + Add another test team to DB + """ + @pytest.fixture + def other_team(self): + team = Team.objects.create(name="TestOtherTeam", + description="This is a test team") + return team + + """ + Add manager user to the DB + """ + @pytest.fixture + def manager(self, team): + manager = User.create_user(username="TestManager", + email="example@gmail.com", + password='xsdDS23', + first_name='Test', + last_name='Test', + role=Role.MANAGER, + team=team) + return manager + + """ + Add an employee to the team in "team" fixture + """ + @pytest.fixture + def employee(self, team): + employee = User.create_user(username="TestEmployee", + email="example@gmail.com", + password='xsdDS23', + first_name='Test', + last_name='Test', + role=Role.EMPLOYEE, + team=team) + return employee + + """ + Add an employee to the team in "other_team" fixture + """ + @pytest.fixture + def employee_other_team(self, other_team): + employee = User.create_user(username="TestEmployee", + email="example@gmail.com", + password='xsdDS23', + first_name='Test', + last_name='Test', + role=Role.EMPLOYEE, + team=other_team) + return employee + + @pytest.mark.parametrize('title, assignee, assigner, priority, status, description, length', [ + ('TestTask', 'employee', 'manager', Priority.CRITICAL, Status.BACKLOG, + 'This is description', 1), + ('TestTask', 'manager', 'employee', Priority.CRITICAL, Status.BACKLOG, + 'This is description', 0), + ('', 'employee', 'manager', Priority.CRITICAL, Status.BACKLOG, + 'This is description', 0), + ('TestTask', 'employee', 'manager', 'INVALID', Status.BACKLOG, + 'This is description', 0), + ('TestTask', 'employee', 'manager', Priority.CRITICAL, 'INVALID', + 'This is description', 0), + ('TestTask', 'employee_other_team', 'manager', Priority.CRITICAL, Status.BACKLOG, + 'This is description', 0), + ], + ids=[ + "test_add_task_to_db", + "test_assigned_by_non_manager", + "test_no_title", + "test_invalid_priority", + "test_invalid_status", + "test_assign_other_team", + ] + ) + def test_create_task(self, request, title, assignee, assigner, priority, status, description, length): + assignee = request.getfixturevalue(assignee) + created_by = request.getfixturevalue(assigner) + try: + Task.create_task(title=title, + assignee=assignee, + created_by=created_by, + priority=priority, + status=status, + description=description) + except ValueError: + pass + assert len(Task.objects.all()) == length