Ejemplo n.º 1
0
 def setUp(self) -> None:
     ProjectFactory(start_date=timezone.now(), suspended=True)
     ProjectFactory(
         start_date=timezone.now() - timezone.timedelta(days=2),
         stop_date=timezone.now() - timezone.timedelta(days=1),
     )
     ProjectFactory(start_date=timezone.now())
Ejemplo n.º 2
0
class RemoveUserFromProjectTests(TestCase):
    def setUp(self) -> None:
        self.number_of_reports = 6
        self.project = ProjectFactory(name="Test project")
        self.user = UserFactory()
        self.project.members.add(self.user)
        self._generate_user_reports()

    def test_that_removing_user_from_project_should_not_delete_user_reports_made_for_project(
            self):
        user_reports = Report.objects.filter(author=self.user,
                                             project=self.project)

        self.assertIn(self.user, self.project.members.all())
        self._check_number_of_reports(user_reports)

        self.project.members.remove(self.user)
        self.project.refresh_from_db()
        self.assertNotIn(self.user, self.project.members.all())

        self._check_number_of_reports(user_reports)

    def _generate_user_reports(self):
        for _report_number in range(self.number_of_reports):
            ReportFactory(author=self.user, project=self.project)

    def _check_number_of_reports(self, user_reports):
        self.assertEqual(user_reports.count(), self.number_of_reports)
        self.assertEqual(Report.objects.all().count(), self.number_of_reports)
Ejemplo n.º 3
0
 def setUp(self):
     super().setUp()
     self.user = AdminUserFactory()
     self.new_user = AdminUserFactory()
     self.other_project = ProjectFactory()
     self.project_start_date = timezone.now() + timezone.timedelta(days=-1)
     self.project = ProjectFactory(
         name="aaa", start_date=self.project_start_date, stop_date=timezone.now() + timezone.timedelta(days=6)
     )
     self.report = ReportFactory(author=self.user, project=self.project)
     self.project.members.add(self.user)
     self.data = {"project": self.project.pk, "author": self.user.pk}
     self.url_single_user = reverse(
         "export-data",
         kwargs={
             "pk": self.user.pk,
             "year": str(self.report.creation_date.year),
             "month": str(self.report.creation_date.month),
         },
     )
     self.url_project = reverse(
         "export-project-reports",
         kwargs={
             "pk": self.data["project"],
             "year": str(self.report.creation_date.year),
             "month": str(self.report.creation_date.month),
         },
     )
     self.workbook_for_project = generate_xlsx_for_project(self.project)
     self.workbook_for_user = generate_xlsx_for_single_user(self.user)
 def setUp(self):
     self.user = UserFactory()
     self.client.force_login(self.user)
     self.task_activity = TaskActivityTypeFactory()
     self.report = ReportFactory(
         author=self.user,
         date=datetime.datetime.now().date(),
         task_activity=self.task_activity,
         work_hours=datetime.timedelta(hours=8),
     )
     self.report.project.members.add(self.user)
     self.url = reverse(
         "join-project",
         kwargs={
             "year": datetime.datetime.now().date().year,
             "month": datetime.datetime.now().date().month
         },
     )
     self.success_url = reverse(
         "custom-report-list",
         kwargs={
             "year": datetime.datetime.now().date().year,
             "month": datetime.datetime.now().date().month
         },
     )
     self.new_project = ProjectFactory(name="New Project",
                                       start_date=datetime.datetime.now())
Ejemplo n.º 5
0
 def setUp(self):
     self.admin = AdminUserFactory()
     self.employee = UserFactory()
     self.manager = ManagerUserFactory()
     self.project = ProjectFactory()
     self.project.managers.add(self.admin)
     self.project.managers.add(self.manager)
     self.project.members.add(self.employee)
     self.url = reverse("custom-users-notifications")
