diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..cc8cd0d --- /dev/null +++ b/conftest.py @@ -0,0 +1,85 @@ +import pytest +from users.models import User, Team, Role +from tasks.models import Task, Priority, Status + +DEFAULT_VALID_PASSWORD = "ssSSAD231!@" +DEFAULT_MAIL_EXTENSION = "@redhat.com" +DEFAULT_DATA_DESCRIPTION = "This is test data" + + +@pytest.fixture +def teams(): + """ + Adds teams to DB + """ + + return tuple(Team.objects.create(name=f"Team{i}", + description=DEFAULT_DATA_DESCRIPTION) + for i in range(1, 4)) + + +@pytest.fixture +def users(teams): + """ + Adds users to DB + """ + team1, team2, team3 = teams + employees = [] + managers = [] + users_counter = 0 + for i in range(3): + for team in (team1, team2): + employee = User.create_user(username=f"User{users_counter}", + email=f"User{users_counter}{DEFAULT_MAIL_EXTENSION}", + password=DEFAULT_VALID_PASSWORD, + first_name=f"User{users_counter}", + last_name=f"User{users_counter}", + role=Role.EMPLOYEE, + team=team) + employees.append(employee) + users_counter += 1 + for i, team in enumerate((team1, team2, team3)): + manager = User.create_user(username=f"Manager{i}", + email=f"Manager{i}{DEFAULT_MAIL_EXTENSION}", + password=DEFAULT_VALID_PASSWORD, + first_name=f"Manager{i}", + last_name=f"Manager{i}", + role=Role.MANAGER, + team=team) + managers.append(manager) + return teams, tuple(employees), tuple(managers) + + +@pytest.fixture +def tasks(users): + """ + Adds tasks to DB + """ + teams, employees, managers = users + tasks = [] + tasks_counter = 0 + for team in teams: + team_employees = User.objects.filter(team=team, role=Role.EMPLOYEE) + team_manager = User.objects.filter(team=team, role=Role.MANAGER).first() + for employee in team_employees: + task = Task.objects.create(title=f"Task{tasks_counter}", + assignee=employee, + created_by=team_manager, + priority=Priority.LOW, + status=Status.BACKLOG, + description=DEFAULT_DATA_DESCRIPTION) + tasks.append(task) + return teams, employees, managers, tuple(tasks) + + +@pytest.fixture +def test_db(tasks): + """ + Returns test DB components as tuples. + The test DB contains: 3 teams, 3 users per team, 2 tasks per user, 1 manager per team. + """ + teams = tasks[0] + employees = tasks[1] + managers = tasks[2] + task = tasks[3] + return teams, employees, managers, task diff --git a/tasks/models.py b/tasks/models.py index c84a870..15bd2fe 100644 --- a/tasks/models.py +++ b/tasks/models.py @@ -53,6 +53,16 @@ def filter_by_symbol(cls, priority_filter): print(priority_filter) return cls.objects.filter(priority=priority_filter) + def change_assignee(self, new_assignee): + if new_assignee not in User.objects.all(): + raise Exception + prev_assignee = self.assignee + if new_assignee is None: + raise TypeError("Valid user must be provided") + if new_assignee.team != prev_assignee.team: + 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): diff --git a/tasks/tests/test_change_assignee.py b/tasks/tests/test_change_assignee.py new file mode 100644 index 0000000..fd11ac4 --- /dev/null +++ b/tasks/tests/test_change_assignee.py @@ -0,0 +1,53 @@ +import pytest +from tasks.models import Task +from users.models import Team, User, Role +from django.contrib.auth.models import User as DjangoUser + + +@pytest.mark.django_db +class TestChangeTaskAssignee: + def test_change_task_assignee(self, test_db): + team = test_db[0][0] + employee_1, employee_2 = User.objects.filter(team=team)[:2] + task = Task.objects.filter(assignee=employee_1).first() + assert task is not None + assert task.assignee == employee_1 + task.change_assignee(employee_2) + assert task.assignee == employee_2 + + @pytest.mark.parametrize("invalid_input", ["INVALID VALUE", None, 2]) + def test_change_assignee_invalid_input(self, test_db, invalid_input): + _, _, _, tasks = test_db + task = tasks[0] + assignee = task.assignee + with pytest.raises(Exception): + task.change_assignee(invalid_input) + assert assignee == task.assignee + + def test_change_assignee_other_team(self, test_db): + teams = Team.objects.all() + assert len(teams) >= 2 + team_1 = teams[0] + team_2 = teams[1] + employee_1 = User.objects.filter(team=team_1).first() + employee_2 = User.objects.filter(team=team_2).first() + task = Task.objects.filter(assignee=employee_1).first() + with pytest.raises(Exception): + task.change_assignee(employee_2) + assert task.assignee == employee_1 + + @pytest.fixture + def out_db_user(self, test_db): + team = test_db[0][0] + django_user = DjangoUser.objects.create(username="testtest", + password="ssdalhSFDAJ23!", + email="example@redhat.com", + first_name="test", + last_name="test") + user = User(user=django_user, role=Role.EMPLOYEE, team=team) + return user + + def test_change_assignee_not_db_user(self, test_db, out_db_user): + task = test_db[3][0] + with pytest.raises(Exception): + task.change_assignee(out_db_user)