Ejemplo n.º 6
0
 def setUp(self):
     super().setUp()
     self.project = ProjectFactory()
     self.url = reverse("custom-project-update",
                        kwargs={"pk": self.project.pk})
     self.data = {
         "name": "New Example Project Name",
         "start_date": self.project.start_date,
         "managers": [self.user.pk],
         "members": [self.user.pk],
     }
 def setUp(self):
     self.user = ManagerUserFactory()
     project_1 = ProjectFactory()
     project_1.members.add(self.user)
     project_2 = ProjectFactory()
     project_2.managers.add(self.user)
     self.projects = [project_1, project_2]
     self.current_time = timezone.now()
     self.hours_per_report = 4
     self.total_hours = 160
     self._generate_user_reports_for_current_and_previous_month_with_uneven_total_hours(
     )
     self.expected_work_hours_stats = self._get_total_hours_per_project_and_percentage_from_month(
     )
Ejemplo n.º 8
0
 def test_custom_report_detail_view_should_not_update_report_if_author_is_not_a_member_of_selected_project(
         self):
     other_project = ProjectFactory()
     other_project.save()
     old_description = self.data["description"]
     self.data["description"] = "Some other description"
     old_project = self.data["project"]
     self.data["project"] = other_project.pk
     response = self.client.post(path=self.url, data=self.data)
     self.report.refresh_from_db()
     self.assertEqual(response.status_code, 200)
     self.assertIsNotNone(response.context_data["form"].errors)
     self.assertEqual(old_description, self.report.description)
     self.assertEqual(old_project, self.report.project.pk)
Ejemplo n.º 9
0
 def setUp(self):
     super().setUp()
     self.user = AdminUserFactory()
     self.task_activity = TaskActivityTypeFactory()
     self.project = ProjectFactory()
     self.project.members.add(self.user)
     self.client.force_login(self.user)
     self.report = ReportFactory(author=self.user,
                                 project=self.project,
                                 task_activity=self.task_activity)
     self.url = reverse("project-report-detail", args=(self.report.pk, ))
     self.data = {
         "date": self.report.date,
         "description": self.report.description,
         "project": self.report.project.pk,
         "author": self.report.author.pk,
         "task_activity": self.task_activity.pk,
         "work_hours": self.report.work_hours_str,
         "current-project-pk": self.report.project.pk,
     }
     self.expected_success_message = SuccessPopUpMessages.REPORT_UPDATE_BY_ADMIN.value.format(
         report_author_email=self.report.author.email,
         report_date=self.report.date,
         project_name=self.report.project.name,
     )
Ejemplo n.º 10
0
 def test_view_should_not_display_reports_for_any_other_managers(self):
     user = ManagerUserFactory()
     other_project = ProjectFactory()
     other_project.managers.add(user)
     self.client.force_login(user)
     response = self.client.get(self.url)
     self.assertEqual(response.status_code, 404)
Ejemplo n.º 11
0
    def test_user_is_manager_of_current_project_mixin_should_not_limit_view_project_queryset_if_user_is_not_manager(
            self):
        user = UserFactory(user_type=CustomUser.UserType.EMPLOYEE.name)

        manager_project = ProjectFactory()
        manager_project.managers.add(user)
        # Project without current user as manager.
        ProjectFactory()

        assert Project.objects.count() == 2

        request = self.request_factory.get("anything")
        request.user = user
        response = self.view(request)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.context_data["object_list"]), 2)
Ejemplo n.º 12
0
    def test_that_add_users_to_projects_function_should_add_number_of_users_to_projects_from_specified_range(
            self):
        projects_list = []
        users_list = []

        for _project_number in range(3):
            projects_list.append(
                ProjectFactory(start_date=timezone.now(), suspended=True))

        for _user_number in range(15):
            users_list.append(
                UserFactory(user_type=CustomUser.UserType.EMPLOYEE.name))

        max_number_of_users_to_add = 6

        GenerateTestDataCommand().add_users_to_projects(
            users_list,
            projects_list,
            CustomUser.UserType.EMPLOYEE.name,
            ProjectType.SUSPENDED.name,
            max_number_of_users_to_add,
        )

        self.assertTrue(Project.objects.all()[0].members.count() in range(
            0, max_number_of_users_to_add + 1))
        self.assertTrue(Project.objects.all()[1].members.count() in range(
            0, max_number_of_users_to_add + 1))
        self.assertTrue(Project.objects.all()[2].members.count() in range(
            0, max_number_of_users_to_add + 1))
Ejemplo n.º 13
0
 def setUp(self):
     super().setUp()
     self.task_type = TaskActivityType(pk=1, name="Other")
     self.task_type.save()
     self.user = AdminUserFactory()
     self.project = ProjectFactory()
     self.project.members.add(self.user)
     self.client.force_login(self.user)
     current_date = datetime.datetime.now().date()
     self.report = ReportFactory(
         author=self.user,
         project=self.project,
         date=current_date,
         task_activity=self.task_type,
         work_hours=datetime.timedelta(hours=8),
     )
     self.data = {
         "date": timezone.now().date(),
         "description": "Some other description",
         "project": self.report.project.pk,
         "author": self.user.pk,
         "task_activity": self.report.task_activity.pk,
         "work_hours": "8.00",
     }
     self.year = current_date.year
     self.month = current_date.month
     self.url = reverse(
         "project-report-list", kwargs={"pk": self.project.pk, "year": self.year, "month": self.month}
     )
Ejemplo n.º 14
0
 def setUp(self):
     super().setUp()
     self.user_admin = AdminUserFactory()
     self.client.force_login(self.user_admin)
     self.project = ProjectFactory()
     self.task_activity = TaskActivityTypeFactory()
     self.url = reverse("task-activities")
Ejemplo n.º 15
0
    def _generate_test_projects(number_of_projects_to_fill_database,
                                project_type):
        project_list = []

        for _ in range(number_of_projects_to_fill_database):
            if project_type == ProjectType.SUSPENDED.name:
                project = ProjectFactory(suspended=True)
            elif project_type == ProjectType.ACTIVE.name:
                project = ProjectFactory()
            elif project_type == ProjectType.COMPLETED.name:
                project = ProjectFactory(stop_date=timezone.now() -
                                         relativedelta(days=1))
            else:
                raise UnsupportedProjectTypeException
            project_list.append(project)

        return project_list
Ejemplo n.º 16
0
    def test_custom_report_detail_view_should_contains_in_project_field_all_project_related_with_user(
            self):
        other_user_project = ProjectFactory()
        other_user_project.members.add(self.user)
        response = self.client.get(self.url)

        self.assertEqual(response.status_code, 200)
        self.assertCountEqual(
            self.user.projects.all(),
            response.context_data["form"].fields["project"].queryset)
Ejemplo n.º 17
0
    def test_filter_completed_projects_should_return_all_projects_with_stop_date_less_than_current_date(
            self):
        project_not_completed = ProjectFactory()
        self.project.stop_date = timezone.now() - timezone.timedelta(days=1)
        self.project.save()

        completed_projects = Project.objects.filter_completed()

        self.assertIn(self.project, completed_projects)
        self.assertNotIn(project_not_completed, completed_projects)
Ejemplo n.º 18
0
 def test_projects_list_view_for_manager_should_show_only_projects_in_which_he_is_manager(
         self):
     manager_user = UserFactory(user_type=CustomUser.UserType.MANAGER.name)
     manager_project = ProjectFactory()
     manager_project.managers.add(manager_user)
     self.client.force_login(manager_user)
     response = self.client.get(self.url)
     self.assertEqual(response.status_code, 200)
     self.assertContains(response, manager_project.name)
     self.assertNotContains(response, self.project.name)
Ejemplo n.º 19
0
    def test_custom_report_detail_view_should_contains_chosen_project_even_user_is_no_longer_in_project(
            self):
        other_user_project = ProjectFactory()
        other_user_project.members.add(self.user)
        self.user.projects.remove(self.report.project)

        response = self.client.get(self.url)

        self.assertIn(self.report.project,
                      response.context_data["form"].fields["project"].queryset)
Ejemplo n.º 20
0
 def setUp(self):
     super().setUp()
     self.user_admin = AdminUserFactory()
     self.client.force_login(self.user_admin)
     self.active_task_activity = TaskActivityTypeFactory()
     self.not_active_task_activity = TaskActivityTypeFactory(
         is_active=False)
     self.project = ProjectFactory()
     self.url = reverse(
         "disable-task-activity",
         kwargs={"task_activity_pk": self.active_task_activity.pk})
Ejemplo n.º 21
0
    def test_create_report_form_view_should_contain_only_active_projects_related_to_user(
            self):
        other_project = ProjectFactory()

        response = self.client.get(self.url)
        project_choices = response.context["form"].fields["project"].queryset

        self.assertEqual(response.status_code, 200)
        self.assertIn(self.report.project, project_choices)
        self.assertNotIn(other_project, project_choices)
        self.assertIn(self.report.project, Project.objects.filter_active())
Ejemplo n.º 22
0
 def setUp(self):
     super().setUp()
     self.project_1 = ProjectFactory(
         name="aaa",
         start_date=timezone.now() + timezone.timedelta(days=1),
         stop_date=timezone.now() + timezone.timedelta(days=6),
     )
     self.project_2 = ProjectFactory(
         name="abc",
         start_date=timezone.now() + timezone.timedelta(days=2),
         stop_date=timezone.now() + timezone.timedelta(days=5),
     )
     self.project_3 = ProjectFactory(
         name="cca",
         start_date=timezone.now() + timezone.timedelta(days=3),
         stop_date=timezone.now() + timezone.timedelta(days=4),
     )
     self.project_1.members.add(UserFactory())
     self.project_2.members.add(UserFactory(), UserFactory(), UserFactory())
     self.project_3.members.add(UserFactory(), UserFactory())
     self.url = reverse("custom-projects-list")
Ejemplo n.º 23
0
    def test_that_remove_all_random_user_projects_function_should_remove_all_user_projects(
            self, user_type):
        projects = [
            ProjectFactory(start_date=timezone.now()) for _project in range(3)
        ]
        user = UserFactory(user_type=user_type)

        for project in projects:
            project.members.add(user)

        GenerateTestDataCommand().remove_all_random_user_projects(user_type)

        self.assertEqual(user.projects.count(), 0)
Ejemplo n.º 24
0
    def setUp(self):
        super().setUp()
        self.user = UserFactory()

        self.project_1 = ProjectFactory()
        self.project_2 = ProjectFactory()
        self.project_3 = ProjectFactory()

        self.project_1_report_1 = ReportFactory(author=self.user,
                                                project=self.project_1)
        self.project_2_report_1 = ReportFactory(author=self.user,
                                                project=self.project_2)
        self.project_3_report_1 = ReportFactory(author=self.user,
                                                project=self.project_3)

        self.project_3_report_2 = ReportFactory(author=self.user,
                                                project=self.project_3)
        self.project_1_report_2 = ReportFactory(author=self.user,
                                                project=self.project_1)
        self.project_2_report_2 = ReportFactory(author=self.user,
                                                project=self.project_2)

        self.user.projects.add(self.project_1, self.project_2, self.project_3)
Ejemplo n.º 25
0
    def setUp(self):
        super().setUp()
        self.user = UserFactory()
        self.project = ProjectFactory()
        self.project.members.add(self.user)
        current_date = timezone.now()
        self.report = ReportFactory(author=self.user, project=self.project, date=current_date)

        self.url = reverse(
            "author-report-project-list",
            kwargs={
                "pk": self.project.pk,
                "user_pk": self.user.pk,
                "year": current_date.year,
                "month": current_date.month,
            },
        )
Ejemplo n.º 26
0
 def setUp(self):
     super().setUp()
     self.admin = AdminUserFactory()
     self.user = UserFactory()
     self.task_activity = TaskActivityTypeFactory()
     self.project = ProjectFactory()
     self.project.members.add(self.user)
     self.client.force_login(self.admin)
     self.report = ReportFactory(author=self.user,
                                 project=self.project,
                                 task_activity=self.task_activity)
     self.url = reverse("admin-report-detail",
                        kwargs={"pk": self.report.pk})
     self.data = {
         "date": timezone.now().date(),
         "description": "Some other description",
         "project": self.report.project.pk,
         "author": self.user.pk,
         "task_activity": self.task_activity.pk,
         "work_hours": "8:00",
         "current-project-pk": self.report.project.pk,
     }
Ejemplo n.º 27
0
 def setUp(self):
     super().setUp()
     self.employee1 = UserFactory(first_name="Cezar", last_name="Goldstein")
     self.employee2 = UserFactory(first_name="", last_name="", email="*****@*****.**")
     self.employee3 = UserFactory(first_name="Abimelek", last_name="Zuckerberg")
     self.manager = ManagerUserFactory()
     self.project = ProjectFactory()
     self.project.members.add(self.employee1)
     self.project.members.add(self.employee2)
     self.project.members.add(self.employee3)
     reports_in_day = 2
     # creating reports in desc order
     number_of_days = 4
     self.year = "2019"
     self.month = "06"
     for i in range(number_of_days, 0, -1):
         for _ in range(reports_in_day):
             ReportFactory(author=self.employee2, project=self.project, date=f"{self.year}-{self.month}-{i}")
             ReportFactory(author=self.employee3, project=self.project, date=f"{self.year}-{self.month}-{i}")
             ReportFactory(author=self.employee1, project=self.project, date=f"{self.year}-{self.month}-{i}")
     self.report_asc = Report.objects.filter(author__id=self.employee1.pk).order_by("date")
     self.reports_per_user = reports_in_day * number_of_days
Ejemplo n.º 28
0
 def setUp(self):
     super().setUp()
     self.project = ProjectFactory()
     self.url = reverse("custom-projects-list")
Ejemplo n.º 29
0
 def setUp(self):
     super().setUp()
     self.project = ProjectFactory()
     self.url = reverse("custom-project-detail",
                        kwargs={"pk": self.project.pk})
Ejemplo n.º 30
0
class ProjectUpdateViewTestCase(ProjectBaseTests):
    def setUp(self):
        super().setUp()
        self.project = ProjectFactory()
        self.url = reverse("custom-project-update",
                           kwargs={"pk": self.project.pk})
        self.data = {
            "name": "New Example Project Name",
            "start_date": self.project.start_date,
            "managers": [self.user.pk],
            "members": [self.user.pk],
        }

    def test_project_update_view_should_display_update_template(self):
        response = self.client.get(self.url)
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, self.project.name)
        self.assertTemplateUsed(response, ProjectUpdateView.template_name)

    def test_project_update_view_should_return_404_status_code_on_get_if_project_does_not_exist(
            self):
        response = self.client.get(
            reverse("custom-project-update",
                    kwargs={"pk": self.project.pk + 1}))
        self.assertEqual(response.status_code, 404)

    def test_project_create_view_should_exclude_inactive_users_from_members_field(
            self):
        inactive_user = UserFactory(is_active=False)
        response = self.client.get(self.url)
        form = response.context_data["form"]
        self.assertTrue(inactive_user not in form.fields["members"].queryset)

    def test_project_update_view_should_update_project_on_post(self):
        response = self.client.post(self.url, self.data)
        self.assertEqual(response.status_code, 302)
        self.project.refresh_from_db()
        self.assertEqual(self.project.name, self.data["name"])

    def test_project_update_view_should_update_project_on_post_if_data_is_invalid(
            self):
        del self.data["name"]
        response = self.client.post(self.url, self.data)
        self.assertEqual(response.status_code, 200)
        self.project.refresh_from_db()
        self.assertFormError(response, "form", "name",
                             "This field is required.")

    def test_project_update_view_should_not_update_managers_if_user_is_manager(
            self):
        assert self.project.managers.count() == 0
        user_manager = UserFactory(user_type=CustomUser.UserType.MANAGER.name)
        self.client.force_login(user=user_manager)
        self.data["managers"] = [self.user.pk, user_manager.pk]
        response = self.client.post(self.url, self.data)
        self.assertEqual(response.status_code, 404)
        self.project.refresh_from_db()
        self.assertEqual(self.project.managers.count(), 0)

    def test_project_update_view_should_display_all_aggregated_managers_on_get(
            self):
        self.project.managers.add(self.user)
        other_manager = ManagerUserFactory()

        response = self.client.get(self.url)

        self.assertEqual(response.status_code, 200)
        self.assertIn(self.user,
                      response.context_data["form"].initial["managers"])
        self.assertNotIn(other_manager,
                         response.context_data["form"].initial["managers"])
        self.assertContains(response, self.user)
        self.assertContains(response, other_manager)